1 /* xbt_str.cpp - various helping functions to deal with strings */
3 /* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "simgrid/Exception.hpp"
10 #include "xbt/str.h" /* headers of these functions */
11 #include "xbt/string.hpp"
13 /** @brief Parse an integer out of a string, or raise an error
15 * The @a str is passed as argument to your @a error_msg, as follows:
16 * @verbatim throw std::invalid_argument(simgrid::xbt::string_printf(error_msg, str)); @endverbatim
18 long int xbt_str_parse_int(const char* str, const char* error_msg)
21 if (str == nullptr || str[0] == '\0')
22 throw std::invalid_argument(simgrid::xbt::string_printf(error_msg, str));
24 long int res = strtol(str, &endptr, 10);
25 if (endptr[0] != '\0')
26 throw std::invalid_argument(simgrid::xbt::string_printf(error_msg, str));
31 /** @brief Parse a double out of a string, or raise an error
33 * The @a str is passed as argument to your @a error_msg, as follows:
34 * @verbatim throw std::invalid_argument(simgrid::xbt::string_printf(error_msg, str)); @endverbatim
36 double xbt_str_parse_double(const char* str, const char* error_msg)
39 if (str == nullptr || str[0] == '\0')
40 throw std::invalid_argument(simgrid::xbt::string_printf(error_msg, str));
42 double res = strtod(str, &endptr);
43 if (endptr[0] != '\0')
44 throw std::invalid_argument(simgrid::xbt::string_printf(error_msg, str));