From: Javier Sancho Date: Sat, 23 Jan 2016 06:10:19 +0000 (+0100) Subject: Source code restyling X-Git-Url: https://git.jsancho.org/?a=commitdiff_plain;h=845c98271c1c4e10200ea0d15f6cafabb52d3ea9;p=bongodb.git Source code restyling --- diff --git a/src/bongodb.c b/src/bongodb.c index 05e2dc3..469d6d2 100644 --- a/src/bongodb.c +++ b/src/bongodb.c @@ -24,169 +24,197 @@ /* BongoDB Client */ -bongo_client_t *bongo_client_new(void) +bongo_client_t * +bongo_client_new (void) { - bongo_client_t *bongo_client; + bongo_client_t *client; - bongo_client = malloc(sizeof(bongo_client_t)); - bongo_client->n_databases = 0; - bongo_client->databases = NULL; + client = malloc (sizeof (bongo_client_t)); + client->n_databases = 0; + client->databases = NULL; - return bongo_client; + return client; } -bongo_database_node_t *bongo_database_node_new(bongo_client_t *bongo_client, const char *db_name) +bongo_database_node_t * +bongo_database_node_new (bongo_client_t client, + const char *db_name) { - bongo_database_node_t *bongo_database_node; + bongo_database_node_t *database_node; - if (bongo_client == NULL) + if (client == NULL) return NULL; - bongo_database_node = malloc(sizeof(bongo_database_node_t)); - bongo_database_node->db_name = db_name; - bongo_database_node->n_collections = 0; - bongo_database_node->collections = NULL; - bongo_database_node->next = (struct bongo_database_node_t *)bongo_client->databases; + database_node = malloc (sizeof (bongo_database_node_t)); + database_node->db_name = db_name; + database_node->n_collections = 0; + database_node->collections = NULL; + database_node->next = (struct bongo_database_node_t *) client->databases; - bongo_client->databases = (bongo_database_node_t *)bongo_database_node; - bongo_client->n_databases++; + client->databases = (bongo_database_node_t *) database_node; + client->n_databases++; - return bongo_database_node; + return database_node; } -bongo_collection_node_t *bongo_collection_node_new(bongo_database_node_t *bongo_database_node, const char *col_name) +bongo_collection_node_t * +bongo_collection_node_new (bongo_database_node_t *database_node, + const char *col_name) { - bongo_collection_node_t *bongo_collection_node; + bongo_collection_node_t *collection_node; - if (bongo_database_node == NULL) + if (database_node == NULL) return NULL; - bongo_collection_node = malloc(sizeof(bongo_collection_node_t)); - bongo_collection_node->col_name = col_name; - bongo_collection_node->next = (struct bongo_collection_node_t *)bongo_database_node->collections; + collection_node = malloc (sizeof (bongo_collection_node_t)); + collection_node->col_name = col_name; + collection_node->next = (struct bongo_collection_node_t *) database_node->collections; - bongo_database_node->collections = (bongo_collection_node_t *)bongo_collection_node; - bongo_database_node->n_collections++; + database_node->collections = (bongo_collection_node_t *) collection_node; + database_node->n_collections++; - return bongo_collection_node; + return collection_node; } -bongo_database_node_t *bongo_database_node_search(bongo_client_t *bongo_client, const char *db_name) +bongo_database_node_t * +bongo_database_node_search (bongo_client_t *client, + const char *db_name) { - bongo_database_node_t *bongo_database_node; + bongo_database_node_t *database_node; - if (bongo_client == NULL) + if (client == NULL) return NULL; - bongo_database_node = bongo_client->databases; - while (bongo_database_node != NULL && strcmp(bongo_database_node->db_name, db_name) != 0) - bongo_database_node = (bongo_database_node_t *)bongo_database_node->next; + database_node = client->databases; + while (database_node != NULL && strcmp (database_node->db_name, db_name) != 0) + database_node = (bongo_database_node_t *) database_node->next; - return bongo_database_node; + return database_node; } -bongo_collection_node_t *bongo_collection_node_search(bongo_database_node_t *bongo_database_node, const char *col_name) +bongo_collection_node_t * +bongo_collection_node_search (bongo_database_node_t *database_node, + const char *col_name) { - bongo_collection_node_t *bongo_collection_node; + bongo_collection_node_t *collection_node; - if (bongo_database_node == NULL) + if (database_node == NULL) return NULL; - bongo_collection_node = bongo_database_node->collections; - while (bongo_collection_node != NULL && strcmp(bongo_collection_node->col_name, col_name) != 0) - bongo_collection_node = (bongo_collection_node_t *)bongo_collection_node->next; + collection_node = database_node->collections; + while (collection_node != NULL && strcmp (collection_node->col_name, col_name) != 0) + collection_node = (bongo_collection_node_t *) collection_node->next; - return bongo_collection_node; + return collection_node; } -void bongo_collection_node_destroy(bongo_collection_node_t *bongo_collection_node) +void +bongo_collection_node_destroy (bongo_collection_node_t *collection_node) { - free(bongo_collection_node); + free (collection_node); } -void bongo_database_node_destroy(bongo_database_node_t *bongo_database_node) +void +bongo_database_node_destroy (bongo_database_node_t *database_node) { - bongo_collection_node_t *bongo_collection_node; + bongo_collection_node_t *collection_node; - bongo_collection_node = bongo_database_node->collections; - while (bongo_collection_node != NULL) + collection_node = database_node->collections; + while (collection_node != NULL) { - bongo_database_node->collections = (bongo_collection_node_t *)bongo_collection_node->next; - bongo_collection_node_destroy(bongo_collection_node); - bongo_collection_node = bongo_database_node->collections; + database_node->collections = (bongo_collection_node_t *) collection_node->next; + collection_node_destroy (collection_node); + collection_node = database_node->collections; } - free(bongo_database_node); + free (database_node); } -void bongo_client_destroy(bongo_client_t *bongo_client) +void +bongo_client_destroy (bongo_client_t *client) { - bongo_database_node_t *bongo_database_node; + bongo_database_node_t *database_node; - bongo_database_node = bongo_client->databases; - while (bongo_database_node != NULL) + database_node = client->databases; + while (database_node != NULL) { - bongo_client->databases = (bongo_database_node_t *)bongo_database_node->next; - bongo_database_node_destroy(bongo_database_node); - bongo_database_node = bongo_client->databases; + client->databases = (bongo_database_node_t *) database_node->next; + database_node_destroy (database_node); + database_node = client->databases; } - free(bongo_client); + free (client); } /* BongoDB Database */ -bongo_database_t *bongo_client_get_database(bongo_client_t *bongo_client, const char *db_name) +bongo_database_t * +bongo_client_get_database (bongo_client_t *client, + const char *db_name) { - bongo_database_node_t *bongo_database_node; - bongo_database_t *bongo_database; + bongo_database_node_t *database_node; + bongo_database_t *database; - if (bongo_client == NULL) + if (client == NULL) return NULL; - bongo_database_node = bongo_database_node_search(bongo_client, db_name); - if (bongo_database_node == NULL) - bongo_database_node = bongo_database_node_new(bongo_client, db_name); + database_node = bongo_database_node_search(client, db_name); + if (database_node == NULL) + database_node = bongo_database_node_new(client, db_name); - bongo_database = malloc(sizeof(bongo_database_t)); - bongo_database->database_node = bongo_database_node; + database = malloc(sizeof(bongo_database_t)); + database->database_node = database_node; - return bongo_database; + return database; } -void bongo_database_destroy(bongo_database_t *bongo_database) +void +bongo_database_destroy(bongo_database_t *database) { - free(bongo_database); + free(database); } /* BongoDB Collection */ -bongo_collection_t *bongo_client_get_collection(bongo_client_t *bongo_client, const char *db_name, const char *col_name) +bongo_collection_t * +bongo_client_get_collection(bongo_client_t *client, + const char *db_name, + const char *col_name) { - bongo_database_t *bongo_database; - bongo_collection_node_t *bongo_collection_node; - bongo_collection_t *bongo_collection; + bongo_database_t *database; + bongo_collection_node_t *collection_node; + bongo_collection_t *collection; - if (bongo_client == NULL) + if (client == NULL) return NULL; - bongo_database = bongo_client_get_database(bongo_client, db_name); + database = bongo_client_get_database(client, db_name); - bongo_collection_node = bongo_collection_node_search(bongo_database->database_node, col_name); - if (bongo_collection_node == NULL) - bongo_collection_node = bongo_collection_node_new(bongo_database->database_node, col_name); + collection_node = bongo_collection_node_search(database->database_node, col_name); + if (collection_node == NULL) + collection_node = bongo_collection_node_new(database->database_node, col_name); - bongo_database_destroy(bongo_database); + bongo_database_destroy(database); - bongo_collection = malloc(sizeof(bongo_collection_t)); - bongo_collection->collection_node = bongo_collection_node; + collection = malloc(sizeof(bongo_collection_t)); + collection->collection_node = collection_node; - return bongo_collection; + return collection; } -void bongo_collection_destroy(bongo_collection_t *bongo_collection) +bool +bongo_collection_insert (bongo_collection_t *collection, + bongo_insert_flags_t flags, + const char *document, + const bongo_write_concern_t *write_concern, + bongo_error_t *error) { - free(bongo_collection); +} + +void +bongo_collection_destroy(bongo_collection_t *collection) +{ + free(collection); } diff --git a/src/bongodb.h b/src/bongodb.h index cb02d7b..1cf6acf 100644 --- a/src/bongodb.h +++ b/src/bongodb.h @@ -25,12 +25,12 @@ typedef struct { char *key; char *value; - bongo_record_tag_t *next; + struct bongo_record_tag_t *next; } bongo_record_tag_t; typedef struct { bongo_record_tag_t *tags; - bongo_record_node_t *next; + struct bongo_record_node_t *next; } bongo_record_node_t; typedef struct { @@ -52,14 +52,33 @@ typedef struct { bongo_database_node_t *databases; } bongo_client_t; -bongo_client_t *bongo_client_new(void); -bongo_database_node_t *bongo_database_node_new(bongo_client_t *bongo_client, const char *db_name); -bongo_collection_node_t *bongo_collection_node_new(bongo_database_node_t *bongo_database_node, const char *col_name); -bongo_database_node_t *bongo_database_node_search(bongo_client_t *bongo_client, const char *db_name); -bongo_collection_node_t *bongo_collection_node_search(bongo_database_node_t *bongo_database_node, const char *col_name); -void bongo_collection_node_destroy(bongo_collection_node_t *bongo_collection_node); -void bongo_database_node_destroy(bongo_database_node_t *bongo_database_node); -void bongo_client_destroy(bongo_client_t *bongo_client); +bongo_client_t * +bongo_client_new (void); + +bongo_database_node_t * +bongo_database_node_new (bongo_client_t *client, + const char *db_name); + +bongo_collection_node_t * +bongo_collection_node_new (bongo_database_node_t *database_node, + const char *col_name); + +bongo_database_node_t * +bongo_database_node_search (bongo_client_t *client, + const char *db_name); + +bongo_collection_node_t * +bongo_collection_node_search (bongo_database_node_t *database_node, + const char *col_name); + +void +bongo_collection_node_destroy (bongo_collection_node_t *collection_node); + +void +bongo_database_node_destroy (bongo_database_node_t *database_node); + +void +bongo_client_destroy (bongo_client_t *client); /* BongoDB Database */ @@ -68,8 +87,12 @@ typedef struct { bongo_database_node_t *database_node; } bongo_database_t; -bongo_database_t *bongo_client_get_database(bongo_client_t *bongo_client, const char *db_name); -void bongo_database_destroy(bongo_database_t *bongo_database); +bongo_database_t * +bongo_client_get_database (bongo_client_t *client, + const char *db_name); + +void +bongo_database_destroy (bongo_database_t *database); /* BongoDB Collection */ @@ -78,8 +101,20 @@ typedef struct { bongo_collection_node_t *collection_node; } bongo_collection_t; -bongo_collection_t *bongo_client_get_collection(bongo_client_t *bongo_client, const char *db_name, const char *col_name); -void bongo_collection_destroy(bongo_collection_t *bongo_collection); +bongo_collection_t * +bongo_client_get_collection (bongo_client_t *client, + const char *db_name, + const char *col_name); + +bool +bongo_collection_insert (bongo_collection_t *collection, + bongo_insert_flags_t flags, + const char *document, + const bongo_write_concern_t *write_concern, + bongo_error_t *error); + +void +bongo_collection_destroy (bongo_collection_t *collection); #endif /* __BONGODB_H_ */ diff --git a/src/jsmn.h b/src/jsmn.h deleted file mode 100644 index 01ca99c..0000000 --- a/src/jsmn.h +++ /dev/null @@ -1,76 +0,0 @@ -#ifndef __JSMN_H_ -#define __JSMN_H_ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * JSON type identifier. Basic types are: - * o Object - * o Array - * o String - * o Other primitive: number, boolean (true/false) or null - */ -typedef enum { - JSMN_UNDEFINED = 0, - JSMN_OBJECT = 1, - JSMN_ARRAY = 2, - JSMN_STRING = 3, - JSMN_PRIMITIVE = 4 -} jsmntype_t; - -enum jsmnerr { - /* Not enough tokens were provided */ - JSMN_ERROR_NOMEM = -1, - /* Invalid character inside JSON string */ - JSMN_ERROR_INVAL = -2, - /* The string is not a full JSON packet, more bytes expected */ - JSMN_ERROR_PART = -3 -}; - -/** - * JSON token description. - * @param type type (object, array, string etc.) - * @param start start position in JSON data string - * @param end end position in JSON data string - */ -typedef struct { - jsmntype_t type; - int start; - int end; - int size; -#ifdef JSMN_PARENT_LINKS - int parent; -#endif -} jsmntok_t; - -/** - * JSON parser. Contains an array of token blocks available. Also stores - * the string being parsed now and current position in that string - */ -typedef struct { - unsigned int pos; /* offset in the JSON string */ - unsigned int toknext; /* next token to allocate */ - int toksuper; /* superior token node, e.g parent object or array */ -} jsmn_parser; - -/** - * Create JSON parser over an array of tokens - */ -void jsmn_init(jsmn_parser *parser); - -/** - * Run JSON parser. It parses a JSON data string into and array of tokens, each describing - * a single JSON object. - */ -int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, - jsmntok_t *tokens, unsigned int num_tokens); - -#ifdef __cplusplus -} -#endif - -#endif /* __JSMN_H_ */ diff --git a/src/jsmn.h b/src/jsmn.h new file mode 120000 index 0000000..d9dbf5c --- /dev/null +++ b/src/jsmn.h @@ -0,0 +1 @@ +../../jsmn/jsmn.h \ No newline at end of file diff --git a/src/test_bongodb.c b/src/test_bongodb.c index 24b7cfa..c8aecd1 100644 --- a/src/test_bongodb.c +++ b/src/test_bongodb.c @@ -23,57 +23,66 @@ #include #include -int test_bongodb() +int +test_bongodb() { bongo_client_t *bongo_client; - bongo_database_t *bongo_database; - bongo_collection_t *bongo_collection; + bongo_database_t *database; + bongo_collection_t *collection; + const char *document = "{\"name\": \"Jack\", \"age\": 27, \"list\": [1, 2, 3]}"; + bongo_error_t error; - bongo_client = bongo_client_new(); - printf("Databases: %d\n", bongo_client->n_databases); - bongo_database = bongo_client_get_database(bongo_client, "db_test"); - printf("Databases: %d\n", bongo_client->n_databases); + bongo_client = bongo_client_new (); + printf ("Databases: %d\n", bongo_client->n_databases); + database = bongo_client_get_database (bongo_client, "db_test"); + printf ("Databases: %d\n", bongo_client->n_databases); - printf("Collections: %d\n", bongo_database->database_node->n_collections); - bongo_collection = bongo_client_get_collection(bongo_client, "db_test", "col_test"); - printf("Collections: %d\n", bongo_database->database_node->n_collections); + printf ("Collections: %d\n", database->database_node->n_collections); + collection = bongo_client_get_collection (bongo_client, "db_test", "col_test"); + printf ("Collections: %d\n", database->database_node->n_collections); - bongo_collection_destroy(bongo_collection); - bongo_database_destroy(bongo_database); - bongo_client_destroy(bongo_client); + if (!bongo_collection_insert (collection, BONGO_INSERT_NONE, document, NULL, &error)) { + fprintf (stderr, "%s\n", error.message); + } + + bongo_collection_destroy (collection); + bongo_database_destroy (database); + bongo_client_destroy (bongo_client); - printf("test_bongodb: PASSED\n"); + printf ("test_bongodb: PASSED\n"); return 0; } -int test_jsmn() +int +test_jsmn() { jsmn_parser parser; jsmntok_t *tokens; int n_tokens; const char *js = "{\"name\": \"Jack\", \"age\": 27, \"list\": [1, 2, 3]}"; - jsmn_init(&parser); - n_tokens = jsmn_parse(&parser, js, strlen(js), NULL, 1); - tokens = malloc(sizeof(jsmntok_t) * n_tokens); - jsmn_init(&parser); - n_tokens = jsmn_parse(&parser, js, strlen(js), tokens, n_tokens); - printf("Tokens: %d\n", n_tokens); + jsmn_init (&parser); + n_tokens = jsmn_parse (&parser, js, strlen (js), NULL, 1); + tokens = malloc (sizeof (jsmntok_t) * n_tokens); + jsmn_init (&parser); + n_tokens = jsmn_parse (&parser, js, strlen (js), tokens, n_tokens); + printf ("Tokens: %d\n", n_tokens); for (int i = 0; i < n_tokens; i++) { - printf(" %d, %d, %d, %d: %.*s\n", tokens[i].type, tokens[i].start, tokens[i].end, tokens[i].size, tokens[i].end - tokens[i].start, js + tokens[i].start); + printf (" %d, %d, %d, %d: %.*s\n", tokens[i].type, tokens[i].start, tokens[i].end, tokens[i].size, tokens[i].end - tokens[i].start, js + tokens[i].start); } - free(tokens); + free (tokens); - printf("test_jsmn: PASSED\n"); + printf ("test_jsmn: PASSED\n"); return 0; } -int main() +int +main() { - test_bongodb(); - test_jsmn(); + test_bongodb (); + test_jsmn (); return 0; }