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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/stat.h>
#include "main.h"
#include "list.h"
#include "homeDirector.h"
#include "textFile.h"
#include "configFile.h"
#include "language.h"
#include "director.h"
static textFile_t *languageFile;
static char fontFile[STR_FILE_NAME_SIZE];
static int fontSize;
static char* getHead(textFile_t *ts)
{
int i;
for( i = 0 ; i < ts->text->count ; i++ )
{
char *line;
line = (char *) (ts->text->list[i]);
if( strncmp(line, "HEAD", 4) == 0 )
{
return line;
}
}
return NULL;
}
static char* getSupportLanguage()
{
director_t *director;
char *envLang;
char lang[STR_SIZE];
int i;
printf("I search language..\n");
envLang = getenv("LANG");
if( envLang == NULL )
{
printf("Environment LANG not defined, I use default value \"en\"\n");
return strdup("en");
}
memset(lang, 0, STR_SIZE);
strncpy(lang, envLang, 2);
director = loadDirector(PATH_LANG);
if( director == NULL )
{
fprintf(stderr, "Do not open director %s\n", PATH_LANG);
return strdup("en");
}
for( i = 0 ; i < director->list->count ; i++ )
{
char *line;
line = (char *)director->list->list[i];
if( strncmp(lang, line, 2) == 0 )
{
printf("I choice lang %s in file %s\n", lang, line);
return strdup(lang);
}
}
destroyDirector(director);
printf("Lang %s not support, I use default language english\n", lang);
return strdup("en");
}
int initLanguage()
{
textFile_t *languageTypeFile;
char path[STR_PATH_SIZE];
char *lang;
char *head;
sprintf(path, "%s/lang.conf", getHomeDirector());
printf("open %s\n", path);
languageTypeFile = loadTextFile(path);
if( languageTypeFile == NULL )
{
char *defaultLang;
fprintf(stderr, "Don't load %s\n", path);
fprintf(stderr, "I create %s\n", path);
languageTypeFile = newTextFile(path);
defaultLang = getSupportLanguage();
addList(languageTypeFile->text, strdup(defaultLang) );
saveTextFile(languageTypeFile);
free(defaultLang);
}
if( languageTypeFile == NULL )
{
fprintf(stderr, "Don't create %s\n", path);
return -1;
}
if( languageTypeFile->text->count == 0 )
{
fprintf(stderr, "File %s empty\n", path);
return -1;
}
lang = ((char *)languageTypeFile->text->list[0]);
printf("select lang %s\n", lang);
sprintf(path, PATH_LANG "%s.lang", lang);
destroyTextFile(languageTypeFile);
accessExistFile(path);
languageFile = loadTextFile(path);
if( languageFile == NULL )
{
fprintf(stderr, "Don't load %s\n", path);
return -1;
}
head = getHead(languageFile);
if( head != NULL )
{
char val[STR_SIZE];
if( getValue(head, "fontfile", val, STR_SIZE) == 0 )
{
strcpy(fontFile, val);
}
else
{
fprintf(stderr, "fontfile in lang file %s not found\n", path);
return -1;
}
if( getValue(head, "fontsize", val, STR_SIZE) == 0 )
{
fontSize = atoi(val);
}
else
{
fprintf(stderr, "fontsize in lang file %s not found\n", path);
return -1;
}
}
else
{
fprintf(stderr, "head in lang file %s not found\n", path);
return -1;
}
return 0;
}
char* getMyText(char *key)
{
int i;
int len;
len = strlen(key);
for( i = 0 ; i < languageFile->text->count ; i++ )
{
char *line;
line = (char *) (languageFile->text->list[i]);
if( strncmp(key, line, len) == 0 )
{
return line+strlen(key)+1;
}
}
return NULL;
}
char* getLanguageFont()
{
return fontFile;
}
int getLanguageSize()
{
return fontSize;
}
void quitLanguage()
{
destroyTextFile(languageFile);
}
|