-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGatherFiles.cpp
More file actions
263 lines (224 loc) · 5.86 KB
/
GatherFiles.cpp
File metadata and controls
263 lines (224 loc) · 5.86 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "GatherFiles.h"
/**
24/08/2011
- Removed need for bGlobal and bOpt in listFiles()
- Removed unnecessary padding in lsf and cov output files
- Allowed the identification of various xyz files, as we can now search through them
- Removed check on filename for lsf and cov output, as guaranteed to be > 0
02/10/2011
- Took out dereferencing of print vector
24/10/2011
- Set her up to deal with folders inside folders. Previously this just returned null
**/
using namespace std;
GatherFiles::GatherFiles()
{
// counter = 1; // Appenditure Counter
lsf_filename = "";
covariance_filename = ""; // Filenames
}
vector<string> GatherFiles::listFiles( const string &name, const bool &bXYZ)
// This will discover all files within a working directory that we can review
// Inputs: string(dirName) - Directory name
{
// INPUT VARIABLES //
const string directory_word="/";
string sub = name.substr(name.size()-1);
vector<string> files;
/////////////////////
// CHECK FOR DIRECTORIES //
if (cmpStr(sub,directory_word))
{
if (bXYZ)
{
files = listXYZFiles(name.substr(0,name.size()-1));
}
else
{
files = listImageFiles(name.substr(0,name.size()-1));
}
}
else
files.push_back(name);
return files;
}
vector<string> GatherFiles::listImageFiles( const string &dirName )
// This will discover all files within a working directory that we can review
// Inputs: string(dirName) - Directory name
{
vector<string> files;
DIR *dirp = opendir( dirName.c_str() );
if (dirp)
{
struct dirent *dp = NULL;
while ((dp = readdir( dirp )) != NULL )
{
string file( dp->d_name );
// cout << file << endl;
if ( file == "." || file == ".." )
{
// skip these
continue;
}
if ( dp->d_type & DT_DIR )
{
// found a directory; recurse into it.
string filePath = dirName + "/" + file;
vector<string> files_temp = listImageFiles(filePath);
files.insert(files.end(), files_temp.begin(), files_temp.end());
}
else
{
//regular file found
const string zcon="zcon";
const string txt=".txt";
string sub = file.substr(file.size()-4);
// cout << file << endl;
if (cmpStr(sub,zcon) || cmpStr(sub,txt))
{
// cout << sub << endl;
files.push_back(dirName + "/" + file);
}
}
}
closedir(dirp);
}
return files;
}
vector<string> GatherFiles::listXYZFiles( const string &dirName )
// This will discover all files within a working directory that we can review
// Inputs: string(dirName) - Directory name
{
vector<string> files;
DIR *dirp = opendir( dirName.c_str() );
if (dirp)
{
struct dirent *dp = NULL;
while ((dp = readdir( dirp )) != NULL )
{
string file( dp->d_name );
if ( file == "." || file == ".." )
{
// skip these
continue;
}
if ( dp->d_type & DT_DIR )
{
// found a directory; recurse into it.
string filePath = dirName + "/" + file;
vector<string> files_temp = listXYZFiles(filePath);
files.insert(files.end(), files_temp.begin(), files_temp.end());
}
else
{
//regular file found
const string xyz=".xyz";
string sub = file.substr(file.size()-4);
if (cmpStr(sub,xyz))
{
files.push_back(dirName + "/" + file);
}
}
}
closedir(dirp);
}
return files;
}
bool GatherFiles::prepareOutputFiles(bool check, bool bLsf, const string lsf, bool bCov, const string cov)
// Prepare the output files for data to be written to them
// Inputs - check to see if we are saving data
// - check if we are running lsf analysis
// - lsf filename
// - check if we are running covariance analysis
// - covariance filename
{
// Error check
bool error = false;
if (check)
{
vector<string> empty;
//cout << bLsf << " " << lsf << endl;
//cout << bCov << " " << cov << endl;
if (bLsf && !error)
{
setLsfFilename(lsf);
error = write(empty,lsf);
}
if (bCov && !error)
{
setCovarianceFilename(cov);
error = write(empty,cov);
// cout << cov << " " << covariance_filename << endl;
}
}
// cout << lsf_filename << " " << covariance_filename << endl;
// Return error check
return error;
}
bool GatherFiles::write(vector<string> data, const string filename)
// This is just a text outputting function to file
// Inputs vector<string>(data) - Data string to be written
// string(filename) - File to be written too
// Outputs: Success or Failure
{
ofstream outData;
outData.open(filename.c_str(), ios::out);
if(!outData.is_open())
{
cout << "Error writing to file: " << filename << endl;
return true;
}
else
{
//outData << "=== Data Output to : " << filename << " === " << endl;
for (vector<string>::iterator it = data.begin(); it != data.end(); ++it)
{
outData << *it << endl;
}
outData.close();
return false;
}
}
bool GatherFiles::appendToOutputFiles(vector<string> lsf, vector<string> cov)
// Append data to output files
// Inputs - lsf data
// - covariance data
{
// Error check
bool error = false;
// cout << lsf_filename.length() << " " << lsf_filename << " " << covariance_filename.length() << " " << covariance_filename << endl;
if (lsf_filename.size() > 0)
{
error = append(lsf,lsf_filename);
}
if (covariance_filename.size() > 0)
{
append(cov,covariance_filename);
}
// Return error check
return error;
}
bool GatherFiles::append(vector<string> data, const string filename)
// This is just a text appending function to file
// Inputs vector<string>(data) - Data string to be written
// string(filename) - File to be written too
// Outputs: Success or Failure
{
ofstream outData;
outData.open(filename.c_str(), ios::app);
if(!outData.is_open())
{
cout << "Error appending to file: " << filename << endl;
return false;
}
else
{
for (vector<string>::iterator it = data.begin(); it != data.end(); ++it)
{
outData << *it << endl;
}
outData.close();
//counter++;
return true;
}
}