diff --git a/rcl_yaml_param_parser/src/parser.c b/rcl_yaml_param_parser/src/parser.c index 48bc286442..704470055f 100644 --- a/rcl_yaml_param_parser/src/parser.c +++ b/rcl_yaml_param_parser/src/parser.c @@ -772,40 +772,48 @@ static void * get_value( } /// Check for int - errno = 0; - ival = strtol(value, &endptr, 0); - if ((0 == errno) && (NULL != endptr)) { - if ((NULL != endptr) && (endptr != value)) { - if (('\0' != *value) && ('\0' == *endptr)) { - *val_type = DATA_TYPE_INT64; - ret_val = allocator.zero_allocate(1U, sizeof(int64_t), allocator.state); - if (NULL == ret_val) { - return NULL; + if (style != YAML_SINGLE_QUOTED_SCALAR_STYLE && + style != YAML_DOUBLE_QUOTED_SCALAR_STYLE) + { + errno = 0; + ival = strtol(value, &endptr, 0); + if ((0 == errno) && (NULL != endptr)) { + if ((NULL != endptr) && (endptr != value)) { + if (('\0' != *value) && ('\0' == *endptr)) { + *val_type = DATA_TYPE_INT64; + ret_val = allocator.zero_allocate(1U, sizeof(int64_t), allocator.state); + if (NULL == ret_val) { + return NULL; + } + *((int64_t *)ret_val) = ival; + return ret_val; } - *((int64_t *)ret_val) = ival; - return ret_val; } } } /// Check for float - errno = 0; - endptr = NULL; - dval = strtod(value, &endptr); - if ((0 == errno) && (NULL != endptr)) { - if ((NULL != endptr) && (endptr != value)) { - if (('\0' != *value) && ('\0' == *endptr)) { - *val_type = DATA_TYPE_DOUBLE; - ret_val = allocator.zero_allocate(1U, sizeof(double), allocator.state); - if (NULL == ret_val) { - return NULL; + if (style != YAML_SINGLE_QUOTED_SCALAR_STYLE && + style != YAML_DOUBLE_QUOTED_SCALAR_STYLE) + { + errno = 0; + endptr = NULL; + dval = strtod(value, &endptr); + if ((0 == errno) && (NULL != endptr)) { + if ((NULL != endptr) && (endptr != value)) { + if (('\0' != *value) && ('\0' == *endptr)) { + *val_type = DATA_TYPE_DOUBLE; + ret_val = allocator.zero_allocate(1U, sizeof(double), allocator.state); + if (NULL == ret_val) { + return NULL; + } + *((double *)ret_val) = dval; + return ret_val; } - *((double *)ret_val) = dval; - return ret_val; } } + errno = 0; } - errno = 0; /// It is a string *val_type = DATA_TYPE_STRING; diff --git a/rcl_yaml_param_parser/test/string_array_with_quoted_number.yaml b/rcl_yaml_param_parser/test/string_array_with_quoted_number.yaml new file mode 100644 index 0000000000..d6d304a2b6 --- /dev/null +++ b/rcl_yaml_param_parser/test/string_array_with_quoted_number.yaml @@ -0,0 +1,4 @@ +initial_params_node: + ros__parameters: + sa1: ["Four", "score"] + sa2: ["and", "7"] diff --git a/rcl_yaml_param_parser/test/test_parse_yaml.cpp b/rcl_yaml_param_parser/test/test_parse_yaml.cpp index 339de5edda..274e62f79a 100644 --- a/rcl_yaml_param_parser/test/test_parse_yaml.cpp +++ b/rcl_yaml_param_parser/test/test_parse_yaml.cpp @@ -22,7 +22,6 @@ #include "rcutils/filesystem.h" static char cur_dir[1024]; -rcutils_allocator_t allocator = rcutils_get_default_allocator(); TEST(test_file_parser, correct_syntax) { rcutils_reset_error(); @@ -43,6 +42,27 @@ TEST(test_file_parser, correct_syntax) { allocator.deallocate(path, allocator.state); } +TEST(test_file_parser, string_array_with_quoted_number) { + rcutils_reset_error(); + EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024)); + rcutils_allocator_t allocator = rcutils_get_default_allocator(); + char * test_path = rcutils_join_path(cur_dir, "test", allocator); + char * path = rcutils_join_path(test_path, "string_array_with_quoted_number.yaml", allocator); + fprintf(stderr, "cur_path: %s\n", path); + EXPECT_TRUE(rcutils_exists(path)); + rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator); + EXPECT_TRUE(params_hdl); + if (params_hdl) { + bool res = rcl_parse_yaml_file(path, params_hdl); + fprintf(stderr, "%s\n", rcutils_get_error_string().str); + EXPECT_TRUE(res); + rcl_yaml_node_struct_print(params_hdl); + rcl_yaml_node_struct_fini(params_hdl); + } + allocator.deallocate(test_path, allocator.state); + allocator.deallocate(path, allocator.state); +} + TEST(test_file_parser, multi_ns_correct_syntax) { rcutils_reset_error(); EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024));