-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilities.cpp
190 lines (154 loc) · 4.61 KB
/
Utilities.cpp
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
188
189
190
#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
enum TextEncoding
{
ASCII = 0,
UTF_8,
UTF_16,
UTF_32
};
std::vector<std::string> splitString(std::string str, const std::string token){
std::vector<std::string>result;
while(str.size()){
int index = str.find(token);
if(index != std::string::npos){
result.push_back(str.substr(0,index));
str = str.substr(index+token.size());
if(str.size()==0)result.push_back(str);
}else{
result.push_back(str);
str = "";
}
}
return result;
}
std::wstring convertStringToWstring(const std::string& str)
{
using namespace std;
const ctype<wchar_t>& CType = use_facet<ctype<wchar_t> >(locale());
vector<wchar_t> wideStringBuffer(str.length());
CType.widen(str.data(), str.data() + str.length(), &wideStringBuffer[0]);
return wstring(&wideStringBuffer[0], wideStringBuffer.size());
}
std::string convertWstringToString(const std::wstring& str)
{
std::locale const loc("");
wchar_t const* from = str.c_str();
std::size_t const len = str.size();
std::vector<char> buffer(len + 1);
std::use_facet<std::ctype<wchar_t> >(loc).narrow(from, from + len, '_', &buffer[0]);
return std::string(&buffer[0], &buffer[len]);
}
TextEncoding DetermineFileTextEncoding(const std::wstring& filePath)
{
using namespace std;
const short BOM_SIZE = 4;
char buffer[BOM_SIZE];
std::ifstream fin(filePath, ios::in | ios::binary);
fin.read(buffer, BOM_SIZE);
/* Text is utf-16 */
if ((buffer[0] == '\xFF' && buffer[1] == '\xFE') || (buffer[0] == '\xFE' && buffer[1] == '\xFF'))
{
return TextEncoding::UTF_16;
}
if ((buffer[0] == '\xFF' && buffer[1] == '\xFE') && (buffer[2] == '\x00' && buffer[3] == '\x00'))
{
return TextEncoding::UTF_16;
}
if ((buffer[0] == '\x00' && buffer[1] == '\x00') && (buffer[2] == '\xFE' && buffer[3] == '\xFF'))
{
return TextEncoding::UTF_16;
}
/* Most probably text is utf-8 */
return TextEncoding::UTF_8;
}
std::wstring ReadFileAsUnicode(const std::wstring& filePath)
{
std::wstring ws;
TextEncoding encoding = DetermineFileTextEncoding(filePath);
switch(encoding)
{
case TextEncoding::UTF_16:
{
std::stringstream ss;
std::ifstream fin(filePath);
ss << fin.rdbuf(); // dump file contents into a stringstream
std::string const &s = ss.str();
if (s.size()%sizeof(wchar_t) != 0)
{
std::wstring errorMsg = std::wstring(L"Invalid data on file path : ") + filePath;
throw std::exception(convertWstringToString(errorMsg).c_str());
}
ws.resize(s.size()/sizeof(wchar_t));
std::memcpy(&ws[0],s.c_str(),s.size());
}
break;
case TextEncoding::UTF_8:
{
std::stringstream ss;
std::ifstream fin(filePath);
ss << fin.rdbuf();
std::string const &s = ss.str();
ws = convertStringToWstring(s);
}
break;
default:
std::wstring errorMsg = std::wstring(L"Unsupported file format : file path : ") + filePath;
throw std::exception(convertWstringToString(errorMsg).c_str());
break;
};
return ws;
}
static inline std::string tolower(std::string str)
{
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
return str;
}
static inline std::string <rim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
static inline std::string &rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
static inline std::string &trim(std::string &s) {
return ltrim(rtrim(s));
}
static inline std::string &RemoveWhiteSpaces(std::string &s) {
return ltrim(rtrim(s));
}
/** This will pad a given string to given length. Extra characters will be filled with spaces. */
inline std::wstring PadString(const std::wstring& baseString, int lenght)
{
std::wstringstream wss;
wss << std::setw(lenght) << std::setfill(L' ') << std::left;
wss << baseString;
return wss.str();
}
void WriteStringsToFile(const std::string file_path, std::vector<std::string> vec) const
{
std::ofstream myfile;
myfile.open(file_path.c_str());
for (const auto &str : vec)
{
myfile << xpath << "\n";
}
myfile.close();
}
std::vector<std::string> ReadFileForStrings(const std::string file_path) const
{
std::vector<std::string> result;
std::ifstream infile(file_path.c_str());
std::string line;
while (std::getline(infile, line))
{
result.push_back(line);
}
return result;
}