6 #include <boost/filesystem.hpp> 7 #include <boost/filesystem/operations.hpp> 8 #include <boost/filesystem/path.hpp> 9 #include <boost/program_options.hpp> 22 class CommandLineProcessor
29 std::cerr <<
"No directory specified" << std::endl;
33 if (dir[dir.length() - 1] !=
'/')
38 if (!boost::filesystem::exists(dir))
40 std::cerr <<
"Directory [" << dir <<
"] does not exist" << std::endl;
44 if (!boost::filesystem::is_directory(dir))
46 std::cerr <<
"Given directory [" << dir <<
"] is not a directory" << std::endl;
53 bool ValidateOutputFile(std::string& outputFileName)
55 if (outputFileName.empty())
57 std::cerr <<
"No output file name specified" << std::endl;
61 if (boost::filesystem::exists(outputFileName))
63 std::cerr <<
"Output file [" << outputFileName <<
"] already exists" << std::endl;
67 if (boost::filesystem::is_directory(outputFileName))
69 std::cerr <<
"Output file [" << outputFileName <<
"] is a directory" << std::endl;
73 boost::filesystem::path outputPath(outputFileName);
74 if (!boost::filesystem::exists(outputPath.parent_path()))
76 std::cerr <<
"Directory [" << outputPath.parent_path().c_str() <<
"] does not exist" << std::endl;
83 bool ValidateBindingId(
const std::string&
id)
85 if (!std::all_of(
id.begin(),
id.end(), ::isdigit))
87 std::cerr <<
"Invalid input binding Id" << std::endl;
94 bool ProcessCommandLine(
int argc,
char* argv[])
96 namespace po = boost::program_options;
98 po::options_description desc(
"Options");
102 (
"help,h",
"Display help messages")
103 (
"indir,i", po::value<std::string>(&m_InputDirectory)->required(),
104 "Directory that .raw files are stored in")
105 (
"outfile,o", po::value<std::string>(&m_OutputFileName)->required(),
106 "Output CSV file path")
107 (
"layer-binding-id,l", po::value<std::string>(&m_InputBindingId)->default_value(
"0"),
108 "Input layer binding Id, Defaults to 0");
110 catch (
const std::exception& e)
112 std::cerr <<
"Fatal internal error: [" << e.what() <<
"]" << std::endl;
116 po::variables_map vm;
120 po::store(po::parse_command_line(argc, argv, desc), vm);
122 if (vm.count(
"help"))
124 std::cout << desc << std::endl;
130 catch (
const po::error& e)
132 std::cerr << e.what() << std::endl << std::endl;
133 std::cerr << desc << std::endl;
142 if (!ValidateOutputFile(m_OutputFileName))
147 if(!ValidateBindingId(m_InputBindingId))
155 std::string GetInputDirectory() {
return m_InputDirectory;}
156 std::string GetOutputFileName() {
return m_OutputFileName;}
157 std::string GetInputBindingId() {
return m_InputBindingId;}
160 std::string m_InputDirectory;
161 std::string m_OutputFileName;
162 std::string m_InputBindingId;
167 int main(
int argc,
char* argv[])
169 CommandLineProcessor cmdline;
170 if (!cmdline.ProcessCommandLine(argc, argv))
175 namespace fs = boost::filesystem;
177 const std::string fileFormat(
".raw");
179 const std::string rawDirectory(cmdline.GetInputDirectory());
180 const std::string outputPath(cmdline.GetOutputFileName());
181 const std::string bindingId(cmdline.GetInputBindingId());
183 std::vector<fs::path> rawFiles;
184 for (
auto& entry : boost::make_iterator_range(fs::directory_iterator(rawDirectory), {}))
186 if (entry.path().extension().c_str() == fileFormat)
188 rawFiles.push_back(entry.path());
192 if (!rawFiles.empty())
194 unsigned int pass = 0;
195 std::ofstream refinementData;
196 refinementData.open(outputPath, std::ofstream::out);
197 if (refinementData.is_open())
199 for (
auto const& raw : rawFiles)
201 refinementData << pass <<
", " << bindingId <<
", " << raw.c_str() <<
"\n";
204 std::cerr <<
"Failed to write to output file: " << outputPath << std::endl;
209 refinementData.close();
213 std::cerr <<
"Failed to open output file: " << outputPath << std::endl;
219 std::cerr <<
"No matching files with the \".raw\" extension found in the directory: " 220 << rawDirectory << std::endl;
int main(int argc, char *argv[])
bool ValidateDirectory(std::string &dir)