-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTAsciiFileReader.cpp
More file actions
175 lines (150 loc) · 3.74 KB
/
TAsciiFileReader.cpp
File metadata and controls
175 lines (150 loc) · 3.74 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
/*
* TAsciiFileReader.cpp
*
* Created on: Mar 22, 2016
* Author: mss
* Adjusted for HV monitoring CAEN Gecko logs on 28.02.2019
*/
#include "TAsciiFileReader.h"
namespace std {
template <typename TStorage>
TAsciiFileReader<TStorage>::TAsciiFileReader() {
ResetDataLow();
}
template <typename TStorage>
TAsciiFileReader<TStorage>::~TAsciiFileReader() {
}
template <typename TStorage>
void TAsciiFileReader<TStorage>::ResetDataLow(){
fileOpened = false;
filePath = "x";
fileContents = "x";
vStorage.clear();
}
template <typename TStorage>
bool TAsciiFileReader<TStorage>::CheckVectorIndex(const vector<int16_t>& v, const uint32_t index){
if(index<v.size()){
return true;
}
else{
log << " <W> TAsciiFileReader<TStorage>::CheckVectorIndex: Vector index out of bounds: " << index << ">=" << v.size() << endl;
return false;
}
}
template <typename TStorage>
bool TAsciiFileReader<TStorage>::CheckVectorLutValue(const vector<int16_t>& v, const uint32_t index){
if(CheckVectorIndex(v,index)){
if(v.at(index)>=0){
return true;
}
else{
log << " <W> TAsciiFileReader<TStorage>::CheckVectorLutValue: Vector value at: " << index << " is <0: " << v.at(index) << endl;
return false;
}
}
else{
return false;
}
}
template <typename TStorage>
int16_t TAsciiFileReader<TStorage>::ReadFile(const string& path){
if(IsFileOpened()){
if(verbose>0){
cout << " <W> TAsciiFileReader::ReadFile(): Opening another map file on top of existing one. Overwriting input form old file." << endl;
}
ResetDataLow();
ResetData();
}
filePath = path;
ifstream infile;
infile.open(filePath.c_str(), ios_base::in);
if(infile.fail()==true){
if(verbose>0){
cerr << " <W> TAsciiReader: Error opening ascii file: " << filePath << endl;
}
return -1;
}
if(!getline(infile,comment)){
if(verbose>0){
cerr << "<E> TAsciiReader: Error opening ascii file (it is probably a folder!): " << filePath << endl;
}
return -2;
}
stringstream ssFileData;
infile.seekg(infile.beg);
ssFileData << infile.rdbuf();
infile.close();
fileContents = ssFileData.str();
int16_t err = ReadString();
if(err == 0){
FillLutVectors();
fileOpened = true;
}
else{
fileOpened = false;
}
return err;
}
template <typename TStorage>
int16_t TAsciiFileReader<TStorage>::ReadString() {
stringstream ssFileContents(GetFileContents());
int32_t iLine=0;
string sLine;
getline(ssFileContents,sLine);
while(!ssFileContents.eof()){
int16_t err = ReadLine(sLine, iLine);
if(err < 0){
return err;
}
if(err == 1){
break;
}
iLine++;
getline(ssFileContents,sLine);
}
return 0;
}
/*
template <typename TStorage>
streampos TAsciiFileReader<TStorage>::GetFileSize() const{
streampos filesize = 0;
ifstream f(filePath.c_str(), ios::binary);
filesize = f.tellg();
f.seekg( 0, ios::end );
filesize = f.tellg() - filesize;
f.close();
return filesize;
}
*/
// -------------------------------
// --- Getter / setter methods ---
// -------------------------------
template <typename TStorage>
const stringstream& TAsciiFileReader<TStorage>::GetLog() const {
return log;
}
template <typename TStorage>
const string& TAsciiFileReader<TStorage>::GetComment() const {
return comment;
}
template <typename TStorage>
const string& TAsciiFileReader<TStorage>::GetFileContents() const {
return fileContents;
}
template <typename TStorage>
bool TAsciiFileReader<TStorage>::IsFileOpened() const {
return fileOpened;
}
template <typename TStorage>
const string& TAsciiFileReader<TStorage>::GetFilePath() const {
return filePath;
}
template <typename TStorage>
vector<TStorage>& TAsciiFileReader<TStorage>::GetStorage(){
return vStorage;
}
template <typename TStorage>
void TAsciiFileReader<TStorage>::SetVerbose(int v){
verbose = v;
}
} /* namespace std */