-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
211 lines (162 loc) · 3.99 KB
/
main.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* main.cpp
*
* Created on: 20 Mar 2014
* Author: DetlevCM
*/
// Fiel Streams and IO
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <fstream>
// For strtod -> string to double
#include <stdlib.h>
// Math Operations
//#include <math.h>
#include <cmath>
// To get the CPU time
#include <time.h>
// For Vectors
#include <vector>
// For strings, C strings right now...
//#include <cstring>
#include<string.h>
using namespace std;
void ProcessFiles(ifstream&, ofstream&, int);
// quick short progarm to parse the xml output from the PetroOxy into
// a more handy output format
int main(int argc, char* argv[])
{
string filename;
string input;
int type = 0; // output type 0 = csv (default), 1 = txt
cout << "Please enter the filename - the ending may be included or omitted.\n";
cout << "The filetype output can be changed by inputting 'csv' or 'txt'. \n";
cout << "The program can be exited by entering 'quit'. \n\n";
bool InputIsFilename = true;
//cin >> filename;
while(true) // loop until user quits
{
//cin >> input;
getline(cin, input);
if(strcmp(input.c_str(),"quit")==0) // user wants to quit
{
//EndProgram = true;
return 0;
}
InputIsFilename = true;
if(strcmp(input.c_str(),"csv")==0) // user wants csv, default
{
type = 0;
InputIsFilename = false;
}
if(strcmp(input.c_str(),"txt")==0) // user wants csv, default
{
type = 1;
InputIsFilename = false;
}
if(InputIsFilename)
{
filename = input;
// filter file ending away if entered
size_t found = filename.find(".xml");
if (found!=string::npos) // it is a measurement
{
char * cstr, *p;
cstr = new char [filename.size()+1];
strcpy (cstr, filename.c_str());
p=strtok (cstr,".xml");
filename = p;
delete[] cstr;
delete[] p;
}
string input_filename = filename+".xml";
string output_filename;
if(type==0)
{
output_filename = filename+".csv";
}
if(type==1)
{
output_filename = filename+".txt";
}
ifstream DataInputFile;
DataInputFile.open(input_filename.c_str());
ofstream DataOutputFile(output_filename.c_str(),ios::out);
if (DataInputFile.is_open()) {
if (DataOutputFile.is_open())
{
if(type == 0)
{
DataOutputFile << "Time,Pressure,Temperature,HeatingPower,\n";
}
if(type == 1)
{
DataOutputFile << "Time Pressure Temperature HeatingPower\n";
}
}
else cout << "Unable to open output file";
// Handle Files
ProcessFiles( DataInputFile, DataOutputFile, type);
DataOutputFile.close();
DataInputFile.close();
}
else cout << "Unable to open input file";
}
}
return 0;
}
// Function that handles the input and output files
void ProcessFiles(ifstream& DataInputFile, ofstream& DataOutputFile, int type)
{
vector<string> line(4);
while(DataInputFile.good())
{
getline(DataInputFile,line[0]);
size_t found = line[0].find("<Time>");
if (found!=string::npos) // it is a measurement
{
/*
* <Measurement>
* <Time>60</Time>
* <Pressure>844</Pressure>
* <Temperature>130.2</Temperature>
* <HeatingPower>-1</HeatingPower>
* </Measurement>
*/
// line 1 will contain Time
getline(DataInputFile,line[1]); // this is pressure
getline(DataInputFile,line[2]); // this is temperature
getline(DataInputFile,line[3]); // this is heating power
int i;
vector< string > Readout;
for(i=0;i<4;i++)
{
char * cstr, *p;
string str = line[i];
cstr = new char [str.size()+1];
strcpy (cstr, str.c_str());
p=strtok (cstr,"<>");
while (p!=NULL)
{
Readout.push_back(p);
//cout << p << "\n";
p=strtok(NULL,"<>");
}
delete[] cstr;
delete[] p;
if(type == 0)
{
DataOutputFile << Readout[2] << ",";//strtok(Readout[2].c_str(),NULL);
}
if(type == 1)
{
DataOutputFile << Readout[2] << " ";//strtok(Readout[2].c_str(),NULL);
}
Readout.clear();
}
DataOutputFile << "\n";
DataOutputFile.flush();
}
}
}