Ignore:
Timestamp:
08/29/11 05:50:24 (21 months ago)
Author:
reyalp
Message:

+ add support for getting and setting propcases of any size from lua, using get_prop_str and set_prop_str
+ add binstr.lua module to support converting between strings of binary values and numbers
documentation http://chdk.setepontos.com/index.php?topic=2509.msg72440#msg72440

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/core/luascript.c

    r1183 r1306  
    362362} 
    363363 
     364/* 
     365val=get_prop(id) 
     366get propcase value identified by id 
     367the propcase is read as a short and sign extended to an int 
     368*/ 
    364369static int luaCB_get_prop( lua_State* L ) 
    365370{ 
    366371  lua_pushnumber( L, shooting_get_prop( luaL_checknumber( L, 1 ) ) ); 
    367372  return 1; 
     373} 
     374 
     375/* 
     376val=get_prop_str(prop_id,length) 
     377get the value of a propertycase as a string 
     378numeric values may be extracted using string.byte or or the binstr.lua module 
     379returns the value as a string, or false if the underlying propcase call returned non-zero 
     380*/ 
     381static int luaCB_get_prop_str( lua_State* L ) { 
     382    void *buf; 
     383    unsigned size; 
     384    unsigned prop_id = luaL_checknumber( L, 1 ); 
     385    size = luaL_checknumber( L, 2 ); 
     386    buf = malloc(size); 
     387    if(!buf) { 
     388        return luaL_error( L, "malloc failed in luaCB_get_prop" ); 
     389    } 
     390    if(get_property_case(prop_id,buf,size) == 0) { 
     391        lua_pushlstring( L, buf, size ); 
     392    } else { 
     393        lua_pushboolean( L, 0); 
     394    } 
     395    free(buf); 
     396    return 1; 
    368397} 
    369398 
     
    536565} 
    537566 
     567/* 
     568set_prop(id,value) 
     569the value is treated as a short 
     570*/ 
    538571static int luaCB_set_prop( lua_State* L ) 
    539572{ 
    540   int to, to1; 
    541   to = luaL_checknumber( L, 1 ); 
    542   to1 = luaL_checknumber( L, 2 ); 
    543   shooting_set_prop(to, to1); 
    544   return 0; 
     573  shooting_set_prop(luaL_checknumber( L, 1 ), luaL_checknumber( L, 2 )); 
     574  return 0; 
     575} 
     576 
     577/* 
     578status=set_prop_str(prop_id,value) 
     579set propertycase value as a string. Length is taken from the string 
     580numeric propcase values may be assembled by setting byte values using string.char or the binstr module 
     581status: boolean - true if the underlying propcase call returns 0, otherwise false 
     582*/ 
     583static int luaCB_set_prop_str( lua_State *L ) { 
     584    int prop_id; 
     585    unsigned len; 
     586    const char *str; 
     587    prop_id = luaL_checknumber( L, 1 ); 
     588    str = luaL_checklstring( L, 2, &len ); 
     589    if(str && len > 0) { 
     590        lua_pushboolean( L, (set_property_case(prop_id,(void *)str,len) == 0)); 
     591    } else { 
     592        return luaL_error( L, "invalid value"); 
     593    } 
     594    return 1; 
    545595} 
    546596 
     
    17011751  FUNC(get_near_limit) 
    17021752  FUNC(get_prop) 
     1753  FUNC(get_prop_str) 
    17031754  FUNC(get_raw_count) 
    17041755  FUNC(get_raw_nr) 
     
    17251776  FUNC(set_nd_filter) 
    17261777  FUNC(set_prop) 
     1778  FUNC(set_prop_str) 
    17271779  FUNC(set_raw_nr) 
    17281780  FUNC(set_raw) 
Note: See TracChangeset for help on using the changeset viewer.