Ejemplo n.º 1
0
bool
CIniParser::LoadFrom( CIOAccess& fileAccess )
{GUCEF_TRACE;

    // @TODO: take escape sequences into account
    if ( fileAccess.IsValid() )
    {
        bool isNewSection = true;
        CString sectionName;
        while ( !fileAccess.Eof() )
        {
            // Get the current line from the file, trimming white space on both ends
            CString line = fileAccess.ReadLine().Trim( true ).Trim( false );
            if( line.Length() > 0 )
            {
                // Get rid of any trailing commentary on this line
                Int32 commentaryIndex = line.HasChar( ';', false );
                if ( commentaryIndex > -1 )
                {
                    Int32 dummy = 0;
                    if ( !IsCharIndexWithinQuotes( line, commentaryIndex, dummy, dummy ) )
                    {
                        // we found a semicolon which is not contained within a quotation
                        // thus this is a commentary section which we should remove
                        line = line.CutChars( line.Length()-commentaryIndex, false ).Trim( false );
                    }
                }

                if( line.Length() > 0 )
                {
                    // Check if this is a section tag line
                    if ( ( line[ 0 ] == '[' ) && ( line[ line.Length()-1 ] == ']' ) )
                    {
                        sectionName = line.SubstrFromRange( 1, line.Length()-1 );
                        isNewSection = true;
                    }
                    else
                    {
                        Int32 equalsIndex = FindIndexOfNonQuotedEquals( line );
                        if ( equalsIndex > -1 )
                        {
                            // get the key and value strings
                            CString sectionBeforeEquals = StripQuotation( line.SubstrFromRange( 0, equalsIndex ) );
                            CString sectionAfterEquals = StripQuotation( line.SubstrFromRange( equalsIndex+1, line.Length() ) );

                            if ( ( sectionBeforeEquals.Length() > 0 ) &&
                                 ( sectionAfterEquals.Length() > 0 )   )
                            {
                                if ( isNewSection )
                                {
                                    TIniSection dummySection;
                                    m_iniData.push_back( dummySection );
                                    TIniSection& newSection = (*m_iniData.rbegin());
                                    newSection.sectionName = sectionName;
                                    newSection.sectionData.SetAllowDuplicates( true );
                                    newSection.sectionData.SetAllowMultipleValues( true );

                                    isNewSection = false;
                                }

                                TIniSection& newSection = (*m_iniData.rbegin());
                                newSection.sectionData.Set( sectionBeforeEquals, sectionAfterEquals );
                            }
                        }
                        // else:
                        // Line with junk on it we do not support
                        // we will try and be robust and ignore this line
                    }
                }
            }
        }
        return true;
    }
    return false;
}
Ejemplo n.º 2
0
void OptionsDB::SetFromCommandLine(const std::vector<std::string>& args) {
    //bool option_changed = false;

    for (unsigned int i = 1; i < args.size(); ++i) {
        std::string current_token(args[i]);
        if (current_token.find("--") == 0) {
            std::string option_name = current_token.substr(2);

            std::map<std::string, Option>::iterator it = m_options.find(option_name);

            if (it == m_options.end() || !it->second.recognized) { // unrecognized option: may be registered later on so we'll store it for now
                // Check for more parameters (if this is the last one, assume that it is a flag).
                std::string value_str("-");
                if (i + 1 < static_cast<unsigned int>(args.size())) {
                    value_str = args[i + 1]; // copy assignment
                    StripQuotation(value_str);
                }

                if (value_str.at(0) == '-') { // this is either the last parameter or the next parameter is another option, assume this one is a flag
                    m_options[option_name] = Option(static_cast<char>(0), option_name, true, boost::lexical_cast<std::string>(false),
                                                    "", 0, false, true, false);
                } else { // the next parameter is the value, store it as a string to be parsed later
                    m_options[option_name] = Option(static_cast<char>(0), option_name, value_str, value_str,
                                                    "", new Validator<std::string>(), false, false, false); // don't attempt to store options that have only been specified on the command line
                }

                WarnLogger() << "Option \"" << option_name << "\", was specified on the command line but was not recognized.  It may not be registered yet or could be a typo.";
            } else {
                Option& option = it->second;
                if (option.value.empty())
                    throw std::runtime_error("The value member of option \"--" + option.name + "\" is undefined.");

                if (!option.flag) { // non-flag
                    try {
                        // ensure a parameter exists...
                        if (i + 1 >= static_cast<unsigned int>(args.size()))
                            throw std::runtime_error("the option \"" + option.name +
                                                     "\" was specified, at the end of the list, with no parameter value.");
                        // get parameter value
                        std::string value_str(args[++i]);
                        StripQuotation(value_str);
                        // ensure parameter is actually a parameter, and not the next option name (which would indicate
                        // that the option was specified without a parameter value, as if it was a flag)
                        if (value_str.at(0) == '-')
                            throw std::runtime_error("the option \"" + option.name +
                                                     "\" was followed by the parameter \"" + value_str +
                                                     "\", which appears to be an option flag, not a parameter value, because it begins with a \"-\" character.");
                        m_dirty |= option.SetFromString(value_str);
                    } catch (const std::exception& e) {
                        throw std::runtime_error("OptionsDB::SetFromCommandLine() : the following exception was caught when attempting to set option \"" + option.name + "\": " + e.what() + "\n\n");
                    }
                } else { // flag
                    option.value = true;
                }
            }

            //option_changed = true;
        } else if (current_token.find('-') == 0
#ifdef FREEORION_MACOSX
                && current_token.find("-psn") != 0 // Mac OS X passes a process serial number to all applications using Carbon or Cocoa, it should be ignored here
#endif
            ) {
            std::string single_char_options = current_token.substr(1);

            if (single_char_options.size() == 0) {
                throw std::runtime_error("A \'-\' was given with no options.");
            } else {
                for (unsigned int j = 0; j < single_char_options.size(); ++j) {
                    std::map<char, std::string>::iterator short_name_it = Option::short_names.find(single_char_options[j]);

                    if (short_name_it == Option::short_names.end())
                        throw std::runtime_error(std::string("Unknown option \"-") + single_char_options[j] + "\" was given.");

                    std::map<std::string, Option>::iterator name_it = m_options.find(short_name_it->second);

                    if (name_it == m_options.end())
                        throw std::runtime_error("Option \"--" + short_name_it->second + "\", abbreviated as \"-" + short_name_it->first + "\", could not be found.");

                    Option& option = name_it->second;
                    if (option.value.empty())
                        throw std::runtime_error("The value member of option \"--" + option.name + "\" is undefined.");

                    if (!option.flag) {
                        if (j < single_char_options.size() - 1)
                            throw std::runtime_error(std::string("Option \"-") + single_char_options[j] + "\" was given with no parameter.");
                        else
                            m_dirty |= option.SetFromString(args[++i]);
                    } else {
                        option.value = true;
                    }
                }
            }
        }
    }
}