blob: 1a14ebab07a8d07ac58eda6dcbc7fd708f80f19e [file] [log] [blame]
Sadik Armagan8271f812019-04-19 09:55:06 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include <boost/filesystem.hpp>
7#include <boost/filesystem/operations.hpp>
8#include <boost/filesystem/path.hpp>
9#include <boost/program_options.hpp>
10
11#include <algorithm>
12#include <fstream>
13#include <iostream>
14#include <string>
15
16namespace
17{
18
19// parses the command line to extract
20// * the directory -i to look through .raw files from (must exist)
21// * the name of the file -o the output CSV file path (must not already exist)
22class CommandLineProcessor
23{
24public:
25 bool ValidateDirectory(std::string& dir)
26 {
27 if (dir.empty())
28 {
29 std::cerr << "No directory specified" << std::endl;
30 return false;
31 }
32
33 if (dir[dir.length() - 1] != '/')
34 {
35 dir += "/";
36 }
37
38 if (!boost::filesystem::exists(dir))
39 {
40 std::cerr << "Directory [" << dir << "] does not exist" << std::endl;
41 return false;
42 }
43
44 if (!boost::filesystem::is_directory(dir))
45 {
46 std::cerr << "Given directory [" << dir << "] is not a directory" << std::endl;
47 return false;
48 }
49
50 return true;
51 }
52
53 bool ValidateOutputFile(std::string& outputFileName)
54 {
55 if (outputFileName.empty())
56 {
57 std::cerr << "No output file name specified" << std::endl;
58 return false;
59 }
60
61 if (boost::filesystem::exists(outputFileName))
62 {
63 std::cerr << "Output file [" << outputFileName << "] already exists" << std::endl;
64 return false;
65 }
66
67 if (boost::filesystem::is_directory(outputFileName))
68 {
69 std::cerr << "Output file [" << outputFileName << "] is a directory" << std::endl;
70 return false;
71 }
72
73 boost::filesystem::path outputPath(outputFileName);
74 if (!boost::filesystem::exists(outputPath.parent_path()))
75 {
76 std::cerr << "Directory [" << outputPath.parent_path().c_str() << "] does not exist" << std::endl;
77 return false;
78 }
79
80 return true;
81 }
82
83 bool ProcessCommandLine(int argc, char* argv[])
84 {
85 namespace po = boost::program_options;
86
87 po::options_description desc("Options");
88 try
89 {
90 desc.add_options()
91 ("help,h", "Display help messages")
92 ("indir,i", po::value<std::string>(&m_InputDirectory)->required(),
93 "Directory that .raw files are stored in")
94 ("outfile,o", po::value<std::string>(&m_OutputFileName)->required(),
95 "Output CSV file path");
96 }
97 catch (const std::exception& e)
98 {
99 std::cerr << "Fatal internal error: [" << e.what() << "]" << std::endl;
100 return false;
101 }
102
103 po::variables_map vm;
104
105 try
106 {
107 po::store(po::parse_command_line(argc, argv, desc), vm);
108
109 if (vm.count("help"))
110 {
111 std::cout << desc << std::endl;
112 return false;
113 }
114
115 po::notify(vm);
116 }
117 catch (const po::error& e)
118 {
119 std::cerr << e.what() << std::endl << std::endl;
120 std::cerr << desc << std::endl;
121 return false;
122 }
123
124 if (!ValidateDirectory(m_InputDirectory))
125 {
126 return false;
127 }
128
129 if (!ValidateOutputFile(m_OutputFileName))
130 {
131 return false;
132 }
133
134 return true;
135 }
136
137 std::string GetInputDirectory() {return m_InputDirectory;}
138 std::string GetOutputFileName() {return m_OutputFileName;}
139
140private:
141 std::string m_InputDirectory;
142 std::string m_OutputFileName;
143};
144
145} // namespace anonymous
146
147int main(int argc, char* argv[])
148{
149 CommandLineProcessor cmdline;
150 if (!cmdline.ProcessCommandLine(argc, argv))
151 {
152 return -1;
153 }
154
155 namespace fs = boost::filesystem;
156
157 const std::string fileFormat(".raw");
158 const std::string bindingId("0");
159
160 const std::string rawDirectory(cmdline.GetInputDirectory());
161 const std::string outputPath(cmdline.GetOutputFileName());
162
163 std::vector<fs::path> rawFiles;
164 for (auto& entry : boost::make_iterator_range(fs::directory_iterator(rawDirectory), {}))
165 {
166 if (entry.path().extension().c_str() == fileFormat)
167 {
168 rawFiles.push_back(entry.path());
169 }
170 }
171
172 if (!rawFiles.empty())
173 {
174 unsigned int pass = 0;
175 std::ofstream refinementData;
176 refinementData.open(outputPath, std::ofstream::out);
177 if (refinementData.is_open())
178 {
179 for (auto const& raw : rawFiles)
180 {
181 refinementData << pass << ", " << bindingId << ", " << raw.c_str() << "\n";
182 if (!refinementData)
183 {
184 std::cerr << "Failed to write to output file: " << outputPath << std::endl;
185 continue;
186 }
187 ++pass;
188 }
189 refinementData.close();
190 }
191 else
192 {
193 std::cerr << "Failed to open output file: " << outputPath << std::endl;
194 return -1;
195 }
196 }
197 else
198 {
199 std::cerr << "No matching files with the \".raw\" extension found in the directory: "
200 << rawDirectory << std::endl;
201 return -1;
202 }
203
204 return 0;
205}