Esempio n. 1
0
void Config::set(const char* section, const char* key, double value) {
	ConfigSection* sc = add_section(section);
	char* locale = nls_locale_to_c();

	char tmp[32];
	snprintf(tmp, sizeof(tmp)-1, "%g", value);

	nls_locale_from_c(locale);
	sc->add_entry(key, tmp);
}
Esempio n. 2
0
void Config::set(const char* section, const char* key, long value) {
	ConfigSection* sc = add_section(section);
	char tmp[128];
	snprintf(tmp, sizeof(tmp)-1, "%ld", value);
	sc->add_entry(key, tmp);
}
Esempio n. 3
0
void Config::set(const char* section, const char* key, const char* value) {
	ConfigSection* sc = add_section(section);
	// it will create entry, if needed
	sc->add_entry(key, value);
}
Esempio n. 4
0
void Config::set(const char* section, const char* key, bool value) {
	ConfigSection* sc = add_section(section);
	const char* v = ((value) ? "1" : "0");
	sc->add_entry(key, v);
}
Esempio n. 5
0
bool Config::load(const char* fname) {
	E_ASSERT(fname != NULL);

	clear();

	FILE *f = fopen(fname, "r");
	if (!f) {
		errcode = CONF_ERR_FILE;
		return false;
	}

	// set default return values
	errcode = CONF_SUCCESS;
	bool status = true;

	// we must have at least one section
	bool sect_found = false;

	// use fixed sizes for sections and keys
	char section[ESECT_MAX];
	char keybuf[EKEY_MAX];

	// line and value can grow
	int buflen = ELINE_SIZE_START;
	char* buf = new char[buflen];

	// use the same size as for line
	int valbuflen = buflen;
	char* valbuf = new char[valbuflen];

	char *bufp;
	ConfigSection* tsect = NULL;

	while(config_getline(&buf, &buflen, f) != -1) {
		++linenum;

		bufp = buf;
		EAT_SPACES(bufp);

		// comment or empty line
		if (*bufp == COMMENT || *bufp == '\0')
			continue;

		// we found an section
		if (*bufp == SECT_OPEN) {
			sect_found = true;
			bufp++;
			if (!scan_section(bufp, section, sizeof(section))) {
				errcode = CONF_ERR_BAD;
				status = false;
				break;
			} else {
				// first check if section exists, or create if not
				tsect = find_section(section);
				if (!tsect) {
					++sectnum;
					tsect = new ConfigSection(section);
					section_list.push_back(tsect);
				}
			}
		}
		// data part
		else {
			// file without sections
			if (!sect_found) {
				errcode = CONF_ERR_SECTION;
				status = false;
				break;
			}

			/*
			 * check if size of valbuf is less than buflen;
			 * in that case make it size as buflen (better would be to use 
			 * buflen - EKEY_MAX - '=' - <spaces>, but that would complicate thing,
			 * also more size does not hurts :P)
			 */
			if(valbuflen < buflen) {
				valbuflen = buflen;
				delete [] valbuf;
				valbuf = new char[valbuflen];
			}

			if (!scan_keyvalues(bufp, keybuf, valbuf, buflen, EKEY_MAX, valbuflen)) {
				errcode = CONF_ERR_BAD;
				status = false;
				break;
			}

			E_ASSERT(tsect != NULL && "Entry without a section ?!");
			tsect->add_entry(keybuf, valbuf);
		}
	}

	fclose(f);
	delete [] buf;
	delete [] valbuf;

	return status;
}