aboutsummaryrefslogtreecommitdiffstats
path: root/tools/rescompiler/main.cpp
blob: 98a5b9413bc2bca079f3849bc94f10710e0bf8dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <library/cpp/resource/registry.h>

#include <util/stream/output.h>
#include <util/stream/file.h>
#include <util/digest/city.h>
#include <util/string/cast.h>
#include <util/string/hex.h>
#include <util/string/vector.h>
#include <util/string/split.h>

using namespace NResource;

static inline void GenOne(const TString& data, const TString& key, IOutputStream& out) {
    const TString name = "name" + ToString(CityHash64(key.data(), key.size()));

    out << "static const unsigned char " << name << "[] = {";

    const TString c = Compress(data);
    char buf[16];

    for (size_t i = 0; i < c.size(); ++i) {
        if ((i % 10) == 0) {
            out << "\n    ";
        }

        const char ch = c[i];

        out << "0x" << TStringBuf(buf, HexEncode(&ch, 1, buf)) << ", ";
    }

    out << "\n};\n\nstatic const NResource::TRegHelper REG_" << name << "(\"" << key << "\", TStringBuf((const char*)" << name << ", sizeof(" << name << ")));\n";
}

int main(int argc, char** argv) {
    if ((argc < 4) || (argc % 2)) {
        Cerr << "usage: " << argv[0] << " outfile [infile path]+ [- key=value]+" << Endl;

        return 1;
    }

    TFixedBufferFileOutput out(argv[1]);

    argv = argv + 2;

    out << "#include <library/cpp/resource/registry.h>\n\n";

    while (*argv) {
        if ("-"sv == *argv) {
            TVector<TString> items = StringSplitter(TString(*(argv + 1))).Split('=').Limit(2).ToList<TString>();
            GenOne(TString(items[1]), TString(items[0]), out);
        } else {
            const char* key = *(argv + 1);
            if (*key == '-') {
                ++key;
            }
            GenOne(TUnbufferedFileInput(*argv).ReadAll(), key, out);
        }
        argv += 2;
    }
}