blob: 0d2252dfc97ca524c394569e7fc14b9039a91ea3 [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
Francis Murtagh532a29d2020-06-29 11:50:01 +01006#include <Filesystem.hpp>
Sadik Armagan8271f812019-04-19 09:55:06 +01007#include <boost/program_options.hpp>
8
9#include <algorithm>
10#include <fstream>
11#include <iostream>
12#include <string>
13
14namespace
15{
16
17// parses the command line to extract
18// * the directory -i to look through .raw files from (must exist)
19// * the name of the file -o the output CSV file path (must not already exist)
20class CommandLineProcessor
21{
22public:
23 bool ValidateDirectory(std::string& dir)
24 {
25 if (dir.empty())
26 {
27 std::cerr << "No directory specified" << std::endl;
28 return false;
29 }
30
31 if (dir[dir.length() - 1] != '/')
32 {
33 dir += "/";
34 }
35
Francis Murtagh532a29d2020-06-29 11:50:01 +010036 if (!fs::exists(dir))
Sadik Armagan8271f812019-04-19 09:55:06 +010037 {
38 std::cerr << "Directory [" << dir << "] does not exist" << std::endl;
39 return false;
40 }
41
Francis Murtagh532a29d2020-06-29 11:50:01 +010042 if (!fs::is_directory(dir))
Sadik Armagan8271f812019-04-19 09:55:06 +010043 {
44 std::cerr << "Given directory [" << dir << "] is not a directory" << std::endl;
45 return false;
46 }
47
48 return true;
49 }
50
51 bool ValidateOutputFile(std::string& outputFileName)
52 {
53 if (outputFileName.empty())
54 {
55 std::cerr << "No output file name specified" << std::endl;
56 return false;
57 }
58
Francis Murtagh532a29d2020-06-29 11:50:01 +010059 if (fs::exists(outputFileName))
Sadik Armagan8271f812019-04-19 09:55:06 +010060 {
61 std::cerr << "Output file [" << outputFileName << "] already exists" << std::endl;
62 return false;
63 }
64
Francis Murtagh532a29d2020-06-29 11:50:01 +010065 if (fs::is_directory(outputFileName))
Sadik Armagan8271f812019-04-19 09:55:06 +010066 {
67 std::cerr << "Output file [" << outputFileName << "] is a directory" << std::endl;
68 return false;
69 }
70
Francis Murtagh532a29d2020-06-29 11:50:01 +010071 fs::path outputPath(outputFileName);
72 if (!fs::exists(outputPath.parent_path()))
Sadik Armagan8271f812019-04-19 09:55:06 +010073 {
74 std::cerr << "Directory [" << outputPath.parent_path().c_str() << "] does not exist" << std::endl;
75 return false;
76 }
77
78 return true;
79 }
80
FinnWilliamsArmd6507c92019-08-19 17:17:19 +010081 bool ValidateBindingId(const std::string& id)
82 {
83 if (!std::all_of(id.begin(), id.end(), ::isdigit))
84 {
85 std::cerr << "Invalid input binding Id" << std::endl;
86 return false;
87 }
88
89 return true;
90 }
91
Sadik Armagan8271f812019-04-19 09:55:06 +010092 bool ProcessCommandLine(int argc, char* argv[])
93 {
94 namespace po = boost::program_options;
95
96 po::options_description desc("Options");
97 try
98 {
99 desc.add_options()
100 ("help,h", "Display help messages")
101 ("indir,i", po::value<std::string>(&m_InputDirectory)->required(),
102 "Directory that .raw files are stored in")
103 ("outfile,o", po::value<std::string>(&m_OutputFileName)->required(),
FinnWilliamsArmd6507c92019-08-19 17:17:19 +0100104 "Output CSV file path")
105 ("layer-binding-id,l", po::value<std::string>(&m_InputBindingId)->default_value("0"),
106 "Input layer binding Id, Defaults to 0");
Sadik Armagan8271f812019-04-19 09:55:06 +0100107 }
108 catch (const std::exception& e)
109 {
110 std::cerr << "Fatal internal error: [" << e.what() << "]" << std::endl;
111 return false;
112 }
113
114 po::variables_map vm;
115
116 try
117 {
118 po::store(po::parse_command_line(argc, argv, desc), vm);
119
120 if (vm.count("help"))
121 {
122 std::cout << desc << std::endl;
123 return false;
124 }
125
126 po::notify(vm);
127 }
128 catch (const po::error& e)
129 {
130 std::cerr << e.what() << std::endl << std::endl;
131 std::cerr << desc << std::endl;
132 return false;
133 }
134
135 if (!ValidateDirectory(m_InputDirectory))
136 {
137 return false;
138 }
139
140 if (!ValidateOutputFile(m_OutputFileName))
141 {
142 return false;
143 }
144
FinnWilliamsArmd6507c92019-08-19 17:17:19 +0100145 if(!ValidateBindingId(m_InputBindingId))
146 {
147 return false;
148 }
149
Sadik Armagan8271f812019-04-19 09:55:06 +0100150 return true;
151 }
152
153 std::string GetInputDirectory() {return m_InputDirectory;}
154 std::string GetOutputFileName() {return m_OutputFileName;}
FinnWilliamsArmd6507c92019-08-19 17:17:19 +0100155 std::string GetInputBindingId() {return m_InputBindingId;}
Sadik Armagan8271f812019-04-19 09:55:06 +0100156
157private:
158 std::string m_InputDirectory;
159 std::string m_OutputFileName;
FinnWilliamsArmd6507c92019-08-19 17:17:19 +0100160 std::string m_InputBindingId;
Sadik Armagan8271f812019-04-19 09:55:06 +0100161};
162
163} // namespace anonymous
164
165int main(int argc, char* argv[])
166{
167 CommandLineProcessor cmdline;
168 if (!cmdline.ProcessCommandLine(argc, argv))
169 {
170 return -1;
171 }
172
Sadik Armagan8271f812019-04-19 09:55:06 +0100173 const std::string fileFormat(".raw");
Sadik Armagan8271f812019-04-19 09:55:06 +0100174
175 const std::string rawDirectory(cmdline.GetInputDirectory());
176 const std::string outputPath(cmdline.GetOutputFileName());
FinnWilliamsArmd6507c92019-08-19 17:17:19 +0100177 const std::string bindingId(cmdline.GetInputBindingId());
Sadik Armagan8271f812019-04-19 09:55:06 +0100178
179 std::vector<fs::path> rawFiles;
180 for (auto& entry : boost::make_iterator_range(fs::directory_iterator(rawDirectory), {}))
181 {
182 if (entry.path().extension().c_str() == fileFormat)
183 {
184 rawFiles.push_back(entry.path());
185 }
186 }
187
188 if (!rawFiles.empty())
189 {
190 unsigned int pass = 0;
191 std::ofstream refinementData;
192 refinementData.open(outputPath, std::ofstream::out);
193 if (refinementData.is_open())
194 {
195 for (auto const& raw : rawFiles)
196 {
197 refinementData << pass << ", " << bindingId << ", " << raw.c_str() << "\n";
198 if (!refinementData)
199 {
200 std::cerr << "Failed to write to output file: " << outputPath << std::endl;
201 continue;
202 }
203 ++pass;
204 }
205 refinementData.close();
206 }
207 else
208 {
209 std::cerr << "Failed to open output file: " << outputPath << std::endl;
210 return -1;
211 }
212 }
213 else
214 {
215 std::cerr << "No matching files with the \".raw\" extension found in the directory: "
216 << rawDirectory << std::endl;
217 return -1;
218 }
219
220 return 0;
221}