summaryrefslogtreecommitdiff
path: root/utils/messages.c
blob: 4d07a47ef7c13144fea33407d6dfc9b13af42345 (plain)
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
/*
 * 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 2004 James Bursa <bursa@users.sourceforge.net>
 */

/** \file
 * Localised message support (implementation).
 *
 * Native language messages are loaded from a file and stored hashed by key for
 * fast access.
 */

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#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

/** Maximum length of a key. */
#define MAX_KEY_LENGTH 24

/** Entry in the messages hash table. */
struct messages_entry {
	struct messages_entry *next;  /**< Next entry in this hash chain. */
	char key[MAX_KEY_LENGTH];
	char value[1];
};

/** Localised messages hash table. */
static struct messages_entry *messages_table[HASH_SIZE];


static unsigned int messages_hash(const char *s);


/**
 * Read keys and values from messages file.
 *
 * \param  path  pathname of messages file
 *
 * The messages are merged with any previously loaded messages. Any keys which
 * are present already are replaced with the new value.
 *
 * Exits through die() in case of error.
 */

void messages_load(const char *path)
{
	char s[300];
	FILE *fp;

	fp = fopen(path, "r");
	if (!fp) {
		snprintf(s, sizeof s, "Unable to open messages file "
				"\"%.100s\": %s", path, strerror(errno));
		s[sizeof s - 1] = 0;
		LOG(("%s", s));
		die(s);
	}

	while (fgets(s, sizeof s, fp)) {
		char *colon, *value;
		unsigned int slot;
		struct messages_entry *entry;
		size_t length;

		if (s[0] == 0 || s[0] == '#')
			continue;

		s[strlen(s) - 1] = 0;  /* remove \n at end */
		colon = strchr(s, ':');
		if (!colon)
			continue;
		*colon = 0;  /* terminate key */
		value = colon + 1;
		length = strlen(value);

		entry = malloc(sizeof *entry + length + 1);
		if (!entry) {
			snprintf(s, sizeof s, "Not enough memory to load "
					"messages file \"%.100s\".", path);
			s[sizeof s - 1] = 0;
			LOG(("%s", s));
			die(s);
		}
		strncpy(entry->key, s, MAX_KEY_LENGTH);
		strcpy(entry->value, value);
		slot = messages_hash(entry->key);
		entry->next = messages_table[slot];
		messages_table[slot] = entry;
	}

	fclose(fp);
}


/**
 * Fast lookup of a message by key.
 *
 * \param  key  key of message
 * \return  value of message, or key if not found
 */

const char *messages_get(const char *key)
{
	struct messages_entry *entry;

	for (entry = messages_table[messages_hash(key)];
			entry && strcasecmp(entry->key, key) != 0;
			entry = entry->next)
		;
	if (!entry)
		return key;
	return entry->value;
}


/**
 * Hash function for keys.
 */

unsigned int messages_hash(const char *s)
{
	unsigned int i, z = 0;
	if (!s)
		return 0;
	for (i = 0; i != MAX_KEY_LENGTH && s[i]; i++)
		z += s[i] & 0x1f;  /* lower 5 bits, case insensitive */
	return z % HASH_SIZE;
}


/**
 * Dump set of loaded messages.
 */

void messages_dump(void)
{
	unsigned int i;
	for (i = 0; i != HASH_SIZE; i++) {
		struct messages_entry *entry;
		for (entry = messages_table[i]; entry; entry = entry->next)
			printf("%.20s:%s\n", entry->key, entry->value);
	}
}