Header And Logo

PostgreSQL
| The world's most advanced open source database.

quotes.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * quotes.c
00004  *    string quoting and escaping functions
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  * Portions Copyright (c) 1994, Regents of the University of California
00008  *
00009  *
00010  * IDENTIFICATION
00011  *    src/port/quotes.c
00012  *
00013  *-------------------------------------------------------------------------
00014  */
00015 
00016 #include "c.h"
00017 
00018 /*
00019  * Escape (by doubling) any single quotes or backslashes in given string
00020  *
00021  * Note: this is used to process postgresql.conf entries and to quote
00022  * string literals in pg_basebackup for creating recovery.conf.
00023  * Since postgresql.conf strings are defined to treat backslashes as escapes,
00024  * we have to double backslashes here.
00025  *
00026  * Since this function is only used for parsing or creating configuration
00027  * files, we do not care about encoding considerations.
00028  *
00029  * Returns a malloced() string that it's the responsibility of the caller
00030  * to free.
00031  */
00032 char *
00033 escape_single_quotes_ascii(const char *src)
00034 {
00035     int         len = strlen(src),
00036                 i,
00037                 j;
00038     char       *result = malloc(len * 2 + 1);
00039 
00040     if (!result)
00041         return NULL;
00042 
00043     for (i = 0, j = 0; i < len; i++)
00044     {
00045         if (SQL_STR_DOUBLE(src[i], true))
00046             result[j++] = src[i];
00047         result[j++] = src[i];
00048     }
00049     result[j] = '\0';
00050     return result;
00051 }