Skip to content

Instantly share code, notes, and snippets.

@dylanj
Created October 21, 2011 07:06
Show Gist options
  • Save dylanj/1303271 to your computer and use it in GitHub Desktop.
Save dylanj/1303271 to your computer and use it in GitHub Desktop.
get some sqlite quotes going on yo'
char *sqlite_sanitize( char* str )
{
char *safe_str;
int str_len = strlen( str );
int str_bad_chars = 0;
for( int i = 0; i < str_len; i++ )
if ( str[i] == '\'' || str[i] == '\"' )
str_bad_chars++;
safe_str = alloca( str_len + ( str_bad_chars * 2 ) ); //, sizeof( char ) );
int j = 0;
for( int i = 0; i < str_len; i++ ) {
if ( str[i] == '\'' ) {
safe_str[j] = '\\';
safe_str[j+1] = '\'';
j++;
} else if ( str[i] == '\\' ) {
safe_str[j] = '\'';
safe_str[j+1] = '\"';
j++;
} else {
safe_str[j] = str[i];
}
j++;
}
return safe_str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment