Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Drop xbt_str_split().
[simgrid.git] / src / xbt / xbt_str.cpp
1 /* xbt_str.cpp - various helping functions to deal with strings             */
2
3 /* Copyright (c) 2007-2020. The SimGrid Team. All rights reserved.          */
4
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. */
7
8 #include "simgrid/Exception.hpp"
9 #include "xbt/misc.h"
10 #include "xbt/str.h" /* headers of these functions */
11 #include "xbt/string.hpp"
12 #include <array>
13
14 /** @brief Just like @ref xbt_str_split_quoted (Splits a string into a dynar of strings), but without memory allocation
15  *
16  * The string passed as argument must be writable (not const)
17  * The elements of the dynar are just parts of the string passed as argument.
18  * So if you don't store that argument elsewhere, you should free it in addition to freeing the dynar. This can be done
19  * by simply freeing the first argument of the dynar:
20  *  free(xbt_dynar_get_ptr(dynar,0));
21  *
22  * Actually this function puts a bunch of \0 in the memory area you passed as argument to separate the elements, and
23  * pushes the address of each chunk in the resulting dynar. Yes, that's uneven. Yes, that's gory. But that's efficient.
24  */
25 xbt_dynar_t xbt_str_split_quoted_in_place(char *s) {
26   xbt_dynar_t res = xbt_dynar_new(sizeof(char *), nullptr);
27   char* beg;
28   char* end; /* pointers around the parsed chunk */
29   int in_simple_quote = 0;
30   int in_double_quote = 0;
31   int done            = 0;
32   int ctn             = 0; /* Got something in this block */
33
34   if (s[0] == '\0')
35     return res;
36
37   beg = s;
38
39   /* do not trim leading spaces: caller responsibility to clean his cruft */
40   end = beg;
41
42   while (not done) {
43     switch (*end) {
44     case '\\':
45       ctn = 1;
46       /* Protected char; move it closer */
47       memmove(end, end + 1, strlen(end));
48       if (*end == '\0')
49         throw std::invalid_argument("String ends with \\");
50       end++;                    /* Pass the protected char */
51       break;
52     case '\'':
53       ctn = 1;
54       if (not in_double_quote) {
55         in_simple_quote = not in_simple_quote;
56         memmove(end, end + 1, strlen(end));
57       } else {
58         /* simple quote protected by double ones */
59         end++;
60       }
61       break;
62     case '"':
63       ctn = 1;
64       if (not in_simple_quote) {
65         in_double_quote = not in_double_quote;
66         memmove(end, end + 1, strlen(end));
67       } else {
68         /* double quote protected by simple ones */
69         end++;
70       }
71       break;
72     case ' ':
73     case '\t':
74     case '\n':
75     case '\0':
76       if (*end == '\0' && (in_simple_quote || in_double_quote)) {
77         throw std::invalid_argument(simgrid::xbt::string_printf("End of string found while searching for %c in %s",
78                                                                 (in_simple_quote ? '\'' : '"'), s));
79       }
80       if (in_simple_quote || in_double_quote) {
81         end++;
82         break;
83       }
84       if (*end == '\0')
85         done = 1;
86
87       *end = '\0';
88       if (ctn) {
89         /* Found a separator. Push the string if contains something */
90         xbt_dynar_push(res, &beg);
91       }
92       ctn = 0;
93
94       if (done)
95         break;
96
97       beg = ++end;
98       /* trim within the string, manually to speed things up */
99       while (*beg == ' ')
100         beg++;
101       end = beg;
102       break;
103     default:
104       ctn = 1;
105       end++;
106     }
107   }
108   return res;
109 }
110
111 /** @brief Splits a string into a dynar of strings, taking quotes into account
112  *
113  * It basically does the same argument separation than the shell, where white spaces can be escaped and where arguments
114  * are never split within a quote group.
115  * Several subsequent spaces are ignored (unless within quotes, of course).
116  * You may want to trim the input string, if you want to avoid empty entries
117  */
118 xbt_dynar_t xbt_str_split_quoted(const char *s)
119 {
120   xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
121   xbt_dynar_t parsed;
122   char *str_to_free;            /* we have to copy the string before, to handle backslashes */
123   unsigned int cursor;
124   char *p;
125
126   if (s[0] == '\0')
127     return res;
128   str_to_free = xbt_strdup(s);
129
130   parsed = xbt_str_split_quoted_in_place(str_to_free);
131   xbt_dynar_foreach(parsed,cursor,p) {
132     char *q=xbt_strdup(p);
133     xbt_dynar_push(res,&q);
134   }
135   xbt_free(str_to_free);
136   xbt_dynar_shrink(res, 0);
137   xbt_dynar_free(&parsed);
138   return res;
139 }
140
141 /** @brief Parse an integer out of a string, or raise an error
142  *
143  * The @a str is passed as argument to your @a error_msg, as follows:
144  * @verbatim throw std::invalid_argument(simgrid::xbt::string_printf(error_msg, str)); @endverbatim
145  */
146 long int xbt_str_parse_int(const char* str, const char* error_msg)
147 {
148   char* endptr;
149   if (str == nullptr || str[0] == '\0')
150     throw std::invalid_argument(simgrid::xbt::string_printf(error_msg, str));
151
152   long int res = strtol(str, &endptr, 10);
153   if (endptr[0] != '\0')
154     throw std::invalid_argument(simgrid::xbt::string_printf(error_msg, str));
155
156   return res;
157 }
158
159 /** @brief Parse a double out of a string, or raise an error
160  *
161  * The @a str is passed as argument to your @a error_msg, as follows:
162  * @verbatim throw std::invalid_argument(simgrid::xbt::string_printf(error_msg, str)); @endverbatim
163  */
164 double xbt_str_parse_double(const char* str, const char* error_msg)
165 {
166   char *endptr;
167   if (str == nullptr || str[0] == '\0')
168     throw std::invalid_argument(simgrid::xbt::string_printf(error_msg, str));
169
170   double res = strtod(str, &endptr);
171   if (endptr[0] != '\0')
172     throw std::invalid_argument(simgrid::xbt::string_printf(error_msg, str));
173
174   return res;
175 }