NetSurf
findfile.c
Go to the documentation of this file.
1/*
2 * Copyright 2008 Daniel Silverstone <dsilvers@netsurf-browser.org>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include "utils/filepath.h"
24#include "utils/log.h"
25
27
28char **respaths; /** resource search path vector */
29
30#define MAX_LANGV_SIZE 32
31
32/**
33 * goes through the environment in appropriate order to find configured language
34 *
35 * \return language to use or "C" if nothing appropriate is set
36 */
37static const char *get_language_env(void)
38{
39 const char *envstr;
40
41 envstr = getenv("LANGUAGE");
42 if ((envstr != NULL) && (envstr[0] != 0)) {
43 return envstr;
44 }
45
46 envstr = getenv("LC_ALL");
47 if ((envstr != NULL) && (envstr[0] != 0)) {
48 return envstr;
49 }
50
51 envstr = getenv("LC_MESSAGES");
52 if ((envstr != NULL) && (envstr[0] != 0)) {
53 return envstr;
54 }
55
56 envstr = getenv("LANG");
57 if ((envstr != NULL) && (envstr[0] != 0)) {
58 return envstr;
59 }
60
61 return "C";
62}
63
64/**
65 * build a string vector of language names
66 */
67static char **get_language_names(void)
68{
69 char **langv; /* output string vector of languages */
70 int langc; /* count of languages in vector */
71 const char *envlang; /* colon separated list of languages from environment */
72 int lstart = 0; /* offset to start of current language */
73 int lunder = 0; /* offset to underscore in current language */
74 int lend = 0; /* offset to end of current language */
75 char *nlang;
76
77 langv = calloc(MAX_LANGV_SIZE + 2, sizeof(char *));
78 if (langv == NULL) {
79 return NULL;
80 }
81
82 envlang = get_language_env();
83
84 for (langc = 0; langc < MAX_LANGV_SIZE; langc++) {
85 /* work through envlang splitting on : */
86 while ((envlang[lend] != 0) &&
87 (envlang[lend] != ':') &&
88 (envlang[lend] != '.')) {
89 if (envlang[lend] == '_') {
90 lunder = lend;
91 }
92 lend++;
93 }
94 /* place language in string vector */
95 nlang = malloc(lend - lstart + 1);
96 memcpy(nlang, envlang + lstart, lend - lstart);
97 nlang[lend - lstart] = 0;
98 langv[langc] = nlang;
99
100 /* add language without specialisation to vector */
101 if (lunder != lstart) {
102 nlang = malloc(lunder - lstart + 1);
103 memcpy(nlang, envlang + lstart, lunder - lstart);
104 nlang[lunder - lstart] = 0;
105 langv[++langc] = nlang;
106 }
107
108 /* if we stopped at the dot, move to the colon delimiter */
109 while ((envlang[lend] != 0) &&
110 (envlang[lend] != ':')) {
111 lend++;
112 }
113 if (envlang[lend] == 0) {
114 /* reached end of environment language list */
115 break;
116 }
117 lend++;
118 lstart = lunder = lend;
119 }
120 return langv;
121}
122
123/**
124 * Create an array of valid paths to search for resources.
125 *
126 * The idea is that all the complex path computation to find resources
127 * is performed here, once, rather than every time a resource is
128 * searched for.
129 */
130char **
131fb_init_resource_path(const char *resource_path)
132{
133 char **pathv; /* resource path string vector */
134 char **respath; /* resource paths vector */
135 char **langv;
136
137 pathv = filepath_path_to_strvec(resource_path);
138
139 langv = get_language_names();
140
141 respath = filepath_generate(pathv, (const char * const *)langv);
142
144
146
147 return respath;
148}
149
150
151
152/*
153 * Local Variables:
154 * c-basic-offset: 8
155 * End:
156 */
157
char ** filepath_path_to_strvec(const char *path)
Convert a colon separated list of path elements into a string vector.
Definition: filepath.c:313
void filepath_free_strvec(char **pathv)
Free a string vector.
Definition: filepath.c:356
char ** filepath_generate(char *const *pathv, const char *const *langv)
Merge two string vectors into a resource search path vector.
Definition: filepath.c:184
Utility routines to obtain paths to file resources.
char ** respaths
resource search path vector
Definition: findfile.c:28
char ** fb_init_resource_path(const char *resource_path)
Create an array of valid paths to search for resources.
Definition: findfile.c:131
#define MAX_LANGV_SIZE
resource search path vector
Definition: findfile.c:30
static char ** get_language_names(void)
build a string vector of language names
Definition: findfile.c:67
static const char * get_language_env(void)
goes through the environment in appropriate order to find configured language
Definition: findfile.c:37
Interface to utility string handling.