webmcp
changeset 124:ece4e6f683c9
Fixed namespace problems in JSON library code
author | jbe |
---|---|
date | Sat Jul 26 23:40:43 2014 +0200 (2014-07-26) |
parents | 402fce94f98c |
children | e3e5bf890aad |
files | libraries/json/json.c |
line diff
1.1 --- a/libraries/json/json.c Sat Jul 26 05:27:42 2014 +0200 1.2 +++ b/libraries/json/json.c Sat Jul 26 23:40:43 2014 +0200 1.3 @@ -5,22 +5,23 @@ 1.4 1.5 #define JSON_UPVAL_ARYLEN lua_upvalueindex(1) 1.6 #define JSON_UPVAL_NULLS lua_upvalueindex(2) 1.7 +#define JSON_UPVAL_METATABLE lua_upvalueindex(3) 1.8 1.9 -#define JSON_VALUE 0 1.10 -#define JSON_OBJECT_KEY 1 1.11 -#define JSON_OBJECT_KEY_TERMINATOR 2 1.12 -#define JSON_OBJECT_VALUE 3 1.13 -#define JSON_OBJECT_SEPARATOR 4 1.14 -#define JSON_ARRAY_VALUE 5 1.15 -#define JSON_ARRAY_SEPARATOR 6 1.16 -#define JSON_END 7 1.17 +#define JSON_STATE_VALUE 0 1.18 +#define JSON_STATE_OBJECT_KEY 1 1.19 +#define JSON_STATE_OBJECT_KEY_TERMINATOR 2 1.20 +#define JSON_STATE_OBJECT_VALUE 3 1.21 +#define JSON_STATE_OBJECT_SEPARATOR 4 1.22 +#define JSON_STATE_ARRAY_VALUE 5 1.23 +#define JSON_STATE_ARRAY_SEPARATOR 6 1.24 +#define JSON_STATE_END 7 1.25 1.26 static int json_import(lua_State *L) { 1.27 const char *str; 1.28 size_t total; 1.29 size_t pos = 0; 1.30 size_t level = 0; 1.31 - int mode = JSON_VALUE; 1.32 + int mode = JSON_STATE_VALUE; 1.33 char c; 1.34 luaL_Buffer luabuf; 1.35 char *cbuf; 1.36 @@ -33,14 +34,14 @@ 1.37 while (c = str[pos], c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\f') pos++; 1.38 switch (c) { 1.39 case 0: 1.40 - if (mode == JSON_END) return 1; 1.41 + if (mode == JSON_STATE_END) return 1; 1.42 json_import_unexpected_eof: 1.43 lua_pushnil(L); 1.44 if (level == 0) lua_pushliteral(L, "Empty string"); 1.45 else lua_pushliteral(L, "Unexpected end of JSON document"); 1.46 return 2; 1.47 case '{': 1.48 - if (mode != JSON_VALUE && mode != JSON_OBJECT_VALUE && mode != JSON_ARRAY_VALUE) 1.49 + if (mode != JSON_STATE_VALUE && mode != JSON_STATE_OBJECT_VALUE && mode != JSON_STATE_ARRAY_VALUE) 1.50 goto json_import_syntax_error; 1.51 pos++; 1.52 lua_newtable(L); 1.53 @@ -48,11 +49,11 @@ 1.54 lua_pushvalue(L, -2); 1.55 lua_pushvalue(L, -2); 1.56 lua_rawset(L, JSON_UPVAL_NULLS); 1.57 - mode = JSON_OBJECT_KEY; 1.58 + mode = JSON_STATE_OBJECT_KEY; 1.59 level++; 1.60 goto json_import_loop; 1.61 case '[': 1.62 - if (mode != JSON_VALUE && mode != JSON_OBJECT_VALUE && mode != JSON_ARRAY_VALUE) 1.63 + if (mode != JSON_STATE_VALUE && mode != JSON_STATE_OBJECT_VALUE && mode != JSON_STATE_ARRAY_VALUE) 1.64 goto json_import_syntax_error; 1.65 pos++; 1.66 lua_newtable(L); 1.67 @@ -61,15 +62,15 @@ 1.68 lua_pushvalue(L, -2); 1.69 lua_rawset(L, JSON_UPVAL_NULLS); 1.70 lua_pushinteger(L, 0); // length of array (since it may contain nil's) 1.71 - mode = JSON_ARRAY_VALUE; 1.72 + mode = JSON_STATE_ARRAY_VALUE; 1.73 level++; 1.74 goto json_import_loop; 1.75 case '}': 1.76 - if (mode != JSON_OBJECT_KEY && mode != JSON_OBJECT_SEPARATOR) 1.77 + if (mode != JSON_STATE_OBJECT_KEY && mode != JSON_STATE_OBJECT_SEPARATOR) 1.78 goto json_import_syntax_error; 1.79 goto json_import_close; 1.80 case ']': 1.81 - if (mode != JSON_ARRAY_VALUE && mode != JSON_ARRAY_SEPARATOR) 1.82 + if (mode != JSON_STATE_ARRAY_VALUE && mode != JSON_STATE_ARRAY_SEPARATOR) 1.83 goto json_import_syntax_error; 1.84 lua_pushvalue(L, -2); // use array table as key 1.85 lua_insert(L, -2); // use length of array as value 1.86 @@ -80,26 +81,26 @@ 1.87 lua_pop(L, 1); // pop table that stores set of NULL values 1.88 if (--level) { 1.89 if (lua_type(L, -2) == LUA_TNUMBER) { 1.90 - mode = JSON_ARRAY_VALUE; 1.91 + mode = JSON_STATE_ARRAY_VALUE; 1.92 } else { 1.93 - mode = JSON_OBJECT_VALUE; 1.94 + mode = JSON_STATE_OBJECT_VALUE; 1.95 } 1.96 goto json_import_process_value; 1.97 } else { 1.98 - mode = JSON_END; 1.99 + mode = JSON_STATE_END; 1.100 } 1.101 goto json_import_loop; 1.102 case ':': 1.103 - if (mode != JSON_OBJECT_KEY_TERMINATOR) 1.104 + if (mode != JSON_STATE_OBJECT_KEY_TERMINATOR) 1.105 goto json_import_syntax_error; 1.106 pos++; 1.107 - mode = JSON_OBJECT_VALUE; 1.108 + mode = JSON_STATE_OBJECT_VALUE; 1.109 goto json_import_loop; 1.110 case ',': 1.111 - if (mode == JSON_OBJECT_SEPARATOR) { 1.112 - mode = JSON_OBJECT_KEY; 1.113 - } else if (mode == JSON_ARRAY_SEPARATOR) { 1.114 - mode = JSON_ARRAY_VALUE; 1.115 + if (mode == JSON_STATE_OBJECT_SEPARATOR) { 1.116 + mode = JSON_STATE_OBJECT_KEY; 1.117 + } else if (mode == JSON_STATE_ARRAY_SEPARATOR) { 1.118 + mode = JSON_STATE_ARRAY_VALUE; 1.119 } else { 1.120 goto json_import_syntax_error; 1.121 } 1.122 @@ -179,20 +180,20 @@ 1.123 } 1.124 json_import_process_value: 1.125 switch (mode) { 1.126 - case JSON_OBJECT_KEY: 1.127 + case JSON_STATE_OBJECT_KEY: 1.128 if (lua_type(L, -1) != LUA_TSTRING) goto json_import_syntax_error; 1.129 - mode = JSON_OBJECT_KEY_TERMINATOR; 1.130 + mode = JSON_STATE_OBJECT_KEY_TERMINATOR; 1.131 goto json_import_loop; 1.132 - case JSON_OBJECT_VALUE: 1.133 + case JSON_STATE_OBJECT_VALUE: 1.134 if (lua_isnil(L, -1)) { 1.135 lua_pushvalue(L, -2); 1.136 lua_pushboolean(L, 1); 1.137 lua_rawset(L, -5); 1.138 } 1.139 lua_rawset(L, -4); 1.140 - mode = JSON_OBJECT_SEPARATOR; 1.141 + mode = JSON_STATE_OBJECT_SEPARATOR; 1.142 goto json_import_loop; 1.143 - case JSON_ARRAY_VALUE: 1.144 + case JSON_STATE_ARRAY_VALUE: 1.145 aryidx = lua_tointeger(L, -2) + 1; 1.146 if (lua_isnil(L, -1)) { 1.147 lua_pushinteger(L, aryidx); 1.148 @@ -202,10 +203,10 @@ 1.149 lua_rawseti(L, -4, aryidx); 1.150 lua_pop(L, 1); 1.151 lua_pushinteger(L, aryidx); 1.152 - mode = JSON_ARRAY_SEPARATOR; 1.153 + mode = JSON_STATE_ARRAY_SEPARATOR; 1.154 goto json_import_loop; 1.155 - case JSON_VALUE: 1.156 - mode = JSON_END; 1.157 + case JSON_STATE_VALUE: 1.158 + mode = JSON_STATE_END; 1.159 goto json_import_loop; 1.160 } 1.161 json_import_syntax_error: