@@ -157,7 +157,6 @@ unsigned short search_params_length = (unsigned short) sizeof(search_params_type
157
157
int KAOS_EXPORT Kaos_search ()
158
158
{
159
159
unsigned long dict_length = kaos .getDictLength (search_params_name [0 ]);
160
- enum Type dict_type = kaos .getDictType (search_params_name [0 ]);
161
160
162
161
for (unsigned long i = 0 ; i < dict_length ; i ++ ) {
163
162
char * key = kaos .getDictKeyByIndex (search_params_name [0 ], (long long ) i );
@@ -218,6 +217,88 @@ int KAOS_EXPORT Kaos_search()
218
217
return 0 ;
219
218
}
220
219
220
+ // dict json.replace(dict target, any needle, any replacement)
221
+
222
+ char * replace_params_name [] = {
223
+ "target" ,
224
+ "needle" ,
225
+ "replacement"
226
+ };
227
+ unsigned replace_params_type [] = {
228
+ K_DICT ,
229
+ K_ANY ,
230
+ K_ANY
231
+ };
232
+ unsigned short replace_params_length = (unsigned short ) sizeof (replace_params_type ) / sizeof (unsigned );
233
+ int KAOS_EXPORT Kaos_replace ()
234
+ {
235
+ unsigned long dict_length = kaos .getDictLength (replace_params_name [0 ]);
236
+ enum Type dict_type = kaos .getDictType (replace_params_name [0 ]);
237
+
238
+ kaos .startBuildingDict ();
239
+
240
+ for (unsigned long i = 0 ; i < dict_length ; i ++ ) {
241
+ char * key = kaos .getDictKeyByIndex (replace_params_name [0 ], (long long ) i );
242
+ enum ValueType value_type = kaos .getDictElementValueType (replace_params_name [0 ], key );
243
+
244
+ bool x_b , y_b ;
245
+ long long x_i , y_i ;
246
+ long double x_f , y_f ;
247
+ char * x_s , * y_s ;
248
+ char * replacement ;
249
+
250
+ switch (value_type )
251
+ {
252
+ case V_BOOL :
253
+ x_b = kaos .getVariableBool (replace_params_name [1 ]);
254
+ y_b = kaos .getDictElementBool (replace_params_name [0 ], key );
255
+ if (x_b == y_b ) {
256
+ kaos .createVariableBool (key , kaos .getVariableBool (replace_params_name [2 ]));
257
+ } else {
258
+ kaos .createVariableBool (key , y_b );
259
+ }
260
+ break ;
261
+ case V_INT :
262
+ x_i = kaos .getVariableInt (replace_params_name [1 ]);
263
+ y_i = kaos .getDictElementInt (replace_params_name [0 ], key );
264
+ if (x_i == y_i ) {
265
+ kaos .createVariableInt (key , kaos .getVariableInt (replace_params_name [2 ]));
266
+ } else {
267
+ kaos .createVariableInt (key , y_i );
268
+ }
269
+ break ;
270
+ case V_FLOAT :
271
+ x_f = kaos .getVariableFloat (replace_params_name [1 ]);
272
+ y_f = kaos .getDictElementFloat (replace_params_name [0 ], key );
273
+ if (x_f == y_f ) {
274
+ kaos .createVariableFloat (key , kaos .getVariableFloat (replace_params_name [2 ]));
275
+ } else {
276
+ kaos .createVariableFloat (key , y_f );
277
+ }
278
+ break ;
279
+ case V_STRING :
280
+ x_s = kaos .getVariableString (replace_params_name [1 ]);
281
+ y_s = kaos .getDictElementString (replace_params_name [0 ], key );
282
+ if (strcmp (x_s , y_s ) == 0 ) {
283
+ replacement = kaos .getVariableString (replace_params_name [2 ]);
284
+ kaos .createVariableString (key , replacement );
285
+ free (replacement );
286
+ } else {
287
+ kaos .createVariableString (key , y_s );
288
+ }
289
+ free (x_s );
290
+ free (y_s );
291
+ break ;
292
+ default :
293
+ break ;
294
+ }
295
+ free (key );
296
+ }
297
+
298
+ kaos .returnDict (dict_type );
299
+ return 0 ;
300
+ }
301
+
221
302
int KAOS_EXPORT KaosRegister (struct Kaos _kaos )
222
303
{
223
304
kaos = _kaos ;
@@ -233,6 +314,7 @@ int KAOS_EXPORT KaosRegister(struct Kaos _kaos)
233
314
234
315
// Searching & Replacing
235
316
kaos .defineFunction ("search" , K_STRING , search_params_name , search_params_type , search_params_length );
317
+ kaos .defineFunction ("replace" , K_DICT , replace_params_name , replace_params_type , replace_params_length );
236
318
237
319
return 0 ;
238
320
}
0 commit comments