bool readConfFile(QIODevice &device, QSettings::SettingsMap &map)
{
        // Is this the right conversion?
        const QString& data = QString::fromUtf8(device.readAll().data());

        // Split by a RE which should match any platform's line breaking rules
        QRegExp matchLbr("[\\n\\r]+");
        const QStringList& lines = data.split(matchLbr, QString::SkipEmptyParts);

        //QString currentSection = "";
        //QRegExp sectionRe("^\\[(.+)\\]$");
        QRegExp keyRe("^([^=]+)\\s*=\\s*(.+)$");
        QRegExp cleanComment("#.*$");
        QRegExp cleanWhiteSpaces("^\\s+");
        QRegExp reg1("\\s+$");

        for(int i=0; i<lines.size(); i++)
        {
                QString l = lines.at(i);
                l.replace(cleanComment, "");            // clean comments
                l.replace(cleanWhiteSpaces, "");        // clean whitespace
                l.replace(reg1, "");

                // Otherwise only process if it macthes an re which looks like: key = value
                if (keyRe.exactMatch(l))
                {
                        // Let REs do the work for us exatracting the key and value
                        // and cleaning them up by removing whitespace
                        QString k = keyRe.cap(1);
                        QString v = keyRe.cap(2);
                        v.replace(cleanWhiteSpaces, "");
                        k.replace(reg1, "");

                        // Set the map item.
                        map[k] = QVariant(v);
                }
        }

        return true;
}
Exemple #2
0
Fichier : c.c Projet : LucasAbreu/c
int main (int argc, const char * argv[]) {

    // Get the current directory
    char cwd[1024];
    if (getcwd(cwd, sizeof(cwd)) != NULL)
        ;
    else
        perror("getcwd() error");

    int numargs, all=0, asc=0, desc=0, push=0, uk=0, help=0, current=0, clean=0;
    const char *c;

    numargs = argc;

    while (--numargs > 0) {
        c = argv[numargs];

        if (!strcmp(argv[numargs], "all"))
            all = 1;
        else if (!strcmp(argv[numargs], "-h") || !strcmp(argv[numargs], "--help"))
            help = 1;
        else if (!strcmp(argv[numargs], "."))
            current = 1;
        else if (!strcmp(argv[numargs], "-p") || !strcmp(argv[numargs], "--push"))
            push = 1;
        else if (!strcmp(argv[numargs], "-"))
            desc = 1;
        else if (!strcmp(argv[numargs], "+"))
            asc = 1;
        else if (!strcmp(argv[numargs], "clean"))
            clean = 1;
        else
            uk++;
    }

    int args;
    args = argc;

    if (uk > 2) {
        printUsage();
        return 1;
    }

    switch(args) {
        // Show the comment for the current directory
        case 1: printUsage();
                return 1;
                break;
        case 2:
                // Show the help menu
                if (help) {
                    printUsage();
                    return 1;
                }
                // Show all the files
                else if (all) {
                    makeDir(cwd);
                    printFiles(cwd);
                }
                // Show comment for the current directory
                else if (current) {
                    // checkComment(argv[1], cwd);
                    printCurrentComment(cwd);
                }
                // Show comment for the entered file
                else {
                    // char *file = argv[1];
                    makeDir(cwd);
                    checkComment(argv[1], cwd);
                }
                break;
        case 3:

                if ( current && !clean ) {										// Add comment to the current directory
                    // char *file;
                    // file = "CURRENT";
                    addComment(argv[1], cwd, argv[2], false);
                } else if (all && desc) {					// Print all files in descending order
                    makeDir(cwd);
                    printAllOrder(cwd, true);
                } else if (all && asc) {					// Print all files in ascending order
                    makeDir(cwd);
                    printAllOrder(cwd, false);
                } else if ( clean ){
                    cleanComment( argv[1] );                 // Clean file comment
                }
                else {														// Add comment to to given file
                    makeDir(cwd);
                    addComment(argv[1], cwd, argv[2], false);
                }
                break;

        case 4:
                // Push comment to the current directory
                if (current && push) {
                    addComment(argv[1], cwd, argv[3], true);
                    // Add a comment to the given filename
                }
                // Push comment to given file
                else	if ( push) {
                    makeDir(cwd);
                    addComment(argv[1], cwd, argv[3], true);
                }
                break;

                // Not certain
        default: printUsage();
    }
    return 0;
}