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.
This commit is contained in:
parent
49583ccfab
commit
09fdc7f815
|
@ -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:
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue