Config.txt定義在透過QRegExp分析檔案的開關
main.cpp
#include <QFile>
#include <QDebug>
#include <QTextStream>
#include <QStringList>
int main(int argc, char *argv[])
{
QFile FlagFile ("Config.txt");
QFile CopyFlagFlag ("CopyConfig.txt");
QStringList ConfigStringList;
//
// Test the Config.txt exists or not
//
if (!FlagFile.exists ()) {
qDebug () << "The flag file does not exist!!";
return -1;
}
//
// Try to open Config.txt
//
if (!FlagFile.open (QIODevice::ReadOnly)) {
qDebug () << "Can not oepn FlagFile!!";
return -1;
}
//
// Set QTextStream for Config.txt
//
QTextStream out (&FlagFile);
//
// Insert all the context into ConfigStringList
//
while (!out.atEnd ()) {
ConfigStringList << out.readLine();
}
// for (int i = 0; i < ConfigStringList.size (); i++) {
// qDebug () << ConfigStringList.at (i);
// }
//
// Try to create CopyFlagFlag.txt
//
CopyFlagFlag.open (QIODevice::WriteOnly | QIODevice::Text);
//
// Set QTextStream for CopyConfig.txt
//
QTextStream in (&CopyFlagFlag);
//
// Set the regular expressions for Flag format
//
QRegExp YesNoReg ("^(\\w+\\s*=\\s*)(YES|NO)(\\s*)$");
for (int i = 0; i < ConfigStringList.size (); i++) {
if (YesNoReg.indexIn (ConfigStringList.at (i)) != -1) {
//
// When the line match the regular expressions
//
if (YesNoReg.cap (2) == QString ("YES")) {
//qDebug () << YesNoReg.cap (1) << QString ("NO") << YesNoReg.cap (3);
//
// Invert YES to NO
//
in << YesNoReg.cap (1) << QString ("NO") << YesNoReg.cap (3);
} else {
//qDebug () << YesNoReg.cap (1) << QString ("YES") << YesNoReg.cap (3);
//
// Invert NO to YES
//
in << YesNoReg.cap (1) << QString ("YES") << YesNoReg.cap (3);
}
} else {
//
// When the line does not match the regular expressions
//
//qDebug () << ConfigStringList.at (i);
in << ConfigStringList.at (i);
}
if (i < ConfigStringList.size ()) {
in << endl;
}
}
FlagFile.close ();
CopyFlagFlag.close ();
return 0;
}
如此一開,只會改到開關,其它有幾個空白,有幾個tab完全不會動到。
參考資料:
透過QRegExp分析檔案的開關
QDataStream Class Reference
沒有留言:
張貼留言