1 | /* |
2 | * Copyright 2008-2018 Aerospike, Inc. |
3 | * |
4 | * Portions may be licensed to Aerospike, Inc. under one or more contributor |
5 | * license agreements. |
6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
8 | * use this file except in compliance with the License. You may obtain a copy of |
9 | * the License at http://www.apache.org/licenses/LICENSE-2.0 |
10 | * |
11 | * Unless required by applicable law or agreed to in writing, software |
12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
14 | * License for the specific language governing permissions and limitations under |
15 | * the License. |
16 | */ |
17 | #include <aerospike/mod_lua_geojson.h> |
18 | #include <aerospike/as_val.h> |
19 | #include <aerospike/mod_lua_val.h> |
20 | #include <aerospike/mod_lua_reg.h> |
21 | #include <citrusleaf/alloc.h> |
22 | #include <citrusleaf/cf_byte_order.h> |
23 | #include <stddef.h> |
24 | |
25 | #include "internal.h" |
26 | |
27 | /******************************************************************************* |
28 | * MACROS |
29 | ******************************************************************************/ |
30 | |
31 | #define OBJECT_NAME "geojson" |
32 | #define CLASS_NAME "GeoJSON" |
33 | |
34 | /******************************************************************************* |
35 | * BOX FUNCTIONS |
36 | ******************************************************************************/ |
37 | |
38 | as_geojson * mod_lua_togeojson(lua_State * l, int index) { |
39 | mod_lua_box * box = mod_lua_tobox(l, index, CLASS_NAME); |
40 | return (as_geojson *) mod_lua_box_value(box); |
41 | } |
42 | |
43 | as_geojson * mod_lua_pushgeojson(lua_State * l, as_geojson * b) { |
44 | mod_lua_box * box = mod_lua_pushbox(l, MOD_LUA_SCOPE_LUA, b, CLASS_NAME); |
45 | return (as_geojson *) mod_lua_box_value(box); |
46 | } |
47 | |
48 | #if 0 |
49 | static as_geojson * mod_lua_checkgeojson(lua_State * l, int index) { |
50 | mod_lua_box * box = mod_lua_checkbox(l, index, CLASS_NAME); |
51 | return (as_geojson *) mod_lua_box_value(box); |
52 | } |
53 | #endif |
54 | |
55 | static int mod_lua_geojson_gc(lua_State * l) { |
56 | mod_lua_freebox(l, 1, CLASS_NAME); |
57 | return 0; |
58 | } |
59 | |
60 | /******************************************************************************* |
61 | * FUNCTIONS |
62 | ******************************************************************************/ |
63 | |
64 | static int mod_lua_geojson_new(lua_State * l) |
65 | { |
66 | int argc = lua_gettop(l); |
67 | if (argc != 2) { |
68 | return 0; |
69 | } |
70 | |
71 | const char * geostr = luaL_optstring(l, 2, NULL); |
72 | if (geostr == NULL) { |
73 | return 0; |
74 | } |
75 | |
76 | as_geojson * geo = as_geojson_new(cf_strdup(geostr), true); |
77 | if (geo == NULL) { |
78 | return 0; |
79 | } |
80 | |
81 | mod_lua_pushgeojson(l, geo); |
82 | return 1; |
83 | } |
84 | |
85 | static int mod_lua_geojson_tostring(lua_State * l) |
86 | { |
87 | // we expect 1 arg |
88 | if ( lua_gettop(l) != 1 ) { |
89 | lua_pushinteger(l, 0); |
90 | return 1; |
91 | } |
92 | |
93 | mod_lua_box * box = mod_lua_checkbox(l, 1, CLASS_NAME); |
94 | as_val * val = mod_lua_box_value(box); |
95 | char * str = NULL; |
96 | |
97 | if ( val ) { |
98 | str = as_val_tostring(val); |
99 | } |
100 | |
101 | if ( str ) { |
102 | lua_pushstring(l, str); |
103 | cf_free(str); |
104 | } |
105 | else { |
106 | lua_pushstring(l, "GeoJSON()" ); |
107 | } |
108 | |
109 | return 1; |
110 | } |
111 | |
112 | /****************************************************************************** |
113 | * OBJECT TABLE |
114 | *****************************************************************************/ |
115 | |
116 | static const luaL_reg geojson_object_table[] = { |
117 | {0, 0} |
118 | }; |
119 | |
120 | static const luaL_reg geojson_object_metatable[] = { |
121 | {"__call" , mod_lua_geojson_new}, |
122 | {0, 0} |
123 | }; |
124 | |
125 | /****************************************************************************** |
126 | * CLASS TABLE |
127 | *****************************************************************************/ |
128 | |
129 | static const luaL_reg geojson_class_metatable[] = { |
130 | {"__tostring" , mod_lua_geojson_tostring}, |
131 | {"__gc" , mod_lua_geojson_gc}, |
132 | {0, 0} |
133 | }; |
134 | |
135 | /****************************************************************************** |
136 | * REGISTER |
137 | *****************************************************************************/ |
138 | |
139 | int mod_lua_geojson_register(lua_State * l) { |
140 | mod_lua_reg_object(l, OBJECT_NAME, |
141 | geojson_object_table, geojson_object_metatable); |
142 | mod_lua_reg_class(l, CLASS_NAME, NULL, geojson_class_metatable); |
143 | return 1; |
144 | } |
145 | |