[7d3ae3e] | 1 | /* |
---|
| 2 | * Estonian ID card plugin for web browsers |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2010-2011 Codeborne <info@codeborne.com> |
---|
| 5 | * |
---|
| 6 | * This is free software; you can redistribute it and/or |
---|
| 7 | * modify it under the terms of the GNU Lesser General Public |
---|
| 8 | * License as published by the Free Software Foundation; either |
---|
| 9 | * version 2.1 of the License, or (at your option) any later version. |
---|
| 10 | * |
---|
| 11 | * This software is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 14 | * Lesser General Public License for more details. |
---|
| 15 | * |
---|
| 16 | * You should have received a copy of the GNU Lesser General Public |
---|
| 17 | * License along with this library; if not, write to the Free Software |
---|
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 19 | * |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include <stdlib.h> |
---|
| 23 | #include <string.h> |
---|
| 24 | #include "esteid_map.h" |
---|
| 25 | |
---|
| 26 | EstEID_Map EstEID_mapPut(EstEID_Map map, const char *key, const char *value) { |
---|
| 27 | return EstEID_mapPutNoAlloc(map, strdup(key), strdup(value)); |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | EstEID_Map EstEID_mapPutNoAlloc(EstEID_Map map, const char *key, const char *value) { |
---|
| 31 | if (!map) { |
---|
| 32 | map = (EstEID_Map)malloc(sizeof(struct EstEID_MapEntry)); |
---|
| 33 | map->key = key; |
---|
| 34 | map->value = value; |
---|
| 35 | map->next = NULL; |
---|
| 36 | } |
---|
| 37 | else if (!strcmp(map->key, key)) { |
---|
| 38 | free((void *)map->key); |
---|
| 39 | free((void *)map->value); |
---|
| 40 | map->key = key; |
---|
| 41 | map->value = value; |
---|
| 42 | } |
---|
| 43 | else { |
---|
| 44 | map->next = EstEID_mapPut(map->next, key, value); |
---|
| 45 | } |
---|
| 46 | return map; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | const char *EstEID_mapGet(EstEID_Map map, const char *key) { |
---|
| 50 | if (!map) { |
---|
| 51 | return NULL; |
---|
| 52 | } |
---|
| 53 | if (!strcmp(map->key, key)) { |
---|
| 54 | return map->value; |
---|
| 55 | } |
---|
| 56 | return EstEID_mapGet(map->next, key); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | unsigned int EstEID_mapSize(EstEID_Map map) { |
---|
| 60 | if (!map) return 0; |
---|
| 61 | return 1 + EstEID_mapSize(map->next); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | void EstEID_mapFree(EstEID_Map map) { |
---|
| 65 | if (!map) return; |
---|
| 66 | if (map->next) EstEID_mapFree(map->next); |
---|
| 67 | free((void *)map->value); |
---|
| 68 | free((void *)map->key); |
---|
| 69 | free(map); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | void EstEID_mapPrint(FILE *stream, EstEID_Map map) { |
---|
| 73 | if (!map) return; |
---|
| 74 | fprintf(stream, "\t%s = %s\n", map->key, map->value); |
---|
| 75 | EstEID_mapPrint(stream, map->next); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | EstEID_Map EstEID_mapClone(EstEID_Map map) { |
---|
| 79 | EstEID_Map result; |
---|
| 80 | if (!map) return NULL; |
---|
| 81 | result = EstEID_mapPut(NULL, map->key, map->value); |
---|
| 82 | result->next = EstEID_mapClone(map->next); |
---|
| 83 | return result; |
---|
| 84 | } |
---|
| 85 | |
---|