PostGIS  2.1.10dev-r@@SVN_REVISION@@
shpcommon.c
Go to the documentation of this file.
1 /**********************************************************************
2  * $Id: shpcommon.c 5646 2010-05-27 13:19:12Z pramsey $
3  *
4  * PostGIS - Spatial Types for PostgreSQL
5  * http://postgis.net
6  * Copyright 2010 Mark Cave-Ayland <mark.cave-ayland@siriusit.co.uk>
7  *
8  * This is free software; you can redistribute and/or modify it under
9  * the terms of the GNU General Public Licence. See the COPYING file.
10  *
11  **********************************************************************/
12 
13 /* This file contains functions that are shared between the loader and dumper */
14 
15 #include <stdlib.h>
16 #include "shpcommon.h"
17 
18 
24 char *
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
char * escape_connection_string(char *str)
Escape strings that are to be used as part of a PostgreSQL connection string.
Definition: shpcommon.c:25