PostGIS  2.1.10dev-r@@SVN_REVISION@@
char * escape_copy_string ( char *  str)

Escape input string suitable for COPY.

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

Definition at line 139 of file shp2pgsql-core.c.

References result.

Referenced by ShpLoaderGenerateSQLRowStatement().

140 {
141  /*
142  * Escape the following characters by adding a preceding backslash
143  * tab, backslash, cr, lf
144  *
145  * 1. find # of escaped characters
146  * 2. make new string
147  *
148  */
149 
150  char *result;
151  char *ptr, *optr;
152  int toescape = 0;
153  size_t size;
154 
155  ptr = str;
156 
157  /* Count how many characters we need to escape so we know the size of the string we need to return */
158  while (*ptr)
159  {
160  if (*ptr == '\t' || *ptr == '\\' || *ptr == '\n' || *ptr == '\r')
161  toescape++;
162 
163  ptr++;
164  }
165 
166  /* If we don't have to escape anything, simply return the input pointer */
167  if (toescape == 0)
168  return str;
169 
170  size = ptr - str + toescape + 1;
171  result = calloc(1, size);
172  optr = result;
173  ptr = str;
174 
175  while (*ptr)
176  {
177  if ( *ptr == '\t' || *ptr == '\\' || *ptr == '\n' || *ptr == '\r' )
178  *optr++ = '\\';
179 
180  *optr++ = *ptr++;
181  }
182 
183  *optr = '\0';
184 
185  return result;
186 }
char ** result
Definition: liblwgeom.h:218

Here is the caller graph for this function: