| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
#include <stdlib.h> |
|---|
| 23 |
|
|---|
| 24 |
#include "libswish3.h" |
|---|
| 25 |
|
|---|
| 26 |
extern int SWISH_DEBUG; |
|---|
| 27 |
|
|---|
| 28 |
static void free_hashval( |
|---|
| 29 |
void *val, |
|---|
| 30 |
xmlChar *key |
|---|
| 31 |
); |
|---|
| 32 |
static void merge_hashes( |
|---|
| 33 |
xmlChar *value, |
|---|
| 34 |
xmlHashTablePtr hash1, |
|---|
| 35 |
xmlChar *key |
|---|
| 36 |
); |
|---|
| 37 |
|
|---|
| 38 |
static void |
|---|
| 39 |
free_hashval( |
|---|
| 40 |
void *val, |
|---|
| 41 |
xmlChar *key |
|---|
| 42 |
) |
|---|
| 43 |
{ |
|---|
| 44 |
swish_xfree(val); |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
int |
|---|
| 48 |
swish_hash_add( |
|---|
| 49 |
xmlHashTablePtr hash, |
|---|
| 50 |
xmlChar *key, |
|---|
| 51 |
void *value |
|---|
| 52 |
) |
|---|
| 53 |
{ |
|---|
| 54 |
int ret; |
|---|
| 55 |
ret = xmlHashAddEntry(hash, key, value); |
|---|
| 56 |
if (ret == -1) |
|---|
| 57 |
SWISH_CROAK("xmlHashAddEntry for %s failed", key); |
|---|
| 58 |
|
|---|
| 59 |
return ret; |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
int |
|---|
| 63 |
swish_hash_exists_or_add( |
|---|
| 64 |
xmlHashTablePtr hash, |
|---|
| 65 |
xmlChar *key, |
|---|
| 66 |
xmlChar *value |
|---|
| 67 |
) |
|---|
| 68 |
{ |
|---|
| 69 |
if (!swish_hash_exists(hash, key)) { |
|---|
| 70 |
return swish_hash_add(hash, key, swish_xstrdup( value )); |
|---|
| 71 |
} |
|---|
| 72 |
else { |
|---|
| 73 |
return 1; |
|---|
| 74 |
} |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
void |
|---|
| 78 |
swish_hash_free( |
|---|
| 79 |
xmlHashTablePtr hash |
|---|
| 80 |
) |
|---|
| 81 |
{ |
|---|
| 82 |
xmlHashFree(hash, (xmlHashDeallocator)free_hashval); |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
int |
|---|
| 86 |
swish_hash_replace( |
|---|
| 87 |
xmlHashTablePtr hash, |
|---|
| 88 |
xmlChar *key, |
|---|
| 89 |
void *value |
|---|
| 90 |
) |
|---|
| 91 |
{ |
|---|
| 92 |
int ret; |
|---|
| 93 |
ret = |
|---|
| 94 |
xmlHashUpdateEntry(hash, key, value, (xmlHashDeallocator) free_hashval); |
|---|
| 95 |
if (ret == -1) |
|---|
| 96 |
SWISH_CROAK("xmlHashUpdateEntry for %s failed", key); |
|---|
| 97 |
|
|---|
| 98 |
return ret; |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
int |
|---|
| 102 |
swish_hash_delete( |
|---|
| 103 |
xmlHashTablePtr hash, |
|---|
| 104 |
xmlChar *key |
|---|
| 105 |
) |
|---|
| 106 |
{ |
|---|
| 107 |
int ret; |
|---|
| 108 |
ret = xmlHashRemoveEntry(hash, key, (xmlHashDeallocator) free_hashval); |
|---|
| 109 |
if (ret == -1) |
|---|
| 110 |
SWISH_CROAK("xmlHashRemoveEntry for %s failed", key); |
|---|
| 111 |
|
|---|
| 112 |
return ret; |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
boolean |
|---|
| 116 |
swish_hash_exists( |
|---|
| 117 |
xmlHashTablePtr hash, |
|---|
| 118 |
xmlChar *key |
|---|
| 119 |
) |
|---|
| 120 |
{ |
|---|
| 121 |
return xmlHashLookup(hash, key) ? 1 : 0; |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
void * |
|---|
| 125 |
swish_hash_fetch( |
|---|
| 126 |
xmlHashTablePtr hash, |
|---|
| 127 |
xmlChar *key |
|---|
| 128 |
) |
|---|
| 129 |
{ |
|---|
| 130 |
return xmlHashLookup(hash, key); |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
xmlHashTablePtr |
|---|
| 134 |
swish_init_hash( |
|---|
| 135 |
int size |
|---|
| 136 |
) |
|---|
| 137 |
{ |
|---|
| 138 |
xmlHashTablePtr h; |
|---|
| 139 |
|
|---|
| 140 |
h = xmlHashCreate(size); |
|---|
| 141 |
if (h == NULL) { |
|---|
| 142 |
SWISH_CROAK("error creating hash of size %d", size); |
|---|
| 143 |
return NULL; |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
return h; |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
static void |
|---|
| 150 |
merge_hashes( |
|---|
| 151 |
xmlChar *value, |
|---|
| 152 |
xmlHashTablePtr hash1, |
|---|
| 153 |
xmlChar *key |
|---|
| 154 |
) |
|---|
| 155 |
{ |
|---|
| 156 |
if (swish_hash_exists(hash1, key)) { |
|---|
| 157 |
swish_hash_replace(hash1, key, swish_xstrdup(value)); |
|---|
| 158 |
} |
|---|
| 159 |
else { |
|---|
| 160 |
swish_hash_add(hash1, key, swish_xstrdup(value)); |
|---|
| 161 |
} |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
void |
|---|
| 165 |
swish_hash_merge( |
|---|
| 166 |
xmlHashTablePtr hash1, |
|---|
| 167 |
xmlHashTablePtr hash2 |
|---|
| 168 |
) |
|---|
| 169 |
{ |
|---|
| 170 |
xmlHashScan(hash2, (xmlHashScanner) merge_hashes, hash1); |
|---|
| 171 |
} |
|---|