[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-block] [PATCH v4 02/14] qapi: Move to_c_string() to common.py
From: |
Max Reitz |
Subject: |
[Qemu-block] [PATCH v4 02/14] qapi: Move to_c_string() to common.py |
Date: |
Mon, 24 Jun 2019 19:39:22 +0200 |
This function will be useful for code generation once we allow default
values, so move it to the other "C helper functions". In the process,
rewrite it so it supports all nonprintable and non-ASCII characters.
Signed-off-by: Max Reitz <address@hidden>
---
scripts/qapi/common.py | 26 ++++++++++++++++++++++++++
scripts/qapi/introspect.py | 4 ----
2 files changed, 26 insertions(+), 4 deletions(-)
diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py
index 3396ea4a09..c6754a5856 100644
--- a/scripts/qapi/common.py
+++ b/scripts/qapi/common.py
@@ -2208,6 +2208,32 @@ def c_fname(filename):
return re.sub(r'[^A-Za-z0-9_]', '_', filename)
+# Translates a string to a valid C constant
+def to_c_string(string):
+ result = '"'
+
+ python2 = isinstance(string, bytes)
+ if not python2:
+ # Will return integers when iterated over
+ string = string.encode()
+
+ for c in string:
+ value = ord(c) if python2 else c
+ if value < 0x20 or value > 0x7e:
+ result += '\\%03o' % value
+ else:
+ c = chr(value)
+ if c == '"':
+ result += '\\"'
+ elif c == '\\':
+ result += '\\\\'
+ else:
+ result += c
+
+ result += '"'
+ return result
+
+
def guardstart(name):
return mcgen('''
#ifndef %(name)s
diff --git a/scripts/qapi/introspect.py b/scripts/qapi/introspect.py
index 6a61dd831f..572e0b8331 100644
--- a/scripts/qapi/introspect.py
+++ b/scripts/qapi/introspect.py
@@ -66,10 +66,6 @@ def to_qlit(obj, level=0, suppress_first_indent=False):
return ret
-def to_c_string(string):
- return '"' + string.replace('\\', r'\\').replace('"', r'\"') + '"'
-
-
class QAPISchemaGenIntrospectVisitor(QAPISchemaMonolithicCVisitor):
def __init__(self, prefix, unmask):
--
2.21.0
- [Qemu-block] [PATCH v4 00/14] block: Try to create well-typed json:{} filenames, Max Reitz, 2019/06/24
- [Qemu-block] [PATCH v4 01/14] qapi: Parse numeric values, Max Reitz, 2019/06/24
- [Qemu-block] [PATCH v4 02/14] qapi: Move to_c_string() to common.py,
Max Reitz <=
- [Qemu-block] [PATCH v4 04/14] qapi: Allow optional discriminators, Max Reitz, 2019/06/24
- [Qemu-block] [PATCH v4 05/14] qapi: Document default values for struct members, Max Reitz, 2019/06/24
- [Qemu-block] [PATCH v4 03/14] qapi: Introduce default values for struct members, Max Reitz, 2019/06/24
- [Qemu-block] [PATCH v4 06/14] test-qapi: Print struct members' default values, Max Reitz, 2019/06/24
- [Qemu-block] [PATCH v4 10/14] qapi: Formalize qcow encryption probing, Max Reitz, 2019/06/24
- [Qemu-block] [PATCH v4 09/14] qapi: Formalize qcow2 encryption probing, Max Reitz, 2019/06/24
- [Qemu-block] [PATCH v4 08/14] tests: Add QAPI optional discriminator tests, Max Reitz, 2019/06/24
- [Qemu-block] [PATCH v4 13/14] iotests: qcow2's encrypt.format is now optional, Max Reitz, 2019/06/24
- [Qemu-block] [PATCH v4 07/14] tests: Test QAPI default values for struct members, Max Reitz, 2019/06/24
- [Qemu-block] [PATCH v4 11/14] block: Try to create well typed json:{} filenames, Max Reitz, 2019/06/24