String
SimpleConfig::get_value_portion (const String &str)
{
    String::size_type begin = str.find_first_of ("=");

    if (begin == String::npos || (begin + 1) == str.length ()) return String ("");

    return trim_blank (str.substr (begin + 1, String::npos));
}
void
SimpleConfig::parse_config (std::istream &is, KeyValueRepository &config)
{
    char *conf_line = new char [SCIM_MAX_CONFIG_LINE_LENGTH];

    while (!is.eof()) {
        is.getline(conf_line, SCIM_MAX_CONFIG_LINE_LENGTH);
        if (!is.eof()) {
            String normalized_line = trim_blank(conf_line);

            if ((normalized_line.find_first_of("#") > 0) && (normalized_line.length() != 0)) {
                if (normalized_line.find_first_of("=") == String::npos) {
                    SCIM_DEBUG_CONFIG(2) << " Invalid config line : " << normalized_line << "\n";
                    continue;
                }

                if (normalized_line[0] == '=') {
                    SCIM_DEBUG_CONFIG(2) << " Invalid config line : " << normalized_line << "\n";
                    continue;
                }

                String param = get_param_portion(normalized_line);
                KeyValueRepository::iterator i = config.find(param);

                if (i != config.end()) {
                    SCIM_DEBUG_CONFIG(2) << " Config entry " << normalized_line << " has been read.\n";
                } else {
                    String value = get_value_portion (normalized_line); 
                    config [param] = value;
                    SCIM_DEBUG_CONFIG(2) << " Config entry " << param << "=" << value << " is successfully read.\n";
                }
            }
        }
    }

    delete [] conf_line;
}
Exemple #3
0
void IniFile::parse_config(istream &is, map <string, string> &config)
{
    char *conf_line = new char [MAX_CONFIG_LINE_LENGTH];
	string strsect;

    while (!is.eof()) 
	{
        is.getline(conf_line, MAX_CONFIG_LINE_LENGTH);
        if (!is.eof()) 
		{
			
            string normalized_line = trim_blank(conf_line);

		//	cout << normalized_line << endl;

			int first = normalized_line.find('[');
			int last = normalized_line.rfind(']');
			if( first != string::npos && last != string::npos && first != last + 1)
			{
				strsect = normalized_line.substr(first + 1,last - first - 1);
				continue ;
			}

			if(strsect.empty())
				continue ;

			if( ( first = normalized_line.find('=') )== string::npos)
				continue ;

			string s1 = normalized_line.substr(0, first);
			string s2 = normalized_line.substr(first + 1, string::npos);
			first= s1.find_first_not_of(" \t");
			last = s1.find_last_not_of(" \t");

			if(first == string::npos || last == string::npos)
				continue ;

			string strkey = s1.substr(first, last - first + 1);
			first = s2.find_first_not_of(" \t");
			if(((last = s2.find("\t#", first )) != -1) ||
            		((last = s2.find(" #", first )) != -1) ||
            		((last = s2.find("\t//", first )) != -1)||
            		((last = s2.find(" //", first )) != -1))
    		{
            	s2 = s2.substr(0, last - first);
    		}

    		last = s2.find_last_not_of(" \t");
		    if(first == string::npos || last == string::npos)
    			continue ;

		    string value = s2.substr(first, last - first + 1);

			string mapkey = strsect + MIDDLESTRING;

			mapkey += strkey;
		    config[mapkey] = value;
	//		cout << mapkey << '\t' << value << endl;
    		continue ;

        }
    }

    delete [] conf_line;

}