Переглянути джерело

Nodeset compiler: Improve support for utf8 strings in generated code

Julius Pfrommer 6 роки тому
батько
коміт
5a3260ed58
1 змінених файлів з 15 додано та 5 видалено
  1. 15 5
      tools/nodeset_compiler/backend_open62541_datatypes.py

+ 15 - 5
tools/nodeset_compiler/backend_open62541_datatypes.py

@@ -11,6 +11,9 @@ def generateBooleanCode(value):
         return "true"
     return "false"
 
+def makeCLiteral(value):
+    return value.replace('\\', r'\\\\').replace('\n', r'\\n').replace('\r', r'')
+
 def splitStringLiterals(value, splitLength=500, max_string_length=0):
     """
     Split a string literal longer than splitLength into smaller literals.
@@ -34,21 +37,26 @@ def splitStringLiterals(value, splitLength=500, max_string_length=0):
     return ret
 
 def generateStringCode(value, alloc=False, max_string_length=0):
+    value = makeCLiteral(value)
     return "UA_STRING{}({})".format("_ALLOC" if alloc else "", splitStringLiterals(value, max_string_length=max_string_length))
 
 def generateXmlElementCode(value, alloc=False, max_string_length=0):
+    value = makeCLiteral(value)
     return "UA_XMLELEMENT{}({})".format("_ALLOC" if alloc else "", splitStringLiterals(value, max_string_length=max_string_length))
 
 def generateByteStringCode(value, alloc=False, max_string_length=0):
+    value = makeCLiteral(value)
     return "UA_BYTESTRING{}({})".format("_ALLOC" if alloc else "", splitStringLiterals(value, max_string_length=max_string_length))
 
 def generateLocalizedTextCode(value, alloc=False, max_string_length=0):
-    return "UA_LOCALIZEDTEXT{}(\"{}\", {})".format("_ALLOC" if alloc else "",
-                                                       value.locale, splitStringLiterals(value.text, max_string_length=max_string_length))
+    vt = makeCLiteral(value.text)
+    return "UA_LOCALIZEDTEXT{}(\"{}\", {})".format("_ALLOC" if alloc else "", value.locale,
+                                                   splitStringLiterals(vt, max_string_length=max_string_length))
 
 def generateQualifiedNameCode(value, alloc=False, max_string_length=0):
+    vn = makeCLiteral(value.name)
     return "UA_QUALIFIEDNAME{}(ns[{}], {})".format("_ALLOC" if alloc else "",
-                                                     str(value.ns), splitStringLiterals(value.name, max_string_length=max_string_length))
+                                                     str(value.ns), splitStringLiterals(vn, max_string_length=max_string_length))
 
 def generateNodeIdCode(value):
     if not value:
@@ -56,14 +64,16 @@ def generateNodeIdCode(value):
     if value.i != None:
         return "UA_NODEID_NUMERIC(ns[%s], %s)" % (value.ns, value.i)
     elif value.s != None:
-        return "UA_NODEID_STRING(ns[%s], %s)" % (value.ns, value.s.replace('"', r'\"'))
+        v = makeCLiteral(value.s)
+        return "UA_NODEID_STRING(ns[%s], \"%s\")" % (value.ns, v)
     raise Exception(str(value) + " no NodeID generation for bytestring and guid..")
 
 def generateExpandedNodeIdCode(value):
     if value.i != None:
         return "UA_EXPANDEDNODEID_NUMERIC(ns[%s], %s)" % (str(value.ns), str(value.i))
     elif value.s != None:
-        return "UA_EXPANDEDNODEID_STRING(ns[%s], %s)" % (str(value.ns), value.s.replace('"', r'\"'))
+        vs = makeCLiteral(value.s)
+        return "UA_EXPANDEDNODEID_STRING(ns[%s], \"%s\")" % (str(value.ns), vs)
     raise Exception(str(value) + " no NodeID generation for bytestring and guid..")
 
 def generateDateTimeCode(value):