By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 882 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

how to escape string

P: n/a
I am trying to save a big text string into MySQL, but I guess i need
escape the string firstly, anybody knows any escape function in c for
that? or any other suggests ?

Jun 8 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
qilin wrote:
I am trying to save a big text string into MySQL, but I guess i need
escape the string firstly, anybody knows any escape function in c for
that? or any other suggests ?
There isn't a standard C one, but there a MySQL one, search your docs
for escape.

--
Ian Collins.
Jun 8 '07 #2

P: n/a

"qilin" <gd*****@gmail.comwrote in message
news:11**********************@x35g2000prf.googlegr oups.com...
>I am trying to save a big text string into MySQL, but I guess i need
escape the string firstly, anybody knows any escape function in c for
that? or any other suggests ?
Here's some code from my comma-separated valuses to C struct converter.
You'll have to change it to make the FILE * into a pointer to a buffer -
remember to make it at least twice as big as the string plus one in case
passed a string full of escapes.

/*
write a string as a C string to stream
Params: str - the string
fp - output file
Notes:handles escapes
*/
int dumpstring(const char *str, FILE *fp)
{
char buff[64];

if(!str)
{
fprintf(fp, "0");
return 0;
}
fputc('\"', fp);
while(*str)
{
if(escaped(*str))
{
escape(buff, *str);
fprintf(fp, "%s", buff);
}
fputc(*str, fp);
str++;
}
fputc('\"', fp);

return 0;
}

/*
is a character escaped?
Params: ch - character to test
Returns: 1 if escaped, 0 if normal
*/
int escaped(int ch)
{
return strchr("\\\a\b\n\r\t\'\"\f\v", ch) ? 1 : 0;
}

/*
get the escape sequence for a character
Params: out - output buffer (currently max 2 + nul but allow for more)
ch - the character to escape
*/
void escape(char *out, int ch)
{
switch(ch)
{
case '\n':
strcpy(out, "\\n"); break;
case '\t':
strcpy(out, "\\t"); break;
case '\v':
strcpy(out, "\\v"); break;
case '\b':
strcpy(out, "\\b"); break;
case '\r':
strcpy(out, "\\r"); break;
case '\f':
strcpy(out, "\\f"); break;
case '\a':
strcpy(out, "\\a"); break;
case '\\':
strcpy(out, "\\\\"); break;
case '\'':
strcpy(out, "\\\'"); break;
case '\"':
strcpy(out, "\\\""); break;
default:
out[0] = (char) ch; break;
out[1] = 0;
}
}

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 9 '07 #3

P: n/a
Malcolm McLean wrote:
>
"qilin" <gd*****@gmail.comwrote in message
news:11**********************@x35g2000prf.googlegr oups.com...
>I am trying to save a big text string into MySQL, but I guess i need
escape the string firstly, anybody knows any escape function in c for
that? or any other suggests ?

Here's some code from my comma-separated valuses to C struct converter.
You'll have to change it to make the FILE * into a pointer to a buffer -
remember to make it at least twice as big as the string plus one in case
passed a string full of escapes.
Could this be a classic case of the wrong answer to an OT post?

In the context of MySQL, escaping a string involves escaping characters
that can cause problems in a database, that's why I told the OP to check
the MySQL documentation for the well documented appropriate function.

--
Ian Collins.
Jun 9 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.