aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim-Philipp Müller <tim@centricular.com>2016-09-12 22:05:19 +0100
committerTim-Philipp Müller <tim@centricular.com>2016-09-13 23:44:33 +0100
commit09fdc7f815d8b6dede94a14a15a0c05fd1a621c1 (patch)
tree163373ee92e806c83438e4d4e62c8229006fb176
parent49583ccfabcc3db057521bf2801f75f5bab94e0e (diff)
downloadmeson-09fdc7f815d8b6dede94a14a15a0c05fd1a621c1.zip
meson-09fdc7f815d8b6dede94a14a15a0c05fd1a621c1.tar.gz
meson-09fdc7f815d8b6dede94a14a15a0c05fd1a621c1.tar.bz2
configuration_data: add .set_quoted() convenience method to set quoted string
Quotes in the string will be escaped C-string style with \", but nothing else will be escaped.
-rw-r--r--mesonbuild/interpreter.py8
-rw-r--r--test cases/common/16 configure file/dumpprog.c12
-rw-r--r--test cases/common/16 configure file/meson.build5
3 files changed, 24 insertions, 1 deletions
diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py
index 9584950..316f8c4 100644
--- a/mesonbuild/interpreter.py
+++ b/mesonbuild/interpreter.py
@@ -187,6 +187,7 @@ class ConfigurationDataHolder(InterpreterObject):
self.held_object = build.ConfigurationData()
self.methods.update({'set': self.set_method,
'set10': self.set10_method,
+ 'set_quoted': self.set_quoted_method,
'has' : self.has_method,
})
@@ -211,6 +212,13 @@ class ConfigurationDataHolder(InterpreterObject):
(name, val) = self.validate_args(args)
self.held_object.values[name] = val
+ def set_quoted_method(self, args, kwargs):
+ (name, val) = self.validate_args(args)
+ if not isinstance(val, str):
+ raise InterpreterException("Second argument to set_quoted must be a string.")
+ escaped_val = '\\"'.join(val.split('"'))
+ self.held_object.values[name] = '"' + escaped_val + '"'
+
def set10_method(self, args, kwargs):
(name, val) = self.validate_args(args)
if val:
diff --git a/test cases/common/16 configure file/dumpprog.c b/test cases/common/16 configure file/dumpprog.c
index e0c9868..685240c 100644
--- a/test cases/common/16 configure file/dumpprog.c
+++ b/test cases/common/16 configure file/dumpprog.c
@@ -17,6 +17,18 @@ int main(int argc, char **argv) {
printf("String token defined wrong.\n");
return 1;
}
+ if(strcmp(SHOULD_BE_STRING2, "A \"B\" C") != 0) {
+ printf("String token 2 defined wrong.\n");
+ return 1;
+ }
+ if(strcmp(SHOULD_BE_STRING3, "A \"\" C") != 0) {
+ printf("String token 3 defined wrong.\n");
+ return 1;
+ }
+ if(strcmp(SHOULD_BE_STRING4, "A \" C") != 0) {
+ printf("String token 4 defined wrong.\n");
+ return 1;
+ }
if(SHOULD_BE_ONE != 1) {
printf("One defined incorrectly.\n");
return 1;
diff --git a/test cases/common/16 configure file/meson.build b/test cases/common/16 configure file/meson.build
index e1bdff3..c00af40 100644
--- a/test cases/common/16 configure file/meson.build
+++ b/test cases/common/16 configure file/meson.build
@@ -34,7 +34,10 @@ test('inctest2', executable('prog2', 'prog2.c'))
# Generate a conf file without an input file.
dump = configuration_data()
-dump.set('SHOULD_BE_STRING', '"string"')
+dump.set_quoted('SHOULD_BE_STRING', 'string')
+dump.set_quoted('SHOULD_BE_STRING2', 'A "B" C')
+dump.set_quoted('SHOULD_BE_STRING3', 'A "" C')
+dump.set_quoted('SHOULD_BE_STRING4', 'A " C')
dump.set('SHOULD_BE_RETURN', 'return')
dump.set('SHOULD_BE_DEFINED', true)
dump.set('SHOULD_BE_UNDEFINED', false)