how to escape string

Discussion in 'C Programming' started by qilin, Jun 8, 2007.

  1. qilin

    qilin Guest

    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 ?
    qilin, Jun 8, 2007
    #1
    1. Advertising

  2. qilin

    Ian Collins Guest

    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.
    Ian Collins, Jun 8, 2007
    #2
    1. Advertising

  3. "qilin" <> wrote in message
    news:...
    >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
    Malcolm McLean, Jun 9, 2007
    #3
  4. qilin

    Ian Collins Guest

    Malcolm McLean wrote:
    >
    > "qilin" <> wrote in message
    > news:...
    >> 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.
    Ian Collins, Jun 9, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff S
    Replies:
    12
    Views:
    13,844
    Jeff S
    Nov 10, 2003
  2. Replies:
    7
    Views:
    11,686
    Julian Reschke
    Jan 24, 2004
  3. Bengt Richter

    raw string tail escape revisited

    Bengt Richter, Aug 9, 2003, in forum: Python
    Replies:
    2
    Views:
    410
    Bengt Richter
    Aug 9, 2003
  4. Ivo Woltring

    Escape chars in string

    Ivo Woltring, Nov 8, 2004, in forum: Python
    Replies:
    2
    Views:
    324
    Ivo Woltring
    Nov 9, 2004
  5. slomo
    Replies:
    5
    Views:
    1,506
    Duncan Booth
    Dec 2, 2007
Loading...

Share This Page