Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to escape string

Reply
Thread Tools

how to escape string

 
 
qilin
Guest
Posts: n/a
 
      06-08-2007
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 ?

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      06-08-2007
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.
 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      06-09-2007

"qilin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      06-09-2007
Malcolm McLean wrote:
>
> "qilin" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to read strings cantaining escape character from a file and useit as escape sequences? slomo Python 5 12-02-2007 11:39 AM
escape character in XML text string Testbruger@intervisit.com XML 7 01-24-2004 06:43 PM
Problem With String Containing JavaScript Escape Character Jeff S ASP .Net 12 11-10-2003 07:19 PM
raw string tail escape revisited Bengt Richter Python 2 08-09-2003 05:44 PM



Advertisments