Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make string filtering functions consistent.

  • Loading branch information...
commit 4e61ccfb7d9d8f526e3fbd88d37803f1fbc923f7 1 parent 9b28222
@residuum authored
Showing with 57 additions and 51 deletions.
  1. +9 −11 examples/purest-json-test.pd
  2. +47 −39 libpurest_json.c
  3. +1 −1  purest_json.h
View
20 examples/purest-json-test.pd
@@ -1,4 +1,4 @@
-#N canvas 67 51 1002 589 10;
+#N canvas 65 51 1002 589 10;
#X msg 9 38 url http://127.0.0.1:5984/;
#X obj 363 136 json-encode;
#X msg 824 36 bang;
@@ -36,7 +36,6 @@
#X obj 552 450 json-decode;
#X obj 671 483 print array-done;
#X obj 552 483 print array-decode;
-#X obj 552 422 l2s;
#X obj 552 329 rest-json http://127.0.0.1:5984/;
#N canvas 116 62 1112 677 webservices 0;
#X obj 129 351 rest-json;
@@ -80,15 +79,15 @@ Pd version < 0.43 and without -stderr.;
#X restore -42 467 pd webservices;
#X obj -48 381 print couchdb_return;
#X obj 363 315 print decode_json_data;
-#X obj 363 236 l2s;
+#X obj 552 423 l2s;
#X connect 0 0 30 0;
#X connect 1 0 5 0;
#X connect 2 0 1 0;
#X connect 3 0 1 0;
#X connect 5 0 26 0;
-#X connect 5 0 42 0;
+#X connect 5 0 6 0;
#X connect 5 1 4 0;
-#X connect 6 0 41 0;
+#X connect 6 0 40 0;
#X connect 7 0 1 0;
#X connect 8 0 9 0;
#X connect 10 0 11 0;
@@ -114,13 +113,12 @@ Pd version < 0.43 and without -stderr.;
#X connect 28 0 1 0;
#X connect 29 0 1 0;
#X connect 30 0 13 0;
-#X connect 30 0 40 0;
+#X connect 30 0 39 0;
#X connect 30 1 15 0;
-#X connect 31 0 38 0;
-#X connect 32 0 37 0;
+#X connect 31 0 37 0;
+#X connect 32 0 41 0;
#X connect 33 0 32 0;
#X connect 34 0 36 0;
#X connect 34 1 35 0;
-#X connect 37 0 34 0;
-#X connect 38 0 33 0;
-#X connect 42 0 6 0;
+#X connect 37 0 33 0;
+#X connect 41 0 34 0;
View
86 libpurest_json.c
@@ -1,49 +1,56 @@
#include "purest_json.h"
-void lowercase_unicode(char *orig) {
+char *lowercase_unicode(char *orig) {
char *unicode_intro = "\\u";
char *tmp = strstr(orig, unicode_intro);
char *tmp_without_intro;
- char orig_return[strlen(orig) + 1];
+ char *return_string = (char *) calloc(strlen(orig) + 1, sizeof(char));
short i;
short uni_len = 4; /*TODO: get real length, we just assume 4 for now */
- if (tmp) {
- memset(orig_return, 0, strlen(orig) + 1);
- strncpy(orig_return, orig, strlen(orig) - strlen(tmp));
- do {
- for (i = 2; i < 2 + uni_len; i++) {
- switch (tmp[i]) {
- case 'A':
- tmp[i] = 'a';
- break;
- case 'B':
- tmp[i] = 'b';
- break;
- case 'C':
- tmp[i] = 'c';
- break;
- case 'D':
- tmp[i] = 'd';
- break;
- case 'E':
- tmp[i] = 'e';
- break;
- case 'F':
- tmp[i] = 'f';
- break;
+
+ if (return_string != NULL) {
+ if (tmp) {
+ memset(return_string, 0x00, strlen(orig) + 1);
+ strncpy(return_string, orig, strlen(orig) - strlen(tmp));
+ do {
+ for (i = 2; i < 2 + uni_len; i++) {
+ switch (tmp[i]) {
+ case 'A':
+ tmp[i] = 'a';
+ break;
+ case 'B':
+ tmp[i] = 'b';
+ break;
+ case 'C':
+ tmp[i] = 'c';
+ break;
+ case 'D':
+ tmp[i] = 'd';
+ break;
+ case 'E':
+ tmp[i] = 'e';
+ break;
+ case 'F':
+ tmp[i] = 'f';
+ break;
+ }
}
- }
- strcat(orig_return, unicode_intro);
- tmp_without_intro = tmp + 2;
- tmp = strstr(tmp_without_intro, unicode_intro);
- if (tmp) {
- strncat(orig_return, tmp_without_intro, strlen(tmp_without_intro) - strlen(tmp));
- } else {
- strcat(orig_return, tmp_without_intro);
- }
- } while(tmp);
- strcpy(orig, orig_return);
+ strcat(return_string, unicode_intro);
+ tmp_without_intro = tmp + 2;
+ tmp = strstr(tmp_without_intro, unicode_intro);
+ if (tmp) {
+ strncat(return_string, tmp_without_intro, strlen(tmp_without_intro) - strlen(tmp));
+ } else {
+ strcat(return_string, tmp_without_intro);
+ }
+ } while(tmp);
+ } else {
+ strcpy(return_string, orig);
+ }
+ } else {
+ error("Could not allocate memory");
}
+ return return_string;
}
void output_json(json_object *jobj, t_outlet *data_outlet, t_outlet *done_outlet) {
@@ -154,15 +161,16 @@ void output_json(json_object *jobj, t_outlet *data_outlet, t_outlet *done_outlet
void output_json_string(char *json_string, t_outlet *data_outlet, t_outlet *done_outlet) {
json_object *jobj;
/* Needed because of bug in json-c 0.9 */
- lowercase_unicode(json_string);
+ char* corrected_json_string = lowercase_unicode(json_string);
/* Parse JSON */
- jobj = json_tokener_parse(json_string);
+ jobj = json_tokener_parse(corrected_json_string);
if (!is_error(jobj)) {
output_json(jobj, data_outlet, done_outlet);
json_object_put(jobj);
} else {
error("Not a JSON object");
}
+ free(corrected_json_string);
}
char *remove_backslashes(char *source_string) {
View
2  purest_json.h
@@ -84,5 +84,5 @@ void output_json_string(char *json_string, t_outlet *data_outlet, t_outlet *done
void purest_json_setup(void);
char *remove_backslashes(char *source_string);
int str_ccmp(const char *s1, const char *s2);
-void lowercase_unicode(char *orig);
+char *lowercase_unicode(char *orig);
Please sign in to comment.
Something went wrong with that request. Please try again.