forked from CIShell/CIShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNonJavaAlgorithmWizardExample.cpp
More file actions
187 lines (144 loc) · 6.04 KB
/
NonJavaAlgorithmWizardExample.cpp
File metadata and controls
187 lines (144 loc) · 6.04 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
#include <exception>
#include <iostream>
#include <fstream>
#define PROGRAM_NAME "NonJavaAlgorithmWizardExample"
#define EXPECTED_ARGUMENT_COUNT 5
#define USER_FILE_ARGUMENT_INDEX 1
#define NWB_FILE_ARGUMENT_INDEX 2
#define PLATFORM_FILE_ARGUMENT_INDEX 3
#define OUTPUT_FILE_ARGUMENT_INDEX 4
std::string readUserFile(const std::string& userFileName)
throw(std::exception);
std::string readNWBFile(const std::string& nwbFileName) throw(std::exception);
std::string readPlatformFile(const std::string& platformFileName)
throw(std::exception);
std::string readFileContents(std::ifstream& inputFileStream);
void outputCombinedContentsToFile(const std::string& userFileContents,
const std::string& nwbFileContents,
const std::string& platformFileContents,
const std::string& fileName)
throw(std::exception);
int main (int argumentCount, char* arguments[]) {
if (argumentCount < EXPECTED_ARGUMENT_COUNT) {
std::cerr << "You must provide " << EXPECTED_ARGUMENT_COUNT - 1;
std::cerr << " arguments to the program." << std::endl;
std::cerr << "Expected format:" << std::endl;
std::cerr << "user_file nwb_file platform_file output_file";
std::cerr << std::endl;
} else {
// Process the end-user-specified file.
std::string userFileName = arguments[USER_FILE_ARGUMENT_INDEX];
std::string userFileContents;
try {
userFileContents = readUserFile(userFileName);
std::cout << "Successfully read the file you specified \'";
std::cout << userFileName << "\'." << std::endl;
} catch (std::exception& readUserFileException) {
std::cerr << "There was an error reading your file \'";
std::cerr << userFileName << "\': \"";
std::cerr << readUserFileException.what() << "\"" << std::endl;
return 1;
}
// Process the NWB file.
std::string nwbFileName = arguments[NWB_FILE_ARGUMENT_INDEX];
std::string nwbFileContents;
try {
nwbFileContents = readNWBFile(nwbFileName);
std::cout << "Successfully read the NWB file you selected off of ";
std::cout << "the Data Manager (\'" << nwbFileName << "\').";
std::cout << std:: endl;
} catch (std::exception& readNWBFileException) {
std::cerr << "There was an error reading the NWB file \'";
std::cerr << nwbFileName << "\': \"";
std::cerr << readNWBFileException.what() << "\"" << std::endl;
}
// Process the platform file.
std::string platformFileName = arguments[PLATFORM_FILE_ARGUMENT_INDEX];
std::string platformFileContents;
try {
platformFileContents = readPlatformFile(platformFileName);
std::cout << "Successfully read the platform file \'";
std::cout << platformFileName << "\'." << std::endl;
} catch (std::exception& readPlatformFileException) {
std::cerr << "There was an error reading the platform file \'";
std::cerr << platformFileName << "\': \"";
std::cerr << readPlatformFileException.what() << "\"" << std::endl;
return 1;
}
/*
* Combine the user-specified file contents and the platform file into
* the user-specified output file.
*/
std::string outputFileName = arguments[OUTPUT_FILE_ARGUMENT_INDEX];
try {
outputCombinedContentsToFile(userFileContents,
nwbFileContents,
platformFileContents,
outputFileName);
std::cout << "Successfully wrote the combined contents to the ";
std::cout << "file \'" << outputFileName << "\'." << std::endl;
} catch (std::exception& outputCombinedContentsToFileException) {
std::cerr << "There was an error outputting the combined contents";
std::cerr << " of the file you specified and the platform file ";
std::cerr << "to the file \'" << outputFileName << "\': \"";
std::cerr << outputCombinedContentsToFileException.what();
std::cerr << "\"" << std::endl;
}
}
return 0;
}
std::string readUserFile(const std::string& userFileName)
throw(std::exception) {
std::ifstream userFileStream(userFileName.c_str(), std::ifstream::in);
if (!userFileStream.is_open()) {
throw std::ios_base::failure("Unable to open user file for reading.");
}
std::string userFileContents = readFileContents(userFileStream);
userFileStream.close();
return userFileContents;
}
std::string readNWBFile(const std::string& nwbFileName) throw(std::exception) {
std::ifstream nwbFileStream(nwbFileName.c_str(), std::ifstream::in);
if (!nwbFileStream.is_open()) {
throw std::ios_base::failure("Unable to open NWB file for reading.");
}
std::string nwbFileContents = readFileContents(nwbFileStream);
nwbFileStream.close();
return nwbFileContents;
}
std::string readPlatformFile(const std::string& platformFileName)
throw(std::exception) {
std::ifstream platformFileStream(platformFileName.c_str(),
std::ifstream::in);
if (!platformFileStream.is_open()) {
throw std::ios_base::failure(
"Unable to open platform file for reading.");
}
std::string platformFileContents = readFileContents(platformFileStream);
platformFileStream.close();
return platformFileContents;
}
std::string readFileContents(std::ifstream& inputFileStream) {
std::string fileContents;
while (inputFileStream.good()) {
fileContents += inputFileStream.get();
}
return fileContents;
}
void outputCombinedContentsToFile(const std::string& userFileContents,
const std::string& nwbFileContents,
const std::string& platformFileContents,
const std::string& fileName)
throw(std::exception) {
std::ofstream outputFileStream(fileName.c_str(), std::ofstream::out);
if (!outputFileStream.is_open()) {
throw std::ios_base::failure(
"Unable to open output file for writing.");
}
outputFileStream << "User file contents:" << std::endl;
outputFileStream << userFileContents << std::endl;
outputFileStream << "NWB file contents:" << std::endl;
outputFileStream << nwbFileContents << std::endl;
outputFileStream << "Platform:" << std::endl;
outputFileStream << platformFileContents << std::endl;
}