/* 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);
}
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 {
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 */
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 */
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_ */
#include <stdlib.h>
#include <string.h>
-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;
}