forked from bridgecommand/bc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIniFile.cpp
More file actions
187 lines (156 loc) · 7.41 KB
/
IniFile.cpp
File metadata and controls
187 lines (156 loc) · 7.41 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* Bridge Command 5.0 Ship Simulator
Copyright (C) 2014 James Packer
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
#include "IniFile.hpp"
#include <fstream> //for ini loading
#include <string> //for ini loading
#include "Utilities.hpp" //for ini loading
// Irrlicht Namespaces
using namespace irr;
//Utility functions
namespace IniFile
{
std::string enumerate1(std::string commandName, irr::u32 number)
//Build up a command in the format 'commandName(#)'
{
std::string ans = commandName;
ans.append("(");
ans.append(Utilities::lexical_cast<std::string>(number));
ans.append(")");
return ans;
}
std::string enumerate2(std::string commandName, irr::u32 number1, irr::u32 number2)
//Build up a command in the format 'commandName(#,#)'
{
std::string ans = commandName;
ans.append("(");
ans.append(Utilities::lexical_cast<std::string>(number1));
ans.append(",");
ans.append(Utilities::lexical_cast<std::string>(number2));
ans.append(")");
return ans;
}
std::string trim(const std::string& str, const std::string& trimChr=" \n\r\t")
{
const std::size_t strBegin = str.find_first_not_of(trimChr);
if (strBegin == std::string::npos)
return ""; // no content
const std::size_t strEnd = str.find_last_not_of(trimChr);
const std::size_t strRange = strEnd - strBegin + 1;
return str.substr(strBegin, strRange);
}
std::wstring trim(const std::wstring& str, const std::wstring& trimChr=L" \n\r\t")
{
const std::size_t strBegin = str.find_first_not_of(trimChr);
if (strBegin == std::wstring::npos)
return L""; // no content
const std::size_t strEnd = str.find_last_not_of(trimChr);
const std::size_t strRange = strEnd - strBegin + 1;
return str.substr(strBegin, strRange);
}
std::string iniFileToString(std::string fileName, std::string command)
{
std::ifstream file (fileName.c_str());
std::string valuePart = "";
if (file.is_open())
{
std::string line;
while ( std::getline (file,line) )
{
std::string lowerLine(line); //Duplicate the line so we can make it lowercase, without affecting the result
Utilities::to_lower(lowerLine);
Utilities::to_lower(command); //Make the command lowercase, so we can check this without case sensitivity
//Look for the command and the '=' sign in the line
std::size_t commandFound = lowerLine.find(command);
std::size_t equalsFound = lowerLine.find("=");
//If both found, with equals after the command
if (commandFound!=std::string::npos && equalsFound!=std::string::npos && equalsFound>commandFound)
{
//Check that the part before the '=' exactly matches the command
if (trim(lowerLine.substr(0,equalsFound))==trim(command))
{
//get the value
//try {
valuePart = line.substr(equalsFound+1,std::string::npos);//from equals to end, not including the equals
//}
//catch (const std::out_of_range& oor) {
// std::cerr << "Could not get value for: " << command << " " << oor.what() << '\n';
//}
}
}
}
file.close();
}
else ;//std::cout << "Unable to open file " << fileName << std::endl;
//trim whitespace and " characters if present
valuePart = trim(valuePart);
valuePart = trim(valuePart,"\"");
return valuePart;
}
std::wstring iniFileToWString(std::string fileName, std::wstring command)
{
std::wifstream file (fileName.c_str());
//FIXME: Need to set UTF-8 locale here for file wifstream (required <codecvt> not included in libstdc++ currently, so can't do this at the moment!?
//Currently seems to work for ANSI files (?But may depend on user's locale?)
std::wstring valuePart = L"";
if (file.is_open())
{
std::wstring line;
while ( std::getline (file,line) )
{
std::wstring lowerLine(line); //Duplicate the line so we can make it lowercase, without affecting the result
Utilities::to_lower(lowerLine);
Utilities::to_lower(command); //Make the command lowercase, so we can check this without case sensitivity
//Look for the command and the '=' sign in the line
std::size_t commandFound = lowerLine.find(command);
std::size_t equalsFound = lowerLine.find(L"=");
//If both found, with equals after the command
if (commandFound!=std::string::npos && equalsFound!=std::string::npos && equalsFound>commandFound)
{
//Check that the part before the '=' exactly matches the command
if (trim(lowerLine.substr(0,equalsFound))==trim(command))
{
//get the value
//try {
valuePart = line.substr(equalsFound+1,std::string::npos);//from equals to end, not including the equals
//}
//catch (const std::out_of_range& oor) {
// std::cerr << "Could not get value for: " << command << " " << oor.what() << '\n';
//}
}
}
}
file.close();
}
else ;//std::cout << "Unable to open file " << fileName << std::endl;
//trim whitespace and " characters if present
valuePart = trim(valuePart);
valuePart = trim(valuePart,L"\"");
return valuePart;
}
//Load unsigned integer from an ini file
u32 iniFileTou32(std::string fileName, std::string command)
{
u32 result = 0;
std::string valueString = iniFileToString(fileName, command); //Get the value as a string
result = core::strtoul10(valueString.c_str()); //Convert this into an unsigned int
return result;
}
//Load float from an ini file
f32 iniFileTof32(std::string fileName, std::string command)
{
f32 result = 0;
std::string valueString = iniFileToString(fileName, command); //Get the value as a string
result = core::fast_atof(valueString.c_str()); //Convert this into a float
return result;
}
}