Skip to content

Commit dd7cac7

Browse files
committed
Add file_to_c
1 parent 675b482 commit dd7cac7

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

tools/file_to_c/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
project(file_to_c)
3+
set(CMAKE_CXX_STANDARD 17)
4+
5+
add_executable(file_to_c "file_to_c.cpp")

tools/file_to_c/file_to_c.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2024 RT64 Contributors
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
#include <filesystem>
26+
#include <fstream>
27+
#include <cstdio>
28+
#include <vector>
29+
30+
std::vector<char> read_file(const char* path) {
31+
std::ifstream input_file{path, std::ios::binary};
32+
std::vector<char> ret{};
33+
34+
if (!input_file.good()) {
35+
return ret;
36+
}
37+
38+
// Get the length of the file
39+
input_file.seekg(0, std::ios::end);
40+
ret.resize(input_file.tellg());
41+
42+
// Read the file contents into the vector
43+
input_file.seekg(0, std::ios::beg);
44+
input_file.read(ret.data(), ret.size());
45+
46+
return ret;
47+
}
48+
49+
void create_parent_if_needed(const char* path) {
50+
std::filesystem::path parent_path = std::filesystem::path{path}.parent_path();
51+
if (!parent_path.empty()) {
52+
std::filesystem::create_directories(parent_path);
53+
}
54+
}
55+
56+
int main(int argc, const char** argv) {
57+
if (argc != 6) {
58+
printf("Usage: %s [input file] [array name] [array type] [output C file] [output C header]\n", argv[0]);
59+
return EXIT_SUCCESS;
60+
}
61+
62+
const char* input_path = argv[1];
63+
const char* array_name = argv[2];
64+
const char* array_type = argv[3];
65+
const char* output_c_path = argv[4];
66+
const char* output_h_path = argv[5];
67+
68+
// Read the input file's contents
69+
std::vector<char> contents = read_file(input_path);
70+
71+
if (contents.empty()) {
72+
fprintf(stderr, "Failed to open file %s! (Or it's empty)\n", input_path);
73+
return EXIT_FAILURE;
74+
}
75+
76+
// Create the output directories if they don't exist
77+
create_parent_if_needed(output_c_path);
78+
create_parent_if_needed(output_h_path);
79+
80+
// Write the C file with the array
81+
{
82+
std::ofstream output_c_file{output_c_path};
83+
output_c_file << "extern " << array_type << " " << array_name << "[" << contents.size() << "];\n";
84+
output_c_file << array_type << " " << array_name << "[" << contents.size() << "] = {";
85+
86+
for (char x : contents) {
87+
output_c_file << (int)x << ", ";
88+
}
89+
90+
output_c_file << "};\n";
91+
}
92+
93+
// Write the header file with the extern array
94+
{
95+
std::ofstream output_h_file{output_h_path};
96+
output_h_file <<
97+
"#ifdef __cplusplus\n"
98+
" extern \"C\" {\n"
99+
"#endif\n"
100+
"extern " << array_type << " " << array_name << "[" << contents.size() << "];\n"
101+
"#ifdef __cplusplus\n"
102+
" }\n"
103+
"#endif\n";
104+
}
105+
}

0 commit comments

Comments
 (0)