PostGIS  2.1.10dev-r@@SVN_REVISION@@
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 25 of file shpcommon.c.

References result.

26 {
27  /*
28  * Escape apostrophes and backslashes:
29  * ' -> \'
30  * \ -> \\
31  *
32  * 1. find # of characters
33  * 2. make new string
34  */
35 
36  char *result;
37  char *ptr, *optr;
38  int toescape = 0;
39  size_t size;
40 
41  ptr = str;
42 
43  /* Count how many characters we need to escape so we know the size of the string we need to return */
44  while (*ptr)
45  {
46  if (*ptr == '\'' || *ptr == '\\')
47  toescape++;
48 
49  ptr++;
50  }
51 
52  /* If we don't have to escape anything, simply return the input pointer */
53  if (toescape == 0)
54  return str;
55 
56  size = ptr - str + toescape + 1;
57  result = calloc(1, size);
58  optr = result;
59  ptr = str;
60 
61  while (*ptr)
62  {
63  if (*ptr == '\'' || *ptr == '\\')
64  *optr++ = '\\';
65 
66  *optr++ = *ptr++;
67  }
68 
69  *optr = '\0';
70 
71  return result;
72 }
char ** result
Definition: liblwgeom.h:218