comparison Resources/EmbedResources.py @ 4036:c6e82885f570

merge
author Alain Mazy <alain@mazy.be>
date Wed, 10 Jun 2020 10:28:15 +0200
parents 78ee0155ec67
children
comparison
equal deleted inserted replaced
4035:cc6ed76bba27 4036:c6e82885f570
40 UPCASE_CHECK = True 40 UPCASE_CHECK = True
41 USE_SYSTEM_EXCEPTION = False 41 USE_SYSTEM_EXCEPTION = False
42 EXCEPTION_CLASS = 'OrthancException' 42 EXCEPTION_CLASS = 'OrthancException'
43 OUT_OF_RANGE_EXCEPTION = '::Orthanc::OrthancException(::Orthanc::ErrorCode_ParameterOutOfRange)' 43 OUT_OF_RANGE_EXCEPTION = '::Orthanc::OrthancException(::Orthanc::ErrorCode_ParameterOutOfRange)'
44 INEXISTENT_PATH_EXCEPTION = '::Orthanc::OrthancException(::Orthanc::ErrorCode_InexistentItem)' 44 INEXISTENT_PATH_EXCEPTION = '::Orthanc::OrthancException(::Orthanc::ErrorCode_InexistentItem)'
45 NAMESPACE = 'Orthanc' 45 NAMESPACE = 'Orthanc.EmbeddedResources'
46 FRAMEWORK_PATH = None
46 47
47 ARGS = [] 48 ARGS = []
48 for i in range(len(sys.argv)): 49 for i in range(len(sys.argv)):
49 if not sys.argv[i].startswith('--'): 50 if not sys.argv[i].startswith('--'):
50 ARGS.append(sys.argv[i]) 51 ARGS.append(sys.argv[i])
55 EXCEPTION_CLASS = '::std::runtime_error' 56 EXCEPTION_CLASS = '::std::runtime_error'
56 OUT_OF_RANGE_EXCEPTION = '%s("Parameter out of range")' % EXCEPTION_CLASS 57 OUT_OF_RANGE_EXCEPTION = '%s("Parameter out of range")' % EXCEPTION_CLASS
57 INEXISTENT_PATH_EXCEPTION = '%s("Unknown path in a directory resource")' % EXCEPTION_CLASS 58 INEXISTENT_PATH_EXCEPTION = '%s("Unknown path in a directory resource")' % EXCEPTION_CLASS
58 elif sys.argv[i].startswith('--namespace='): 59 elif sys.argv[i].startswith('--namespace='):
59 NAMESPACE = sys.argv[i][sys.argv[i].find('=') + 1 : ] 60 NAMESPACE = sys.argv[i][sys.argv[i].find('=') + 1 : ]
61 elif sys.argv[i].startswith('--framework-path='):
62 FRAMEWORK_PATH = sys.argv[i][sys.argv[i].find('=') + 1 : ]
60 63
61 if len(ARGS) < 2 or len(ARGS) % 2 != 0: 64 if len(ARGS) < 2 or len(ARGS) % 2 != 0:
62 print ('Usage:') 65 print ('Usage:')
63 print ('python %s [--no-upcase-check] [--system-exception] [--namespace=<Namespace>] <TargetBaseFilename> [ <Name> <Source> ]*' % sys.argv[0]) 66 print ('python %s [--no-upcase-check] [--system-exception] [--namespace=<Namespace>] <TargetBaseFilename> [ <Name> <Source> ]*' % sys.argv[0])
64 exit(-1) 67 exit(-1)
164 167
165 #if defined(_MSC_VER) 168 #if defined(_MSC_VER)
166 # pragma warning(disable: 4065) // "Switch statement contains 'default' but no 'case' labels" 169 # pragma warning(disable: 4065) // "Switch statement contains 'default' but no 'case' labels"
167 #endif 170 #endif
168 171
169 namespace %s 172 """)
170 { 173
171 namespace EmbeddedResources 174
172 { 175 for ns in NAMESPACE.split('.'):
176 header.write('namespace %s {\n' % ns)
177
178
179 header.write("""
173 enum FileResourceId 180 enum FileResourceId
174 { 181 {
175 """ % NAMESPACE) 182 """)
176 183
177 isFirst = True 184 isFirst = True
178 for name in resources: 185 for name in resources:
179 if resources[name]['Type'] == 'File': 186 if resources[name]['Type'] == 'File':
180 if isFirst: 187 if isFirst:
209 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path); 216 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path);
210 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path); 217 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path);
211 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path); 218 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path);
212 219
213 void ListResources(std::list<std::string>& result, DirectoryResourceId id); 220 void ListResources(std::list<std::string>& result, DirectoryResourceId id);
214 } 221
215 } 222 """)
216 """) 223
224
225 for ns in NAMESPACE.split('.'):
226 header.write('}\n')
227
217 header.close() 228 header.close()
218 229
219 230
220 231
221 ##################################################################### 232 #####################################################################
270 281
271 cpp.write('#include "%s.h"\n' % os.path.basename(TARGET_BASE_FILENAME)) 282 cpp.write('#include "%s.h"\n' % os.path.basename(TARGET_BASE_FILENAME))
272 283
273 if USE_SYSTEM_EXCEPTION: 284 if USE_SYSTEM_EXCEPTION:
274 cpp.write('#include <stdexcept>') 285 cpp.write('#include <stdexcept>')
286 elif FRAMEWORK_PATH != None:
287 cpp.write('#include "%s/OrthancException.h"' % FRAMEWORK_PATH)
275 else: 288 else:
276 cpp.write('#include "%s/Core/OrthancException.h"' % os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) 289 cpp.write('#include <OrthancException.h>')
277 290
278 cpp.write(""" 291 cpp.write("""
279 #include <stdint.h> 292 #include <stdint.h>
280 #include <string.h> 293 #include <string.h>
281 294
282 namespace %s 295 """)
283 { 296
284 namespace EmbeddedResources 297 for ns in NAMESPACE.split('.'):
285 { 298 cpp.write('namespace %s {\n' % ns)
286 """ % NAMESPACE)
287 299
288 300
289 for name in resources: 301 for name in resources:
290 if resources[name]['Type'] == 'File': 302 if resources[name]['Type'] == 'File':
291 WriteResource(cpp, resources[name]) 303 WriteResource(cpp, resources[name])
432 size_t size = GetDirectoryResourceSize(id, path); 444 size_t size = GetDirectoryResourceSize(id, path);
433 result.resize(size); 445 result.resize(size);
434 if (size > 0) 446 if (size > 0)
435 memcpy(&result[0], GetDirectoryResourceBuffer(id, path), size); 447 memcpy(&result[0], GetDirectoryResourceBuffer(id, path), size);
436 } 448 }
437 } 449 """)
438 } 450
439 """) 451
452 for ns in NAMESPACE.split('.'):
453 cpp.write('}\n')
454
440 cpp.close() 455 cpp.close()