FreeSWITCH API Documentation  1.7.0
Functions
switch_mprintf.h File Reference
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

SWITCH_BEGIN_EXTERN_C char * switch_mprintf (const char *zFormat,...)
 
char * switch_vmprintf (const char *zFormat, va_list ap)
 
char * switch_snprintfv (char *zBuf, int n, const char *zFormat,...)
 

Function Documentation

SWITCH_BEGIN_EXTERN_C char* switch_mprintf ( const char *  zFormat,
  ... 
)

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 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_mprintf("INSERT INTO TABLES('%q')", zText);
 switch_core_db_exec(db, z, callback1, 0, 0);
 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.

Definition at line 897 of file switch_mprintf.c.

References base_vprintf(), printf_realloc(), SWITCH_PRINT_BUF_SIZE, and sgMprintf::zBase.

Referenced by core_event_handler(), load_mime_types(), preprocess_glob(), Event::serialize(), setup_ringback(), switch_channel_export_variable_var_check(), switch_console_complete(), switch_console_expand_alias(), switch_console_list_uuid(), switch_console_set_alias(), switch_core_add_registration(), switch_core_cert_gen_fingerprint(), switch_core_del_registration(), switch_core_expire_registration(), switch_core_file_close(), switch_core_gen_certs(), switch_core_recovery_flush(), switch_core_recovery_recover(), switch_core_recovery_track(), switch_core_recovery_untrack(), switch_core_session_exec(), switch_format_number(), switch_ivr_wait_for_answer(), switch_load_network_lists(), switch_loadable_module_enumerate_available(), switch_loadable_module_load_file(), switch_odbc_handle_get_error(), switch_play_and_get_digits(), switch_process_import(), switch_simple_email(), switch_stream_write_file_contents(), switch_xml_parse_file(), and tone_detect_set_total_time().

898 {
899  va_list ap;
900  char *z;
901  char zBase[SWITCH_PRINT_BUF_SIZE];
902  va_start(ap, zFormat);
903  z = base_vprintf(printf_realloc, 0, zBase, sizeof(zBase), zFormat, ap);
904  va_end(ap);
905  return z;
906 }
#define SWITCH_PRINT_BUF_SIZE
static char * base_vprintf(void *(*xRealloc)(void *, int), int useInternal, char *zInitBuf, int nInitBuf, const char *zFormat, va_list ap)
static void * printf_realloc(void *old, int size)
char* switch_snprintfv ( char *  zBuf,
int  n,
const char *  zFormat,
  ... 
)

Definition at line 914 of file switch_mprintf.c.

References base_vprintf().

Referenced by do_trans(), parse_presence_data_cols(), switch_cache_db_persistant_execute_trans_full(), and switch_core_sqldb_start().

915 {
916  char *z;
917  va_list ap;
918 
919  va_start(ap, zFormat);
920  z = base_vprintf(0, 0, zBuf, n, zFormat, ap);
921  va_end(ap);
922  return z;
923 }
static char * base_vprintf(void *(*xRealloc)(void *, int), int useInternal, char *zInitBuf, int nInitBuf, const char *zFormat, va_list ap)
char* switch_vmprintf ( const char *  zFormat,
va_list  ap 
)

Definition at line 888 of file switch_mprintf.c.

References base_vprintf(), printf_realloc(), SWITCH_PRINT_BUF_SIZE, and sgMprintf::zBase.

Referenced by cJSON_CreateStringPrintf(), and switch_console_stream_write().

889 {
890  char zBase[SWITCH_PRINT_BUF_SIZE];
891  return base_vprintf(printf_realloc, 0, zBase, sizeof(zBase), zFormat, ap);
892 }
#define SWITCH_PRINT_BUF_SIZE
static char * base_vprintf(void *(*xRealloc)(void *, int), int useInternal, char *zInitBuf, int nInitBuf, const char *zFormat, va_list ap)
static void * printf_realloc(void *old, int size)