xref: /haiku/src/kits/locale/Language.cpp (revision 49546fa993e8de87ddebffd56f890aec71aa42d3)
1 /*
2 ** Copyright 2003, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 ** Distributed under the terms of the MIT License.
4 */
5 
6 
7 #include <Language.h>
8 
9 #include <Path.h>
10 #include <FindDirectory.h>
11 
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <ctype.h>
16 
17 
18 static const char *gBuiltInStrings[] = {
19 	"Yesterday",
20 	"Today",
21 	"Tomorrow",
22 	"Future",
23 
24 	"Sunday",
25 	"Monday",
26 	"Tuesday",
27 	"Wednesday",
28 	"Thursday",
29 	"Friday",
30 	"Saturday",
31 
32 	"Sun",
33 	"Mon",
34 	"Tue",
35 	"Wed",
36 	"Thu",
37 	"Fri",
38 	"Sat",
39 
40 	"January",
41 	"February",
42 	"March",
43 	"April",
44 	"May",
45 	"June",
46 	"July",
47 	"August",
48 	"September",
49 	"October",
50 	"November",
51 	"December",
52 
53 	"Jan",
54 	"Feb",
55 	"Mar",
56 	"Apr",
57 	"May",
58 	"Jun",
59 	"Jul",
60 	"Aug",
61 	"Sep",
62 	"Oct",
63 	"Nov",
64 	"Dec",
65 
66 	"^[yY]",
67 	"^[nN]",
68 	"yes",
69 	"no"
70 };
71 
72 
73 static char *
74 TrimCopy(char *string)
75 {
76 	while (isspace(*string))
77 		string++;
78 
79 	int32 length = strlen(string);
80 	while (length-- > 0 && isspace(string[length]))
81 		string[length] = '\0';
82 
83 	if (length < 0)
84 		return NULL;
85 
86 	return strdup(string);
87 }
88 
89 
90 //	#pragma mark -
91 
92 
93 BLanguage::BLanguage(const char *language)
94 	:
95 	fName(NULL),
96 	fCode(NULL),
97 	fFamily(NULL),
98 	fDirection(B_LEFT_TO_RIGHT)
99 {
100 	for (int32 i = B_NUM_LANGUAGE_STRINGS;i-- > 0;)
101 		fStrings[i] = NULL;
102 
103 	if (language == NULL) {
104 		Default();
105 		return;
106 	}
107 
108 	char name[B_FILE_NAME_LENGTH];
109 	sprintf(name, "locale/languages/%s.language", language);
110 
111 	BPath path;
112 	if (find_directory(B_SYSTEM_DATA_DIRECTORY, &path) < B_OK) {
113 		Default();
114 		return;
115 	}
116 
117 	path.Append(name);
118 
119 	FILE *file = fopen(path.Path(), "r");
120 	if (file == NULL) {
121 		Default();
122 		return;
123 	}
124 
125 	int32 count = -2;
126 	while (fgets(name, B_FILE_NAME_LENGTH, file) != NULL) {
127 		if (count == -2) {
128 			char *family = strchr(name, ',');
129 			if (family == NULL)
130 				break;
131 			*family++ = '\0';
132 
133 			// set the direction of writing
134 			char *direction = strchr(family, ',');
135 			if (direction != NULL) {
136 				*direction++ = '\0';
137 				direction = TrimCopy(direction);
138 
139 				if (!strcasecmp(direction, "ltr"))
140 					fDirection = B_LEFT_TO_RIGHT;
141 				else if (!strcasecmp(direction, "rtl"))
142 					fDirection = B_RIGHT_TO_LEFT;
143 				else if (!strcasecmp(direction, "ttb"))
144 					fDirection = B_TOP_TO_BOTTOM;
145 
146 				free(direction);
147 			}
148 
149 			fName = strdup(language);
150 			fCode = TrimCopy(name);
151 			fFamily = TrimCopy(family);
152 			count++;
153 		} else if (count == -1) {
154 			if (!strcmp(name,"--\n"))
155 				count++;
156 		} else if (count < B_NUM_LANGUAGE_STRINGS) {
157 			char *string = TrimCopy(name);
158 			if (string == NULL)
159 				continue;
160 
161 			fStrings[count++] = string;
162 		}
163 	}
164 
165 	if (count < 0)
166 		Default();
167 
168 	fclose(file);
169 }
170 
171 
172 BLanguage::~BLanguage()
173 {
174 	if (fName != NULL)
175 		free(fName);
176 	if (fCode != NULL)
177 		free(fCode);
178 	if (fFamily != NULL)
179 		free(fFamily);
180 
181 	for (int32 i = B_NUM_LANGUAGE_STRINGS;i-- > 0;)
182 		free(fStrings[i]);
183 }
184 
185 
186 void
187 BLanguage::Default()
188 {
189 	fName = strdup("english");
190 	fCode = strdup("en");
191 	fFamily = strdup("germanic");
192 	fDirection = B_LEFT_TO_RIGHT;
193 
194 	for (int32 i = B_NUM_LANGUAGE_STRINGS;i-- > 0;) {
195 		free(fStrings[i]);
196 		fStrings[i] = strdup(gBuiltInStrings[i]);
197 	}
198 }
199 
200 
201 uint8
202 BLanguage::Direction() const
203 {
204 	return fDirection;
205 }
206 
207 
208 const char *
209 BLanguage::GetString(uint32 id) const
210 {
211 	if (id < B_LANGUAGE_STRINGS_BASE || id > B_LANGUAGE_STRINGS_BASE + B_NUM_LANGUAGE_STRINGS)
212 		return NULL;
213 
214 	return fStrings[id - B_LANGUAGE_STRINGS_BASE];
215 }
216 
217