Browse Source

[1866] revise generate_typeclasscode without using temporary text variables.

wouldn't matter for the usage of this script, but this should be slightly
more efficient.
JINMEI Tatuya 12 years ago
parent
commit
3188ee1246
1 changed files with 29 additions and 27 deletions
  1. 29 27
      src/lib/dns/gen-rdatacode.py.in

+ 29 - 27
src/lib/dns/gen-rdatacode.py.in

@@ -289,51 +289,53 @@ def generate_typeclasscode(fileprefix, basemtime, code2txt, type_or_class):
         print('skip generating ' + outputfile)
         return
 
-    declarationtxt = ''
-    deftxt = ''
-    pydef_txt = ''
-    for code in code2txt.keys():
-        codetxt = code2txt[code].upper()
+    # Create a list of (code, code-text) pairs, where code-text is generally
+    # upper-cased, with applying speicial filters when necessary.
+    def convert(code_txt):
         # Workaround by heuristics: there's a "NULL" RR type, but it would
         # cause conflict with the C/C++ macro.  We use Null as a special case.
-        if codetxt == 'NULL':
-            codetxt = 'Null'
-        # Likewise, convert "NSAP-PTR" to "NSAP_PTR".
-        if codetxt == 'NSAP-PTR':
-            codetxt = "NSAP_PTR"
-        declarationtxt += ' ' * 4 + 'static const RR' + cap_key + '& ' + \
-            codetxt + '();\n'
-        deftxt += '''inline const RR''' + cap_key + '''&
-RR''' + cap_key + '''::''' + codetxt + '''() {
-    static RR''' + cap_key + ''' ''' + lower_key + '''(''' + code + ''');
-    return (''' + lower_key + ''');
-}\n
-'''
-        pydef_txt += '''\
-    installClassVariable(''' + lower_key + '''_type, "''' + codetxt + '''",
-                         createRR''' + cap_key + '''Object(RR''' + \
-        cap_key + '''::''' + codetxt + '''()));
-'''
+        if code_txt == 'null':
+            return 'Null'
+        # Likewise, convert "nsap-ptr" to "NSAP_PTR" as a dash cannot be part
+        # of a C/C++ variable.
+        if code_txt == 'nsap-ptr':
+            return 'NSAP_PTR'
+        return code_txt.upper()
+    codes = [ (code, convert(txt)) for code, txt in code2txt.items() ]
 
     # Dump source code for libdns++
     with  open(placeholder, 'r') as header_temp:
         with open(outputfile, 'w') as header_out:
             header_out.write(heading_txt)
-            for line in header_temp.readlines():
+            for line in header_temp:
                 header_out.write(line)
                 if re.match('\s+// BEGIN_WELL_KNOWN_' + upper_key +
                             '_DECLARATIONS$', line):
-                    header_out.write(declarationtxt)
+                    for code in codes:
+                        header_out.write(' ' * 4 + 'static const RR' +
+                                         cap_key + '& ' + code[1] + '();\n')
                 if re.match('// BEGIN_WELL_KNOWN_' + upper_key +
                             '_DEFINITIONS$', line):
-                    header_out.write('\n' + deftxt)
+                    for code in codes:
+                        header_out.write('''inline const RR''' + cap_key +
+                                         '''&
+RR''' + cap_key + '''::''' + code[1] + '''() {
+    static RR''' + cap_key + ''' ''' + lower_key + '''(''' + code[0] + ''');
+    return (''' + lower_key + ''');
+}\n
+''')
 
     # Dump source code snippet for isc.dns Python module
     with open(py_outputfile, 'w') as py_out:
         py_out.write("    // auto-generated by ../gen-rdatacode.py."
                      "  Don't edit this file.\n")
         py_out.write("\n")
-        py_out.write(pydef_txt)
+        for code in codes:
+            py_out.write('''\
+    installClassVariable(''' + lower_key + '''_type, "''' + code[1] + '''",
+                         createRR''' + cap_key + '''Object(RR''' + \
+        cap_key + '''::''' + code[1] + '''()));
+''')
 
 def generate_rrparam(fileprefix, basemtime):
     placeholder = '@srcdir@/' + fileprefix + '-placeholder.cc'