From 15d57a66349d999edf83bfb892b8405779fc2821 Mon Sep 17 00:00:00 2001 From: James Bursa Date: Sat, 16 Aug 2003 18:39:10 +0000 Subject: [project @ 2003-08-16 18:39:10 by bursa] New portable messages module. svn path=/import/netsurf/; revision=244 --- makefile | 2 +- utils/messages.c | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils/messages.h | 27 +++++++++++ 3 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 utils/messages.c create mode 100644 utils/messages.h diff --git a/makefile b/makefile index 6b52124eb..0612c5722 100644 --- a/makefile +++ b/makefile @@ -9,7 +9,7 @@ CC_DEBUG = gcc OBJECTS_COMMON = cache.o content.o fetch.o fetchcache.o other.o \ css.o css_enum.o parser.o ruleset.o scanner.o \ box.o html.o layout.o textplain.o \ - utils.o + messages.o utils.o OBJECTS = $(OBJECTS_COMMON) \ browser.o netsurf.o \ gif.o gui.o jpeg.o png.o theme.o plugin.o \ diff --git a/utils/messages.c b/utils/messages.c new file mode 100644 index 000000000..cddbfddeb --- /dev/null +++ b/utils/messages.c @@ -0,0 +1,134 @@ +/* + * This file is part of NetSurf, http://netsurf.sourceforge.net/ + * Licensed under the GNU General Public License, + * http://www.opensource.org/licenses/gpl-license + * Copyright 2003 James Bursa + */ + +#include +#include +#include +#include "netsurf/utils/log.h" +#include "netsurf/utils/messages.h" +#include "netsurf/utils/utils.h" + +/* We store the messages in a fixed-size hash table. */ + +#define HASH_SIZE 77 + +struct entry { + const char *key; + const char *value; + struct entry *next; /* next in this hash chain */ +}; + +static struct entry *table[HASH_SIZE]; + +static unsigned int messages_hash(const char *s); + + +/** + * messages_load -- read a messages file into the hash table + */ + +void messages_load(const char *path) +{ + char s[300]; + FILE *fp; + + fp = fopen(path, "r"); + if (fp == 0) { + LOG(("failed to open file '%s'", path)); + return; + } + + while (fgets(s, 300, fp) != 0) { + char *colon; + unsigned int slot; + struct entry *entry; + + if (s[0] == 0 || s[0] == '#') + continue; + colon = strchr(s, ':'); + if (colon == 0) + continue; + s[strlen(s) - 1] = 0; /* remove \n at end */ + *colon = 0; /* terminate key */ + + entry = xcalloc(1, sizeof(*entry)); + entry->key = xstrdup(s); + entry->value = xstrdup(colon + 1); + slot = messages_hash(entry->key); + entry->next = table[slot]; + table[slot] = entry; + } + + fclose(fp); +} + + +/** + * messages_get -- fast lookup of a message by key + */ + +const char *messages_get(const char *key) +{ + char *colon; + const char *value = key; + char key2[40]; + unsigned int slot, len; + struct entry *entry; + + colon = strchr(key, ':'); + if (colon != 0) { + /* fallback appended to key */ + value = colon + 1; + len = colon - key; + if (39 < len) + len = 39; + strncpy(key2, key, len); + key2[len] = 0; + key = key2; + } + + slot = messages_hash(key); + for (entry = table[slot]; + entry != 0 && strcasecmp(entry->key, key) != 0; + entry = entry->next) + ; + if (entry == 0) { + LOG(("using fallback for key '%s'", key)); + return value; + } + return entry->value; +} + + +/** + * messages_hash -- hash function for keys + */ + +unsigned int messages_hash(const char *s) +{ + unsigned int z = 0; + if (s == 0) + return 0; + for (; *s != 0; s++) + z += *s & 0x1f; /* lower 5 bits, case insensitive */ + return (z % (HASH_SIZE - 1)) + 1; +} + + +/** + * messages_dump -- dump contents of hash table + */ + +void messages_dump(void) +{ + unsigned int i; + for (i = 0; i != HASH_SIZE; i++) { + struct entry *entry; + for (entry = table[i]; entry != 0; entry = entry->next) + printf("%s:%s\n", entry->key, entry->value); + } +} diff --git a/utils/messages.h b/utils/messages.h new file mode 100644 index 000000000..fd8457da8 --- /dev/null +++ b/utils/messages.h @@ -0,0 +1,27 @@ +/* + * This file is part of NetSurf, http://netsurf.sourceforge.net/ + * Licensed under the GNU General Public License, + * http://www.opensource.org/licenses/gpl-license + * Copyright 2003 James Bursa + */ + +/** + * The messages module loads a file of keys and associated strings, and + * provides fast lookup by key. The messages file consists of key:value lines, + * comment lines starting with #, and other lines are ignored. Use + * messages_load() to read the file into memory. To lookup a key, use + * messages_get("key") or messages_get("key:fallback") . A pointer to the + * value is returned, and this is shared by all callers. If the key does not + * exist, the parameter will be returned in the first case and a pointer to + * the fallback string in the parameter in the second. Thus the parameter must + * be a constant string. + */ + +#ifndef _NETSURF_UTILS_MESSAGES_H_ +#define _NETSURF_UTILS_MESSAGES_H_ + +void messages_load(const char *path); +const char *messages_get(const char *key); +void messages_dump(void); + +#endif -- cgit v1.2.3