PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ strsplit()

static char** strsplit ( const char *  str,
const char *  delimiter,
int *  n 
)
static

Definition at line 197 of file raster2pgsql.c.

References _, rtalloc(), rtdealloc(), rterror(), and rtrealloc().

Referenced by main().

197  {
198  char *tmp = NULL;
199  char **rtn = NULL;
200  char *token = NULL;
201 
202  *n = 0;
203  if (!str)
204  return NULL;
205 
206  /* copy str to tmp as strtok will mangle the string */
207  tmp = rtalloc(sizeof(char) * (strlen(str) + 1));
208  if (NULL == tmp) {
209  rterror(_("strsplit: Not enough memory"));
210  return NULL;
211  }
212  strcpy(tmp, str);
213 
214  if (!strlen(tmp) || !delimiter || !strlen(delimiter)) {
215  *n = 1;
216  rtn = (char **) rtalloc(*n * sizeof(char *));
217  if (NULL == rtn) {
218  rterror(_("strsplit: Not enough memory"));
219  return NULL;
220  }
221  rtn[0] = (char *) rtalloc(sizeof(char) * (strlen(tmp) + 1));
222  if (NULL == rtn[0]) {
223  rterror(_("strsplit: Not enough memory"));
224  return NULL;
225  }
226  strcpy(rtn[0], tmp);
227  rtdealloc(tmp);
228  return rtn;
229  }
230 
231  token = strtok(tmp, delimiter);
232  while (token != NULL) {
233  if (*n < 1) {
234  rtn = (char **) rtalloc(sizeof(char *));
235  }
236  else {
237  rtn = (char **) rtrealloc(rtn, (*n + 1) * sizeof(char *));
238  }
239  if (NULL == rtn) {
240  rterror(_("strsplit: Not enough memory"));
241  return NULL;
242  }
243 
244  rtn[*n] = NULL;
245  rtn[*n] = (char *) rtalloc(sizeof(char) * (strlen(token) + 1));
246  if (NULL == rtn[*n]) {
247  rterror(_("strsplit: Not enough memory"));
248  return NULL;
249  }
250 
251  strcpy(rtn[*n], token);
252  *n = *n + 1;
253 
254  token = strtok(NULL, delimiter);
255  }
256 
257  rtdealloc(tmp);
258  return rtn;
259 }
#define _(String)
Definition: shpcommon.h:24
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
Definition: rt_context.c:199
void * rtalloc(size_t size)
Wrappers used for managing memory.
Definition: rt_context.c:171
void * rtrealloc(void *mem, size_t size)
Definition: rt_context.c:179
void rtdealloc(void *mem)
Definition: rt_context.c:186
Here is the call graph for this function:
Here is the caller graph for this function: