PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ strreplace()

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

Definition at line 153 of file raster2pgsql.c.

157  {
158  const char *tmp = str;
159  char *result;
160  int found = 0;
161  int length, reslen;
162  int oldlen = strlen(oldstr);
163  int newlen = strlen(newstr);
164  int limit = (count != NULL && *count > 0) ? *count : -1;
165 
166  tmp = str;
167  while ((tmp = strstr(tmp, oldstr)) != NULL && found != limit)
168  found++, tmp += oldlen;
169 
170  length = (int)strlen(str) + found * (newlen - oldlen);
171  if ((result = (char *) rtalloc(length + 1)) == NULL) {
172  rterror(_("strreplace: Not enough memory"));
173  found = -1;
174  }
175  else {
176  tmp = str;
177  limit = found; /* Countdown */
178  reslen = 0; /* length of current result */
179 
180  /* Replace each old string found with new string */
181  while ((limit-- > 0) && (tmp = strstr(tmp, oldstr)) != NULL) {
182  length = (tmp - str); /* Number of chars to keep intouched */
183  strncpy(result + reslen, str, length); /* Original part keeped */
184  strcpy(result + (reslen += length), newstr); /* Insert new string */
185 
186  reslen += newlen;
187  tmp += oldlen;
188  str = tmp;
189  }
190  strcpy(result + reslen, str); /* Copies last part and ending null char */
191  }
192 
193  if (count != NULL) *count = found;
194  return result;
195 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:267
void rterror(const char *fmt,...) __attribute__((format(printf
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
Definition: rt_context.c:191
int count
Definition: genraster.py:57
#define str(s)
Definition: raster2pgsql.c:34
#define _(String)
Definition: shpcommon.h:24

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

Here is the call graph for this function: