summaryrefslogtreecommitdiff
path: root/frontends/framebuffer/findfile.c
blob: fffff46877a9a5ea5ed99a551f25ecda16808ce6 (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
153
154
155
156
157
/*
 * Copyright 2008 Daniel Silverstone <dsilvers@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf 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; version 2 of the License.
 *
 * NetSurf 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, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "utils/filepath.h"
#include "utils/log.h"

#include "framebuffer/findfile.h"

char **respaths; /** resource search path vector */

#define MAX_LANGV_SIZE 32

/**
 * goes through the environment in appropriate order to find configured language
 *
 * \return language to use or "C" if nothing appropriate is set
 */
static const char *get_language_env(void)
{
	const char *envstr;

	envstr = getenv("LANGUAGE");
	if ((envstr != NULL) && (envstr[0] != 0)) {
		return envstr;
	}

	envstr = getenv("LC_ALL");
	if ((envstr != NULL) && (envstr[0] != 0)) {
		return envstr;
	}

	envstr = getenv("LC_MESSAGES");
	if ((envstr != NULL) && (envstr[0] != 0)) {
		return envstr;
	}

	envstr = getenv("LANG");
	if ((envstr != NULL) && (envstr[0] != 0)) {
		return envstr;
	}

	return "C";
}

/**
 * build a string vector of language names
 */
static char **get_language_names(void)
{
	char **langv; /* output string vector of languages */
	int langc; /* count of languages in vector */
	const char *envlang; /* colon separated list of languages from environment */
	int lstart = 0; /* offset to start of current language */
	int lunder = 0; /* offset to underscore in current language */
	int lend = 0; /* offset to end of current language */
	char *nlang;
	
	langv = calloc(MAX_LANGV_SIZE + 2, sizeof(char *));
	if (langv == NULL) {
		return NULL;
	}

	envlang = get_language_env();
	
	for (langc = 0; langc < MAX_LANGV_SIZE; langc++) {
		/* work through envlang splitting on : */
		while ((envlang[lend] != 0) &&
		       (envlang[lend] != ':') &&
		       (envlang[lend] != '.')) {
			if (envlang[lend] == '_') {
				lunder = lend;
			}
			lend++;
		}
		/* place language in string vector */
		nlang = malloc(lend - lstart + 1);
		memcpy(nlang, envlang + lstart, lend - lstart);
		nlang[lend - lstart] = 0;
		langv[langc] = nlang;

		/* add language without specialisation to vector */
		if (lunder != lstart) {
			nlang = malloc(lunder - lstart + 1);
			memcpy(nlang, envlang + lstart, lunder - lstart);
			nlang[lunder - lstart] = 0;
			langv[++langc] = nlang;
		}

		/* if we stopped at the dot, move to the colon delimiter */
		while ((envlang[lend] != 0) &&
		       (envlang[lend] != ':')) {
			lend++;
		}
		if (envlang[lend] == 0) {
			/* reached end of environment language list */
			break;
		}
		lend++;
		lstart = lunder = lend;
	}
	return langv;
}

/**
 * Create an array of valid paths to search for resources.
 *
 * The idea is that all the complex path computation to find resources
 * is performed here, once, rather than every time a resource is
 * searched for.
 */
char **
fb_init_resource_path(const char *resource_path)
{
	char **pathv; /* resource path string vector */
	char **respath; /* resource paths vector */
	char **langv;

	pathv = filepath_path_to_strvec(resource_path);

	langv = get_language_names();

	respath = filepath_generate(pathv, (const char * const *)langv);

	filepath_free_strvec(langv);

	filepath_free_strvec(pathv);

	return respath;
}



/*
 * Local Variables:
 * c-basic-offset: 8
 * End:
 */