Пример #1
0
bool ConfigWrapper::GetwstringValueFromConfig( const wstring& strKey, wstring& strValue )
{
	if ( NUMBER_ZERO == strKey.compare( NULL_STRING ))
	{
		return false;
	}

	if ( NULL == m_pConfigManager )
	{
		return false;
	}

	ConfigValue* pSingleNode = NULL;
//	pSingleNode = m_pConfigManager->SelectSingleNode( strKey );

	TCHAR szNodeValue[MAX_VALUE_LENGTH];

	DWORD dwReturnSize = pSingleNode->GetValue( szNodeValue );
	strValue = szNodeValue;

	if ( NULL != pSingleNode )
	{
		delete pSingleNode;
		pSingleNode = NULL;
	}

	return true;
}
Пример #2
0
ConfigMap* ConfigMapParser::map() throw(PARSEEXCEPTION_THROW)
{
  expect(T::CMT_CBL);
  ConfigMap* cm = new ConfigMap();
  std::string* currComments = 0;
  if(token.type == T::CMT_COMMENT)
    currComments = comments();
  try
  {
    while(token.type == T::CMT_KEY)
    {
      std::string key = token.value;
      nextToken();
      try
      {
        expect(T::CMT_EQ);
        if(prefixValue(token))
        {
          ConfigValue* cv = value();
          if(currComments)
            cv->setComment(*currComments);
          try
          {
            (*cm)[key] = *cv;
          }
          catch(invalid_key& ik)
          {
            P_ERROR(ik.what());
          }
          delete cv;
        }
        else
        {
          P_ERROR("Expected begin of a value (`{`, `[` or VALUE), got `" + T::type2str(token.type) + "`");
        }
      }
      catch(ParseException)
      {
        if(currComments) delete currComments;
        throw;
      }
      expect(T::CMT_SEMICOLON);
    }
    expect(T::CMT_CBR);

    if(currComments)
      delete currComments;
    currComments = 0;
    if(token.type == T::CMT_COMMENT)
      currComments = comments();
  }
  catch(ParseException)
  {
    delete cm;
    if(currComments) delete currComments;
    throw;
  }
  if(currComments) delete currComments;
  return cm;
}
Пример #3
0
bool json::Reader::ParseArray(ConfigValue& value)
{
	value.SetEmptyArray();
	
	_cur++; // Skip '['
	SkipSpaces();
	if(*_cur == ']')
	{
		_cur++;
		return true;
	}
	while(1)
	{
		ConfigValue& elem = value.Append();
		
		if(!ParseValue(elem))
			return false;
		
		SkipSpaces();

		char c = *_cur;
		if(c == ',') // Separator between elements (Optional)
		{
			_cur++;
			continue;
		}
		if(c == ']') // End of array
		{
			_cur++;
			break;
		}
	}
	return true;
}
Пример #4
0
void QuickConfig::syncProperties(QObject *object)
{
	const QMetaObject * const base = object == this ? &staticMetaObject : &QObject::staticMetaObject;
	const QMetaObject * const actual = object->metaObject();

	int propertiesCount = actual->propertyCount();
	for (int index = base->propertyCount(); index < propertiesCount; ++index) {
		QMetaProperty property = actual->property(index);
		QVariant defaultValue = property.read(object);
		QMetaType type(property.userType());

		if (type.flags() & QMetaType::PointerToQObject) {
			QObject *subObject = defaultValue.value<QObject *>();
			if (!subObject) {
				qWarning() << "QuickConfig: null value at" << this << ", property:" << property.name();
				continue;
			}
			m_config.beginGroup(QLatin1String(property.name()));
			syncProperties(subObject);
			m_config.endGroup();
			continue;
		}

		ConfigValue<QVariant> actualValue = m_config.value(QLatin1String(property.name()), defaultValue);
		property.write(object, actualValue.value());

		// Store actualValue as it's destruction will drop onChange listener
		new QuickConfigListener(m_path, m_group, property, object, actualValue, this);

		actualValue.onChange(object, [object, property, defaultValue] (const QVariant &value) {
			property.write(object, value.isValid() ? value : defaultValue);
		});
	}
}
Пример #5
0
void SimpleConfig::setString(const std::string &section, const std::string &key, const std::string &value) {
	ConfigValue *v = _find(section, key);
	if (v) {
		v->setValue(value);
	} else {
		_addValue(section, key, value);
	}
	m_modified = true;
	if (m_autosave) save();
}
Пример #6
0
std::string SimpleConfig::getString(const std::string &section, const std::string &key, std::string default_, bool set) {
	ConfigValue *v = _find(section, key);
	if (v) {
		return v->getValue();
	} else
	if (set) {
		setString(section, key, default_);
	}
	return default_;
}
Пример #7
0
double SimpleConfig::getFloat(const std::string &section, const std::string &key, double default_, bool set) {
	ConfigValue *v = _find(section, key);
	if (v) {
		return atof(v->getValue().c_str());
	} else
	if (set) {
		setFloat(section, key, default_);
	}
	return default_;
}
Пример #8
0
void SimpleConfig::setPath(const std::string &section, const std::string &key, const std::string &value) {
	ConfigValue *v = _find(section, key);
	std::string native = ospath::filter(value);
	if (v) {
		v->setValue(native);
	} else {
		_addValue(section, key, native);
	}
	m_modified = true;
	if (m_autosave) save();
}
Пример #9
0
std::string SimpleConfig::getPath(const std::string &section, const std::string &key, std::string default_, bool set) {
	ConfigValue *v = _find(section, key);
	std::string native = ospath::filter(default_);
	if (v) {
		return ospath::filter(v->getValue());
	} else
	if (set) {
		setString(section, key, native);
	}
	return native;
}
Пример #10
0
int ConfigMapParser::file(ConfigMap* configMap) throw(PARSEEXCEPTION_THROW)
{
  size_t size = configMap->length();
  std::string* currComments = 0;
  if(token.type == T::CMT_COMMENT)
    currComments = comments();
  while(token.type == T::CMT_KEY)
  {
    size_t keyLine = token.line;
    size_t keyColumn = token.column;
    std::string key = token.value;
    nextToken();
    try
    {
      expect(T::CMT_EQ);
      if(prefixValue(token))
      {
        ConfigValue* cv = value();
        if(currComments)
          cv->setComment(*currComments);
        try
        {
          (*configMap)[key] = *cv;
        }
        catch(invalid_key& ik)
        {
          std::cout<<"[EXCEPTION ERROR ] ConfigMapParser.cpp 1"<<std::endl;
          throw ParseException(keyLine, keyColumn, ik.what());
        }
        delete cv;
      }
      else
      {
        P_ERROR("Expected begin of a value (`{`, `[` or VALUE), got `" + T::type2str(token.type) + "`");
      }
    }
    catch(ParseException)
    {
      if(currComments) delete currComments;
      throw;
    }
    expect(T::CMT_SEMICOLON);

    if(currComments)
      delete currComments;
    currComments = 0;
    if(token.type == T::CMT_COMMENT)
      currComments = comments();
  }
  expect(T::CMT_EOF);
  if(currComments) delete currComments;
  return configMap->length() - size;
}
Пример #11
0
ListConfigValue* ConfigMapParser::list() throw(PARSEEXCEPTION_THROW)
{
  expect(T::CMT_ABL);
  ListConfigValue* lcv = new ListConfigValue();
  std::string* currComments = 0;
  try
  {
    if(token.type == T::CMT_COMMENT)
      currComments = comments();
    if(prefixValue(token))
    {
      ConfigValue* cv = value();
      if(currComments)
      {
        cv->setComment(*currComments);
        delete currComments;
        currComments = 0;
      }
      lcv->append(*cv);
      delete cv;
      while(token.type == T::CMT_COMMA)
      {
        nextToken();
        if(token.type == T::CMT_COMMENT)
          currComments = comments();
        if(prefixValue(token))
        {
          ConfigValue* cv = value();
          if(currComments)
          {
            cv->setComment(*currComments);
            delete currComments;
            currComments = 0;
          }
          lcv->append(*cv);
          delete cv;
        }
        else
          break;
      }
    }
    expect(T::CMT_ABR);
  }
  catch(ParseException)
  {
    if(currComments)
      delete currComments;
    delete lcv;
    throw;
  }
  return lcv;
}
Пример #12
0
bool json::Reader::ParseNumber(ConfigValue& value)
{
	bool integer = true; // Number is either integer or float
	for(const char* c = _cur; c != _end; ++c)
	{
		if((*c >= '0' && *c <= '9') || ((*c == '-' || *c == '+') && (c == _cur))) // Allow a negative sign at the start for integers
			continue;
		else if(*c == '.' || *c == 'e' || *c == 'E' || *c == '+') 
		{
			integer = false;
			break;
		}
		else
			break;
	}
	if(!integer)
		return ParseDouble(value);


	bool negative = (*_cur == '-');
	if(negative)
		_cur++;

	uint64_t number = 0;
	while(_cur != _end)
	{
		if(*_cur >= '0' && *_cur <= '9')
		{
			uint32_t digit = *_cur - '0';
			number = number * 10 + digit;
			_cur++;
		}
		else
			break;
	}
	if(negative)
	{
		value.SetInt(-int64_t(number));
	}
	else if(number <= INT64_MAX)
	{
		value.SetInt(int64_t(number));
	}
	else
	{
		value.SetUInt(number);
	}

	return true;
}
Пример #13
0
bool SimpleConfig::getBool(const std::string &section, const std::string &key, bool default_, bool set) {
	ConfigValue *v = _find(section, key);
	if (v) {
		std::string const &value = v->getValue();
		if (value == "true" || atoi(value.c_str())) {
			return true;
		} else {
			return false;
		}
	} else
	if (set) {
		setBool(section, key, default_);
	}
	return default_;
}
Пример #14
0
bool json::Reader::ParseDouble(ConfigValue& value)
{
	char* number_end;
	double number = std::strtod(_cur, &number_end);
	value.SetDouble(number);
	_cur = number_end;
	return true;
}
Пример #15
0
bool FlatConfigFile::GetValue(size_t nIndex, ConfigValue & xValue) const
{
	if(nIndex >= 0 && nIndex < m_xDataArray.size()) {
		xValue.Load(m_xDataArray.at(nIndex).c_str());
		return true;
	}
	return false;
}
Пример #16
0
int ConfigWrapper::GetAllNodeInfoFromConfig( map<wstring, wstring>& mapOperator )
{
	ConfigValue* pConfigValue = NULL;

//	pConfigValue = m_pConfigManager->SelectNodes();
	if ( NULL != pConfigValue )
	{
		pConfigValue->GetOperatorMap( mapOperator );

		delete pConfigValue;
		pConfigValue = NULL;
	}
	else
	{
		return ERROR_NEWPOINTER_FAILED;
	}

	return RETURN_SUCCESS;
}
Пример #17
0
bool ConfigFile::GetValue(const char * strKey, ConfigValue & xValue) const
{
	std::map<std::string, std::string>::iterator it;

	it = m_xDataMap.find(strKey);
	if(it != m_xDataMap.end()) {
		xValue.Load(it->second.c_str());
		return true;
	}
	return false;
}
Пример #18
0
//-------------------------------------------------------------------------------
bool json::Reader::Read(const char* doc, int64_t length, ConfigValue& root)
{
	_cur = _begin = doc;
	_end = doc + length;

	SkipSpaces();
	if(*_cur == '{')
		return ParseObject(root);
	
	// Assume root is an object
	root.SetEmptyObject();

	std::string name;
	while(1)
	{
		SkipSpaces();
		if(_cur == _end)
			break;


		name = "";
		if(!ParseString(name))
		{
			Error("Failed to parse string");
			return false;
		}

		SkipSpaces();
		if(*_cur != '=' && *_cur != ':')
		{
			Error("Expected '=' or ':'");
			return false;
		}
		_cur++;
		
		ConfigValue& elem = root[name.c_str()];
		if(!ParseValue(elem))
		{
			return false; // Failed to parse value
		}

		SkipSpaces();

		char c = *_cur;
		if(c == ',') // Separator between elements (Optional)
		{
			_cur++;
			continue;
		}
	}

	return true;
}
Пример #19
0
bool json::Reader::ParseObject(ConfigValue& value)
{
	value.SetEmptyObject();
	
	_cur++; // Skip '{'
	SkipSpaces();
	if(*_cur == '}') // Empty object
	{
		_cur++;
		return true;
	}	

	std::string name;
	while(1)
	{
		SkipSpaces();

		name = "";
		if(!ParseString(name))
		{
			Error("Failed to parse string");
			break; // Failed to parse string
		}

		SkipSpaces();
		if(*_cur != '=' && *_cur != ':')
		{
			Error("Expected '=' or ':'");
			return false;
		}
		_cur++;

		ConfigValue& elem = value[name.c_str()];
		if(!ParseValue(elem))
			break; // Failed to parse value

		SkipSpaces();

		char c = *_cur;
		if(c == ',') // Separator between elements (Optional)
		{
			_cur++;
			continue;
		}
		if(c == '}') // End of object
		{
			_cur++;
			return true;
		}		
	}	

	return false;
}
Пример #20
0
// Output a ConfigValue from the specified ConfigSource to the stream
void Configurator::config_get_command( string parameters, StreamOutput* stream ){
    string source = shift_parameter(parameters);
    string setting = shift_parameter(parameters);
    if (setting == "") { // output live setting
        setting = source;
        source = "";
        vector<uint16_t> setting_checksums = get_checksums( setting );
        ConfigValue* cv = this->kernel->config->value(setting_checksums);
        string value = "";
        if(cv->found){ value = cv->as_string(); }
        stream->printf( "live: %s is set to %s\r\n", setting.c_str(), value.c_str() );
    } else { // output setting from specified source
        uint16_t source_checksum = get_checksum( source );
        vector<uint16_t> setting_checksums = get_checksums( setting );
        for(int i=0; i < this->kernel->config->config_sources.size(); i++){
            if( this->kernel->config->config_sources[i]->is_named(source_checksum) ){
                string value = this->kernel->config->config_sources[i]->read(setting_checksums);
                stream->printf( "%s: %s is set to %s\r\n", source.c_str(), setting.c_str(), value.c_str() );
                break;
            }
        }
    }
}
Пример #21
0
// Output a ConfigValue from the specified ConfigSource to the stream
void Configurator::config_get_command( string parameters, StreamOutput *stream )
{
    string source = shift_parameter(parameters);
    string setting = shift_parameter(parameters);
    if (setting == "") { // output settings from the config-cache
        setting = source;
        source = "";
        uint16_t setting_checksums[3];
        get_checksums(setting_checksums, setting );
        THEKERNEL->config->config_cache_load(); // need to load config cache first as it is unloaded after booting
        ConfigValue *cv = THEKERNEL->config->value(setting_checksums);
        if(cv != NULL && cv->found) {
            string value = cv->as_string();
            stream->printf( "cached: %s is set to %s\r\n", setting.c_str(), value.c_str() );
        } else {
            stream->printf( "cached: %s is not in config\r\n", setting.c_str());
        }
        THEKERNEL->config->config_cache_clear();

    } else { // output setting from specified source by parsing the config file
        uint16_t source_checksum = get_checksum( source );
        uint16_t setting_checksums[3];
        get_checksums(setting_checksums, setting );
        for(unsigned int i = 0; i < THEKERNEL->config->config_sources.size(); i++) {
            if( THEKERNEL->config->config_sources[i]->is_named(source_checksum) ) {
                string value = THEKERNEL->config->config_sources[i]->read(setting_checksums);
                if(value.empty()) {
                    stream->printf( "%s: %s is not in config\r\n", source.c_str(), setting.c_str() );
                } else {
                    stream->printf( "%s: %s is set to %s\r\n", source.c_str(), setting.c_str(), value.c_str() );
                }
                break;
            }
        }
    }
}
Пример #22
0
// This is a rather ugly and slow way to delete a section.  It can leave stray
// comments in the middle of sections, but will remove the section entirely,
// even if it is multiply declared.  This whole function was a bit of an
// afterthought, since it isn't very likely to be used in the application this
// class was written for.  Sorry to all the purists out there.  :-(
void SimpleConfig::delSection(std::string const &section_name) {
	ConfigDictionary::iterator i = m_last.find(section_name);
	if (i != m_last.end()) {
		ElementList rebuild;
		ElementList::iterator at;
		bool blank = false;
		for (at = m_elements.begin(); at != m_elements.end(); at++) {
			ConfigSection *section = dynamic_cast<ConfigSection *>(*at);
			if (section && section->getName() == section_name) {
				delete *at;
				continue;
			}
			ConfigValue *value = dynamic_cast<ConfigValue *>(*at);
			if (value && value->getSection() == section_name) {
				std::string index = _hash_index(section_name, value->getKey());
				ConfigDictionary::iterator i = m_dict.find(index);
				if (i != m_dict.end()) m_dict.erase(i);
				delete *at;
				continue;
			}
			ConfigComment *comment = dynamic_cast<ConfigComment *>(*at);
			if (comment && comment->getComment() == "") {
				if (blank) {
					delete *at;
					continue;
				}
				blank = true;
			} else blank = false;
			rebuild.push_back(*at);
		}
		m_elements = rebuild;
		m_modified = true;
		if (m_autosave) save();
		m_last.erase(i);
	}
}
Пример #23
0
bool json::Reader::ParseValue(ConfigValue& value)
{
	SkipSpaces();
	bool b = true;
	char c = *_cur;
	switch(c)
	{
	case '{':
		b = ParseObject(value);
		break;
	case '[':
		b = ParseArray(value);
		break;
	case '"':
		{
			std::string str;
			b = ParseString(str);
			if(b)
				value.SetString(str.c_str());
			else
				Error("Failed to parse string");
		}
		break;
	case '0':
	case '1':
	case '2':
	case '3':
	case '4':
	case '5':
	case '6':
	case '7':
	case '8':
	case '9':
	case '-':
		b = ParseNumber(value);
		break;
	case 't': // true
		if(*(++_cur) != 'r' || *(++_cur) != 'u' || *(++_cur) != 'e')
		{
			Error("Expected \"true\"");
			return false;
		}
		++_cur;
		value.SetBool(true);
		break;
	case 'f': // false
		if(*(++_cur) != 'a' || *(++_cur) != 'l' || *(++_cur) != 's' || *(++_cur) != 'e')
		{
			Error("Expected \"false\"");
			return false;
		}
		++_cur;
		value.SetBool(false);
		break;
	case 'n': // null
		if(*(++_cur) != 'u' || *(++_cur) != 'l' || *(++_cur) != 'l')
		{
			Error("Expected \"null\"");
			return false;
		}
		++_cur;
		value.SetNull();
		break;
	default:
		b = false;
	};
	return b;
}
Пример #24
0
void json::Writer::WriteValue(const ConfigValue& node, std::stringstream& out)
{
	switch(node.Type())
	{
	case ConfigValue::NULL_VALUE:
		out << "null";
		break;
	case ConfigValue::BOOL:
		out << node.AsBool();
		break;
	case ConfigValue::INTEGER:
		out << node.AsInt64();
		break;
	case ConfigValue::UINTEGER:
		out << node.AsUInt64();
		break;
	case ConfigValue::FLOAT:
		out << node.AsDouble();
		break;
	case ConfigValue::STRING:
		out << "\"";
		json_internal::WriteString(node.AsString(), out);
		out << "\"";
		break;
	case ConfigValue::ARRAY:
		{
			out << "[";

			_ilevel++;
			int size = node.Size(); 
			for(int i = 0; i < size; ++i)
			{
				if(i != 0)
					out << ",";

				if(_format)
				{
					out << "\n";
					json_internal::WriteTabs(_ilevel, out);
				}
				WriteValue(node[i], out);
			}
			if(_format)
				out << "\n";
			_ilevel--;
			if(_format)
				json_internal::WriteTabs(_ilevel, out);
			out << "]";
		}
		break;
	case ConfigValue::OBJECT:
		{
			out << "{";
			_ilevel++;
			ConfigValue::ConstIterator it, end;
			it = node.Begin(); end = node.End();
			for( ; it != end; ++it)
			{
				if(it != node.Begin())
					out << ",";

				if(_format)
				{
					out << "\n";
					json_internal::WriteTabs(_ilevel, out);
				}
				out << "\"";
				json_internal::WriteString(it->first.c_str(), out);
				out << "\": ";
				WriteValue(it->second, out);
			}
			if(_format)
				out << "\n";
			_ilevel--;
			if(_format)
				json_internal::WriteTabs(_ilevel, out);
			out << "}";
		}
		break;
	};
}