ONE - On-device Neural Engine
Loading...
Searching...
No Matches
def2src.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <iostream>
18#include <fstream>
19#include <cassert>
20
21int fileToArray(const std::string &source, const std::string &dest, const std::string &arrName)
22{
23 FILE *fs = fopen(source.c_str(), "rb");
24 if (!fs)
25 {
26 std::cerr << "source file not found: <" << source << ">" << std::endl;
27 return -1;
28 }
29
30 std::ofstream fo(dest.c_str());
31 if (fo.fail())
32 {
33 std::cerr << "cannot generate file: <" << dest << ">" << std::endl;
34 fclose(fs);
35 return -1;
36 }
37
38 std::cout << "generating <" << dest << ">" << std::endl;
39
40 fo << "#ifndef _" << arrName << "_H_" << std::endl;
41 fo << "#define _" << arrName << "_H_" << std::endl;
42
43 fo << "const char " << arrName << "[] = {" << std::endl;
44
45 int is_error = fseek(fs, 0L, SEEK_SET);
46 assert(!is_error);
47 (void)is_error;
48 size_t bytes;
49 do
50 {
51 char buf[1024];
52 bytes = fread(buf, 1, sizeof(buf), fs);
53 assert(!ferror(fs) && "file read error");
54
55 // convert line
56 for (size_t i = 0; i < bytes; i++)
57 {
58 fo << "0x" << std::hex << static_cast<int>(buf[i]) << ", ";
59 }
60 } while (bytes != 0);
61
62 fo << "};" << std::endl;
63
64 fo << std::endl;
65 fo << "#endif /* _" << arrName << "_H_ */" << std::endl;
66
67 fo.flush();
68 fclose(fs);
69
70 return 0;
71}
72
73std::string extractFileName(std::string path)
74{
75 auto pos = path.find_last_of('/');
76 if (pos != std::string::npos)
77 path = path.substr(pos + 1);
78
79 pos = path.find_first_of('.');
80 if (pos != std::string::npos)
81 path = path.substr(0, pos);
82
83 return path;
84}
85
86int main(int argc, char *argv[])
87{
88 if (argc < 3)
89 return -1;
90
91 std::string OutPutDir = argv[1];
92
93 for (int i = 2; i < argc; i++)
94 {
95 std::string sourceFullFileName = argv[i];
96 std::string filename = extractFileName(sourceFullFileName);
97 // NOLINTNEXTLINE (performance-inefficient-string-concatenation)
98 std::string outputFileName = OutPutDir + "/" + filename + ".generated.h";
99
100 if (fileToArray(sourceFullFileName, outputFileName, filename) != 0)
101 return -1;
102 }
103
104 return 0;
105}
int main(void)
int fileToArray(const std::string &source, const std::string &dest, const std::string &arrName)
Definition def2src.cpp:21
std::string extractFileName(std::string path)
Definition def2src.cpp:73