Mercurial > hg > orthanc
annotate Resources/EmbedResources.py @ 2686:fb2d89acc2d1 jobs
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 28 Jun 2018 12:15:21 +0200 |
parents | 878b59270859 |
children | e0d600882ed2 |
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 |
2447
878b59270859
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
6 # Copyright (C) 2017-2018 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' |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
43 OUT_OF_RANGE_EXCEPTION = 'OrthancException(ErrorCode_ParameterOutOfRange)' |
1584
39ecd34fb1c7
fix standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1469
diff
changeset
|
44 INEXISTENT_PATH_EXCEPTION = 'OrthancException(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): | |
104 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
|
105 |
6babe0c7aa27
Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
106 # 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
|
107 # 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
|
108 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
|
109 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
|
110 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
|
111 |
0 | 112 for f in files: |
113 if f.find('~') == -1: # Ignore Emacs backup files | |
114 if base == '.': | |
129 | 115 r = f |
0 | 116 else: |
129 | 117 r = os.path.join(base, f) |
0 | 118 |
129 | 119 CheckNoUpcase(r) |
20 | 120 r = '/' + r.replace('\\', '/') |
0 | 121 if r in content: |
122 raise Exception("Twice the same filename (check case): " + r) | |
123 | |
124 content[r] = { | |
125 'Filename' : os.path.join(root, f), | |
126 'Index' : counter | |
127 } | |
128 counter += 1 | |
129 | |
130 resources[resourceName] = { | |
131 'Type' : 'Directory', | |
132 'Files' : content | |
133 } | |
134 | |
135 elif os.path.isfile(pathName): | |
136 resources[resourceName] = { | |
137 'Type' : 'File', | |
138 'Index' : counter, | |
139 'Filename' : pathName | |
140 } | |
141 counter += 1 | |
142 | |
143 else: | |
144 raise Exception("Not a regular file, nor a directory: " + pathName) | |
145 | |
146 i += 2 | |
147 | |
148 #pprint.pprint(resources) | |
149 | |
150 | |
151 ##################################################################### | |
152 ## Write .h header | |
153 ##################################################################### | |
154 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
155 header = open(TARGET_BASE_FILENAME + '.h', 'w') |
0 | 156 |
157 header.write(""" | |
158 #pragma once | |
159 | |
160 #include <string> | |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
161 #include <list> |
0 | 162 |
2174
8453d3a26421
disable warning with MSVC
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
163 #if defined(_MSC_VER) |
8453d3a26421
disable warning with MSVC
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
164 # 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
|
165 #endif |
8453d3a26421
disable warning with MSVC
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
166 |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
167 namespace %s |
0 | 168 { |
169 namespace EmbeddedResources | |
170 { | |
171 enum FileResourceId | |
172 { | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
173 """ % NAMESPACE) |
0 | 174 |
175 isFirst = True | |
176 for name in resources: | |
177 if resources[name]['Type'] == 'File': | |
178 if isFirst: | |
179 isFirst = False | |
180 else: | |
181 header.write(',\n') | |
182 header.write(' %s' % name) | |
183 | |
184 header.write(""" | |
185 }; | |
186 | |
187 enum DirectoryResourceId | |
188 { | |
189 """) | |
190 | |
191 isFirst = True | |
192 for name in resources: | |
193 if resources[name]['Type'] == 'Directory': | |
194 if isFirst: | |
195 isFirst = False | |
196 else: | |
197 header.write(',\n') | |
198 header.write(' %s' % name) | |
199 | |
200 header.write(""" | |
201 }; | |
202 | |
203 const void* GetFileResourceBuffer(FileResourceId id); | |
204 size_t GetFileResourceSize(FileResourceId id); | |
205 void GetFileResource(std::string& result, FileResourceId id); | |
206 | |
207 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path); | |
208 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path); | |
209 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
|
210 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
211 void ListResources(std::list<std::string>& result, DirectoryResourceId id); |
0 | 212 } |
213 } | |
214 """) | |
215 header.close() | |
216 | |
217 | |
218 | |
219 ##################################################################### | |
220 ## Write the resource content in the .cpp source | |
221 ##################################################################### | |
222 | |
55 | 223 PYTHON_MAJOR_VERSION = sys.version_info[0] |
224 | |
0 | 225 def WriteResource(cpp, item): |
226 cpp.write(' static const uint8_t resource%dBuffer[] = {' % item['Index']) | |
227 | |
228 f = open(item['Filename'], "rb") | |
229 content = f.read() | |
230 f.close() | |
231 | |
232 # http://stackoverflow.com/a/1035360 | |
233 pos = 0 | |
234 for b in content: | |
55 | 235 if PYTHON_MAJOR_VERSION == 2: |
236 c = ord(b[0]) | |
237 else: | |
238 c = b | |
239 | |
0 | 240 if pos > 0: |
241 cpp.write(', ') | |
242 | |
243 if (pos % 16) == 0: | |
244 cpp.write('\n ') | |
245 | |
55 | 246 if c < 0: |
0 | 247 raise Exception("Internal error") |
248 | |
55 | 249 cpp.write("0x%02x" % c) |
0 | 250 pos += 1 |
251 | |
1377
601d34afdab9
patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
252 # 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
|
253 if pos == 0: |
601d34afdab9
patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
254 cpp.write(' 0') |
601d34afdab9
patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
255 |
0 | 256 cpp.write(' };\n') |
257 cpp.write(' static const size_t resource%dSize = %d;\n' % (item['Index'], pos)) | |
258 | |
259 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
260 cpp = open(TARGET_BASE_FILENAME + '.cpp', 'w') |
0 | 261 |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
262 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
|
263 |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
264 if USE_SYSTEM_EXCEPTION: |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
265 cpp.write('#include <stdexcept>') |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
266 else: |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
267 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
|
268 |
0 | 269 cpp.write(""" |
270 #include <stdint.h> | |
271 #include <string.h> | |
272 | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
273 namespace %s |
0 | 274 { |
275 namespace EmbeddedResources | |
276 { | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
277 """ % NAMESPACE) |
0 | 278 |
279 | |
280 for name in resources: | |
281 if resources[name]['Type'] == 'File': | |
282 WriteResource(cpp, resources[name]) | |
283 else: | |
284 for f in resources[name]['Files']: | |
285 WriteResource(cpp, resources[name]['Files'][f]) | |
286 | |
287 | |
288 | |
289 ##################################################################### | |
290 ## Write the accessors to the file resources in .cpp | |
291 ##################################################################### | |
292 | |
293 cpp.write(""" | |
294 const void* GetFileResourceBuffer(FileResourceId id) | |
295 { | |
296 switch (id) | |
297 { | |
298 """) | |
299 for name in resources: | |
300 if resources[name]['Type'] == 'File': | |
301 cpp.write(' case %s:\n' % name) | |
302 cpp.write(' return resource%dBuffer;\n' % resources[name]['Index']) | |
303 | |
304 cpp.write(""" | |
305 default: | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
306 throw %s; |
0 | 307 } |
308 } | |
309 | |
310 size_t GetFileResourceSize(FileResourceId id) | |
311 { | |
312 switch (id) | |
313 { | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
314 """ % OUT_OF_RANGE_EXCEPTION) |
0 | 315 |
316 for name in resources: | |
317 if resources[name]['Type'] == 'File': | |
318 cpp.write(' case %s:\n' % name) | |
319 cpp.write(' return resource%dSize;\n' % resources[name]['Index']) | |
320 | |
321 cpp.write(""" | |
322 default: | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
323 throw %s; |
0 | 324 } |
325 } | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
326 """ % OUT_OF_RANGE_EXCEPTION) |
0 | 327 |
328 | |
329 | |
330 ##################################################################### | |
331 ## Write the accessors to the directory resources in .cpp | |
332 ##################################################################### | |
333 | |
334 cpp.write(""" | |
335 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path) | |
336 { | |
337 switch (id) | |
338 { | |
339 """) | |
340 | |
341 for name in resources: | |
342 if resources[name]['Type'] == 'Directory': | |
343 cpp.write(' case %s:\n' % name) | |
344 isFirst = True | |
345 for path in resources[name]['Files']: | |
346 cpp.write(' if (!strcmp(path, "%s"))\n' % path) | |
347 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
|
348 cpp.write(' throw %s;\n\n' % INEXISTENT_PATH_EXCEPTION) |
0 | 349 |
350 cpp.write(""" default: | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
351 throw %s; |
0 | 352 } |
353 } | |
354 | |
355 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path) | |
356 { | |
357 switch (id) | |
358 { | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
359 """ % OUT_OF_RANGE_EXCEPTION) |
0 | 360 |
361 for name in resources: | |
362 if resources[name]['Type'] == 'Directory': | |
363 cpp.write(' case %s:\n' % name) | |
364 isFirst = True | |
365 for path in resources[name]['Files']: | |
366 cpp.write(' if (!strcmp(path, "%s"))\n' % path) | |
367 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
|
368 cpp.write(' throw %s;\n\n' % INEXISTENT_PATH_EXCEPTION) |
0 | 369 |
370 cpp.write(""" default: | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
371 throw %s; |
0 | 372 } |
373 } | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
374 """ % OUT_OF_RANGE_EXCEPTION) |
0 | 375 |
376 | |
377 | |
378 | |
379 ##################################################################### | |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
380 ## List the resources in a directory |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
381 ##################################################################### |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
382 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
383 cpp.write(""" |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
384 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
|
385 { |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
386 result.clear(); |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
387 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
388 switch (id) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
389 { |
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 for name in resources: |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
393 if resources[name]['Type'] == 'Directory': |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
394 cpp.write(' case %s:\n' % name) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
395 for path in sorted(resources[name]['Files']): |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
396 cpp.write(' result.push_back("%s");\n' % path) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
397 cpp.write(' break;\n\n') |
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 cpp.write(""" default: |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
400 throw %s; |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
401 } |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
402 } |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
403 """ % OUT_OF_RANGE_EXCEPTION) |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
404 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
405 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
406 |
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 ##################################################################### |
0 | 409 ## Write the convenience wrappers in .cpp |
410 ##################################################################### | |
411 | |
412 cpp.write(""" | |
413 void GetFileResource(std::string& result, FileResourceId id) | |
414 { | |
415 size_t size = GetFileResourceSize(id); | |
416 result.resize(size); | |
417 if (size > 0) | |
418 memcpy(&result[0], GetFileResourceBuffer(id), size); | |
419 } | |
420 | |
421 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path) | |
422 { | |
423 size_t size = GetDirectoryResourceSize(id, path); | |
424 result.resize(size); | |
425 if (size > 0) | |
426 memcpy(&result[0], GetDirectoryResourceBuffer(id, path), size); | |
427 } | |
428 } | |
429 } | |
430 """) | |
431 cpp.close() |