Debian Codegraph @

src/LanguageContainer.cpp

raw
1 // For all support, instructions and copyright go to:
2 // http://e2guardian.org/
3 // Released under the GPL v2, with the OpenSSL exception described in the README file.
4
5 // INCLUDES
6
7 #ifdef HAVE_CONFIG_H
8 #include "e2config.h"
9 #endif
10 #include "LanguageContainer.hpp"
11 #include "RegExp.hpp"
12 #include "String.hpp"
13 #include "Logger.hpp"
14
15 #include <cstdlib>
16 #include <cstdio>
17 #include <unistd.h>
18 #include <algorithm>
19 #include <iostream>
20 #include <fstream>
21 #include <sys/stat.h>
22 #include <sys/time.h>
23
24 // GLOBALS
25
26
27 // IMPLEMENTATION
28
29 // wipe loaded language file
30 void LanguageContainer::reset()
31 {
32 keys.clear();
33 values.clear();
34 }
35
36 // look for the translated string corresponding to the given key
37 //const char *LanguageContainer::getTranslation(const unsigned int index)
38 //char *LanguageContainer::getTranslation(const unsigned int index)
39 std::string LanguageContainer::getTranslation(const unsigned int index)
40 {
41 int i;
42 int s = keys.size();
43 for (i = 0; i < s; i++) {
44 if (keys[i] == index) {
45 return (std::string) values[i];
46 }
47 }
48 String t = "TRANSLATION KEY ";
49 String m(index);
50 t += m;
51 t += " MISSING ";
52 return (std::string) t;
53 }
54
55 // open a language file, containing message names (keys) and translated messages (values)
56 bool LanguageContainer::readLanguageList(const char *filename)
57 {
58 std::string linebuffer; // a string line buffer ;)
59 String v;
60 String line;
61 unsigned int k;
62 std::ifstream languagefile(filename, std::ios::in); // open the file for reading
63 if (!languagefile.good()) {
64 E2LOGGER_error("Error opening messages file (does it exist?): ", filename);
65 return false;
66 }
67 while (!languagefile.eof()) { // keep going until end of file
68 getline(languagefile, linebuffer); // grab a line
69 if (linebuffer.length() == 0) {
70 continue;
71 }
72 line = linebuffer.c_str();
73 k = line.after("\"").before("\",\"").toInteger();
74 v = line.after("\",\"").before("\"");
75 if (k >= 0 && v.length() > 0) {
76 keys.push_back(k);
77 values.push_back(v);
78 }
79 }
80 languagefile.close();
81 return true; // successful read
82 }