Пример #1
0
st_bool CCommandLineParser::ParseCommandLineFile(const char* pFilename, const char* pExeName, SUserSettings& sConfig)
{
    st_bool bSuccess = false;

    FILE* pFile = fopen(pFilename, "r");
    if (pFile)
    {
        // parse the cmd-line options in the file and store in an array of arguments
        CArray<CFixedString> aArguments;
        CFixedString strTemp;
        st_bool bInQuotes = false;

        while (!feof(pFile))
        {
            st_char chTemp = (st_char)fgetc(pFile);

            st_bool bSaveString = false;
            if (chTemp == '#')
            {
                // skip rest of comment line
                while (!feof(pFile) && (chTemp != '\r' && chTemp != '\n'))
                    chTemp = (st_char)fgetc(pFile);
                bSaveString = true;
            }
            else if (chTemp == '\"')
            {
                // quote delimited string
                if (bInQuotes)
                    bSaveString = true;
                bInQuotes = !bInQuotes;
            }
            else if (!bInQuotes && (chTemp == ' ' || chTemp == '\r' || chTemp == '\n'))
            {
                // other whitespace
                bSaveString = true;             
            }
            else
            {
                strTemp += chTemp;
            }

            if (bSaveString && !strTemp.empty( ))
            {
                // save this string as an argument
                aArguments.push_back(strTemp);
                strTemp.resize(0);
            }
        }

        fclose(pFile);

        // convert the array to a standard argv-type data structure
        const st_int32 c_nNumArgs = st_int32(aArguments.size( ) + 1);
        char** argv = new char*[c_nNumArgs];
        for (st_int32 i = 1; i < c_nNumArgs; ++i)
            argv[i] = (char*) aArguments[i - 1].c_str( );
        argv[0] = (char*) pExeName; // normally contains the exe name

        // feed back into the parse routine 
        bSuccess = Parse(c_nNumArgs, argv, sConfig);

        delete [] argv;
    }

    return bSuccess;
}