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