how to escape string

Q

qilin

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 ?
 
I

Ian Collins

qilin said:
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.
 
M

Malcolm McLean

qilin said:
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;
}
}
 
I

Ian Collins

Malcolm said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top