1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
/* pair_ht.c - two (char *) hash table
Copyright 1988-2017 Free Software Foundation, Inc.
This program 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, or (at your option)
any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
02110-1301, USA. */
#include <config.h>
#include "a2ps.h"
/* Hack! */
#define hash_table_s pair_htable
#include "hashtab.h"
#include "message.h"
#include "pair_ht.h"
#include "getshline.h"
#include "lister.h"
#include "quotearg.h"
#include "routines.h"
/************************************************************************
* hash tables with two char * fields *
************************************************************************/
/* Definition of the hash structure */
struct pair
{
char * key;
char * value;
float ratio;
int wx;
};
/*
* Basic routines
*/
static unsigned long
pair_hash_1 (struct pair *pair)
{
return_STRING_HASH_1 (pair->key);
}
static unsigned long
pair_hash_2 (struct pair *pair)
{
return_STRING_HASH_2 (pair->key);
}
static int
pair_hash_cmp (struct pair *x, struct pair *y)
{
return_STRING_COMPARE (x->key, y->key);
}
/*
* For sorting them in alpha order
*/
static int
pair_hash_qcmp (struct pair **x, struct pair **y)
{
return_STRING_COMPARE ((*x)->key, (*y)->key);
}
/* Return the length of the key of PAIR */
static size_t
pair_key_len (struct pair * pair)
{
return strlen (pair->key);
}
/* Fputs the key of PAIR to STREAM */
static int
pair_key_fputs (struct pair * pair, FILE * stream)
{
return fputs (pair->key, stream);
}
/*
* Create the structure that stores the list of pairs
*/
struct hash_table_s *
pair_table_new (void)
{
struct hash_table_s * res;
res = XMALLOC (struct hash_table_s);
hash_init (res, 8,
(hash_func_t) pair_hash_1,
(hash_func_t) pair_hash_2,
(hash_cmp_func_t) pair_hash_cmp);
return res;
}
/*
* Add a pair, with your own allocation for them.
* It KEY is yet used, override its value with VALUE
*/
void
pair_add (struct hash_table_s * table,
const char * key, const char * value)
{
struct pair * item, token;
token.key = (char *) key;
item = (struct pair *) hash_find_item (table, &token);
if (!item) {
item = XMALLOC (struct pair);
item->key = xstrdup(key);
}
if (value)
item->value = xstrdup (value);
else
item->value = NULL;
hash_insert (table, item);
}
/*
* Add a pair, with your own allocation for them.
* It KEY is yet used, override its value with VALUE
*/
void
pair_add2 (struct hash_table_s * table,
const char * key, const char * value, int wx, float ratio)
{
struct pair * item, token;
token.key = (char *) key;
item = (struct pair *) hash_find_item (table, &token);
if (!item) {
item = XMALLOC (struct pair);
item->key = xstrdup(key);
item->wx = wx;
item->ratio = ratio;
}
if (value)
item->value = xstrdup (value);
else
item->value = NULL;
hash_insert (table, item);
}
/*
* Remove a pair.
* It KEY is yet used, override its value with VALUE
*/
void
pair_delete (struct hash_table_s * table, const char * key)
{
struct pair * item, token;
token.key = (char *) key;
item = (struct pair *) hash_find_item (table, &token);
if (item)
hash_delete (table, item);
}
/*
* Get the value associated to KEY in TABLE
* Return NULL upon error (this means that it is not
* valid to enter NULL as a value)
*/
char *
pair_get (struct hash_table_s * table, const char * key)
{
struct pair * item, token;
token.key = (char *) key;
item = (struct pair *) hash_find_item (table, &token);
if (item)
return item->value;
else
return NULL;
}
int
pair_get_wx (struct hash_table_s * table, const char * key)
{
struct pair * item, token;
token.key = (char *) key;
item = (struct pair *) hash_find_item (table, &token);
if (item)
return item->wx;
else
return -1;
}
float
pair_get_ratio (struct hash_table_s * table, const char * key)
{
struct pair * item, token;
token.key = (char *) key;
item = (struct pair *) hash_find_item (table, &token);
if (item)
return item->ratio;
else
return -1;
}
/*
* Return the content of the hash table, ordered
*/
void
pair_table_map (struct hash_table_s * table,
pair_ht_map_fn_t map_fn,
pair_ht_select_fn_t select_fn,
void const * arg)
{
int i, num = 0;
struct pair ** entries;
entries = (struct pair **)
hash_dump (table, NULL,
(hash_cmp_func_t) pair_hash_qcmp);
for (i = 0 ; entries[i] ; i++) {
if (!select_fn
|| select_fn (entries[i]-> key, entries[i]->value))
{
map_fn (num, entries[i]-> key, entries[i]->value, arg);
num++;
}
}
}
/*
* For typically for --list-features
*/
void
pair_table_list_short (struct hash_table_s * table, FILE * stream)
{
struct pair ** entries;
entries = (struct pair **)
hash_dump (table, NULL,
(hash_cmp_func_t) pair_hash_qcmp);
lister_fprint_vertical (NULL, stream,
(void **) entries, (size_t) -1,
(lister_width_t) pair_key_len,
(lister_print_t) pair_key_fputs);
}
/*
* For typically for --list-<something>
*/
void
pair_table_list_long (struct hash_table_s * table, FILE * stream)
{
int i;
struct pair ** entries;
entries = (struct pair **)
hash_dump (table, NULL,
(hash_cmp_func_t) pair_hash_qcmp);
for (i = 0 ; entries[i] ; i++)
fprintf (stream, "%-16s = %s\n",
entries[i]->key,
entries[i]->value ? entries[i]->value : "<NULL>");
putc ('\n', stream);
}
/*
* Mostly for debbuging
*/
void
pair_table_self_print (struct hash_table_s * table, FILE * stream)
{
int i;
struct pair ** entries;
entries = (struct pair **)
hash_dump (table, NULL,
(hash_cmp_func_t) pair_hash_qcmp);
for (i = 0 ; entries[i] ; i++)
fprintf (stream, "%s:%s\n",
entries[i]->key,
entries[i]->value ? entries[i]->value : "<NULL>");
putc ('\n', stream);
}
#define GET_TOKEN(from) (strtok ((from), " \t\n"))
#define CHECK_TOKEN() \
if (token2 == NULL) \
error_at_line (1, 0, file, firstline, \
_("missing argument for `%s'"), quotearg (token));
/*
* Read from a FILE
*/
int
pair_table_load (struct hash_table_s * table, const char *file)
{
FILE * fp;
char *buf = NULL;
size_t bufsiz = 0;
char * token, * token2;
unsigned firstline = 0, lastline = 0;
message (msg_file,
(stderr, "Loading map file `%s'\n", quotearg (file)));
fp = xrfopen (file);
while (getshline_numbered (&firstline, &lastline, &buf, &bufsiz, fp) != -1)
{
token = GET_TOKEN (buf);
if (!token)
/* Blank but not empty */
continue;
if (STREQ (token, "***"))
{
/* Load another map file */
token2 = GET_TOKEN (NULL); /* A map file path */
CHECK_TOKEN ();
pair_table_load (table, token2);
}
else
{
token2 = GET_TOKEN (NULL); /* key */
CHECK_TOKEN ();
pair_add (table, token, token2);
}
}
fclose (fp);
return 1;
}
|