Mercurial > hg > orthanc
annotate Resources/EmbedResources.py @ 3856:dd0fcbf6a791
back to mainline
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 21 Apr 2020 14:45:30 +0200 |
parents | 94f4a18a79cc |
children | 100fbe970762 |
rev | line source |
---|---|
1467
5068de14eef1
Inject version information into Windows binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1377
diff
changeset
|
1 #!/usr/bin/python |
5068de14eef1
Inject version information into Windows binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1377
diff
changeset
|
2 |
143 | 3 # Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1204
diff
changeset
|
5 # Department, University Hospital of Liege, Belgium |
3640
94f4a18a79cc
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3587
diff
changeset
|
6 # Copyright (C) 2017-2020 Osimis S.A., Belgium |
143 | 7 # |
8 # This program is free software: you can redistribute it and/or | |
9 # modify it under the terms of the GNU General Public License as | |
10 # published by the Free Software Foundation, either version 3 of the | |
11 # License, or (at your option) any later version. | |
12 # | |
13 # In addition, as a special exception, the copyright holders of this | |
14 # program give permission to link the code of its release with the | |
15 # OpenSSL project's "OpenSSL" library (or with modified versions of it | |
16 # that use the same license as the "OpenSSL" library), and distribute | |
17 # the linked executables. You must obey the GNU General Public License | |
18 # in all respects for all of the code used other than "OpenSSL". If you | |
19 # modify file(s) with this exception, you may extend this exception to | |
20 # your version of the file(s), but you are not obligated to do so. If | |
21 # you do not wish to do so, delete this exception statement from your | |
22 # version. If you delete this exception statement from all source files | |
23 # in the program, then also delete it here. | |
24 # | |
25 # This program is distributed in the hope that it will be useful, but | |
26 # WITHOUT ANY WARRANTY; without even the implied warranty of | |
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
28 # General Public License for more details. | |
29 # | |
30 # You should have received a copy of the GNU General Public License | |
31 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
32 | |
33 | |
0 | 34 import sys |
35 import os | |
36 import os.path | |
37 import pprint | |
129 | 38 import re |
0 | 39 |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
40 UPCASE_CHECK = True |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
41 USE_SYSTEM_EXCEPTION = False |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
42 EXCEPTION_CLASS = 'OrthancException' |
2690 | 43 OUT_OF_RANGE_EXCEPTION = '::Orthanc::OrthancException(::Orthanc::ErrorCode_ParameterOutOfRange)' |
44 INEXISTENT_PATH_EXCEPTION = '::Orthanc::OrthancException(::Orthanc::ErrorCode_InexistentItem)' | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
45 NAMESPACE = 'Orthanc' |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
46 |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
47 ARGS = [] |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
48 for i in range(len(sys.argv)): |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
49 if not sys.argv[i].startswith('--'): |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
50 ARGS.append(sys.argv[i]) |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
51 elif sys.argv[i].lower() == '--no-upcase-check': |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
52 UPCASE_CHECK = False |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
53 elif sys.argv[i].lower() == '--system-exception': |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
54 USE_SYSTEM_EXCEPTION = True |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
55 EXCEPTION_CLASS = '::std::runtime_error' |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
56 OUT_OF_RANGE_EXCEPTION = '%s("Parameter out of range")' % EXCEPTION_CLASS |
1584
39ecd34fb1c7
fix standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1469
diff
changeset
|
57 INEXISTENT_PATH_EXCEPTION = '%s("Unknown path in a directory resource")' % EXCEPTION_CLASS |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
58 elif sys.argv[i].startswith('--namespace='): |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
59 NAMESPACE = sys.argv[i][sys.argv[i].find('=') + 1 : ] |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
60 |
326 | 61 if len(ARGS) < 2 or len(ARGS) % 2 != 0: |
62 print ('Usage:') | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
63 print ('python %s [--no-upcase-check] [--system-exception] [--namespace=<Namespace>] <TargetBaseFilename> [ <Name> <Source> ]*' % sys.argv[0]) |
326 | 64 exit(-1) |
65 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
66 TARGET_BASE_FILENAME = ARGS[1] |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
67 SOURCES = ARGS[2:] |
0 | 68 |
69 try: | |
70 # Make sure the destination directory exists | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
71 os.makedirs(os.path.normpath(os.path.join(TARGET_BASE_FILENAME, '..'))) |
0 | 72 except: |
73 pass | |
74 | |
75 | |
76 ##################################################################### | |
77 ## Read each resource file | |
78 ##################################################################### | |
79 | |
129 | 80 def CheckNoUpcase(s): |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
81 global UPCASE_CHECK |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
82 if (UPCASE_CHECK and |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
83 re.search('[A-Z]', s) != None): |
129 | 84 raise Exception("Path in a directory with an upcase letter: %s" % s) |
85 | |
0 | 86 resources = {} |
87 | |
88 counter = 0 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
89 i = 0 |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
90 while i < len(SOURCES): |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
91 resourceName = SOURCES[i].upper() |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
92 pathName = SOURCES[i + 1] |
0 | 93 |
94 if not os.path.exists(pathName): | |
95 raise Exception("Non existing path: %s" % pathName) | |
96 | |
97 if resourceName in resources: | |
98 raise Exception("Twice the same resource: " + resourceName) | |
99 | |
100 if os.path.isdir(pathName): | |
101 # The resource is a directory: Recursively explore its files | |
102 content = {} | |
103 for root, dirs, files in os.walk(pathName): | |
3587
76b3228f99b0
Sort file lists
Bernhard M. Wiedemann <bernhard+hgcommit@lsmod.de>
parents:
3354
diff
changeset
|
104 dirs.sort() |
76b3228f99b0
Sort file lists
Bernhard M. Wiedemann <bernhard+hgcommit@lsmod.de>
parents:
3354
diff
changeset
|
105 files.sort() |
0 | 106 base = os.path.relpath(root, pathName) |
1204
6babe0c7aa27
Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
107 |
6babe0c7aa27
Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
108 # Fix issue #24 (Build fails on OSX when directory has .DS_Store files): |
6babe0c7aa27
Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
109 # Ignore folders whose name starts with a dot (".") |
6babe0c7aa27
Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
110 if base.find('/.') != -1: |
6babe0c7aa27
Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
111 print('Ignoring folder: %s' % root) |
6babe0c7aa27
Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
112 continue |
6babe0c7aa27
Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
113 |
0 | 114 for f in files: |
115 if f.find('~') == -1: # Ignore Emacs backup files | |
116 if base == '.': | |
129 | 117 r = f |
0 | 118 else: |
129 | 119 r = os.path.join(base, f) |
0 | 120 |
129 | 121 CheckNoUpcase(r) |
20 | 122 r = '/' + r.replace('\\', '/') |
0 | 123 if r in content: |
124 raise Exception("Twice the same filename (check case): " + r) | |
125 | |
126 content[r] = { | |
127 'Filename' : os.path.join(root, f), | |
128 'Index' : counter | |
129 } | |
130 counter += 1 | |
131 | |
132 resources[resourceName] = { | |
133 'Type' : 'Directory', | |
134 'Files' : content | |
135 } | |
136 | |
137 elif os.path.isfile(pathName): | |
138 resources[resourceName] = { | |
139 'Type' : 'File', | |
140 'Index' : counter, | |
141 'Filename' : pathName | |
142 } | |
143 counter += 1 | |
144 | |
145 else: | |
146 raise Exception("Not a regular file, nor a directory: " + pathName) | |
147 | |
148 i += 2 | |
149 | |
150 #pprint.pprint(resources) | |
151 | |
152 | |
153 ##################################################################### | |
154 ## Write .h header | |
155 ##################################################################### | |
156 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
157 header = open(TARGET_BASE_FILENAME + '.h', 'w') |
0 | 158 |
159 header.write(""" | |
160 #pragma once | |
161 | |
162 #include <string> | |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
163 #include <list> |
0 | 164 |
2174
8453d3a26421
disable warning with MSVC
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
165 #if defined(_MSC_VER) |
8453d3a26421
disable warning with MSVC
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
166 # pragma warning(disable: 4065) // "Switch statement contains 'default' but no 'case' labels" |
8453d3a26421
disable warning with MSVC
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
167 #endif |
8453d3a26421
disable warning with MSVC
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
168 |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
169 namespace %s |
0 | 170 { |
171 namespace EmbeddedResources | |
172 { | |
173 enum FileResourceId | |
174 { | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
175 """ % NAMESPACE) |
0 | 176 |
177 isFirst = True | |
178 for name in resources: | |
179 if resources[name]['Type'] == 'File': | |
180 if isFirst: | |
181 isFirst = False | |
182 else: | |
183 header.write(',\n') | |
184 header.write(' %s' % name) | |
185 | |
186 header.write(""" | |
187 }; | |
188 | |
189 enum DirectoryResourceId | |
190 { | |
191 """) | |
192 | |
193 isFirst = True | |
194 for name in resources: | |
195 if resources[name]['Type'] == 'Directory': | |
196 if isFirst: | |
197 isFirst = False | |
198 else: | |
199 header.write(',\n') | |
200 header.write(' %s' % name) | |
201 | |
202 header.write(""" | |
203 }; | |
204 | |
205 const void* GetFileResourceBuffer(FileResourceId id); | |
206 size_t GetFileResourceSize(FileResourceId id); | |
207 void GetFileResource(std::string& result, FileResourceId id); | |
208 | |
209 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path); | |
210 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path); | |
211 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path); | |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
212 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
213 void ListResources(std::list<std::string>& result, DirectoryResourceId id); |
0 | 214 } |
215 } | |
216 """) | |
217 header.close() | |
218 | |
219 | |
220 | |
221 ##################################################################### | |
222 ## Write the resource content in the .cpp source | |
223 ##################################################################### | |
224 | |
55 | 225 PYTHON_MAJOR_VERSION = sys.version_info[0] |
226 | |
0 | 227 def WriteResource(cpp, item): |
228 cpp.write(' static const uint8_t resource%dBuffer[] = {' % item['Index']) | |
229 | |
230 f = open(item['Filename'], "rb") | |
231 content = f.read() | |
232 f.close() | |
233 | |
234 # http://stackoverflow.com/a/1035360 | |
235 pos = 0 | |
3354
e60e194531e5
optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
236 buffer = [] # instead of appending a few bytes at a time to the cpp file, |
e60e194531e5
optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
237 # we first append each chunk to a list, join it and write it |
e60e194531e5
optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
238 # to the file. We've measured that it was 2-3 times faster in python3. |
e60e194531e5
optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
239 # Note that speed is important since if generation is too slow, |
e60e194531e5
optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
240 # cmake might try to compile the EmbeddedResources.cpp file while it is |
e60e194531e5
optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
241 # still being generated ! |
0 | 242 for b in content: |
55 | 243 if PYTHON_MAJOR_VERSION == 2: |
244 c = ord(b[0]) | |
245 else: | |
246 c = b | |
247 | |
0 | 248 if pos > 0: |
3354
e60e194531e5
optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
249 buffer.append(",") |
0 | 250 |
251 if (pos % 16) == 0: | |
3354
e60e194531e5
optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
252 buffer.append("\n") |
0 | 253 |
55 | 254 if c < 0: |
0 | 255 raise Exception("Internal error") |
256 | |
3354
e60e194531e5
optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
257 buffer.append("0x%02x" % c) |
0 | 258 pos += 1 |
259 | |
3354
e60e194531e5
optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
260 cpp.write("".join(buffer)) |
1377
601d34afdab9
patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
261 # Zero-size array are disallowed, so we put one single void character in it. |
601d34afdab9
patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
262 if pos == 0: |
601d34afdab9
patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
263 cpp.write(' 0') |
601d34afdab9
patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
264 |
0 | 265 cpp.write(' };\n') |
266 cpp.write(' static const size_t resource%dSize = %d;\n' % (item['Index'], pos)) | |
267 | |
268 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
269 cpp = open(TARGET_BASE_FILENAME + '.cpp', 'w') |
0 | 270 |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
271 cpp.write('#include "%s.h"\n' % os.path.basename(TARGET_BASE_FILENAME)) |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
272 |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
273 if USE_SYSTEM_EXCEPTION: |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
274 cpp.write('#include <stdexcept>') |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
275 else: |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
276 cpp.write('#include "%s/Core/OrthancException.h"' % os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
277 |
0 | 278 cpp.write(""" |
279 #include <stdint.h> | |
280 #include <string.h> | |
281 | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
282 namespace %s |
0 | 283 { |
284 namespace EmbeddedResources | |
285 { | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
286 """ % NAMESPACE) |
0 | 287 |
288 | |
289 for name in resources: | |
290 if resources[name]['Type'] == 'File': | |
291 WriteResource(cpp, resources[name]) | |
292 else: | |
293 for f in resources[name]['Files']: | |
294 WriteResource(cpp, resources[name]['Files'][f]) | |
295 | |
296 | |
297 | |
298 ##################################################################### | |
299 ## Write the accessors to the file resources in .cpp | |
300 ##################################################################### | |
301 | |
302 cpp.write(""" | |
303 const void* GetFileResourceBuffer(FileResourceId id) | |
304 { | |
305 switch (id) | |
306 { | |
307 """) | |
308 for name in resources: | |
309 if resources[name]['Type'] == 'File': | |
310 cpp.write(' case %s:\n' % name) | |
311 cpp.write(' return resource%dBuffer;\n' % resources[name]['Index']) | |
312 | |
313 cpp.write(""" | |
314 default: | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
315 throw %s; |
0 | 316 } |
317 } | |
318 | |
319 size_t GetFileResourceSize(FileResourceId id) | |
320 { | |
321 switch (id) | |
322 { | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
323 """ % OUT_OF_RANGE_EXCEPTION) |
0 | 324 |
325 for name in resources: | |
326 if resources[name]['Type'] == 'File': | |
327 cpp.write(' case %s:\n' % name) | |
328 cpp.write(' return resource%dSize;\n' % resources[name]['Index']) | |
329 | |
330 cpp.write(""" | |
331 default: | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
332 throw %s; |
0 | 333 } |
334 } | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
335 """ % OUT_OF_RANGE_EXCEPTION) |
0 | 336 |
337 | |
338 | |
339 ##################################################################### | |
340 ## Write the accessors to the directory resources in .cpp | |
341 ##################################################################### | |
342 | |
343 cpp.write(""" | |
344 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path) | |
345 { | |
346 switch (id) | |
347 { | |
348 """) | |
349 | |
350 for name in resources: | |
351 if resources[name]['Type'] == 'Directory': | |
352 cpp.write(' case %s:\n' % name) | |
353 isFirst = True | |
354 for path in resources[name]['Files']: | |
355 cpp.write(' if (!strcmp(path, "%s"))\n' % path) | |
356 cpp.write(' return resource%dBuffer;\n' % resources[name]['Files'][path]['Index']) | |
1584
39ecd34fb1c7
fix standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1469
diff
changeset
|
357 cpp.write(' throw %s;\n\n' % INEXISTENT_PATH_EXCEPTION) |
0 | 358 |
359 cpp.write(""" default: | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
360 throw %s; |
0 | 361 } |
362 } | |
363 | |
364 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path) | |
365 { | |
366 switch (id) | |
367 { | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
368 """ % OUT_OF_RANGE_EXCEPTION) |
0 | 369 |
370 for name in resources: | |
371 if resources[name]['Type'] == 'Directory': | |
372 cpp.write(' case %s:\n' % name) | |
373 isFirst = True | |
374 for path in resources[name]['Files']: | |
375 cpp.write(' if (!strcmp(path, "%s"))\n' % path) | |
376 cpp.write(' return resource%dSize;\n' % resources[name]['Files'][path]['Index']) | |
1584
39ecd34fb1c7
fix standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1469
diff
changeset
|
377 cpp.write(' throw %s;\n\n' % INEXISTENT_PATH_EXCEPTION) |
0 | 378 |
379 cpp.write(""" default: | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
380 throw %s; |
0 | 381 } |
382 } | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
383 """ % OUT_OF_RANGE_EXCEPTION) |
0 | 384 |
385 | |
386 | |
387 | |
388 ##################################################################### | |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
389 ## List the resources in a directory |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
390 ##################################################################### |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
391 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
392 cpp.write(""" |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
393 void ListResources(std::list<std::string>& result, DirectoryResourceId id) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
394 { |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
395 result.clear(); |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
396 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
397 switch (id) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
398 { |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
399 """) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
400 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
401 for name in resources: |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
402 if resources[name]['Type'] == 'Directory': |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
403 cpp.write(' case %s:\n' % name) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
404 for path in sorted(resources[name]['Files']): |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
405 cpp.write(' result.push_back("%s");\n' % path) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
406 cpp.write(' break;\n\n') |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
407 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
408 cpp.write(""" default: |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
409 throw %s; |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
410 } |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
411 } |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
412 """ % OUT_OF_RANGE_EXCEPTION) |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
413 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
414 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
415 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
416 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
417 ##################################################################### |
0 | 418 ## Write the convenience wrappers in .cpp |
419 ##################################################################### | |
420 | |
421 cpp.write(""" | |
422 void GetFileResource(std::string& result, FileResourceId id) | |
423 { | |
424 size_t size = GetFileResourceSize(id); | |
425 result.resize(size); | |
426 if (size > 0) | |
427 memcpy(&result[0], GetFileResourceBuffer(id), size); | |
428 } | |
429 | |
430 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path) | |
431 { | |
432 size_t size = GetDirectoryResourceSize(id, path); | |
433 result.resize(size); | |
434 if (size > 0) | |
435 memcpy(&result[0], GetDirectoryResourceBuffer(id, path), size); | |
436 } | |
437 } | |
438 } | |
439 """) | |
440 cpp.close() |