PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ escape_connection_string()

char* escape_connection_string ( char *  str)

Escape strings that are to be used as part of a PostgreSQL connection string.

If no characters require escaping, simply return the input pointer. Otherwise return a new allocated string.

Definition at line 113 of file shpcommon.c.

114 {
115  /*
116  * Escape apostrophes and backslashes:
117  * ' -> \'
118  * \ -> \\
119  *
120  * 1. find # of characters
121  * 2. make new string
122  */
123 
124  char *result;
125  char *ptr, *optr;
126  int toescape = 0;
127  size_t size;
128 
129  ptr = str;
130 
131  /* Count how many characters we need to escape so we know the size of the string we need to return */
132  while (*ptr)
133  {
134  if (*ptr == '\'' || *ptr == '\\')
135  toescape++;
136 
137  ptr++;
138  }
139 
140  /* If we don't have to escape anything, simply return the input pointer */
141  if (toescape == 0)
142  return str;
143 
144  size = ptr - str + toescape + 1;
145  result = calloc(1, size);
146  optr = result;
147  ptr = str;
148 
149  while (*ptr)
150  {
151  if (*ptr == '\'' || *ptr == '\\')
152  *optr++ = '\\';
153 
154  *optr++ = *ptr++;
155  }
156 
157  *optr = '\0';
158 
159  return result;
160 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:262
#define str(s)

References result, and str.