Permalink
Browse files

Made functions static where applicable.

Some comments about the files.
Updated License to 2012.
  • Loading branch information...
1 parent 5854491 commit 579e6dea0d022ccfd78690d88dbffb7a55fc4e1e @residuum committed Mar 26, 2012
Showing with 118 additions and 111 deletions.
  1. +1 −1 LICENSE.txt
  2. +4 −0 json-decode.c
  3. +37 −33 json-encode.c
  4. +5 −1 libpurest_json.c
  5. +0 −8 purest_json.h
  6. +71 −68 rest-json.c
View
@@ -1,7 +1,7 @@
Author:
Thomas Mayer <thomas@residuum.org>
-Copyright (c) 2011 Thomas Mayer
+Copyright (c) 2011-2012 Thomas Mayer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
View
@@ -1,3 +1,7 @@
+/*
+ * [json-decode] decodes JSON data and outputs it as lists.
+ * */
+
#include "purest_json.h"
static t_class *json_decode_class;
View
@@ -1,7 +1,44 @@
+/*
+ * [json-encodes] encodes data as JSON and outputs it as a symbol.
+ * */
+
#include "purest_json.h"
static t_class *json_encode_class;
+void json_encode_free_memory(t_json_encode *x) {
+ t_key_value_pair *data_to_free;
+ t_key_value_pair *next_data;
+
+ data_to_free = x->data;
+ while(data_to_free != NULL) {
+ next_data = data_to_free->next;
+ freebytes(data_to_free->key, MAXPDSTRING);
+ freebytes(data_to_free->value, MAXPDSTRING);
+ freebytes(data_to_free, sizeof(t_key_value_pair));
+ data_to_free = next_data;
+ }
+
+ x->data_count = 0;
+ x->data = NULL;
+}
+
+static json_object *create_object(char *value) {
+ json_object *object;
+ char *parsed_string;
+ size_t memsize = 0;
+ /* if stored value is string is starting with { and ending with },
+ then create a json object from it. */
+ if (value[0] == '{' && value[strlen(value) - 1] == '}') {
+ parsed_string = remove_backslashes(value, memsize);;
+ object = json_tokener_parse(parsed_string);
+ freebytes(parsed_string, memsize);
+ } else {
+ object = json_object_new_string(value);
+ }
+ return object;
+}
+
void setup_json0x2dencode(void) {
json_encode_class = class_new(gensym("json-encode"), (t_newmethod)json_encode_new,
(t_method)json_encode_free, sizeof(t_json_encode), 0, A_GIMME, 0);
@@ -86,22 +123,6 @@ void json_encode_bang(t_json_encode *x) {
}
}
-json_object *create_object(char *value) {
- json_object *object;
- char *parsed_string;
- size_t memsize = 0;
- /* if stored value is string is starting with { and ending with },
- then create a json object from it. */
- if (value[0] == '{' && value[strlen(value) - 1] == '}') {
- parsed_string = remove_backslashes(value, memsize);;
- object = json_tokener_parse(parsed_string);
- freebytes(parsed_string, memsize);
- } else {
- object = json_object_new_string(value);
- }
- return object;
-}
-
void json_encode_add(t_json_encode *x, t_symbol *selector, int argcount, t_atom *argvec) {
char *key;
char *value;
@@ -198,20 +219,3 @@ void json_encode_clear(t_json_encode *x, t_symbol *selector, int argcount, t_ato
json_encode_free_memory(x);
}
-
-void json_encode_free_memory(t_json_encode *x) {
- t_key_value_pair *data_to_free;
- t_key_value_pair *next_data;
-
- data_to_free = x->data;
- while(data_to_free != NULL) {
- next_data = data_to_free->next;
- freebytes(data_to_free->key, MAXPDSTRING);
- freebytes(data_to_free->value, MAXPDSTRING);
- freebytes(data_to_free, sizeof(t_key_value_pair));
- data_to_free = next_data;
- }
-
- x->data_count = 0;
- x->data = NULL;
-}
View
@@ -1,6 +1,10 @@
+/*
+ * Shared functions for working with JSON data.
+ * */
+
#include "purest_json.h"
-char *lowercase_unicode(char *orig, size_t memsize) {
+static char *lowercase_unicode(char *orig, size_t memsize) {
char *unicode_intro = "\\u";
char *tmp = strstr(orig, unicode_intro);
char *tmp_without_intro;
View
@@ -54,11 +54,6 @@ void rest_command(t_rest *x, t_symbol *selector, int argcount, t_atom *argvec);
void rest_oauth(t_rest *x, t_symbol *selector, int argcount, t_atom *argvec);
void rest_url(t_rest *x, t_symbol *selector, int argcount, t_atom *argvec);
-void execute_rest(t_rest *x);
-
-/* pthread functions */
-void *execute_rest_thread(void *thread_args);
-
/* json-encode */
void setup_json0x2dencode(void);
void *json_encode_new(t_symbol *selector, int argcount, t_atom *argvec);
@@ -69,8 +64,6 @@ void json_encode_add(t_json_encode *x, t_symbol *selector, int argcount, t_atom
void json_encode_array_add(t_json_encode *x, t_symbol *selector, int argcount, t_atom *argvec);
void json_encode_clear(t_json_encode *x, t_symbol *selector, int argcount, t_atom *argvec);
-json_object *create_object(char *value);
-void json_encode_free_memory(t_json_encode *x);
/* json-decode */
void setup_json0x2ddecode(void);
@@ -85,4 +78,3 @@ 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, size_t memsize);
int str_ccmp(const char *s1, const char *s2);
-char *lowercase_unicode(char *orig, size_t memsize);
View
@@ -1,3 +1,7 @@
+/*
+ * [rest-json] makes RESTful calls to webservices that work with JSON data.
+ * */
+
#include "purest_json.h"
static t_class *rest_class;
@@ -32,6 +36,73 @@ static size_t read_memory_callback(void *ptr, size_t size, size_t nmemb, void *d
return to_copy;
}
+static void *execute_rest_thread(void *thread_args) {
+ t_rest *x = (t_rest *)thread_args;
+ CURL *curl_handle;
+ CURLcode result;
+ t_memory_struct in_memory;
+ t_memory_struct out_memory;
+
+ curl_global_init(CURL_GLOBAL_ALL);
+ curl_handle = curl_easy_init();
+ if (curl_handle) {
+ curl_easy_setopt(curl_handle, CURLOPT_URL, x->complete_url);
+ curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
+ if (strcmp(x->request_type, "PUT") == 0) {
+ curl_easy_setopt(curl_handle, CURLOPT_UPLOAD, TRUE);
+ curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, read_memory_callback);
+ /* Prepare data for reading */
+ in_memory.memory = getbytes(strlen(x->parameters) + 1);
+ if (in_memory.memory == NULL) {
+ error("not enough memory");
+ }
+ in_memory.size = strlen(x->parameters);
+ memcpy(in_memory.memory, x->parameters, strlen(x->parameters));
+ curl_easy_setopt(curl_handle, CURLOPT_READDATA, (void *)&in_memory);
+ } else if (strcmp(x->request_type, "POST") == 0) {
+ curl_easy_setopt(curl_handle, CURLOPT_POST, TRUE);
+ } else if (strcmp(x->request_type, "DELETE") == 0) {
+ curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, "DELETE");
+ }
+ out_memory.memory = getbytes(1);
+ out_memory.size = 0;
+ curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_memory_callback);
+ curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&out_memory);
+ result = curl_easy_perform(curl_handle);
+
+ if (result == CURLE_OK) {
+ output_json_string(out_memory.memory, x->x_ob.ob_outlet, x->done_outlet);
+ /* Free memory */
+ if (out_memory.memory) {
+ freebytes(out_memory.memory, out_memory.size + 1);
+ }
+ free((void *)result);
+ } else {
+ error("Error while performing request: %s", curl_easy_strerror(result));
+ }
+ curl_easy_cleanup(curl_handle);
+ } else {
+ error("Cannot init curl.");
+ }
+ x->is_data_locked = 0;
+ pthread_exit(NULL);
+}
+
+static void execute_rest(t_rest *x) {
+ int rc;
+ pthread_t thread;
+ pthread_attr_t thread_attributes;
+
+ pthread_attr_init(&thread_attributes);
+ pthread_attr_setdetachstate(&thread_attributes, PTHREAD_CREATE_DETACHED);
+ rc = pthread_create(&thread, &thread_attributes, execute_rest_thread, (void *)x);
+ pthread_attr_destroy(&thread_attributes);
+ if (rc) {
+ error("Could not create thread with code %d", rc);
+ x->is_data_locked = 0;
+ }
+}
+
void setup_rest0x2djson(void) {
rest_class = class_new(gensym("rest-json"), (t_newmethod)rest_new,
(t_method)rest_free, sizeof(t_rest), 0, A_GIMME, 0);
@@ -158,71 +229,3 @@ void rest_free (t_rest *x, t_symbol *selector, int argcount, t_atom *argvec) {
freebytes(x->base_url, MAXPDSTRING * sizeof(char));
}
}
-
-void *execute_rest_thread(void *thread_args) {
- t_rest *x = (t_rest *)thread_args;
- CURL *curl_handle;
- CURLcode result;
- t_memory_struct in_memory;
- t_memory_struct out_memory;
-
- curl_global_init(CURL_GLOBAL_ALL);
- curl_handle = curl_easy_init();
- post("url: %s", x->complete_url);
- if (curl_handle) {
- curl_easy_setopt(curl_handle, CURLOPT_URL, x->complete_url);
- curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
- if (strcmp(x->request_type, "PUT") == 0) {
- curl_easy_setopt(curl_handle, CURLOPT_UPLOAD, TRUE);
- curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, read_memory_callback);
- /* Prepare data for reading */
- in_memory.memory = getbytes(strlen(x->parameters) + 1);
- if (in_memory.memory == NULL) {
- error("not enough memory");
- }
- in_memory.size = strlen(x->parameters);
- memcpy(in_memory.memory, x->parameters, strlen(x->parameters));
- curl_easy_setopt(curl_handle, CURLOPT_READDATA, (void *)&in_memory);
- } else if (strcmp(x->request_type, "POST") == 0) {
- curl_easy_setopt(curl_handle, CURLOPT_POST, TRUE);
- } else if (strcmp(x->request_type, "DELETE") == 0) {
- curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, "DELETE");
- }
- out_memory.memory = getbytes(1);
- out_memory.size = 0;
- curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_memory_callback);
- curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&out_memory);
- result = curl_easy_perform(curl_handle);
-
- if (result == CURLE_OK) {
- output_json_string(out_memory.memory, x->x_ob.ob_outlet, x->done_outlet);
- /* Free memory */
- if (out_memory.memory) {
- freebytes(out_memory.memory, out_memory.size + 1);
- }
- free((void *)result);
- } else {
- error("Error while performing request: %s", curl_easy_strerror(result));
- }
- curl_easy_cleanup(curl_handle);
- } else {
- error("Cannot init curl.");
- }
- x->is_data_locked = 0;
- pthread_exit(NULL);
-}
-
-void execute_rest(t_rest *x) {
- int rc;
- pthread_t thread;
- pthread_attr_t thread_attributes;
-
- pthread_attr_init(&thread_attributes);
- pthread_attr_setdetachstate(&thread_attributes, PTHREAD_CREATE_DETACHED);
- rc = pthread_create(&thread, &thread_attributes, execute_rest_thread, (void *)x);
- pthread_attr_destroy(&thread_attributes);
- if (rc) {
- error("Could not create thread with code %d", rc);
- x->is_data_locked = 0;
- }
-}

0 comments on commit 579e6de

Please sign in to comment.