Proper check for rawzor libraries.
[rawtherapee-fixes.git] / rtgui / multilangmgr.cc
blob07bf8c2f1271fff4d193881629329164d1f00f0e
1 /*
2 * This file is part of RawTherapee.
4 * Copyright (c) 2004-2010 Gabor Horvath <hgabor@rawtherapee.com>
6 * RawTherapee 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 3 of the License, or
9 * (at your option) any later version.
11 * RawTherapee 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.
16 * You should have received a copy of the GNU General Public License
17 * along with RawTherapee. If not, see <http://www.gnu.org/licenses/>.
19 #include <glib/gstdio.h>
20 #include <multilangmgr.h>
21 #include <string.h>
23 MultiLangMgr langMgr;
25 Glib::ustring M (std::string key) { return langMgr.getStr (key); }
27 bool MultiLangMgr::load (Glib::ustring fname, MultiLangMgr* fb) {
29 fallBack = fb;
31 FILE *f = g_fopen (fname.c_str(), "rt");
33 if (f==NULL)
34 return false;
36 transTable.clear ();
38 char* buffer = new char[2048];
40 while (buffer = fgets (buffer, 2048, f)) {
41 // find separator
42 int seppos = 0;
43 while (buffer[seppos]!=0 && buffer[seppos]!=';')
44 seppos++;
45 // no separator found
46 if (buffer[seppos]==0)
47 continue;
48 // cut the last \n and \r characters
49 int endpos = strlen(buffer)-1;
50 while (buffer[endpos]=='\n' || buffer[endpos]=='\r')
51 endpos--;
52 buffer[endpos+1] = 0;
53 // replace "\n" to '\n'
54 int j = 0;
55 for (int i=0; i<endpos+1; i++)
56 if (i<endpos && buffer[i]=='\\' && buffer[i+1]=='n') {
57 buffer[j++] = '\n';
58 i++;
60 else
61 buffer[j++] = buffer[i];
62 buffer[j] = 0;
63 // cut to two parts
64 buffer[seppos] = 0;
65 transTable[buffer] = buffer + seppos + 1;
68 fclose (f);
69 delete [] buffer;
70 return true;
73 bool MultiLangMgr::save (Glib::ustring fname) {
75 FILE *f = g_fopen (fname.c_str(), "wt");
77 if (f==NULL)
78 return false;
80 std::map<std::string, Glib::ustring>::iterator r;
81 for (r=transTable.begin (); r!=transTable.end(); r++)
82 fprintf (f, "%s;%s\n", r->first.c_str(), Glib::locale_from_utf8(r->second).c_str());
84 fclose (f);
85 return true;
88 Glib::ustring MultiLangMgr::getStr (std::string key) {
90 std::map<std::string, Glib::ustring>::iterator r = transTable.find (key);
91 if (r!=transTable.end())
92 return r->second;
93 else if (fallBack)
94 return fallBack->getStr (key);
95 else
96 return "";