~ubuntu-branches/ubuntu/intrepid/dansguardian/intrepid-security

« back to all changes in this revision

Viewing changes to src/LanguageContainer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Wirt
  • Date: 2008-04-06 14:47:06 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20080406144706-2r26l1rougdmb1sd
Tags: 2.9.9.3-2
This time build with gcc 4.3 (Closes: #454889) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//Please refer to http://dansguardian.org/?page=copyright
 
2
//for the license for this code.
 
3
//Written by Daniel Barron (daniel@//jadeb/.com).
 
4
//For support go to http://groups.yahoo.com/group/dansguardian
 
5
 
 
6
//  This program 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
//  This program 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 this program; if not, write to the Free Software
 
18
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 
 
20
 
 
21
// INCLUDES
 
22
 
 
23
#include "LanguageContainer.hpp"
 
24
#include "RegExp.hpp"
 
25
#include "String.hpp"
 
26
 
 
27
#include <cstdlib>
 
28
#include <cstdio>
 
29
#include <unistd.h>
 
30
#include <syslog.h>
 
31
#include <algorithm>
 
32
#include <iostream>
 
33
#include <fstream>
 
34
#include <sys/stat.h>
 
35
#include <sys/time.h>
 
36
 
 
37
 
 
38
// GLOBALS
 
39
 
 
40
extern bool is_daemonised;
 
41
 
 
42
 
 
43
// IMPLEMENTATION
 
44
 
 
45
// wipe loaded language file
 
46
void LanguageContainer::reset()
 
47
{
 
48
        keys.clear();
 
49
        values.clear();
 
50
}
 
51
 
 
52
// look for the translated string corresponding to the given key
 
53
const char *LanguageContainer::getTranslation(const unsigned int index)
 
54
{
 
55
        int i;
 
56
        int s = keys.size();
 
57
        for (i = 0; i < s; i++) {
 
58
                if (keys[i] == index) {
 
59
                        return values[i].toCharArray();
 
60
                }
 
61
        }
 
62
        return "MISSING TRANSLATION KEY";
 
63
}
 
64
 
 
65
// open a language file, containing message names (keys) and translated messages (values)
 
66
bool LanguageContainer::readLanguageList(const char *filename)
 
67
{
 
68
        std::string linebuffer;  // a string line buffer ;)
 
69
        String v;
 
70
        String line;
 
71
        unsigned int k;
 
72
        ifstream languagefile(filename, ios::in);  // open the file for reading
 
73
        if (!languagefile.good()) {
 
74
                if (!is_daemonised) {
 
75
                        std::cerr << "Error opening messages file (does it exist?): " << filename << std::endl;
 
76
                }
 
77
                syslog(LOG_ERR, "%s", "Error opening messages file (does it exist?): ");
 
78
                syslog(LOG_ERR, "%s", filename);
 
79
                return false;
 
80
        }
 
81
        while (!languagefile.eof()) {   // keep going until end of file
 
82
                getline(languagefile, linebuffer);  // grab a line
 
83
                if (linebuffer.length() == 0) {
 
84
                        continue;
 
85
                }
 
86
                line = linebuffer.c_str();
 
87
                k = line.after("\"").before("\",\"").toInteger();
 
88
                v = line.after("\",\"").before("\"");
 
89
                if (k > 0 && v.length() > 0) {
 
90
                        keys.push_back(k);
 
91
                        values.push_back(v);
 
92
                }
 
93
        }
 
94
        languagefile.close();
 
95
        return true;  // successful read
 
96
}