PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ strreplace()

static char* strreplace ( const char *  str,
const char *  oldstr,
const char *  newstr,
int *  count 
)
static

Definition at line 144 of file raster2pgsql.c.

148  {
149  const char *tmp = str;
150  char *result;
151  int found = 0;
152  int length, reslen;
153  int oldlen = strlen(oldstr);
154  int newlen = strlen(newstr);
155  int limit = (count != NULL && *count > 0) ? *count : -1;
156 
157  tmp = str;
158  while ((tmp = strstr(tmp, oldstr)) != NULL && found != limit)
159  found++, tmp += oldlen;
160 
161  length = strlen(str) + found * (newlen - oldlen);
162  if ((result = (char *) rtalloc(length + 1)) == NULL) {
163  rterror(_("strreplace: Not enough memory"));
164  found = -1;
165  }
166  else {
167  tmp = str;
168  limit = found; /* Countdown */
169  reslen = 0; /* length of current result */
170 
171  /* Replace each old string found with new string */
172  while ((limit-- > 0) && (tmp = strstr(tmp, oldstr)) != NULL) {
173  length = (tmp - str); /* Number of chars to keep intouched */
174  strncpy(result + reslen, str, length); /* Original part keeped */
175  strcpy(result + (reslen += length), newstr); /* Insert new string */
176 
177  reslen += newlen;
178  tmp += oldlen;
179  str = tmp;
180  }
181  strcpy(result + reslen, str); /* Copies last part and ending null char */
182  }
183 
184  if (count != NULL) *count = found;
185  return result;
186 }
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
int count
Definition: genraster.py:57
#define str(s)
Definition: raster2pgsql.c:34
#define _(String)
Definition: shpcommon.h:24

References _, genraster::count, rtalloc(), rterror(), and str.

Here is the call graph for this function: