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