Example #1
0
bool Config::get_allocated(const char* section, const char* key, char** ret, unsigned int& retsize) {
	retsize = 0;

	ConfigSection* cs = find_section(section);
	if (!cs) {
		errcode = CONF_ERR_SECTION;
		return false;
	}

	ConfigEntry* ce = cs->find_entry(key);
	if (!ce) {
		errcode = CONF_ERR_KEY;
		return false;
	}

	char* value = ce->value;
	retsize = ce->valuelen;

	*ret = new char[retsize + 1];
	strncpy(*ret, value, retsize);
	// terminate, since ce->valuelen does not contain terminating char
	char* p = *ret;
	p[retsize] = '\0';

	return true;
}
Example #2
0
bool Config::get(const char* section, const char* key, char* ret, unsigned int size) {
	ConfigSection* cs = find_section(section);
	if (!cs) {
		errcode = CONF_ERR_SECTION;
		return false;
	}
	ConfigEntry* ce = cs->find_entry(key);
	if (!ce) {
		errcode = CONF_ERR_KEY;
		return false;
	}
	char* value = ce->value;
	strncpy(ret, value, size);

	// again, strncpy does not terminate string if size is less that actual
	if(ce->valuelen > size)
		ret[size-1] = '\0';

	return true;
}
Example #3
0
bool Config::get_localized(const char* section, const char* key, char* ret, unsigned int size) {
	char* lang = getenv("LANG");

	// fallback
	if (!lang)
		return get(section, key, ret, size);

	// do not use default locales
	if (lang[0] == 'C' || (strncmp(lang, "en_US", 5) == 0))
		return get(section, key, ret, size);

	ConfigSection* cs = find_section(section);
	if (!cs) {
		errcode = CONF_ERR_SECTION;
		return false;
	}

	char key_buf[128];

	/*
	 * Config class can accept Name[xxx] names as
	 * keys, so we use it here; first construct
	 * a key name, and try to find it
	 */
	snprintf(key_buf, sizeof(key_buf), "%s[%s]", key, lang);
	bool found = false;

	// first try to find it with full data
	ConfigEntry* ce = cs->find_entry(key_buf);
	if (ce)
		found = true;
	else {
		/*
		 * We will try in this order:
		 * 1. [email protected]
		 * 2. lc_CC@qualifier
		 * 3. lc_CC (language code with country code)
		 * 4. lc
		 */
		char delim[] = {'.', '@', '_'};
		char* p;
		char* code;
		for (int i = 0; i < 3; i++) {
			p = strchr(lang, delim[i]);
			if (p != NULL) {
				int sz = p - lang;
				code = new char[sz+1];
				strncpy(code, lang, sz);
				// damint strncpy does not add this
				code[sz] = '\0';

				snprintf(key_buf, sizeof(key_buf), "%s[%s]", key, code);
				delete [] code;

				ce = cs->find_entry(key_buf);
				if (ce) {
					found = true;
					break;
				}
			}
		}
	}

	if (found) {
		char* value = ce->value;
		strncpy(ret, value, size);
		ret[size-1] = '\0';
		return true;
	} else
		errcode = CONF_ERR_KEY;
	return false;
}
Example #4
0
bool Config::key_exist(const char* section, const char* key) {
	ConfigSection* cs = find_section(section);
	if(!cs)
		return false;
	return (cs->find_entry(key) != NULL);
}