Created
January 8, 2013 22:58
-
-
Save timglabisch/4488814 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <assert.h> | |
typedef struct { | |
char* key; | |
char* val; | |
} data_keyval; | |
typedef struct { | |
int length; | |
data_keyval** items; | |
} data_dictionary; | |
void data_keyval_free(data_keyval* kv) { | |
free(kv->key); | |
free(kv->val); | |
free(kv); | |
} | |
data_keyval* data_keyval_new(const char* k, const char* v) { | |
data_keyval* kv = (data_keyval*)malloc(sizeof(data_keyval)); | |
kv->key = strdup(k); | |
kv->val = strdup(v); | |
return kv; | |
} | |
data_dictionary* data_dictionary_new() { | |
data_dictionary* data = (data_dictionary*)malloc(sizeof(data_dictionary)); | |
data->length = 0; | |
data->items = (data_keyval**)malloc(sizeof(data_keyval**)); | |
return data; | |
} | |
void data_dictionary_freeItems(data_dictionary* dictionary) { | |
if(dictionary->length == 0) | |
goto freeList; | |
int i = 0; | |
for(;i < dictionary->length; i++) | |
data_keyval_free(dictionary->items[i]); | |
freeList: | |
free(dictionary->items); | |
} | |
void data_dictionary_free(data_dictionary* dictionary) { | |
data_dictionary_freeItems(dictionary); | |
free(dictionary); | |
} | |
void data_dictionary_add(data_dictionary* dictionary, char* key, char* val) { | |
data_keyval* kv = data_keyval_new(key, val); | |
dictionary->items = (data_keyval**)realloc(dictionary->items, sizeof(data_keyval**) * (dictionary->length + 1)); | |
dictionary->items[dictionary->length] = kv; | |
dictionary->length++; | |
} | |
char* data_dictionary_get(data_dictionary* dictionary, const char* key) { | |
if(dictionary->length == 0) | |
return NULL; | |
int i = 0; | |
for(; i < dictionary->length; i++) { | |
if(strcmp(dictionary->items[i]->key, key) != 0) | |
continue; | |
return dictionary->items[i]->val; | |
} | |
return NULL; | |
} | |
// @TEST | |
void test_data_keyval_new() { | |
data_keyval* kv = data_keyval_new("key", "val"); | |
assert(strcmp(kv->key, "key") == 0); | |
assert(strcmp(kv->val, "val") == 0); | |
data_keyval_free(kv); | |
} | |
// @TEST | |
void test_data_dictionary_new() { | |
data_dictionary* data = data_dictionary_new(); | |
assert(data != NULL); | |
data_dictionary_free(data); | |
} | |
// @TEST | |
void test_data_dictionary_add() { | |
data_dictionary* data = data_dictionary_new(); | |
assert(data->length == 0); | |
data_dictionary_add(data, "k", "v"); | |
assert(data->length == 1); | |
data_dictionary_add(data, "k2", "v2"); | |
assert(data->length == 2); | |
data_dictionary_free(data); | |
} | |
// @TEST | |
void test_data_dictionary_get() { | |
data_dictionary* data = data_dictionary_new(); | |
assert(data_dictionary_get(data, "unknown") == NULL); | |
data_dictionary_add(data, "k1", "v1"); | |
assert(strcmp(data_dictionary_get(data, "k1"), "v1") == 0); | |
data_dictionary_add(data, "k2", "v2"); | |
assert(strcmp(data_dictionary_get(data, "k1"), "v1") == 0); | |
assert(strcmp(data_dictionary_get(data, "k2"), "v2") == 0); | |
data_dictionary_free(data); | |
} | |
int main() { | |
test_data_dictionary_new(); | |
test_data_keyval_new(); | |
test_data_dictionary_add(); | |
test_data_dictionary_get(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment