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