mirror of
https://github.com/signalwire/freeswitch.git
synced 2026-07-24 05:02:10 +00:00
Adding switch_mprintf (broken out from sqlite)
Adding new %w to mprintf to auto escape both single quote and backslashes Improve the tab completion a tad and fix some sqlite/odbc compat with new mprintf opts Change the default stream writer to use switch_vmprintf so %q/%w can be used there too git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@15875 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
** 2001 September 22
|
||||
**
|
||||
** The author disclaims copyright to this source code. In place of
|
||||
** a legal notice, here is a blessing:
|
||||
**
|
||||
** May you do good and not evil.
|
||||
** May you find forgiveness for yourself and forgive others.
|
||||
** May you share freely, never taking more than you give.
|
||||
**
|
||||
*************************************************************************
|
||||
*/
|
||||
#ifndef SWITCH_MPRINTF_H
|
||||
#define SWITCH_MPRINTF_H
|
||||
|
||||
SWITCH_BEGIN_EXTERN_C
|
||||
|
||||
/**
|
||||
* This routine is a variant of the "sprintf()" from the
|
||||
* standard C library. The resulting string is written into memory
|
||||
* obtained from malloc() so that there is never a possiblity of buffer
|
||||
* overflow. This routine also implement some additional formatting
|
||||
* options that are useful for constructing SQL statements.
|
||||
*
|
||||
* The strings returned by this routine should be freed by calling
|
||||
* switch_core_db_free().
|
||||
*
|
||||
* All of the usual printf formatting options apply. In addition, there
|
||||
* is a "%q" option. %q works like %s in that it substitutes a null-terminated
|
||||
* string from the argument list. But %q also doubles every '\'' character.
|
||||
* %q is designed for use inside a string literal. By doubling each '\''
|
||||
* character it escapes that character and allows it to be inserted into
|
||||
* the string.
|
||||
*
|
||||
* For example, so some string variable contains text as follows:
|
||||
*
|
||||
* char *zText = "It's a happy day!";
|
||||
*
|
||||
* We can use this text in an SQL statement as follows:
|
||||
*
|
||||
* char *z = switch_core_db_mprintf("INSERT INTO TABLES('%q')", zText);
|
||||
* switch_core_db_exec(db, z, callback1, 0, 0);
|
||||
* switch_core_db_free(z);
|
||||
*
|
||||
* Because the %q format string is used, the '\'' character in zText
|
||||
* is escaped and the SQL generated is as follows:
|
||||
*
|
||||
* INSERT INTO table1 VALUES('It''s a happy day!')
|
||||
*
|
||||
* This is correct. Had we used %s instead of %q, the generated SQL
|
||||
* would have looked like this:
|
||||
*
|
||||
* INSERT INTO table1 VALUES('It's a happy day!');
|
||||
*
|
||||
* This second example is an SQL syntax error. As a general rule you
|
||||
* should always use %q instead of %s when inserting text into a string
|
||||
* literal.
|
||||
*/
|
||||
SWITCH_DECLARE(char *) switch_mprintf(const char *zFormat, ...);
|
||||
SWITCH_DECLARE(char *) switch_vmprintf(const char *zFormat, va_list ap);
|
||||
|
||||
SWITCH_END_EXTERN_C
|
||||
#endif /* SWITCH_MPRINTF_H */
|
||||
Reference in New Issue
Block a user