Skip to content
This repository has been archived by the owner on Nov 18, 2023. It is now read-only.

Commit

Permalink
no array functions for [urlparams]
Browse files Browse the repository at this point in the history
  • Loading branch information
residuum committed Mar 2, 2015
1 parent 9254381 commit aced4c5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 26 deletions.
28 changes: 16 additions & 12 deletions json-encode.c
Expand Up @@ -87,29 +87,29 @@ static void jenc_load_json_object(const t_json_encode *const jenc, json_object *

switch (inner_type) {
case json_type_boolean:
kvp_add((struct _kvp_store *)jenc, key,
kvp_val_create(NULL, json_object_get_boolean(val) ? 1 : 0), 0);
kvp_add_simple((struct _kvp_store *)jenc, key,
kvp_val_create(NULL, json_object_get_boolean(val) ? 1 : 0));
break;
case json_type_double:
kvp_add((struct _kvp_store *)jenc, key,
kvp_val_create(NULL, json_object_get_double(val)), 0);
kvp_add_simple((struct _kvp_store *)jenc, key,
kvp_val_create(NULL, json_object_get_double(val)));
break;
case json_type_int:
kvp_add((struct _kvp_store *)jenc, key,
kvp_val_create(NULL, json_object_get_int(val)), 0);
kvp_add_simple((struct _kvp_store *)jenc, key,
kvp_val_create(NULL, json_object_get_int(val)));
break;
case json_type_string:
value = string_create(&value_len, snprintf(NULL, 0, "%s",
json_object_get_string(val)));
sprintf(value, "%s", json_object_get_string(val));
kvp_add((struct _kvp_store *)jenc, key, kvp_val_create(value, 0), 0);
kvp_add_simple((struct _kvp_store *)jenc, key, kvp_val_create(value, 0));
string_free(value, &value_len);
break;
case json_type_object:
value = string_create(&value_len, snprintf(NULL, 0, "%s",
json_object_get_string(val)));
sprintf(value, "%s", json_object_get_string(val));
kvp_add((struct _kvp_store *)jenc, key, kvp_val_create(value, 0), 0);
kvp_add_simple((struct _kvp_store *)jenc, key, kvp_val_create(value, 0));
string_free(value, &value_len);
json_object_put(val);
break;
Expand All @@ -122,14 +122,14 @@ static void jenc_load_json_object(const t_json_encode *const jenc, json_object *
snprintf(NULL, 0, "%s",
json_object_get_string(array_member)));
sprintf(value, "%s", json_object_get_string(array_member));
kvp_add((struct _kvp_store *)jenc, key,
kvp_val_create(value, 0), 1);
kvp_add_array((struct _kvp_store *)jenc, key,
kvp_val_create(value, 0));
string_free(value, &value_len);
}
}
break;
case json_type_null:
kvp_add((struct _kvp_store *)jenc, key, kvp_val_create("", 0), 0);
kvp_add_simple((struct _kvp_store *)jenc, key, kvp_val_create("", 0));
break;
default:
MYERROR("What other JSON type?");
Expand Down Expand Up @@ -224,7 +224,11 @@ static void jenc_add(t_json_encode *const jenc, const int argc, t_atom *const ar
strcat(value, temp_value);
}
}
kvp_add((struct _kvp_store *)jenc, key, kvp_val_create(value, f), is_array);
if (is_array){
kvp_add_array((struct _kvp_store *)jenc, key, kvp_val_create(value, f));
} else {
kvp_add_simple((struct _kvp_store *)jenc, key, kvp_val_create(value, f));
}
string_free(value, &value_len);
}

Expand Down
39 changes: 26 additions & 13 deletions kvp.c
Expand Up @@ -29,7 +29,9 @@ enum _v_type {string_val, float_val, int_val};
struct _v {
size_t slen;
enum _v_type type;
#ifndef NO_ARRAY
struct _v *next; /* makes a linked list for arrays */
#endif
union {
t_float f;
char *s;
Expand All @@ -41,8 +43,10 @@ struct _kvp {
size_t key_len;
char *key;
struct _v *value;
#ifndef NO_ARRAY
struct _v *last; /* simplifies adding to arrays */
unsigned char is_array; /* [json-encode] has arrays, [urlparams] not */
#endif
UT_hash_handle hh;
};

Expand All @@ -65,12 +69,12 @@ static void kvp_insert(struct _kvp_store *store, struct _kvp *new_pair);
static void kvp_replace_value(struct _kvp *kvp, struct _v *value, const unsigned char is_array);
/* adds or replaces items to / in store for simple items */
static void kvp_add_simple(struct _kvp_store *store, char *key, struct _v *value);
#ifndef NO_ARRAY
/* adds value to key value pair as last, adds it to linked list */
static void kvp_add_to_array(struct _kvp *kvp, struct _v *value);
/* adds or replaces items to / in store for array */
static void kvp_add_array(struct _kvp_store *store, char *key, struct _v *value);
/* adds or replaces items to / in store */
static void kvp_add(struct _kvp_store *store, char *key, struct _v *value, const unsigned char is_array);
#endif
/* frees store */
static void kvp_store_free_memory(struct _kvp_store *store);

Expand All @@ -80,7 +84,9 @@ static struct _v *kvp_val_create(const char *const s, const t_float f) {

created = getbytes(sizeof(struct _v));
created->slen = 0;
#ifndef NO_ARRAY
created->next = NULL;
#endif
if (s) {
created->val.s = string_create(&created->slen, strlen(s));
strcpy(created->val.s, s);
Expand All @@ -99,12 +105,16 @@ static struct _v *kvp_val_create(const char *const s, const t_float f) {
}

static void kvp_val_free(struct _v *value) {
#ifndef NO_ARRAY
do {
struct _v *next = value->next;
#endif
string_free(value->val.s, &value->slen);
freebytes(value, sizeof(struct _v));
#ifndef NO_ARRAY
value = next;
} while (value != NULL);
#endif
}

static struct _kvp *kvp_create(const char *const key, struct _v *const value, const unsigned char is_array) {
Expand All @@ -118,9 +128,13 @@ static struct _kvp *kvp_create(const char *const key, struct _v *const value, co
}

created_data->value = value;
created_data->last = value;
strcpy(created_data->key, key);
#ifdef NO_ARRAY
(void) is_array;
#else
created_data->last = value;
created_data->is_array = is_array;
#endif

return created_data;
}
Expand All @@ -142,11 +156,17 @@ static void kvp_insert(struct _kvp_store *const store, struct _kvp *const new_pa
}

static void kvp_replace_value(struct _kvp *const kvp, struct _v *const value, const unsigned char is_array) {
#ifdef NO_ARRAY
(void) is_array;
#else
MYASSERT(kvp->is_array != 1 || is_array != 1, "This should not be called: array values should be appended, not replaced.");

#endif

kvp_val_free(kvp->value);
kvp->value = value;
#ifndef NO_ARRAY
kvp->is_array = is_array;
#endif
}

static void kvp_add_simple(struct _kvp_store *const store, char *const key, struct _v *const value) {
Expand All @@ -161,6 +181,7 @@ static void kvp_add_simple(struct _kvp_store *const store, char *const key, stru
}
}

#ifndef NO_ARRAY
static void kvp_add_to_array(struct _kvp *const kvp, struct _v *const value) {
struct _v *last = kvp->last;

Expand All @@ -183,15 +204,7 @@ static void kvp_add_array(struct _kvp_store *const store, char *const key, struc
kvp_insert(store, kvp);
}
}

static void kvp_add(struct _kvp_store *const store, char *const key, struct _v *const value,
const unsigned char is_array) {
if (!is_array) {
kvp_add_simple(store, key, value);
} else {
kvp_add_array(store, key, value);
}
}
#endif

static void kvp_store_free_memory(struct _kvp_store *const store) {
struct _kvp *it;
Expand Down
2 changes: 1 addition & 1 deletion urlparams.c
Expand Up @@ -172,7 +172,7 @@ void urlparams_add(t_urlparams *const urlp, const t_symbol *const sel, const int
strcat(value, " ");
strcat(value, temp_value);
}
kvp_add((struct _kvp_store *)urlp, key, kvp_val_create(value, 0), 0);
kvp_add_simple((struct _kvp_store *)urlp, key, kvp_val_create(value, 0));
string_free(value, &value_len);
}

Expand Down
2 changes: 2 additions & 0 deletions urlparams.h
Expand Up @@ -31,6 +31,8 @@ THE SOFTWARE.

/* suppresses warning, nothing special */
#define NO_BACKSLASHES 1
/* [urlparams] only has simple items, no array in store */
#define NO_ARRAY 1

/* [urlparams] */
struct _urlparams;
Expand Down

0 comments on commit aced4c5

Please sign in to comment.