PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ rtpg_strreplace()

char* rtpg_strreplace ( const char *  str,
const char *  oldstr,
const char *  newstr,
int *  count 
)

Definition at line 55 of file rtpg_internal.c.

Referenced by RASTER_colorMap(), RASTER_mapAlgebra2(), RASTER_mapAlgebraExpr(), RASTER_nMapAlgebraExpr(), and rtpg_removespaces().

59  {
60  const char *tmp = str;
61  char *result;
62  int found = 0;
63  int length, reslen;
64  int oldlen = strlen(oldstr);
65  int newlen = strlen(newstr);
66  int limit = (count != NULL && *count > 0) ? *count : -1;
67 
68  tmp = str;
69  while ((tmp = strstr(tmp, oldstr)) != NULL && found != limit)
70  found++, tmp += oldlen;
71 
72  length = strlen(str) + found * (newlen - oldlen);
73  if ((result = (char *) palloc(length + 1)) == NULL) {
74  fprintf(stderr, "Not enough memory\n");
75  found = -1;
76  }
77  else {
78  tmp = str;
79  limit = found; /* Countdown */
80  reslen = 0; /* length of current result */
81 
82  /* Replace each old string found with new string */
83  while ((limit-- > 0) && (tmp = strstr(tmp, oldstr)) != NULL) {
84  length = (tmp - str); /* Number of chars to keep intouched */
85  strncpy(result + reslen, str, length); /* Original part keeped */
86  strcpy(result + (reslen += length), newstr); /* Insert new string */
87 
88  reslen += newlen;
89  tmp += oldlen;
90  str = tmp;
91  }
92  strcpy(result + reslen, str); /* Copies last part and ending null char */
93  }
94 
95  if (count != NULL) *count = found;
96  return result;
97 }
int count
Definition: genraster.py:56
Here is the caller graph for this function: