ONE - On-device Neural Engine
Loading...
Searching...
No Matches
def2src.cpp File Reference
#include <iostream>
#include <fstream>
#include <cassert>

Go to the source code of this file.

Functions

int fileToArray (const std::string &source, const std::string &dest, const std::string &arrName)
 
std::string extractFileName (std::string path)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ extractFileName()

std::string extractFileName ( std::string  path)

Definition at line 73 of file def2src.cpp.

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}

Referenced by main().

◆ fileToArray()

int fileToArray ( const std::string &  source,
const std::string &  dest,
const std::string &  arrName 
)

Definition at line 21 of file def2src.cpp.

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}

Referenced by main().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 86 of file def2src.cpp.

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 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

References extractFileName(), and fileToArray().