Mercurial > hg > orthanc
annotate Resources/EmbedResources.py @ 1499:940549bb944a
notes
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 07 Aug 2015 16:49:53 +0200 |
parents | bf9b8bfea0e7 |
children | 39ecd34fb1c7 |
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)' |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
43 NAMESPACE = 'Orthanc' |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
44 |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
45 ARGS = [] |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
46 for i in range(len(sys.argv)): |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
47 if not sys.argv[i].startswith('--'): |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
48 ARGS.append(sys.argv[i]) |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
49 elif sys.argv[i].lower() == '--no-upcase-check': |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
50 UPCASE_CHECK = False |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
51 elif sys.argv[i].lower() == '--system-exception': |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
52 USE_SYSTEM_EXCEPTION = True |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
53 EXCEPTION_CLASS = '::std::runtime_error' |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
54 OUT_OF_RANGE_EXCEPTION = '%s("Parameter out of range")' % EXCEPTION_CLASS |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
55 elif sys.argv[i].startswith('--namespace='): |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
56 NAMESPACE = sys.argv[i][sys.argv[i].find('=') + 1 : ] |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
57 |
326 | 58 if len(ARGS) < 2 or len(ARGS) % 2 != 0: |
59 print ('Usage:') | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
60 print ('python %s [--no-upcase-check] [--system-exception] [--namespace=<Namespace>] <TargetBaseFilename> [ <Name> <Source> ]*' % sys.argv[0]) |
326 | 61 exit(-1) |
62 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
63 TARGET_BASE_FILENAME = ARGS[1] |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
64 SOURCES = ARGS[2:] |
0 | 65 |
66 try: | |
67 # Make sure the destination directory exists | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
68 os.makedirs(os.path.normpath(os.path.join(TARGET_BASE_FILENAME, '..'))) |
0 | 69 except: |
70 pass | |
71 | |
72 | |
73 ##################################################################### | |
74 ## Read each resource file | |
75 ##################################################################### | |
76 | |
129 | 77 def CheckNoUpcase(s): |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
78 global UPCASE_CHECK |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
79 if (UPCASE_CHECK and |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
80 re.search('[A-Z]', s) != None): |
129 | 81 raise Exception("Path in a directory with an upcase letter: %s" % s) |
82 | |
0 | 83 resources = {} |
84 | |
85 counter = 0 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
86 i = 0 |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
87 while i < len(SOURCES): |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
88 resourceName = SOURCES[i].upper() |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
89 pathName = SOURCES[i + 1] |
0 | 90 |
91 if not os.path.exists(pathName): | |
92 raise Exception("Non existing path: %s" % pathName) | |
93 | |
94 if resourceName in resources: | |
95 raise Exception("Twice the same resource: " + resourceName) | |
96 | |
97 if os.path.isdir(pathName): | |
98 # The resource is a directory: Recursively explore its files | |
99 content = {} | |
100 for root, dirs, files in os.walk(pathName): | |
101 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
|
102 |
6babe0c7aa27
Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
103 # 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
|
104 # 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
|
105 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
|
106 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
|
107 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
|
108 |
0 | 109 for f in files: |
110 if f.find('~') == -1: # Ignore Emacs backup files | |
111 if base == '.': | |
129 | 112 r = f |
0 | 113 else: |
129 | 114 r = os.path.join(base, f) |
0 | 115 |
129 | 116 CheckNoUpcase(r) |
20 | 117 r = '/' + r.replace('\\', '/') |
0 | 118 if r in content: |
119 raise Exception("Twice the same filename (check case): " + r) | |
120 | |
121 content[r] = { | |
122 'Filename' : os.path.join(root, f), | |
123 'Index' : counter | |
124 } | |
125 counter += 1 | |
126 | |
127 resources[resourceName] = { | |
128 'Type' : 'Directory', | |
129 'Files' : content | |
130 } | |
131 | |
132 elif os.path.isfile(pathName): | |
133 resources[resourceName] = { | |
134 'Type' : 'File', | |
135 'Index' : counter, | |
136 'Filename' : pathName | |
137 } | |
138 counter += 1 | |
139 | |
140 else: | |
141 raise Exception("Not a regular file, nor a directory: " + pathName) | |
142 | |
143 i += 2 | |
144 | |
145 #pprint.pprint(resources) | |
146 | |
147 | |
148 ##################################################################### | |
149 ## Write .h header | |
150 ##################################################################### | |
151 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
152 header = open(TARGET_BASE_FILENAME + '.h', 'w') |
0 | 153 |
154 header.write(""" | |
155 #pragma once | |
156 | |
157 #include <string> | |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
158 #include <list> |
0 | 159 |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
160 namespace %s |
0 | 161 { |
162 namespace EmbeddedResources | |
163 { | |
164 enum FileResourceId | |
165 { | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
166 """ % NAMESPACE) |
0 | 167 |
168 isFirst = True | |
169 for name in resources: | |
170 if resources[name]['Type'] == 'File': | |
171 if isFirst: | |
172 isFirst = False | |
173 else: | |
174 header.write(',\n') | |
175 header.write(' %s' % name) | |
176 | |
177 header.write(""" | |
178 }; | |
179 | |
180 enum DirectoryResourceId | |
181 { | |
182 """) | |
183 | |
184 isFirst = True | |
185 for name in resources: | |
186 if resources[name]['Type'] == 'Directory': | |
187 if isFirst: | |
188 isFirst = False | |
189 else: | |
190 header.write(',\n') | |
191 header.write(' %s' % name) | |
192 | |
193 header.write(""" | |
194 }; | |
195 | |
196 const void* GetFileResourceBuffer(FileResourceId id); | |
197 size_t GetFileResourceSize(FileResourceId id); | |
198 void GetFileResource(std::string& result, FileResourceId id); | |
199 | |
200 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path); | |
201 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path); | |
202 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
|
203 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
204 void ListResources(std::list<std::string>& result, DirectoryResourceId id); |
0 | 205 } |
206 } | |
207 """) | |
208 header.close() | |
209 | |
210 | |
211 | |
212 ##################################################################### | |
213 ## Write the resource content in the .cpp source | |
214 ##################################################################### | |
215 | |
55 | 216 PYTHON_MAJOR_VERSION = sys.version_info[0] |
217 | |
0 | 218 def WriteResource(cpp, item): |
219 cpp.write(' static const uint8_t resource%dBuffer[] = {' % item['Index']) | |
220 | |
221 f = open(item['Filename'], "rb") | |
222 content = f.read() | |
223 f.close() | |
224 | |
225 # http://stackoverflow.com/a/1035360 | |
226 pos = 0 | |
227 for b in content: | |
55 | 228 if PYTHON_MAJOR_VERSION == 2: |
229 c = ord(b[0]) | |
230 else: | |
231 c = b | |
232 | |
0 | 233 if pos > 0: |
234 cpp.write(', ') | |
235 | |
236 if (pos % 16) == 0: | |
237 cpp.write('\n ') | |
238 | |
55 | 239 if c < 0: |
0 | 240 raise Exception("Internal error") |
241 | |
55 | 242 cpp.write("0x%02x" % c) |
0 | 243 pos += 1 |
244 | |
1377
601d34afdab9
patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
245 # 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
|
246 if pos == 0: |
601d34afdab9
patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
247 cpp.write(' 0') |
601d34afdab9
patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
248 |
0 | 249 cpp.write(' };\n') |
250 cpp.write(' static const size_t resource%dSize = %d;\n' % (item['Index'], pos)) | |
251 | |
252 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
253 cpp = open(TARGET_BASE_FILENAME + '.cpp', 'w') |
0 | 254 |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
255 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
|
256 |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
257 if USE_SYSTEM_EXCEPTION: |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
258 cpp.write('#include <stdexcept>') |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
259 else: |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
260 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
|
261 |
0 | 262 cpp.write(""" |
263 #include <stdint.h> | |
264 #include <string.h> | |
265 | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
266 namespace %s |
0 | 267 { |
268 namespace EmbeddedResources | |
269 { | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
270 """ % NAMESPACE) |
0 | 271 |
272 | |
273 for name in resources: | |
274 if resources[name]['Type'] == 'File': | |
275 WriteResource(cpp, resources[name]) | |
276 else: | |
277 for f in resources[name]['Files']: | |
278 WriteResource(cpp, resources[name]['Files'][f]) | |
279 | |
280 | |
281 | |
282 ##################################################################### | |
283 ## Write the accessors to the file resources in .cpp | |
284 ##################################################################### | |
285 | |
286 cpp.write(""" | |
287 const void* GetFileResourceBuffer(FileResourceId id) | |
288 { | |
289 switch (id) | |
290 { | |
291 """) | |
292 for name in resources: | |
293 if resources[name]['Type'] == 'File': | |
294 cpp.write(' case %s:\n' % name) | |
295 cpp.write(' return resource%dBuffer;\n' % resources[name]['Index']) | |
296 | |
297 cpp.write(""" | |
298 default: | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
299 throw %s; |
0 | 300 } |
301 } | |
302 | |
303 size_t GetFileResourceSize(FileResourceId id) | |
304 { | |
305 switch (id) | |
306 { | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
307 """ % OUT_OF_RANGE_EXCEPTION) |
0 | 308 |
309 for name in resources: | |
310 if resources[name]['Type'] == 'File': | |
311 cpp.write(' case %s:\n' % name) | |
312 cpp.write(' return resource%dSize;\n' % resources[name]['Index']) | |
313 | |
314 cpp.write(""" | |
315 default: | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
316 throw %s; |
0 | 317 } |
318 } | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
319 """ % OUT_OF_RANGE_EXCEPTION) |
0 | 320 |
321 | |
322 | |
323 ##################################################################### | |
324 ## Write the accessors to the directory resources in .cpp | |
325 ##################################################################### | |
326 | |
327 cpp.write(""" | |
328 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path) | |
329 { | |
330 switch (id) | |
331 { | |
332 """) | |
333 | |
334 for name in resources: | |
335 if resources[name]['Type'] == 'Directory': | |
336 cpp.write(' case %s:\n' % name) | |
337 isFirst = True | |
338 for path in resources[name]['Files']: | |
339 cpp.write(' if (!strcmp(path, "%s"))\n' % path) | |
340 cpp.write(' return resource%dBuffer;\n' % resources[name]['Files'][path]['Index']) | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
341 cpp.write(' throw %s("Unknown path in a directory resource");\n\n' % EXCEPTION_CLASS) |
0 | 342 |
343 cpp.write(""" default: | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
344 throw %s; |
0 | 345 } |
346 } | |
347 | |
348 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path) | |
349 { | |
350 switch (id) | |
351 { | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
352 """ % OUT_OF_RANGE_EXCEPTION) |
0 | 353 |
354 for name in resources: | |
355 if resources[name]['Type'] == 'Directory': | |
356 cpp.write(' case %s:\n' % name) | |
357 isFirst = True | |
358 for path in resources[name]['Files']: | |
359 cpp.write(' if (!strcmp(path, "%s"))\n' % path) | |
360 cpp.write(' return resource%dSize;\n' % resources[name]['Files'][path]['Index']) | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
361 cpp.write(' throw %s("Unknown path in a directory resource");\n\n' % EXCEPTION_CLASS) |
0 | 362 |
363 cpp.write(""" default: | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
364 throw %s; |
0 | 365 } |
366 } | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
367 """ % OUT_OF_RANGE_EXCEPTION) |
0 | 368 |
369 | |
370 | |
371 | |
372 ##################################################################### | |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
373 ## List the resources in a directory |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
374 ##################################################################### |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
375 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
376 cpp.write(""" |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
377 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
|
378 { |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
379 result.clear(); |
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 switch (id) |
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 """) |
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 for name in resources: |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
386 if resources[name]['Type'] == 'Directory': |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
387 cpp.write(' case %s:\n' % name) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
388 for path in sorted(resources[name]['Files']): |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
389 cpp.write(' result.push_back("%s");\n' % path) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
390 cpp.write(' break;\n\n') |
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(""" default: |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
393 throw %s; |
323
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 } |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
396 """ % OUT_OF_RANGE_EXCEPTION) |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
397 |
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 ##################################################################### |
0 | 402 ## Write the convenience wrappers in .cpp |
403 ##################################################################### | |
404 | |
405 cpp.write(""" | |
406 void GetFileResource(std::string& result, FileResourceId id) | |
407 { | |
408 size_t size = GetFileResourceSize(id); | |
409 result.resize(size); | |
410 if (size > 0) | |
411 memcpy(&result[0], GetFileResourceBuffer(id), size); | |
412 } | |
413 | |
414 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path) | |
415 { | |
416 size_t size = GetDirectoryResourceSize(id, path); | |
417 result.resize(size); | |
418 if (size > 0) | |
419 memcpy(&result[0], GetDirectoryResourceBuffer(id, path), size); | |
420 } | |
421 } | |
422 } | |
423 """) | |
424 cpp.close() |