123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- #include "strmap.h"
- typedef struct Pair Pair;
- typedef struct Bucket Bucket;
- struct Pair {
- char *key;
- char *value;
- };
- struct Bucket {
- unsigned int count;
- Pair *pairs;
- };
- struct StrMap {
- unsigned int count;
- Bucket *buckets;
- };
- static Pair * get_pair(Bucket *bucket, const char *key);
- static unsigned long hash(const char *str);
- StrMap * strmap_new(unsigned int capacity)
- {
- StrMap *map;
-
- map = malloc(sizeof(StrMap));
- if (map == NULL) {
- return NULL;
- }
- map->count = capacity;
- map->buckets = malloc(map->count * sizeof(Bucket));
- if (map->buckets == NULL) {
- free(map);
- return NULL;
- }
- memset(map->buckets, 0, map->count * sizeof(Bucket));
- return map;
- }
- void strmap_delete(StrMap *map)
- {
- unsigned int i, j, n, m;
- Bucket *bucket;
- Pair *pair;
- if (map == NULL) {
- return;
- }
- n = map->count;
- bucket = map->buckets;
- i = 0;
- while (i < n) {
- m = bucket->count;
- pair = bucket->pairs;
- j = 0;
- while(j < m) {
- free(pair->key);
- free(pair->value);
- pair++;
- j++;
- }
- free(bucket->pairs);
- bucket++;
- i++;
- }
- free(map->buckets);
- free(map);
- }
- int strmap_get(const StrMap *map, const char *key, char *out_buf, unsigned int n_out_buf)
- {
- unsigned int index;
- Bucket *bucket;
- Pair *pair;
- if (map == NULL) {
- return 0;
- }
- if (key == NULL) {
- return 0;
- }
- index = hash(key) % map->count;
- bucket = &(map->buckets[index]);
- pair = get_pair(bucket, key);
- if (pair == NULL) {
- return 0;
- }
- if (out_buf == NULL && n_out_buf == 0) {
- return strlen(pair->value) + 1;
- }
- if (out_buf == NULL) {
- return 0;
- }
- if (strlen(pair->value) >= n_out_buf) {
- return 0;
- }
- strcpy(out_buf, pair->value);
- return 1;
- }
- int strmap_exists(const StrMap *map, const char *key)
- {
- unsigned int index;
- Bucket *bucket;
- Pair *pair;
- if (map == NULL) {
- return 0;
- }
- if (key == NULL) {
- return 0;
- }
- index = hash(key) % map->count;
- bucket = &(map->buckets[index]);
- pair = get_pair(bucket, key);
- if (pair == NULL) {
- return 0;
- }
- return 1;
- }
- int strmap_put(StrMap *map, const char *key, const char *value)
- {
- unsigned int key_len, value_len, index;
- Bucket *bucket;
- Pair *tmp_pairs, *pair;
- char *tmp_value;
- char *new_key, *new_value;
- if (map == NULL) {
- return 0;
- }
- if (key == NULL || value == NULL) {
- return 0;
- }
- key_len = strlen(key);
- value_len = strlen(value);
-
- index = hash(key) % map->count;
- bucket = &(map->buckets[index]);
-
- if ((pair = get_pair(bucket, key)) != NULL) {
-
- if (strlen(pair->value) < value_len) {
-
- tmp_value = realloc(pair->value, (value_len + 1) * sizeof(char));
- if (tmp_value == NULL) {
- return 0;
- }
- pair->value = tmp_value;
- }
-
- strcpy(pair->value, value);
- return 1;
- }
-
- new_key = malloc((key_len + 1) * sizeof(char));
- if (new_key == NULL) {
- return 0;
- }
- new_value = malloc((value_len + 1) * sizeof(char));
- if (new_value == NULL) {
- free(new_key);
- return 0;
- }
-
- if (bucket->count == 0) {
-
- bucket->pairs = malloc(sizeof(Pair));
- if (bucket->pairs == NULL) {
- free(new_key);
- free(new_value);
- return 0;
- }
- bucket->count = 1;
- }
- else {
-
- tmp_pairs = realloc(bucket->pairs, (bucket->count + 1) * sizeof(Pair));
- if (tmp_pairs == NULL) {
- free(new_key);
- free(new_value);
- return 0;
- }
- bucket->pairs = tmp_pairs;
- bucket->count++;
- }
-
- pair = &(bucket->pairs[bucket->count - 1]);
- pair->key = new_key;
- pair->value = new_value;
-
- strcpy(pair->key, key);
- strcpy(pair->value, value);
- return 1;
- }
- int strmap_get_count(const StrMap *map)
- {
- unsigned int i, j, n, m;
- unsigned int count;
- Bucket *bucket;
- Pair *pair;
- if (map == NULL) {
- return 0;
- }
- bucket = map->buckets;
- n = map->count;
- i = 0;
- count = 0;
- while (i < n) {
- pair = bucket->pairs;
- m = bucket->count;
- j = 0;
- while (j < m) {
- count++;
- pair++;
- j++;
- }
- bucket++;
- i++;
- }
- return count;
- }
- int strmap_enum(const StrMap *map, strmap_enum_func enum_func, const void *obj)
- {
- unsigned int i, j, n, m;
- Bucket *bucket;
- Pair *pair;
- if (map == NULL) {
- return 0;
- }
- if (enum_func == NULL) {
- return 0;
- }
- bucket = map->buckets;
- n = map->count;
- i = 0;
- while (i < n) {
- pair = bucket->pairs;
- m = bucket->count;
- j = 0;
- while (j < m) {
- enum_func(pair->key, pair->value, obj);
- pair++;
- j++;
- }
- bucket++;
- i++;
- }
- return 1;
- }
- static Pair * get_pair(Bucket *bucket, const char *key)
- {
- unsigned int i, n;
- Pair *pair;
- n = bucket->count;
- if (n == 0) {
- return NULL;
- }
- pair = bucket->pairs;
- i = 0;
- while (i < n) {
- if (pair->key != NULL && pair->value != NULL) {
- if (strcmp(pair->key, key) == 0) {
- return pair;
- }
- }
- pair++;
- i++;
- }
- return NULL;
- }
- static unsigned long hash(const char *str)
- {
- unsigned long hash = 5381;
- int c;
- c = 0;
- while (c == *str++) {
- hash = ((hash << 5) + hash) + c;
- }
- return hash;
- }
|