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
|
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <iostream>
#include <fstream>
#include <cassert>
int fileToArray(const std::string &source, const std::string &dest, const std::string &arrName)
{
FILE *fs = fopen(source.c_str(), "rb");
if (!fs)
{
std::cerr << "source file not found: <" << source << ">" << std::endl;
return -1;
}
std::ofstream fo(dest.c_str());
if (fo.fail())
{
std::cerr << "cannot generate file: <" << dest << ">" << std::endl;
fclose(fs);
return -1;
}
std::cout << "generating <" << dest << ">" << std::endl;
fo << "#ifndef _" << arrName << "_H_" << std::endl;
fo << "#define _" << arrName << "_H_" << std::endl;
fo << "const char " << arrName << "[] = {" << std::endl;
int is_error = fseek(fs, 0L, SEEK_SET);
assert(!is_error);
(void)is_error;
size_t bytes;
do
{
char buf[1024];
bytes = fread(buf, 1, sizeof(buf), fs);
assert(!ferror(fs) && "file read error");
// convert line
for (size_t i = 0; i < bytes; i++)
{
fo << "0x" << std::hex << static_cast<int>(buf[i]) << ", ";
}
} while (bytes != 0);
fo << "};" << std::endl;
fo << std::endl;
fo << "#endif /* _" << arrName << "_H_ */" << std::endl;
fo.flush();
fclose(fs);
return 0;
}
std::string extractFileName(std::string path)
{
auto pos = path.find_last_of('/');
if (pos != std::string::npos)
path = path.substr(pos + 1);
pos = path.find_first_of('.');
if (pos != std::string::npos)
path = path.substr(0, pos);
return path;
}
int main(int argc, char *argv[])
{
if (argc < 3)
return -1;
std::string OutPutDir = argv[1];
for (int i = 2; i < argc; i++)
{
std::string sourceFullFileName = argv[i];
std::string filename = extractFileName(sourceFullFileName);
// NOLINTNEXTLINE (performance-inefficient-string-concatenation)
std::string outputFileName = OutPutDir + "/" + filename + ".generated.h";
if (fileToArray(sourceFullFileName, outputFileName, filename) != 0)
return -1;
}
return 0;
}
|