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_del_registration(), switch_core_expire_registration(), switch_core_file_close(), switch_core_session_exec(), switch_format_number(), switch_ivr_wait_for_answer(), switch_load_network_lists(), switch_loadable_module_enumerate_available(), switch_odbc_handle_get_error(), switch_process_import(), switch_simple_email(), switch_stream_write_file_contents(), and switch_xml_parse_file().

00898 {
00899         va_list ap;
00900         char *z;
00901         char zBase[SWITCH_PRINT_BUF_SIZE];
00902         va_start(ap, zFormat);
00903         z = base_vprintf(printf_realloc, 0, zBase, sizeof(zBase), zFormat, ap);
00904         va_end(ap);
00905         return z;
00906 }

char* switch_snprintfv ( char *  zBuf,
int  n,
const char *  zFormat,
  ... 
)

Definition at line 914 of file switch_mprintf.c.

References base_vprintf().

Referenced by parse_presence_data_cols(), switch_cache_db_persistant_execute_trans(), and switch_core_sqldb_start().

00915 {
00916         char *z;
00917         va_list ap;
00918 
00919         va_start(ap, zFormat);
00920         z = base_vprintf(0, 0, zBuf, n, zFormat, ap);
00921         va_end(ap);
00922         return z;
00923 }

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 switch_console_stream_write().

00889 {
00890         char zBase[SWITCH_PRINT_BUF_SIZE];
00891         return base_vprintf(printf_realloc, 0, zBase, sizeof(zBase), zFormat, ap);
00892 }


Generated on Sun May 20 04:00:12 2012 for FreeSWITCH API Documentation by  doxygen 1.4.7