main.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <library/cpp/resource/registry.h>
  2. #include <util/stream/output.h>
  3. #include <util/stream/file.h>
  4. #include <util/digest/city.h>
  5. #include <util/string/cast.h>
  6. #include <util/string/hex.h>
  7. #include <util/string/vector.h>
  8. #include <util/string/split.h>
  9. using namespace NResource;
  10. static inline void GenOne(const TString& data, const TString& key, IOutputStream& out) {
  11. const TString name = "name" + ToString(CityHash64(key.data(), key.size()));
  12. out << "static const unsigned char " << name << "[] = {";
  13. const TString c = Compress(data);
  14. char buf[16];
  15. for (size_t i = 0; i < c.size(); ++i) {
  16. if ((i % 10) == 0) {
  17. out << "\n ";
  18. }
  19. const char ch = c[i];
  20. out << "0x" << TStringBuf(buf, HexEncode(&ch, 1, buf)) << ", ";
  21. }
  22. out << "\n};\n\nstatic const NResource::TRegHelper REG_" << name << "(\"" << key << "\", TStringBuf((const char*)" << name << ", sizeof(" << name << ")));\n";
  23. }
  24. int main(int argc, char** argv) {
  25. if ((argc < 4) || (argc % 2)) {
  26. Cerr << "usage: " << argv[0] << " outfile [infile path]+ [- key=value]+" << Endl;
  27. return 1;
  28. }
  29. TFixedBufferFileOutput out(argv[1]);
  30. argv = argv + 2;
  31. out << "#include <library/cpp/resource/registry.h>\n\n";
  32. while (*argv) {
  33. if ("-"sv == *argv) {
  34. TVector<TString> items = StringSplitter(TString(*(argv + 1))).Split('=').Limit(2).ToList<TString>();
  35. GenOne(TString(items[1]), TString(items[0]), out);
  36. } else {
  37. const char* key = *(argv + 1);
  38. if (*key == '-') {
  39. ++key;
  40. }
  41. GenOne(TUnbufferedFileInput(*argv).ReadAll(), key, out);
  42. }
  43. argv += 2;
  44. }
  45. }