Example #1
0
	void Colour::addStandardNamedColoursLua(const char *filename)
	{
		if (filename != NULL)
		{
			LuaState lua(false);
			if (lua.loadFile(filename))
			{
				lua_getglobal(lua, "colours");
				if (lua_istable(lua, -1))
				{
					/* table is in the stack at index 't' */
					lua_pushnil(lua);  /* first key */
					while (lua_next(lua, -2) != 0) {
						/* uses 'key' (at index -2) and 'value' (at index -1) */
						if (lua_isstring(lua, -2))
						{
							Colour c;
							bool parsed = false;
							const char *name = lua_tostring(lua, -2);
							if (lua_isstring(lua, -1))
							{
								const char *value = lua_tostring(lua, -1);
								c.parseFromString(value);
								parsed = true;
							}
							else if (lua_isnumber(lua, -1))
							{
								int value = static_cast<unsigned int>(lua_tointeger(lua, -1));
								c.parseFromUint(value);
								parsed = true;
							}
							if (parsed)
							{
								string lowerName = Utils::toLowerCase(name);
								addNamedColour(lowerName, c);
							}
						}
						/* removes 'value'; keeps 'key' for next iteration */
						lua_pop(lua, 1);
					}
				}
			}
			lua.close();
		}
	}