diff options
author | Christian Pointner <equinox@helsinki.at> | 2009-11-02 11:13:07 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2009-11-02 11:13:07 (GMT) |
commit | bb4a1f01ff4dd9650f0dec7085ae7733373b42cf (patch) | |
tree | 111bf42f37bf4e1afc794559e72ec7d8220106aa /key_value_storage.c |
switched to svn directory structure
Diffstat (limited to 'key_value_storage.c')
-rw-r--r-- | key_value_storage.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/key_value_storage.c b/key_value_storage.c new file mode 100644 index 0000000..8ed21dd --- /dev/null +++ b/key_value_storage.c @@ -0,0 +1,92 @@ +/* + * rhctl + * + * Copyright (C) 2009 Christian Pointner <equinox@spreadspace.org> + * + * This file is part of rhctl. + * + * rhctl is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * rhctl is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with rhctl. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdio.h> +#include <string.h> + +#include "key_value_storage.h" + +void key_value_storage_init(key_value_storage_t* stor) +{ + if(!stor) + return; + + string_list_init(&(stor->keys_)); + string_list_init(&(stor->values_)); +} + +void key_value_storage_clear(key_value_storage_t* stor) +{ + if(!stor) + return; + + string_list_clear(&(stor->keys_)); + string_list_clear(&(stor->values_)); +} + +int key_value_storage_add(key_value_storage_t* stor, const char* key, const char* value) +{ + if(!stor || !key || !value) + return -1; + + int ret = string_list_add(&(stor->keys_), key); + if(ret!=0) + return ret; + + ret = string_list_add(&(stor->values_), value); + if(ret!=0) + return ret; + + return 0; +} + +char* key_value_storage_find(key_value_storage_t* stor, const char* key) +{ + if(!stor || !key) + return NULL; + + string_list_element_t* k = stor->keys_.first_; + string_list_element_t* v = stor->values_.first_; + while(v && k) { + if(!strcmp(k->string_, key)) + return v->string_; + + k = k->next_; + v = v->next_; + } + + return NULL; +} + +void key_value_storage_print(key_value_storage_t* stor) +{ + if(!stor) + return; + + string_list_element_t* k = stor->keys_.first_; + string_list_element_t* v = stor->values_.first_; + while(v && k) { + printf("'%s' -> '%s'\n", k->string_, v->string_); + k = k->next_; + v = v->next_; + } + printf("\n"); +} |