forked from OpenXcom/OpenXcom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLanguage.cpp
More file actions
156 lines (143 loc) · 3.27 KB
/
Copy pathLanguage.cpp
File metadata and controls
156 lines (143 loc) · 3.27 KB
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
/*
* Copyright 2010 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenXcom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Language.h"
#include <iostream>
#include <fstream>
/**
* Initializes an empty language file.
*/
Language::Language() : _name(""), _strings()
{
}
/**
*
*/
Language::~Language()
{
}
/**
* Loads pairs of null-terminated strings contained in
* a raw text file into the Language. Each pair is made of
* an ID and a localized string.
* @param filename Filename of the LNG file.
* @sa <a href="../language_id.html">Reference Table</a>
*/
void Language::loadLng(const std::string &filename)
{
_strings.clear();
// Load file and put text in map
std::ifstream txtFile (filename.c_str(), std::ios::in | std::ios::binary);
if (!txtFile)
{
throw "Failed to load LNG";
}
char value;
std::string buffer, bufid, bufstr;
bool first = true, id = true;
while (txtFile.read(&value, 1))
{
if (value != '\0')
{
buffer += value;
}
else
{
// Get language name
if (first)
{
_name = buffer;
first = false;
}
else
{
// Get ID
if (id)
{
bufid = buffer;
}
// Get string
else
{
bufstr = buffer;
_strings[bufid] = bufstr;
}
id = !id;
}
buffer.clear();
}
}
if (!txtFile.eof())
throw "Invalid data from file";
txtFile.close();
}
/**
* Returns the language's name in its native language.
* @return Language name.
*/
std::string Language::getName() const
{
return _name;
}
/**
* Returns the localizable string with the specified ID.
* If it's not found, just returns the ID.
* @param id ID of the string.
* @return String with the requested ID.
*/
std::string Language::getString(const std::string &id) const
{
std::map<std::string, std::string>::const_iterator s = _strings.find(id);
if (s == _strings.end())
{
std::cout << "WARNING: " << id << " not found in " << _name << std::endl;
return id;
}
else
{
return s->second;
}
}
/**
* Outputs all the language IDs and strings
* to an HTML table.
*/
void Language::toHtml() const
{
std::ofstream htmlFile ("lang.html", std::ios::out);
htmlFile << "<table border=\"1\" width=\"100%\">" << std::endl;
htmlFile << "<tr><th>ID String</th><th>English String</th></tr>" << std::endl;
for (std::map<std::string, std::string>::const_iterator i = _strings.begin(); i != _strings.end(); i++)
{
htmlFile << "<tr><td>" << i->first << "</td><td>";
for (std::string::const_iterator j = i->second.begin(); j != i->second.end(); j++)
{
if (*j == 2 || *j == '\n')
{
htmlFile << "<br />";
}
else
{
htmlFile << *j;
}
}
htmlFile << "</td></tr>" << std::endl;
}
htmlFile << "</table>" << std::endl;
htmlFile.close();
}