PostGIS  2.2.7dev-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 29 of file shpcommon.c.

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