-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringResource.cpp
More file actions
100 lines (83 loc) · 2.07 KB
/
StringResource.cpp
File metadata and controls
100 lines (83 loc) · 2.07 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
/** @file StringResource.cpp */
#include "StringResource.h"
#include "FileTools.h"
std::map<std::string, std::string> StringResource::strings;
/**
* \brief Constructor.
*/
StringResource::StringResource()
{
}
/**
* \brief Destructor.
*/
StringResource::~StringResource()
{
}
/**
* \brief Initializes the text resource by loading all strings.
*
* The strings are loaded from the language-specific file "text/strings.dat"
* and stored into memory for future access by get_string().
*/
void StringResource::initialize()
{
strings.clear();
std::istream& file = FileTools::data_file_open("text/strings.dat", false);
std::string line;
//Read each line
int i = 0;
while(std::getline(file, line))
{
i++;
//Ignore empty lines or lines starting with '#'
size_t index = line.find_first_of(" \t");
//Insert Debug assertion
std::string key = line.substr(0, index);
//get the value
do
{
index++;
} while (index < line.size() && (line[index] == ' ' || line[index] == '\t' || line[index] == '\r'));
//Insert Debug assertion
std::string value = line.substr(index);
if(value[value.size() - 1] == '\r')
{
//If the file has DOS line endings, remove the trailing '\r'
value = value.substr(0, value.size() - 1);
}
strings[key] = value;
}
FileTools::data_file_close(file);
}
/**
* \brief Closes the text resource.
*/
void StringResource::quit()
{
strings.clear();
}
/**
* \brief Returns whether a string exists in the language-specific file
* "text/strings.dat" for the current language.
* \param key Id of a string.
* \return true if the string exists.
*/
bool StringResource::exists(const std::string& key)
{
return strings.find(key) != strings.end();
}
/**
* \brief Returns a string stored in the language-specific file
* "text/strings.dat" for the current language.
* \param key Id of the string to retrieve. It must exist.
* \return The corresponding localized string.
*/
const std::string& StringResource::get_string(const std::string& key)
{
if(!exists(key))
{
std::cerr << "Cannot find string w3ith ky '" << key << "'\n";
}
return strings[key];
}