Exemple #1
0
void IniConfig::Read(const FileSystem::FileData &data)
{
	StringRange buffer = data.AsStringRange();
	buffer = buffer.StripUTF8BOM();

	while (!buffer.Empty()) {
		StringRange line = buffer.ReadLine().StripSpace();

		// if the line is a comment, skip it
		if (line.Empty() || (line[0] == '#')) continue;
		const char *kend = line.FindChar('=');
		// if there's no '=' sign, skip the line
		if (kend == line.end) {
			fprintf(stderr, "WARNING: ignoring invalid line in config file:\n   '%.*s'\n", int(line.Size()), line.begin);
			continue;
		}

		StringRange key(line.begin, kend);
		StringRange value(kend + 1, line.end);
		// strip whitespace
		key.end = key.RFindNonSpace();
		value = value.StripSpace();

		m_map[key.ToString()] = value.ToString();
	}
}
Exemple #2
0
static void pi_lua_dofile(lua_State *l, const FileSystem::FileData &code)
{
	assert(l);
	LUA_DEBUG_START(l);
	// XXX make this a proper protected call (after working out the implications -- *sigh*)
	lua_pushcfunction(l, &pi_lua_panic);
	if (luaL_loadbuffer(l, code.GetData(), code.GetSize(), code.GetInfo().GetPath().c_str())) {
		pi_lua_panic(l);
	} else {
		int ret = lua_pcall(l, 0, 0, -2);
		if (ret) {
			const char *emsg = lua_tostring(l, -1);
			if (emsg) { fprintf(stderr, "lua error: %s\n", emsg); }
			switch (ret) {
				case LUA_ERRRUN:
					fprintf(stderr, "Lua runtime error in pi_lua_dofile('%s')\n",
							code.GetInfo().GetAbsolutePath().c_str());
					break;
				case LUA_ERRMEM:
					fprintf(stderr, "Memory allocation error in Lua pi_lua_dofile('%s')\n",
							code.GetInfo().GetAbsolutePath().c_str());
					break;
				case LUA_ERRERR:
					fprintf(stderr, "Error running error handler in pi_lua_dofile('%s')\n",
							code.GetInfo().GetAbsolutePath().c_str());
					break;
				default: abort();
			}
			lua_pop(l, 1);
		}
	}
	lua_pop(l, 1);
	LUA_DEBUG_END(l, 0);
}
Exemple #3
0
void IniConfig::Read(const FileSystem::FileData &data)
{
	StringRange buffer = data.AsStringRange();
	buffer = buffer.StripUTF8BOM();

	std::string section_name;
	MapType *section_map = 0;

	while (!buffer.Empty()) {
		StringRange line = buffer.ReadLine().StripSpace();

		// if the line is a comment, skip it
		if (line.Empty() || (line[0] == '#')) continue;

		// check for a section header
		if ((line.Size() >= 2) && (line[0] == '[') && (line.end[-1] == ']')) {
			++line.begin;
			--line.end;
			section_name = line.ToString();
			section_map = 0;
			continue;
		}

		const char *kend = line.FindChar('=');
		// if there's no '=' sign, skip the line
		if (kend == line.end) {
			Output("WARNING: ignoring invalid line in config file:\n   '%.*s'\n", int(line.Size()), line.begin);
			continue;
		}

		StringRange key(line.begin, kend);
		StringRange value(kend + 1, line.end);
		// strip whitespace
		key.end = key.RFindNonSpace();
		value = value.StripSpace();

		if (!section_map)
			section_map = &m_map[section_name];

		(*section_map)[key.ToString()] = value.ToString();
	}
}