superstudio 0.8.2102

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,19 @@
1
+ #ifndef JSON_VALUE_
2
+ #define JSON_VALUE_
3
+
4
+ #include <stdint.h>
5
+ #include <stdlib.h>
6
+ #include <string.h>
7
+ #include "json_builder.h"
8
+ #include "finalize.h"
9
+
10
+ void set_key_values(
11
+ JSONDocumentBuilder* builder,
12
+ unsigned long* string_sizes,
13
+ char** row,
14
+ unsigned long visible_depth,
15
+ JSONLevelBuilder* level_definitions,
16
+ JSONObject* parent_object
17
+ );
18
+
19
+ #endif
@@ -0,0 +1,157 @@
1
+ #include "json_value_array.h"
2
+ #include "fnv_64.h"
3
+ #include "hash_linked_list.h"
4
+ #include "ss_alloc.h"
5
+
6
+ void add_to_array(
7
+ JSONDocumentBuilder* builder,
8
+ char* string_value,
9
+ unsigned long string_size,
10
+ char* column_name,
11
+ unsigned long column_name_length,
12
+ unsigned long repeatable,
13
+ unsigned long quoted,
14
+ JSONObject* parent_object
15
+ )
16
+ {
17
+ ArrayValueJSON* array_search;
18
+ ArrayValueListItem* array_values_search;
19
+ unsigned char item_added = 0;
20
+ int repeat_found = 0;
21
+
22
+ array_search = parent_object->array_values;
23
+ while(!item_added) {
24
+ if (!array_search->set_flag) {
25
+ array_search->name = column_name;
26
+ array_search->name_characters = column_name_length;
27
+ array_search->quoted = quoted;
28
+
29
+ array_search->value_list = (ArrayValueListItem*)ss_alloc(builder->memory_stack, 1, sizeof(ArrayValueListItem));
30
+ array_search->last_list_value = array_search->value_list;
31
+ array_search->next_value = (ArrayValueJSON*)ss_alloc(builder->memory_stack, 1, sizeof(ArrayValueJSON));
32
+
33
+ array_search->set_flag = 1;
34
+ parent_object->value_array_count += 1;
35
+
36
+ builder->json_char_count += column_name_length;
37
+ builder->json_char_count += 6; // Two quotes, a colon, starting and ending brackets, a comma (might want to add the comma space somewhere else)
38
+ }
39
+
40
+ if (!memcmp(array_search->name, column_name, column_name_length) && (array_search->name_characters == column_name_length) && !item_added) {
41
+ builder->json_char_count += 1; // This array already existed, it needs a comma
42
+ if (repeatable) {
43
+ initialize_value_item(builder, array_search->last_list_value, string_size, string_value, quoted);
44
+ array_search->last_list_value = array_search->last_list_value->next_value;
45
+ item_added = 1;
46
+ } else {
47
+ array_values_search = array_search->value_list;
48
+ while(array_values_search->set_flag && !repeat_found) {
49
+ if (((memcmp(array_values_search->array_value, string_value, string_size) == 0) && (array_values_search->value_characters == string_size))) {
50
+ repeat_found = 1;
51
+ }
52
+ array_values_search = array_values_search->next_value;
53
+ }
54
+ if (!repeat_found) {
55
+ initialize_value_item(builder, array_search->last_list_value, string_size, string_value, quoted);
56
+ array_search->last_list_value = array_search->last_list_value->next_value;
57
+ item_added = 1;
58
+ }
59
+ }
60
+ }
61
+ array_search = array_search->next_value;
62
+ }
63
+ }
64
+
65
+ void initialize_value_item(JSONDocumentBuilder* builder, ArrayValueListItem *target, unsigned long value_characters, char* array_value, unsigned long quoted)
66
+ {
67
+ target->next_value = (ArrayValueListItem*)ss_alloc(builder->memory_stack, 1, sizeof(ArrayValueListItem));
68
+ target->value_characters = value_characters;
69
+
70
+ target->array_value = array_value;
71
+
72
+ target->set_flag = 1;
73
+
74
+ builder->json_char_count += value_characters;
75
+ if (quoted) { builder->json_char_count += 2; }
76
+ }
77
+
78
+ void set_value_arrays(
79
+ JSONDocumentBuilder* builder,
80
+ JSONLevelBuilder* level_definitions,
81
+ unsigned long column_count,
82
+ char** row_strings,
83
+ unsigned long* string_sizes,
84
+ unsigned long accessing_depth,
85
+ JSONObject* parent_object
86
+ )
87
+ {
88
+ unsigned long counter = 0;
89
+ while(counter < column_count) {
90
+ if ((level_definitions->depth_array[counter] == (accessing_depth + 1)) && (level_definitions->do_not_hash[counter])) {
91
+ add_to_array(builder,
92
+ row_strings[counter],
93
+ string_sizes[counter],
94
+ level_definitions->column_names[counter],
95
+ level_definitions->column_name_lengths[counter],
96
+ level_definitions->repeating_array_columns[counter],
97
+ level_definitions->quote_array[counter],
98
+ parent_object
99
+ );
100
+ }
101
+ counter++;
102
+ }
103
+ }
104
+
105
+ unsigned long finalize_value_array(JSONDocumentBuilder* builder, ArrayValueJSON* value_arrays, unsigned long counter)
106
+ {
107
+ ArrayValueListItem* value_array_values = NULL;
108
+
109
+ while(value_arrays->set_flag) {
110
+ value_array_values = value_arrays->value_list;
111
+
112
+ memcpy(builder->resulting_json + counter, oq, 1);
113
+ counter++;
114
+
115
+ memcpy(builder->resulting_json + counter, value_arrays->name, value_arrays->name_characters);
116
+ counter += value_arrays->name_characters;
117
+
118
+ memcpy(builder->resulting_json + counter, oqc, 2);
119
+ counter += 2;
120
+
121
+ memcpy(builder->resulting_json + counter, ob, 1);
122
+ counter++;
123
+
124
+ while(value_array_values->set_flag) {
125
+ if (value_arrays->quoted) {
126
+ memcpy(builder->resulting_json + counter, oq, 1);
127
+ counter++;
128
+ }
129
+
130
+ memcpy(builder->resulting_json + counter, value_array_values->array_value, value_array_values->value_characters);
131
+ counter += value_array_values->value_characters;
132
+
133
+ if (value_arrays->quoted) {
134
+ memcpy(builder->resulting_json + counter, oq, 1);
135
+ counter++;
136
+ }
137
+
138
+ if (value_array_values->next_value->set_flag) {
139
+ memcpy(builder->resulting_json + counter, cm, 1);
140
+ counter++;
141
+ }
142
+
143
+ value_array_values = value_array_values->next_value;
144
+ }
145
+
146
+ memcpy(builder->resulting_json + counter, cb, 1);
147
+ counter++;
148
+
149
+ if (value_arrays->next_value->set_flag) {
150
+ memcpy(builder->resulting_json + counter, cm, 1);
151
+ counter++;
152
+ }
153
+
154
+ value_arrays = value_arrays->next_value;
155
+ }
156
+ return counter;
157
+ }
@@ -0,0 +1,23 @@
1
+ #ifndef ARRAY_JSON_VALUE_
2
+ #define ARRAY_JSON_VALUE_
3
+
4
+ #include <stdint.h>
5
+ #include <stdlib.h>
6
+ #include <string.h>
7
+ #include "json_builder.h"
8
+ #include "finalize.h"
9
+
10
+ void add_to_array(
11
+ JSONDocumentBuilder* builder,
12
+ char* string_value,
13
+ unsigned long string_size,
14
+ char* column_name,
15
+ unsigned long column_name_length,
16
+ unsigned long repeatable,
17
+ unsigned long quoted,
18
+ JSONObject* parent_object
19
+ );
20
+ void set_value_arrays(JSONDocumentBuilder* builder, JSONLevelBuilder* level_definitions, unsigned long column_count, char** row_strings, unsigned long* string_sizes, unsigned long accessing_depth, JSONObject* parent_object);
21
+ void initialize_value_item(JSONDocumentBuilder* builder, ArrayValueListItem *target, unsigned long value_characters, char* array_value, unsigned long quoted);
22
+
23
+ #endif
@@ -0,0 +1,334 @@
1
+ #include <jsonbroker.h>
2
+ #include "ss_alloc.h"
3
+
4
+ static void deallocate_broker(void * broker)
5
+ {
6
+ JSONDocumentBuilder *builder;
7
+
8
+ builder = (JSONDocumentBuilder*)broker;
9
+
10
+ collapse_stack(builder->memory_stack);
11
+ free(builder->memory_stack->stack_top);
12
+ builder->memory_stack->stack_top = NULL;
13
+ free(builder->memory_stack);
14
+ builder->memory_stack = NULL;
15
+
16
+ free(broker);
17
+ }
18
+
19
+ void json_broker_mark(JSONDocumentBuilder *builder)
20
+ {
21
+
22
+ }
23
+
24
+ static VALUE json_broker_allocate(VALUE klass)
25
+ {
26
+ JSONDocumentBuilder *builder = (JSONDocumentBuilder*)malloc(sizeof(JSONDocumentBuilder));
27
+ json_builder_initialize(builder);
28
+ return Data_Wrap_Struct(klass, json_broker_mark, deallocate_broker, builder);
29
+ }
30
+
31
+ static VALUE json_broker_set_mapper(VALUE self, VALUE tags)
32
+ {
33
+ JSONDocumentBuilder *builder;
34
+ unsigned long length = RARRAY_LENINT(tags);
35
+
36
+ Check_Type(tags, T_ARRAY);
37
+ Data_Get_Struct(self, JSONDocumentBuilder, builder);
38
+ set_column_count(builder, length);
39
+
40
+ struct RArray* cTags = RARRAY(tags);
41
+ VALUE* tag_pointer = RARRAY_PTR(cTags);
42
+ unsigned long counter = 0;
43
+
44
+ struct RString* in_loop_rstring;
45
+ char* in_loop_string;
46
+ unsigned long in_loop_string_size;
47
+ char* mapping_array[length];
48
+ unsigned long mapping_array_lengths[length];
49
+
50
+ while(counter < length) {
51
+ Check_Type(tag_pointer[counter], T_STRING);
52
+ in_loop_rstring = RSTRING(tag_pointer[counter]);
53
+ in_loop_string = RSTRING_PTR(in_loop_rstring);
54
+ in_loop_string_size = RSTRING_LEN(in_loop_rstring);
55
+ mapping_array[counter] = in_loop_string;
56
+ mapping_array_lengths[counter] = in_loop_string_size;
57
+ counter++;
58
+ }
59
+ set_mapping_array(builder, mapping_array, mapping_array_lengths);
60
+ return Qnil;
61
+ }
62
+
63
+ static VALUE json_broker_set_column_names(VALUE self, VALUE names)
64
+ {
65
+ JSONDocumentBuilder *builder;
66
+ unsigned long length = RARRAY_LENINT(names);
67
+
68
+ Check_Type(names, T_ARRAY);
69
+ Data_Get_Struct(self, JSONDocumentBuilder, builder);
70
+
71
+ struct RArray* cNames = RARRAY(names);
72
+ VALUE* name_pointer = RARRAY_PTR(cNames);
73
+
74
+ struct RString* in_loop_rstring;
75
+ char* in_loop_string;
76
+ unsigned long in_loop_string_size;
77
+ char* names_array[length];
78
+ unsigned long names_sizes[length];
79
+ unsigned long counter = 0;
80
+
81
+ while(counter < length) {
82
+ Check_Type(name_pointer[counter], T_STRING);
83
+ in_loop_rstring = RSTRING(name_pointer[counter]);
84
+ in_loop_string = RSTRING_PTR(in_loop_rstring);
85
+ in_loop_string_size = RSTRING_LEN(in_loop_rstring);
86
+ names_array[counter] = in_loop_string;
87
+ names_sizes[counter] = in_loop_string_size;
88
+ counter++;
89
+ }
90
+ set_column_names_sizes(builder, names_array, names_sizes);
91
+ return Qnil;
92
+ }
93
+
94
+ static VALUE json_broker_set_single_node_names(VALUE self, VALUE keys)
95
+ {
96
+ JSONDocumentBuilder* builder;
97
+ unsigned long length = RARRAY_LENINT(keys);
98
+
99
+ Check_Type(keys, T_ARRAY);
100
+ Data_Get_Struct(self, JSONDocumentBuilder, builder);
101
+
102
+ struct RArray* cKeys = RARRAY(keys);
103
+ VALUE* key_pointer = RARRAY_PTR(cKeys);
104
+
105
+ struct RString* in_loop_rstring;
106
+ char* in_loop_string;
107
+ unsigned long in_loop_string_size;
108
+ char* key_array[length];
109
+ unsigned long key_sizes[length];
110
+ unsigned long counter = 0;
111
+
112
+ while(counter < length) {
113
+ Check_Type(key_pointer[counter], T_STRING);
114
+ in_loop_rstring = RSTRING(key_pointer[counter]);
115
+ in_loop_string = RSTRING_PTR(in_loop_rstring);
116
+ in_loop_string_size = RSTRING_LEN(in_loop_rstring);
117
+ key_array[counter] = in_loop_string;
118
+ key_sizes[counter] = in_loop_string_size;
119
+
120
+ counter++;
121
+ }
122
+ set_single_node_key_names(builder, key_array, key_sizes);
123
+ return Qnil;
124
+ }
125
+
126
+ static VALUE json_broker_set_array_node_names(VALUE self, VALUE keys)
127
+ {
128
+ JSONDocumentBuilder* builder;
129
+ unsigned long length = RARRAY_LENINT(keys);
130
+
131
+ Check_Type(keys, T_ARRAY);
132
+ Data_Get_Struct(self, JSONDocumentBuilder, builder);
133
+
134
+ struct RArray* cKeys = RARRAY(keys);
135
+ VALUE* key_pointer = RARRAY_PTR(cKeys);
136
+
137
+ struct RString* in_loop_rstring;
138
+ char* in_loop_string;
139
+ unsigned long in_loop_string_size;
140
+ char* key_array[length];
141
+ unsigned long key_sizes[length];
142
+ unsigned long counter = 0;
143
+
144
+ while(counter < length) {
145
+ Check_Type(key_pointer[counter], T_STRING);
146
+ in_loop_rstring = RSTRING(key_pointer[counter]);
147
+ in_loop_string = RSTRING_PTR(in_loop_rstring);
148
+ in_loop_string_size = RSTRING_LEN(in_loop_rstring);
149
+ key_array[counter] = in_loop_string;
150
+ key_sizes[counter] = in_loop_string_size;
151
+
152
+ counter++;
153
+ }
154
+ set_array_node_key_names(builder, key_array, key_sizes);
155
+ return Qnil;
156
+ }
157
+
158
+ static VALUE json_broker_set_quotes(VALUE self, VALUE quotes)
159
+ {
160
+ JSONDocumentBuilder *builder;
161
+ Check_Type(quotes, T_ARRAY);
162
+ Data_Get_Struct(self, JSONDocumentBuilder, builder);
163
+
164
+ unsigned long length = get_column_count(builder);
165
+ struct RArray* cQuotes = RARRAY(quotes);
166
+ VALUE* quotes_pointer = RARRAY_PTR(cQuotes);
167
+ unsigned long quotes_array[length];
168
+ unsigned long counter = 0;
169
+
170
+ while(counter < length) {
171
+ quotes_array[counter] = FIX2LONG(quotes_pointer[counter]);
172
+ counter++;
173
+ }
174
+ set_quote_array(builder, quotes_array);
175
+ return Qnil;
176
+ }
177
+
178
+ static VALUE json_broker_set_hashing(VALUE self, VALUE do_not_hash)
179
+ {
180
+ JSONDocumentBuilder *builder;
181
+ Check_Type(do_not_hash, T_ARRAY);
182
+ Data_Get_Struct(self, JSONDocumentBuilder, builder);
183
+ unsigned long length = get_column_count(builder);
184
+
185
+ struct RArray* cDoNotHash = RARRAY(do_not_hash);
186
+ VALUE* hashing_pointer = RARRAY_PTR(cDoNotHash);
187
+ unsigned long hashing_array[length];
188
+ unsigned long counter = 0;
189
+
190
+ while(counter < length) {
191
+ hashing_array[counter] = FIX2LONG(hashing_pointer[counter]);
192
+ counter++;
193
+ }
194
+ set_hashing_array(builder, hashing_array);
195
+ return Qnil;
196
+ }
197
+
198
+ static VALUE json_broker_set_depths(VALUE self, VALUE depths, VALUE real_depths)
199
+ {
200
+ JSONDocumentBuilder *builder;
201
+ Check_Type(depths, T_ARRAY);
202
+ Data_Get_Struct(self, JSONDocumentBuilder, builder);
203
+
204
+ unsigned long length = get_column_count(builder);
205
+ struct RArray* cDepths = RARRAY(depths);
206
+ struct RArray* cRealDepths = RARRAY(real_depths);
207
+ VALUE* depths_pointer = RARRAY_PTR(cDepths);
208
+ VALUE* real_depths_pointer = RARRAY_PTR(cRealDepths);
209
+
210
+ unsigned long depths_array[length];
211
+ unsigned long real_depths_array[length];
212
+ unsigned long counter = 0;
213
+ unsigned long max_depth = 0;
214
+ unsigned long max_real_depth = 0;
215
+
216
+ while(counter < length) {
217
+ depths_array[counter] = FIX2LONG(depths_pointer[counter]);
218
+ real_depths_array[counter] = FIX2LONG(real_depths_pointer[counter]);
219
+ if (depths_array[counter] > max_depth) {
220
+ max_depth = depths_array[counter];
221
+ }
222
+ if (real_depths_array[counter] > max_real_depth) {
223
+ max_real_depth = real_depths_array[counter];
224
+ }
225
+ counter++;
226
+ }
227
+ set_depth_array(builder, depths_array, max_depth, real_depths_array, max_real_depth);
228
+ return Qnil;
229
+ }
230
+
231
+ static VALUE json_broker_get_mapper_length(VALUE self)
232
+ {
233
+ JSONDocumentBuilder *builder;
234
+ Data_Get_Struct(self, JSONDocumentBuilder, builder);
235
+ unsigned long get_count = get_column_count(builder);
236
+ VALUE count = LONG2FIX(get_count);
237
+ return count;
238
+ }
239
+
240
+ static VALUE json_broker_set_row_count(VALUE self, VALUE row_count)
241
+ {
242
+ JSONDocumentBuilder *builder;
243
+ Data_Get_Struct(self, JSONDocumentBuilder, builder);
244
+ unsigned long set_count = FIX2LONG(row_count);
245
+ set_row_count(builder, set_count);
246
+ return Qnil;
247
+ }
248
+
249
+ static VALUE json_broker_set_repeating_array_columns(VALUE self, VALUE repeating)
250
+ {
251
+ JSONDocumentBuilder *builder;
252
+ Data_Get_Struct(self, JSONDocumentBuilder, builder);
253
+
254
+ unsigned long length = get_column_count(builder);
255
+ struct RArray* cRepeats = RARRAY(repeating);
256
+ VALUE* repeating_pointer = RARRAY_PTR(cRepeats);
257
+
258
+ unsigned long repeats_array[length];
259
+ unsigned long counter = 0;
260
+
261
+ while(counter < length) {
262
+ repeats_array[counter] = FIX2LONG(repeating_pointer[counter]);
263
+ counter++;
264
+ }
265
+
266
+ set_repeating_array_columns(builder, repeats_array);
267
+ return Qnil;
268
+ }
269
+
270
+ static VALUE json_broker_get_row_count(VALUE self)
271
+ {
272
+ JSONDocumentBuilder *builder;
273
+ Data_Get_Struct(self, JSONDocumentBuilder, builder);
274
+ unsigned long get_count = get_row_count(builder);
275
+ VALUE count = LONG2FIX(get_count);
276
+ return count;
277
+ }
278
+
279
+ static VALUE json_broker_consume_row(VALUE self, VALUE row)
280
+ {
281
+ JSONDocumentBuilder *builder;
282
+ Check_Type(row, T_ARRAY);
283
+ Data_Get_Struct(self, JSONDocumentBuilder, builder);
284
+ struct RArray* cRow = RARRAY(row);
285
+ VALUE* row_pointer = RARRAY_PTR(cRow);
286
+
287
+ unsigned long length = RARRAY_LENINT(row);
288
+ unsigned long counter = 0;
289
+ struct RString* in_loop_rstring;
290
+ char* in_loop_string;
291
+ char* row_strings[length];
292
+ unsigned long string_sizes[length];
293
+
294
+ while(counter < length) {
295
+ Check_Type(row_pointer[counter], T_STRING);
296
+ in_loop_rstring = RSTRING(row_pointer[counter]);
297
+ in_loop_string = RSTRING_PTR(in_loop_rstring);
298
+
299
+ row_strings[counter] = in_loop_string;
300
+ string_sizes[counter] = RSTRING_LEN(in_loop_rstring);
301
+ counter++;
302
+ }
303
+ consume_row(builder, row_strings, string_sizes, 0, 0, length, NULL, NULL, 4, 0, builder->root_level->search_list);
304
+ return Qnil;
305
+ }
306
+
307
+ static VALUE json_broker_finalize_json(VALUE self)
308
+ {
309
+ JSONDocumentBuilder *builder;
310
+ Data_Get_Struct(self, JSONDocumentBuilder, builder);
311
+ char* final_json = finalize_json(builder);
312
+
313
+ return rb_str_new2(final_json);
314
+ }
315
+
316
+ void Init_jsonbroker()
317
+ {
318
+ VALUE cJsonBroker = rb_define_class_under(mSuperstudio, "JsonBroker", rb_cObject);
319
+
320
+ rb_define_alloc_func(cJsonBroker, json_broker_allocate);
321
+ rb_define_method(cJsonBroker, "set_mapper", json_broker_set_mapper, 1);
322
+ rb_define_method(cJsonBroker, "set_column_names", json_broker_set_column_names, 1);
323
+ rb_define_method(cJsonBroker, "set_row_count", json_broker_set_row_count, 1);
324
+ rb_define_method(cJsonBroker, "set_quotes", json_broker_set_quotes, 1);
325
+ rb_define_method(cJsonBroker, "set_hashing", json_broker_set_hashing, 1);
326
+ rb_define_method(cJsonBroker, "set_depths", json_broker_set_depths, 2);
327
+ rb_define_method(cJsonBroker, "set_repeating_arrays", json_broker_set_repeating_array_columns, 1);
328
+ rb_define_method(cJsonBroker, "get_row_count", json_broker_get_row_count, 0);
329
+ rb_define_method(cJsonBroker, "get_column_count", json_broker_get_mapper_length, 0);
330
+ rb_define_method(cJsonBroker, "consume_row", json_broker_consume_row, 1);
331
+ rb_define_method(cJsonBroker, "set_single_node_names", json_broker_set_single_node_names, 1);
332
+ rb_define_method(cJsonBroker, "set_array_node_names", json_broker_set_array_node_names, 1);
333
+ rb_define_method(cJsonBroker, "finalize_json", json_broker_finalize_json, 0);
334
+ }