|
|
@@ -2,8 +2,8 @@
|
|
|
#include <string>
|
|
|
#include <map>
|
|
|
#include <algorithm>
|
|
|
-#include <filesystem>
|
|
|
-#include <fstream>
|
|
|
+#include <cstdio>
|
|
|
+#include <dirent.h>
|
|
|
#include <ctime>
|
|
|
|
|
|
std::string to_hex_string(const std::string& input) {
|
|
|
@@ -20,16 +20,34 @@ std::string to_hex_string(const std::string& input) {
|
|
|
|
|
|
std::map<std::string, std::string> generate_python_sources() {
|
|
|
std::map<std::string, std::string> sources;
|
|
|
-
|
|
|
- for (const auto& file : std::filesystem::directory_iterator("python")) {
|
|
|
- if (file.path().extension() == ".py") {
|
|
|
- std::string key = file.path().stem().string();
|
|
|
- std::ifstream f(file.path());
|
|
|
- std::string content((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
|
|
- sources[key] = to_hex_string(content);
|
|
|
+ DIR *dir;
|
|
|
+ struct dirent *ent;
|
|
|
+ if ((dir = opendir ("python")) != NULL)
|
|
|
+ {
|
|
|
+ while ((ent = readdir (dir)) != NULL)
|
|
|
+ {
|
|
|
+ std::string filename = ent->d_name;
|
|
|
+ size_t pos = filename.rfind(".py");
|
|
|
+ if (pos != std::string::npos)
|
|
|
+ {
|
|
|
+ std::string key = filename.substr(0, filename.length() - 3);
|
|
|
+ std::string filepath = "python/" + filename;
|
|
|
+ FILE* file = fopen(filepath.c_str(), "r");
|
|
|
+ if(file == NULL) exit(2);
|
|
|
+ std::string content;
|
|
|
+ char buffer[1024];
|
|
|
+ while (fgets(buffer, sizeof(buffer), file) != NULL)
|
|
|
+ {
|
|
|
+ content += buffer;
|
|
|
+ }
|
|
|
+ fclose(file);
|
|
|
+ sources[key] = to_hex_string(content);
|
|
|
+ }
|
|
|
}
|
|
|
+ closedir (dir);
|
|
|
+ }else{
|
|
|
+ exit(1);
|
|
|
}
|
|
|
-
|
|
|
return sources;
|
|
|
}
|
|
|
|
|
|
@@ -46,8 +64,8 @@ std::string generate_header(const std::map<std::string, std::string>& sources) {
|
|
|
header += "\n#include <map>\n#include <string>\n\nnamespace pkpy{\n";
|
|
|
header += " inline static std::map<std::string, std::string> kPythonLibs = {\n";
|
|
|
|
|
|
- for (const auto& [key, value] : sources) {
|
|
|
- header += " {\"" + key + "\", \"" + value + "\"},\n";
|
|
|
+ for (auto it=sources.begin(); it!=sources.end(); ++it) {
|
|
|
+ header += " {\"" + it->first + "\", \"" + it->second + "\"},\n";
|
|
|
}
|
|
|
|
|
|
header += " };\n";
|
|
|
@@ -59,7 +77,8 @@ std::string generate_header(const std::map<std::string, std::string>& sources) {
|
|
|
int main() {
|
|
|
auto sources = generate_python_sources();
|
|
|
std::string header = generate_header(sources);
|
|
|
- std::ofstream header_file("src/_generated.h");
|
|
|
- header_file << header;
|
|
|
+ FILE* f = fopen("src/_generated.h", "w");
|
|
|
+ fprintf(f, "%s", header.c_str());
|
|
|
+ fclose(f);
|
|
|
return 0;
|
|
|
}
|