-1

I have the following code:

lua_getglobal(L, "lgd");

lua_getfield(L, -1, "value_pos_x");
cr->value_pos_x = lua_tointeger(L, -1);

if (!lua_isinteger(L, -1))
    printf("value_pos_x allows only numbers;");

lua_getfield(L, -2, "value_pos_y");
cr->value_pos_y = lua_tointeger(L, -1);

if (!lua_isinteger(L, -1))
    printf("value_pos_y allows only numbers;");

lua_getfield(L, -3, "time");
    cr->time = lua_tointeger(L, -1);

if (!lua_isinteger(L, -1))
    printf("time allows only numbers;");

The code works perfectly. The question I wanted to know if it is possible to keep only one message and that would apply to each function for example:

lua_getglobal(L, "lgd");

lua_getfield(L, -1, "value_pos_x");
cr->value_pos_x = lua_tointeger(L, -1);

lua_getfield(L, -2, "value_pos_y");
cr->value_pos_y = lua_tointeger(L, -1);

lua_getfield(L, -3, "time");
cr->time = lua_tointeger(L, -1);

if (lua_tointeger(L, -1) != lua_isinteger(L, -1))
        printf("The entry %s is invalid;", capture_lua_getfield_name);
Chozie
  • 73
  • 1
  • 9
  • I don't understand the question. You can certainly write a "helper" function to reduce the duplication among those sets of lines for getting "integer" values but that's not exactly what it would look like. – Etan Reisner Nov 11 '15 at 15:09
  • Topic edited to better understand, what I intend to do is a message to say that lua_tointeger only accepts numbers, for example if I use a string or boolean value it shows the alert message. – Chozie Nov 11 '15 at 16:06
  • Yes, you want to be able to easily handle a handful of "integer" values and alert when they aren't valid. You can easily refactor your existing code into a helper function which lets you do that instead of duplicating the entire set of lines every time. Have you tried? I'm not sure what you think `lua_tointeger() != lua_isinteger()` is going to do for you (since they return a `lua_Integer` and a boolean `int` respectively) but checking that you got a valid integer is certainly possible. – Etan Reisner Nov 11 '15 at 16:11
  • How can I do? by checking only occurs in `lua_getfield (L, -3, "time");` the other two can't do reading the message. Another question is how can I capture the lua_getfield name for example: `The entry value_pos_x is invalid;` – Chozie Nov 11 '15 at 16:18
  • A macro can replace those lines as-is (and include the final lines and error message too). A function could replace everything but the assignment (and that could be done with the return value of the function or similar). – Etan Reisner Nov 11 '15 at 16:30
  • I did not understand, could you give an example? – Chozie Nov 11 '15 at 16:38

1 Answers1

2

A macro something like this (untested and written in SO editing box):

#define GET_INTEGER_WARN(ind, fld) do { \
    lua_getfield(L, ind, #fld); \
    cr->fld = lua_tointeger(L, -1); \
    \
    if (!lua_isinteger(L, -1)) \
        printf(#fld" allows only numbers;"); \
    } while (0)

Would let you do something like this:

lua_getglobal(L, "lgd");

GET_INTEGER_WARN(-1, value_pos_x);

GET_INTEGER_WARN(-2, value_pos_y);

GET_INTEGER_WARN(-3, time);

A function like this (same caveats as before):

lua_Integer
get_integer_warn(lua_State *L, int ind, char *fld)
{
    lua_getfield(L, ind, fld);

    if (!lua_isinteger(L, -1))
        printf("%s allows only numbers", fld);

    return lua_tointeger(L, -1);
}

Would let you do something like this:

cr->value_pos_x = get_integer_warn(L, -1, "value_pos_x")
cr->value_pos_y = get_integer_warn(L, -2, "value_pos_y")
cr->time = get_integer_warn(L, -3, "time")
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • Etan you helped me understand how to proceed. Now one last question if the function cr-> is different from lua_getfield as I can do the checking. E.g: `lua_getfield(L, -1, "value_time_expire"); cr->expire = lua_tointeger(L, -1);` – Chozie Nov 11 '15 at 17:09
  • The macro version requires that they are the same. The function version does not. The function version leaves assignment to your `cr` structure separate from the lua-side key name. You could absolutely do `cr->expire = get_integer_warn(L, -3, "value_time_expire")` or whatever. – Etan Reisner Nov 11 '15 at 17:22
  • I Understand, thank you Etan for explaining, problem solved. – Chozie Nov 11 '15 at 17:29