muscle: refactor

* src/muscle-tab.c (muscle_lookup, muscle_entry_new): New.
(muscle_insert, muscle_grow, muscle_find_const, muscle_find): Use them.
This commit is contained in:
Akim Demaille
2013-04-04 15:18:29 +02:00
parent 9fb61ca8d8
commit c56d0037d2

View File

@@ -60,6 +60,19 @@ hash_muscle (const void *x, size_t tablesize)
return hash_string (m->key, tablesize); return hash_string (m->key, tablesize);
} }
/* Create a fresh muscle name KEY, and insert in the hash table. */
static void *
muscle_entry_new (char const *key)
{
muscle_entry *res = xmalloc (sizeof *res);
res->key = key;
res->value = NULL;
res->storage = NULL;
if (!hash_insert (muscle_table, res))
xalloc_die ();
return res;
}
static void static void
muscle_entry_free (void *entry) muscle_entry_free (void *entry)
{ {
@@ -89,26 +102,26 @@ muscle_free (void)
obstack_free (&muscle_obstack, NULL); obstack_free (&muscle_obstack, NULL);
} }
/* Look for the muscle named KEY. Return NULL if does not exist. */
static
muscle_entry *
muscle_lookup (char const *key)
{
muscle_entry probe;
probe.key = key;
return hash_lookup (muscle_table, &probe);
}
void void
muscle_insert (char const *key, char const *value) muscle_insert (char const *key, char const *value)
{ {
muscle_entry probe; muscle_entry *entry = muscle_lookup (key);
muscle_entry *entry; if (entry)
probe.key = key;
entry = hash_lookup (muscle_table, &probe);
if (!entry)
{
/* First insertion in the hash. */
entry = xmalloc (sizeof *entry);
entry->key = key;
if (!hash_insert (muscle_table, entry))
xalloc_die ();
}
else
free (entry->storage); free (entry->storage);
else
/* First insertion in the hash. */
entry = muscle_entry_new (key);
entry->value = value; entry->value = value;
entry->storage = NULL; entry->storage = NULL;
} }
@@ -123,22 +136,9 @@ muscle_insert (char const *key, char const *value)
void void
muscle_grow (const char *key, const char *val, const char *separator) muscle_grow (const char *key, const char *val, const char *separator)
{ {
muscle_entry probe; muscle_entry *entry = muscle_lookup (key);
muscle_entry *entry = NULL;
probe.key = key; if (entry)
entry = hash_lookup (muscle_table, &probe);
if (!entry)
{
/* First insertion in the hash. */
entry = xmalloc (sizeof *entry);
entry->key = key;
if (!hash_insert (muscle_table, entry))
xalloc_die ();
entry->value = entry->storage = xstrdup (val);
}
else
{ {
/* Grow the current value. */ /* Grow the current value. */
char *new_val; char *new_val;
@@ -148,6 +148,12 @@ muscle_grow (const char *key, const char *val, const char *separator)
entry->value = entry->storage = xstrdup (new_val); entry->value = entry->storage = xstrdup (new_val);
obstack_free (&muscle_obstack, new_val); obstack_free (&muscle_obstack, new_val);
} }
else
{
/* First insertion in the hash. */
entry = muscle_entry_new (key);
entry->value = entry->storage = xstrdup (val);
}
} }
/*------------------------------------------------------------------. /*------------------------------------------------------------------.
@@ -182,8 +188,9 @@ muscle_code_grow (const char *key, const char *val, location loc)
} }
void muscle_pair_list_grow (const char *muscle, void
const char *a1, const char *a2) muscle_pair_list_grow (const char *muscle,
const char *a1, const char *a2)
{ {
char *pair; char *pair;
obstack_sgrow (&muscle_obstack, "["); obstack_sgrow (&muscle_obstack, "[");
@@ -200,29 +207,19 @@ void muscle_pair_list_grow (const char *muscle,
char const * char const *
muscle_find_const (char const *key) muscle_find_const (char const *key)
{ {
muscle_entry probe; muscle_entry *entry = muscle_lookup (key);
muscle_entry *result = NULL; return entry ? entry->value : NULL;
probe.key = key;
result = hash_lookup (muscle_table, &probe);
if (result)
return result->value;
return NULL;
} }
char * char *
muscle_find (char const *key) muscle_find (char const *key)
{ {
muscle_entry probe; muscle_entry *entry = muscle_lookup (key);
muscle_entry *result = NULL; if (entry)
probe.key = key;
result = hash_lookup (muscle_table, &probe);
if (result)
{ {
aver (result->value == result->storage); aver (entry->value == entry->storage);
return result->storage; return entry->storage;
} }
return NULL; return NULL;
} }