Created
October 21, 2011 07:06
-
-
Save dylanj/1303271 to your computer and use it in GitHub Desktop.
get some sqlite quotes going on yo'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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