PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ rtpg_strsplit()

char** rtpg_strsplit ( const char *  str,
const char *  delimiter,
int *  n 
)

Definition at line 142 of file rtpg_internal.c.

Referenced by RASTER_colorMap(), RASTER_reclass(), and rtpg_assignHookGDALEnabledDrivers().

142  {
143  char *tmp = NULL;
144  char **rtn = NULL;
145  char *token = NULL;
146 
147  *n = 0;
148  if (!str)
149  return NULL;
150 
151  /* copy str to tmp as strtok will mangle the string */
152  tmp = palloc(sizeof(char) * (strlen(str) + 1));
153  if (NULL == tmp) {
154  fprintf(stderr, "Not enough memory\n");
155  return NULL;
156  }
157  strcpy(tmp, str);
158 
159  if (!strlen(tmp) || !delimiter || !strlen(delimiter)) {
160  *n = 1;
161  rtn = (char **) palloc(*n * sizeof(char *));
162  if (NULL == rtn) {
163  fprintf(stderr, "Not enough memory\n");
164  return NULL;
165  }
166  rtn[0] = (char *) palloc(sizeof(char) * (strlen(tmp) + 1));
167  if (NULL == rtn[0]) {
168  fprintf(stderr, "Not enough memory\n");
169  return NULL;
170  }
171  strcpy(rtn[0], tmp);
172  pfree(tmp);
173  return rtn;
174  }
175 
176  token = strtok(tmp, delimiter);
177  while (token != NULL) {
178  if (*n < 1) {
179  rtn = (char **) palloc(sizeof(char *));
180  }
181  else {
182  rtn = (char **) repalloc(rtn, (*n + 1) * sizeof(char *));
183  }
184  if (NULL == rtn) {
185  fprintf(stderr, "Not enough memory\n");
186  return NULL;
187  }
188 
189  rtn[*n] = NULL;
190  rtn[*n] = (char *) palloc(sizeof(char) * (strlen(token) + 1));
191  if (NULL == rtn[*n]) {
192  fprintf(stderr, "Not enough memory\n");
193  return NULL;
194  }
195 
196  strcpy(rtn[*n], token);
197  *n = *n + 1;
198 
199  token = strtok(NULL, delimiter);
200  }
201 
202  pfree(tmp);
203  return rtn;
204 }
Here is the caller graph for this function: