Esempio n. 1
0
File: lsyck.c Progetto: cobexer/RPM5
void lua_syck_emitter_handler(SyckEmitter *e, st_data_t data)
{
    struct emitter_xtra *bonus = (struct emitter_xtra *)e->bonus;
    int type = lua_type(bonus->L, -1);
    char buf[30];		/* find a better way, if possible */

    switch (type)
    {
    case LUA_TBOOLEAN:
        if (lua_toboolean(bonus->L, -1))
            strcpy(buf, "true");
        else
            strcpy(buf, "false");
        syck_emit_scalar(e, "boolean", scalar_none, 0, 0, 0, (char *)buf, strlen(buf));
        break;
    case LUA_TSTRING:
        syck_emit_scalar(e, "string", scalar_none, 0, 0, 0, (char *)lua_tostring(bonus->L, -1), lua_strlen(bonus->L, -1));
        break;
    case LUA_TNUMBER:
        /* should handle floats as well */
        snprintf(buf, sizeof(buf), "%i", (int)lua_tonumber(bonus->L, -1));
        syck_emit_scalar(e, "number", scalar_none, 0, 0, 0, buf, strlen(buf));
        break;
    case LUA_TTABLE:
        if (luaL_getn(bonus->L, -1) > 0) {			/* treat it as an array */
            syck_emit_seq(e, "table", seq_none);
            lua_pushnil(bonus->L);  /* first key */
            while (lua_next(bonus->L, -2) != 0) {
                /* `key' is at index -2 and `value' at index -1 */
                syck_emit_item(e, (st_data_t)((long)bonus->id++));
                lua_pop(bonus->L, 1);  /* removes `value'; keeps `key' for next iteration */

            }
            syck_emit_end(e);
        } else {									/* treat it as a map */
            syck_emit_map(e, "table", map_none);
            lua_pushnil(bonus->L);
            while (lua_next(bonus->L, -2) != 0) {
                lua_pushvalue(bonus->L, -2);
                syck_emit_item(e, (st_data_t)((long)bonus->id++));
                lua_pop(bonus->L, 1);
                syck_emit_item(e, (st_data_t)((long)bonus->id++));
                lua_pop(bonus->L, 1);
            }
            syck_emit_end(e);
        }
        break;
    }

    bonus->id++;
}
Esempio n. 2
0
void lua_syck_emitter_handler(SyckEmitter *e, st_data_t data)
{
	struct emitter_xtra *bonus = (struct emitter_xtra *)e->bonus;
	int type = lua_type(bonus->L, -1);
	char buf[32];		/* find a better way, if possible */
	
	switch (type)
	{
		case LUA_TBOOLEAN:
			if (lua_toboolean(bonus->L, -1))
				strcpy(buf, "true");
			else
				strcpy(buf, "false");
			syck_emit_scalar(e, "boolean", scalar_none, 0, 0, 0, (char *)buf, strlen(buf));
			break;
		case LUA_TSTRING:
			syck_emit_scalar(e, "string", scalar_none, 0, 0, 0, (char *)lua_tostring(bonus->L, -1), lua_strlen(bonus->L, -1));
			break;
		case LUA_TNUMBER:
			; lua_Number lnum; //is it an int or a float?
			lnum = lua_tonumber(bonus->L, -1);
			int asInt = lnum;
			if(asInt == lnum)
				snprintf(buf, sizeof(buf), "%i", asInt);
			else
			{
				snprintf(buf, sizeof(buf), "%f", lnum);
				/* Remove trailing zeroes after the decimal point */
				int k;
				for (k = strlen(buf) - 1; buf[k] == '0' && buf[k - 1] != '.'; k--)
					buf[k] = '\0';
			}
			syck_emit_scalar(e, "number", scalar_none, 0, 0, 0, buf, strlen(buf));
			break;
		case LUA_TTABLE:
			if (luaL_getn(bonus->L, -1) > 0) {			/* treat it as an array */
				syck_emit_seq(e, "table", seq_none);
				lua_pushnil(bonus->L);  /* first key */
				while (lua_next(bonus->L, -2) != 0) {
					/* `key' is at index -2 and `value' at index -1 */
					syck_emit_item(e, bonus->id++);
					lua_pop(bonus->L, 1);  /* removes `value'; keeps `key' for next iteration */

				}
				syck_emit_end(e);
			} else {									/* treat it as a map */
				syck_emit_map(e, "table", map_none);
				lua_pushnil(bonus->L);
				while (lua_next(bonus->L, -2) != 0) {
					lua_pushvalue(bonus->L, -2);
					syck_emit_item(e, bonus->id++);
					lua_pop(bonus->L, 1);
					syck_emit_item(e, bonus->id++);
					lua_pop(bonus->L, 1);
				}
				syck_emit_end(e);
			}
			break;
	}

	bonus->id++;
}