resourcec.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <vector>
  5. #include <string>
  6. #include <algorithm>
  7. std::string mangle(std::string filename);
  8. void encode(std::istream& file, std::ostream& out);
  9. int main(int argc, char** argv)
  10. {
  11. std::vector<std::string> files;
  12. std::string outFile;
  13. std::string headerFile;
  14. std::string namespc;
  15. std::string flag = "";
  16. for (int i = 1; i < argc; i++) {
  17. if (flag == "-o") {
  18. outFile = argv[i];
  19. flag = "";
  20. }
  21. else if (flag == "-d") {
  22. headerFile = argv[i];
  23. flag = "";
  24. }
  25. else if (flag == "-n") {
  26. namespc = argv[i];
  27. flag = "";
  28. }
  29. else if (argv[i][0] == '-') {
  30. flag = argv[i];
  31. }
  32. else {
  33. files.push_back(argv[i]);
  34. flag = "";
  35. }
  36. }
  37. std::ofstream out(outFile);
  38. std::ofstream hout(headerFile);
  39. std::string uh = mangle(headerFile);
  40. std::string un = mangle(namespc);
  41. std::transform(uh.begin(), uh.end(), uh.begin(), ::toupper);
  42. std::transform(un.begin(), un.end(), un.begin(), ::toupper);
  43. hout << "#include <string>\n\n";
  44. hout << "#ifndef RESOURCE_" << un << "_" << uh << "\n";
  45. hout << "#define RESOURCE_" << un << "_" << uh << "\n";
  46. out << "#include <string>\n\n";
  47. if (namespc != "") {
  48. hout << "namespace " << namespc << " {\n\n";
  49. out << "namespace " << namespc << " {\n\n";
  50. }
  51. for (auto&& filename : files) {
  52. std::ifstream inFile(filename);
  53. out << "extern const std::string " << mangle(filename) << " = \"";
  54. hout << "extern const std::string " << mangle(filename) << ";\n";
  55. encode(inFile, out);
  56. out << "\";\n";
  57. }
  58. if (namespc != "") {
  59. hout << "}\n\n";
  60. out << "}\n\n";
  61. }
  62. hout << "\n#endif // RESOURCE_" << un << "_" << uh << "\n";
  63. }
  64. std::string mangle(std::string filename)
  65. {
  66. const size_t lastDelim = filename.find_last_of("\\/");
  67. if (lastDelim != std::string::npos)
  68. {
  69. filename.erase(0, lastDelim + 1);
  70. }
  71. std::replace(filename.begin(), filename.end(), '.', '_');
  72. std::replace(filename.begin(), filename.end(), ':', '_');
  73. return filename;
  74. }
  75. void encode(std::istream& file, std::ostream& out)
  76. {
  77. while(true) {
  78. int x = file.get();
  79. if (!file) break;
  80. out << "\\x" << std::hex << std::setfill('0') << std::setw(2) << x;
  81. }
  82. out << "\x00";
  83. }