changeset 1469:bf9b8bfea0e7

more generic EmbedResources.py
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 30 Jul 2015 14:12:12 +0200
parents 332af9bcabc8
children b6a80a5ac500
files Resources/CMake/AutoGeneratedCode.cmake Resources/EmbedResources.py
diffstat 2 files changed, 53 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/Resources/CMake/AutoGeneratedCode.cmake	Thu Jul 30 10:14:41 2015 +0200
+++ b/Resources/CMake/AutoGeneratedCode.cmake	Thu Jul 30 14:12:12 2015 +0200
@@ -6,17 +6,28 @@
 
 macro(EmbedResources)
   # Convert a semicolon separated list to a whitespace separated string
+  set(SCRIPT_OPTIONS)
   set(SCRIPT_ARGUMENTS)
   set(DEPENDENCIES)
   set(IS_PATH_NAME false)
+
+  # Loop over the arguments of the function
   foreach(arg ${ARGN})
-    if (${IS_PATH_NAME})
-      list(APPEND SCRIPT_ARGUMENTS "${arg}")
-      list(APPEND DEPENDENCIES "${arg}")
-      set(IS_PATH_NAME false)
+    # Extract the first character of the argument
+    string(SUBSTRING "${arg}" 0 1 FIRST_CHAR)
+    if (${FIRST_CHAR} STREQUAL "-")
+      # If the argument starts with a dash "-", this is an option to
+      # EmbedResources.py
+      list(APPEND SCRIPT_OPTIONS ${arg})
     else()
-      list(APPEND SCRIPT_ARGUMENTS "${arg}")
-      set(IS_PATH_NAME true)
+      if (${IS_PATH_NAME})
+        list(APPEND SCRIPT_ARGUMENTS "${arg}")
+        list(APPEND DEPENDENCIES "${arg}")
+        set(IS_PATH_NAME false)
+      else()
+        list(APPEND SCRIPT_ARGUMENTS "${arg}")
+        set(IS_PATH_NAME true)
+      endif()
     endif()
   endforeach()
 
@@ -28,6 +39,7 @@
     COMMAND 
     ${PYTHON_EXECUTABLE}
     "${ORTHANC_ROOT}/Resources/EmbedResources.py"
+    ${SCRIPT_OPTIONS}
     "${AUTOGENERATED_DIR}/EmbeddedResources"
     ${SCRIPT_ARGUMENTS}
     DEPENDS
--- a/Resources/EmbedResources.py	Thu Jul 30 10:14:41 2015 +0200
+++ b/Resources/EmbedResources.py	Thu Jul 30 14:12:12 2015 +0200
@@ -37,16 +37,27 @@
 import re
 
 UPCASE_CHECK = True
+USE_SYSTEM_EXCEPTION = False
+EXCEPTION_CLASS = 'OrthancException'
+OUT_OF_RANGE_EXCEPTION = 'OrthancException(ErrorCode_ParameterOutOfRange)'
+NAMESPACE = 'Orthanc'
+
 ARGS = []
 for i in range(len(sys.argv)):
     if not sys.argv[i].startswith('--'):
         ARGS.append(sys.argv[i])
     elif sys.argv[i].lower() == '--no-upcase-check':
         UPCASE_CHECK = False
+    elif sys.argv[i].lower() == '--system-exception':
+        USE_SYSTEM_EXCEPTION = True
+        EXCEPTION_CLASS = '::std::runtime_error'
+        OUT_OF_RANGE_EXCEPTION = '%s("Parameter out of range")' % EXCEPTION_CLASS
+    elif sys.argv[i].startswith('--namespace='):
+        NAMESPACE = sys.argv[i][sys.argv[i].find('=') + 1 : ]
 
 if len(ARGS) < 2 or len(ARGS) % 2 != 0:
     print ('Usage:')
-    print ('python %s [--no-upcase-check] <TargetBaseFilename> [ <Name> <Source> ]*' % sys.argv[0])
+    print ('python %s [--no-upcase-check] [--system-exception] [--namespace=<Namespace>] <TargetBaseFilename> [ <Name> <Source> ]*' % sys.argv[0])
     exit(-1)
 
 TARGET_BASE_FILENAME = ARGS[1]
@@ -146,13 +157,13 @@
 #include <string>
 #include <list>
 
-namespace Orthanc
+namespace %s
 {
   namespace EmbeddedResources
   {
     enum FileResourceId
     {
-""")
+""" % NAMESPACE)
 
 isFirst = True
 for name in resources:
@@ -241,19 +252,22 @@
 
 cpp = open(TARGET_BASE_FILENAME + '.cpp', 'w')
 
+cpp.write('#include "%s.h"\n' % os.path.basename(TARGET_BASE_FILENAME))
+
+if USE_SYSTEM_EXCEPTION:
+    cpp.write('#include <stdexcept>')
+else:
+    cpp.write('#include "%s/Core/OrthancException.h"' % os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
+
 cpp.write("""
-#include "%s.h"
-#include "%s/Core/OrthancException.h"
-
 #include <stdint.h>
 #include <string.h>
 
-namespace Orthanc
+namespace %s
 {
   namespace EmbeddedResources
   {
-""" % (os.path.basename(TARGET_BASE_FILENAME),
-       os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))))
+""" % NAMESPACE)
 
 
 for name in resources:
@@ -282,7 +296,7 @@
 
 cpp.write("""
       default:
-        throw OrthancException(ErrorCode_ParameterOutOfRange);
+        throw %s;
       }
     }
 
@@ -290,7 +304,7 @@
     {
       switch (id)
       {
-""")
+""" % OUT_OF_RANGE_EXCEPTION)
 
 for name in resources:
     if resources[name]['Type'] == 'File':
@@ -299,10 +313,10 @@
 
 cpp.write("""
       default:
-        throw OrthancException(ErrorCode_ParameterOutOfRange);
+        throw %s;
       }
     }
-""")
+""" % OUT_OF_RANGE_EXCEPTION)
 
 
 
@@ -324,10 +338,10 @@
         for path in resources[name]['Files']:
             cpp.write('        if (!strcmp(path, "%s"))\n' % path)
             cpp.write('          return resource%dBuffer;\n' % resources[name]['Files'][path]['Index'])
-        cpp.write('        throw OrthancException("Unknown path in a directory resource");\n\n')
+        cpp.write('        throw %s("Unknown path in a directory resource");\n\n' % EXCEPTION_CLASS)
 
 cpp.write("""      default:
-        throw OrthancException(ErrorCode_ParameterOutOfRange);
+        throw %s;
       }
     }
 
@@ -335,7 +349,7 @@
     {
       switch (id)
       {
-""")
+""" % OUT_OF_RANGE_EXCEPTION)
 
 for name in resources:
     if resources[name]['Type'] == 'Directory':
@@ -344,13 +358,13 @@
         for path in resources[name]['Files']:
             cpp.write('        if (!strcmp(path, "%s"))\n' % path)
             cpp.write('          return resource%dSize;\n' % resources[name]['Files'][path]['Index'])
-        cpp.write('        throw OrthancException("Unknown path in a directory resource");\n\n')
+        cpp.write('        throw %s("Unknown path in a directory resource");\n\n' % EXCEPTION_CLASS)
 
 cpp.write("""      default:
-        throw OrthancException(ErrorCode_ParameterOutOfRange);
+        throw %s;
       }
     }
-""")
+""" % OUT_OF_RANGE_EXCEPTION)
 
 
 
@@ -376,10 +390,10 @@
         cpp.write('        break;\n\n')
 
 cpp.write("""      default:
-        throw OrthancException(ErrorCode_ParameterOutOfRange);
+        throw %s;
       }
     }
-""")
+""" % OUT_OF_RANGE_EXCEPTION)