-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAuxiliary.cpp
More file actions
240 lines (200 loc) · 6.21 KB
/
Auxiliary.cpp
File metadata and controls
240 lines (200 loc) · 6.21 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "Auxiliary.h"
#include <sstream>
#include <fstream>
namespace Auxiliary
{
// Функция переводит элемент типа Т в std::string (в качестве типа может быть число)
template <typename T>
std::string ToString(T value)
{
std::ostringstream oStrStrm;
oStrStrm << value;
return oStrStrm.str();
}
template std::string ToString<bool>(bool value);
template std::string ToString<unsigned>(unsigned value);
template std::string ToString<int>(int value);
template std::string ToString<long>(long value);
template std::string ToString<float>(float value);
template std::string ToString<double>(double value);
// Функция переводит из std::string в элемент типа T
template <typename T>
T FromString(const std::string& s)
{
std::istringstream iss(s);
T response;
iss >> response;
return response;
}
template bool FromString<bool>(const std::string& s);
template unsigned FromString<unsigned>(const std::string& s);
template int FromString<int>(const std::string& s);
template long FromString<long>(const std::string& s);
template float FromString<float>(const std::string& s);
template double FromString<double>(const std::string& s);
std::string BoolToString(bool value){
return value ? "true" : "false";
}
// Функция выводит строку звездочек
void StarsStringToFlow(std::ofstream& outFile)
{
outFile << "************************************************************" << '\n';
}
//********************************************************************************************//
// Класс, делящий текст из файла, на списки слов (на каждую строчку - список)
//********************************************************************************************//
SeparatorFlowToTheWords::SeparatorFlowToTheWords()
{
m_splitStringsTable.clear();
m_dividers.clear();
m_emptySymbols.clear();
m_endLinesSymbols.clear();
m_isFixDividers = false;
m_isFixEndLinesSyms = false;
m_currStr = "";
m_currSplitStr = NULL;
m_errID = -1;
}
SeparatorFlowToTheWords::~SeparatorFlowToTheWords()
{
for (unsigned i = 0; i < m_splitStringsTable.size(); ++i)
{
delete m_splitStringsTable[i];
}
}
std::vector<splitString>* SeparatorFlowToTheWords::GetStringsStore(){return& m_splitStringsTable;}
splitString SeparatorFlowToTheWords::GetString(unsigned i)
{
if (i < m_splitStringsTable.size())
{
return m_splitStringsTable[i];
}
return NULL;
}
unsigned SeparatorFlowToTheWords::GetSizeOfStore(){return m_splitStringsTable.size();}
bool SeparatorFlowToTheWords::IsEmptyStore(){return m_splitStringsTable.size() > 0 ? false : true;}
bool SeparatorFlowToTheWords::AreThereErrors(){return m_errID != -1;}
int SeparatorFlowToTheWords::GetErrorID(){return m_errID;}
void SeparatorFlowToTheWords::TranslatesFilesToStringsStore(const char *fileName)
{
// Поток ввода из файла
std::ifstream fin;
// Пробуем открыть файл на чтение.
fin.open(fileName, std::ios::in | std::ios::binary);
// Если не вышло
if (!fin)
{
m_errID = 10;
return;
}
char c;
char bufer[201];
char* bufPtr;
// Считываем символы из файла, преобразуем их в слова
while(!fin.eof())
{
fin.read(bufer, 200);
bufer[fin.gcount()] = '\0';
bufPtr = bufer;
while (*bufPtr != '\0')
{
c = *bufPtr++;
if (IsDivider(c))
{
AddString();
if (m_isFixDividers)
{
m_currStr += c;
AddString();
}
continue;
}
if (IsEmptySymbol(c))
{
AddString();
continue;
}
if (IsEndLinesSymbol(c))
{
AddString();
if (m_isFixEndLinesSyms)
{
m_currStr += c;
AddString();
}
AddSplitSring();
continue;
}
m_currStr += c;
}
}
// Указываем, что чтение из файла завершено
if (m_currSplitStr)
{
m_splitStringsTable.push_back(m_currSplitStr);
m_currSplitStr = NULL;
}
// Закрываем файл
fin.close();
}
void SeparatorFlowToTheWords::SetIsFixDividers(bool flag /* = true */){m_isFixDividers = flag;}
void SeparatorFlowToTheWords::SetIsFixEndLinesSymbols(bool flag /* = true */){m_isFixEndLinesSyms = flag;}
void SeparatorFlowToTheWords::AddDivider(char c){m_dividers.push_back(c);}
void SeparatorFlowToTheWords::AddEmptySymbol(char c){m_emptySymbols.push_back(c);}
void SeparatorFlowToTheWords::AddEndLinesSymbol(char c){m_endLinesSymbols.push_back(c);}
bool SeparatorFlowToTheWords::IsDivider(char c){
std::list<char>::iterator iter;
for(iter = m_dividers.begin(); iter != m_dividers.end(); ++iter)
{
if (*iter == c)
{
return true;
}
}
return false;
}
bool SeparatorFlowToTheWords::IsEmptySymbol(char c){
std::list<char>::iterator iter;
for(iter = m_emptySymbols.begin(); iter != m_emptySymbols.end(); ++iter)
{
if (*iter == c)
{
return true;
}
}
return false;
}
bool SeparatorFlowToTheWords::IsEndLinesSymbol(char c){
std::list<char>::iterator iter;
for(iter = m_endLinesSymbols.begin(); iter != m_endLinesSymbols.end(); ++iter)
{
if (*iter == c)
{
return true;
}
}
return false;
}
void SeparatorFlowToTheWords::AddString()
{
if (m_currStr != "")
{
if (!m_currSplitStr)
{
m_currSplitStr = new std::vector<std::string>;
m_currSplitStr->clear();
}
m_currSplitStr->push_back(m_currStr);
m_currStr = "";
}
}
void SeparatorFlowToTheWords::AddSplitSring()
{
if (m_currSplitStr)
{
m_splitStringsTable.push_back(m_currSplitStr);
m_currSplitStr = NULL;
}
}
//********************************************************************************************//
}