PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ strreplace()

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

Definition at line 141 of file raster2pgsql.c.

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

Referenced by add_overview_constraints(), add_raster_constraints(), and insert_records().

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