]> git.vomp.tv Git - vompserver.git/blob - i18n.c
15 years that line of code has been waiting to crash
[vompserver.git] / i18n.c
1 /*
2     Copyright 2007 Mark Calderbank
3
4     This file is part of VOMP.
5
6     VOMP 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; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21 #include "i18n.h"
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <glob.h>
26
27 using namespace std;
28
29 I18n::I18n(char* tconfigDir)
30 {
31   configDir = tconfigDir;
32 }
33
34 void I18n::findLanguages(void)
35 {
36   glob_t globbuf;
37   char line[1000];
38
39   CodeList.clear();
40   FileList.clear();
41   
42   string l10nGlob = configDir;
43   l10nGlob += "/l10n/*";
44   glob(l10nGlob.c_str(), 0, NULL, &globbuf);
45   for (unsigned int i=0; i < globbuf.gl_pathc; i++)
46   {
47     FILE *f = fopen(globbuf.gl_pathv[i], "r");
48     if (f)
49     {
50       while (fgets(line, 1000, f) && strncmp(line, "l10n-vomp:", 10) == 0)
51       {
52         string langline = line;
53         string code, name;
54
55         string::size_type pos_start, pos_end;
56         pos_start = langline.find_first_not_of(" \t\r\n", 10);
57         if (pos_start == string::npos) break;
58         pos_end = langline.find_first_of(" \t", pos_start);
59         if (pos_end == string::npos) break;
60         code = langline.substr(pos_start, pos_end - pos_start);
61         pos_start = langline.find_first_not_of(" \t\r\n", pos_end);
62         if (pos_start == string::npos) break;
63         pos_end = langline.find_last_not_of(" \t\r\n");
64         name = langline.substr(pos_start, pos_end + 1 - pos_start);
65         CodeList[code] = name;
66         FileList.insert(lang_file(code, globbuf.gl_pathv[i]));
67       }
68       fclose(f);
69     }
70   }
71   globfree(&globbuf);
72 }
73
74 I18n::trans_table I18n::getLanguageContent(const string code)
75 {
76   trans_table Translations;
77   if (CodeList.count(code) == 0) return Translations;
78   LanguageCode = code;
79
80   pair<lang_file_list::const_iterator, lang_file_list::const_iterator> range;
81   range = FileList.equal_range(code);
82   lang_file_list::const_iterator iter;
83   for (iter = range.first; iter != range.second; ++iter)
84   {
85     FILE *f;
86     char line[1000];
87     string key; 
88     f = fopen((*iter).second.c_str(), "r");
89     if (f)
90     {
91       while (fgets(line, 1000, f))
92       {
93         int linetype = 0;
94         string::size_type offset = 0;
95         string fileline = line;
96         if (fileline.compare(0, 2, "x:") == 0)
97         { // New key to be translated
98           linetype = 1; offset = 2;
99         }
100         if (fileline.compare(0, code.size() + 1, code + ":") == 0)
101         { // Translation for previous key
102           if (key.empty()) continue; // Translation without preceding key
103           linetype = 2; offset = code.size() + 1;
104         }
105         if (linetype != 0)
106         {
107           string::size_type start, end;
108           start = fileline.find_first_not_of(" \t\r\n",offset);
109           if (start == string::npos)
110           {
111             if (linetype == 2) Translations[key].clear();
112             continue;
113           }
114           end = fileline.find_last_not_of(" \t\r\n");
115           string text = fileline.substr(start, end + 1 - start);
116           if (text.length() > 1) // Strip quotes if at both ends
117           {
118             if (text[0] == '"' && text[text.length()-1] == '"')
119               text = text.substr(1, text.length()-2);
120           }
121           if (linetype == 1) key = text;
122           if (linetype == 2) Translations[key] = text;
123         }
124       }
125       fclose(f);
126     }
127   }
128   return Translations;
129 }
130
131 const I18n::lang_code_list& I18n::getLanguageList(void)
132 {
133   return CodeList;
134 }