Mercurial > hg > orthanc
annotate Resources/EmbedResources.py @ 2206:27106f7e3759
more refactoring of the worklist sample
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 08 Dec 2016 16:29:34 +0100 |
parents | 8453d3a26421 |
children | a3a65de1840f |
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 |
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 |
2174
8453d3a26421
disable warning with MSVC
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
162 #if defined(_MSC_VER) |
8453d3a26421
disable warning with MSVC
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
163 # 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
|
164 #endif |
8453d3a26421
disable warning with MSVC
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
165 |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
166 namespace %s |
0 | 167 { |
168 namespace EmbeddedResources | |
169 { | |
170 enum FileResourceId | |
171 { | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
172 """ % NAMESPACE) |
0 | 173 |
174 isFirst = True | |
175 for name in resources: | |
176 if resources[name]['Type'] == 'File': | |
177 if isFirst: | |
178 isFirst = False | |
179 else: | |
180 header.write(',\n') | |
181 header.write(' %s' % name) | |
182 | |
183 header.write(""" | |
184 }; | |
185 | |
186 enum DirectoryResourceId | |
187 { | |
188 """) | |
189 | |
190 isFirst = True | |
191 for name in resources: | |
192 if resources[name]['Type'] == 'Directory': | |
193 if isFirst: | |
194 isFirst = False | |
195 else: | |
196 header.write(',\n') | |
197 header.write(' %s' % name) | |
198 | |
199 header.write(""" | |
200 }; | |
201 | |
202 const void* GetFileResourceBuffer(FileResourceId id); | |
203 size_t GetFileResourceSize(FileResourceId id); | |
204 void GetFileResource(std::string& result, FileResourceId id); | |
205 | |
206 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path); | |
207 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path); | |
208 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
|
209 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
210 void ListResources(std::list<std::string>& result, DirectoryResourceId id); |
0 | 211 } |
212 } | |
213 """) | |
214 header.close() | |
215 | |
216 | |
217 | |
218 ##################################################################### | |
219 ## Write the resource content in the .cpp source | |
220 ##################################################################### | |
221 | |
55 | 222 PYTHON_MAJOR_VERSION = sys.version_info[0] |
223 | |
0 | 224 def WriteResource(cpp, item): |
225 cpp.write(' static const uint8_t resource%dBuffer[] = {' % item['Index']) | |
226 | |
227 f = open(item['Filename'], "rb") | |
228 content = f.read() | |
229 f.close() | |
230 | |
231 # http://stackoverflow.com/a/1035360 | |
232 pos = 0 | |
233 for b in content: | |
55 | 234 if PYTHON_MAJOR_VERSION == 2: |
235 c = ord(b[0]) | |
236 else: | |
237 c = b | |
238 | |
0 | 239 if pos > 0: |
240 cpp.write(', ') | |
241 | |
242 if (pos % 16) == 0: | |
243 cpp.write('\n ') | |
244 | |
55 | 245 if c < 0: |
0 | 246 raise Exception("Internal error") |
247 | |
55 | 248 cpp.write("0x%02x" % c) |
0 | 249 pos += 1 |
250 | |
1377
601d34afdab9
patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
251 # 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
|
252 if pos == 0: |
601d34afdab9
patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
253 cpp.write(' 0') |
601d34afdab9
patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
254 |
0 | 255 cpp.write(' };\n') |
256 cpp.write(' static const size_t resource%dSize = %d;\n' % (item['Index'], pos)) | |
257 | |
258 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
259 cpp = open(TARGET_BASE_FILENAME + '.cpp', 'w') |
0 | 260 |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
261 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
|
262 |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
263 if USE_SYSTEM_EXCEPTION: |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
264 cpp.write('#include <stdexcept>') |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
265 else: |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
266 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
|
267 |
0 | 268 cpp.write(""" |
269 #include <stdint.h> | |
270 #include <string.h> | |
271 | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
272 namespace %s |
0 | 273 { |
274 namespace EmbeddedResources | |
275 { | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
276 """ % NAMESPACE) |
0 | 277 |
278 | |
279 for name in resources: | |
280 if resources[name]['Type'] == 'File': | |
281 WriteResource(cpp, resources[name]) | |
282 else: | |
283 for f in resources[name]['Files']: | |
284 WriteResource(cpp, resources[name]['Files'][f]) | |
285 | |
286 | |
287 | |
288 ##################################################################### | |
289 ## Write the accessors to the file resources in .cpp | |
290 ##################################################################### | |
291 | |
292 cpp.write(""" | |
293 const void* GetFileResourceBuffer(FileResourceId id) | |
294 { | |
295 switch (id) | |
296 { | |
297 """) | |
298 for name in resources: | |
299 if resources[name]['Type'] == 'File': | |
300 cpp.write(' case %s:\n' % name) | |
301 cpp.write(' return resource%dBuffer;\n' % resources[name]['Index']) | |
302 | |
303 cpp.write(""" | |
304 default: | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
305 throw %s; |
0 | 306 } |
307 } | |
308 | |
309 size_t GetFileResourceSize(FileResourceId id) | |
310 { | |
311 switch (id) | |
312 { | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
313 """ % OUT_OF_RANGE_EXCEPTION) |
0 | 314 |
315 for name in resources: | |
316 if resources[name]['Type'] == 'File': | |
317 cpp.write(' case %s:\n' % name) | |
318 cpp.write(' return resource%dSize;\n' % resources[name]['Index']) | |
319 | |
320 cpp.write(""" | |
321 default: | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
322 throw %s; |
0 | 323 } |
324 } | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
325 """ % OUT_OF_RANGE_EXCEPTION) |
0 | 326 |
327 | |
328 | |
329 ##################################################################### | |
330 ## Write the accessors to the directory resources in .cpp | |
331 ##################################################################### | |
332 | |
333 cpp.write(""" | |
334 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path) | |
335 { | |
336 switch (id) | |
337 { | |
338 """) | |
339 | |
340 for name in resources: | |
341 if resources[name]['Type'] == 'Directory': | |
342 cpp.write(' case %s:\n' % name) | |
343 isFirst = True | |
344 for path in resources[name]['Files']: | |
345 cpp.write(' if (!strcmp(path, "%s"))\n' % path) | |
346 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
|
347 cpp.write(' throw %s;\n\n' % INEXISTENT_PATH_EXCEPTION) |
0 | 348 |
349 cpp.write(""" default: | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
350 throw %s; |
0 | 351 } |
352 } | |
353 | |
354 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path) | |
355 { | |
356 switch (id) | |
357 { | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
358 """ % OUT_OF_RANGE_EXCEPTION) |
0 | 359 |
360 for name in resources: | |
361 if resources[name]['Type'] == 'Directory': | |
362 cpp.write(' case %s:\n' % name) | |
363 isFirst = True | |
364 for path in resources[name]['Files']: | |
365 cpp.write(' if (!strcmp(path, "%s"))\n' % path) | |
366 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
|
367 cpp.write(' throw %s;\n\n' % INEXISTENT_PATH_EXCEPTION) |
0 | 368 |
369 cpp.write(""" default: | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
370 throw %s; |
0 | 371 } |
372 } | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
373 """ % OUT_OF_RANGE_EXCEPTION) |
0 | 374 |
375 | |
376 | |
377 | |
378 ##################################################################### | |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
379 ## List the resources in a directory |
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 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
382 cpp.write(""" |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
383 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
|
384 { |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
385 result.clear(); |
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 switch (id) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
388 { |
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 for name in resources: |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
392 if resources[name]['Type'] == 'Directory': |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
393 cpp.write(' case %s:\n' % name) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
394 for path in sorted(resources[name]['Files']): |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
395 cpp.write(' result.push_back("%s");\n' % path) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
396 cpp.write(' break;\n\n') |
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 cpp.write(""" default: |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
399 throw %s; |
323
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 } |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
402 """ % OUT_OF_RANGE_EXCEPTION) |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
403 |
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 ##################################################################### |
0 | 408 ## Write the convenience wrappers in .cpp |
409 ##################################################################### | |
410 | |
411 cpp.write(""" | |
412 void GetFileResource(std::string& result, FileResourceId id) | |
413 { | |
414 size_t size = GetFileResourceSize(id); | |
415 result.resize(size); | |
416 if (size > 0) | |
417 memcpy(&result[0], GetFileResourceBuffer(id), size); | |
418 } | |
419 | |
420 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path) | |
421 { | |
422 size_t size = GetDirectoryResourceSize(id, path); | |
423 result.resize(size); | |
424 if (size > 0) | |
425 memcpy(&result[0], GetDirectoryResourceBuffer(id, path), size); | |
426 } | |
427 } | |
428 } | |
429 """) | |
430 cpp.close() |