mirror of
https://github.com/The-Powder-Toy/The-Powder-Toy.git
synced 2025-09-02 04:22:34 +02:00
Port to_array.py to C++
This gets rid of a dependency on Python in $PATH (although Python is likely installed if we're using Meson). Nix people will like this a lot.
This commit is contained in:
52
data/ToArray.cpp
Normal file
52
data/ToArray.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <fstream>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc != 5)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
auto *outputCppPath = argv[1];
|
||||
auto *outputHPath = argv[2];
|
||||
auto *inputAnyPath = argv[3];
|
||||
auto *symbolName = argv[4];
|
||||
std::ifstream inputAny(inputAnyPath, std::ios::binary);
|
||||
std::ofstream outputCpp(outputCppPath);
|
||||
if (!outputCpp)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
outputCpp << "#include \"" << outputHPath << "\"\nconst unsigned char " << symbolName << "[] = { ";
|
||||
auto dataLen = 0U;
|
||||
while (true)
|
||||
{
|
||||
if (inputAny.eof())
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (!inputAny)
|
||||
{
|
||||
return 3;
|
||||
}
|
||||
char ch;
|
||||
inputAny.read(&ch, 1);
|
||||
outputCpp << (unsigned int)(unsigned char)(ch) << ", ";
|
||||
dataLen += 1;
|
||||
}
|
||||
outputCpp << " }; const unsigned int " << symbolName << "_size = " << dataLen << ";\n";
|
||||
if (!outputCpp)
|
||||
{
|
||||
return 4;
|
||||
}
|
||||
std::ofstream outputH(outputHPath);
|
||||
if (!outputH)
|
||||
{
|
||||
return 5;
|
||||
}
|
||||
outputH << "#pragma once\nextern const unsigned char " << symbolName << "[]; extern const unsigned int " << symbolName << "_size;\n";
|
||||
if (!outputH)
|
||||
{
|
||||
return 6;
|
||||
}
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user