-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathres2c.py
executable file
·121 lines (89 loc) · 3.74 KB
/
res2c.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# DISTRHO Plugin Framework (DPF)
# Copyright (C) 2012-2019 Filipe Coelho <[email protected]>
#
# Permission to use, copy, modify, and/or distribute this software for any purpose with
# or without fee is hereby granted, provided that the above copyright notice and this
# permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
# TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
# NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
# IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import os, sys
# -----------------------------------------------------
def res2c(namespace, filenames):
fdH = open("%s.hpp" % namespace, "w")
fdH.write("/* (Auto-generated binary data file). */\n")
fdH.write("\n")
fdH.write("#ifndef BINARY_%s_HPP\n" % namespace.upper())
fdH.write("#define BINARY_%s_HPP\n" % namespace.upper())
fdH.write("\n")
fdH.write("namespace %s\n" % namespace)
fdH.write("{\n")
fdC = open("%s.cpp" % namespace, "w")
fdC.write("/* (Auto-generated binary data file). */\n")
fdC.write("\n")
fdC.write("#include \"%s.hpp\"\n" % namespace)
fdC.write("\n")
tempIndex = 1
for filename in filenames:
shortFilename = filename.rsplit(os.sep, 1)[-1].split(".", 1)[0]
shortFilename = shortFilename.replace(" ", "_").replace("-", "_").replace("@","_")
resData = open(filename, 'rb').read()
print("Generating data for \"%s\"" % (filename))
fdH.write(" extern const char* %sData;\n" % shortFilename)
fdH.write(" const unsigned int %sDataSize = %i;\n" % (shortFilename, len(resData)))
if tempIndex != len(filenames):
fdH.write("\n")
fdC.write("static const unsigned char temp_%s_%i[] = {\n" % (shortFilename, tempIndex))
curColumn = 1
fdC.write(" ")
for data in resData:
if curColumn == 0:
fdC.write(" ")
fdC.write(" %3u," % data)
if curColumn > 20:
fdC.write("\n ")
curColumn = 1
else:
curColumn += 1
fdC.write("};\n")
fdC.write("const char* %s::%sData = (const char*)temp_%s_%i;\n" % (namespace, shortFilename, shortFilename, tempIndex))
if tempIndex != len(filenames):
fdC.write("\n")
tempIndex += 1
fdH.write("}\n")
fdH.write("\n")
fdH.write("#endif // BINARY_%s_HPP\n" % namespace.upper())
fdH.write("\n")
fdH.close()
fdC.write("\n")
fdC.close()
# -----------------------------------------------------
if __name__ == '__main__':
if len(sys.argv) not in (3, 4):
print("Usage: %s <namespace> <resource-folder> [output-folder=$CWD]" % sys.argv[0])
quit()
namespace = sys.argv[1].replace("-","_")
resFolder = sys.argv[2]
outFolder = sys.argv[3] if len(sys.argv) == 4 else None
if not os.path.exists(resFolder):
print("Folder '%s' does not exist" % resFolder)
quit()
if outFolder is not None and not os.path.exists(outFolder):
print("Output folder '%s' does not exist" % outFolder)
quit()
# find resource files
resFiles = []
for root, dirs, files in os.walk(resFolder):
for name in files:
resFiles.append(os.path.abspath(os.path.join(root, name)))
resFiles.sort()
if outFolder is not None:
os.chdir(outFolder)
# create code now
res2c(namespace, resFiles)