[CODE] calling char function from sprintf

From: Artovil (artovil@arcanerealms.org)
Date: 09/05/01


Hello,

I have been wrestling with my MySQL conversion now for a week or so, and
everything is going quite well, I already got actions into MySQL, including
the OLC part of it.

One thing though, I sent a couple of questions a week or so ago about
escaping ' from strings.  I think Mike supplied a void, but I needed a
char, so he sent me one off-list.  We have used that and remodeled it
slightly, and it seems to work ok, but for one thing.

When I use it in a sprintf all the values set with the char function end up
being the same.  What am I doing wrong?

Here's a faked query string:
----8<----8<----8<----8<----8<----8<----8<----
     sprintf(query_str, "REPLACE INTO %s SET "
         "Name = '%s', "
         "Password = '%s', "
         "Email = '%s', "
         "Title = '%s', "
         "Description = '%s';"
           MYSQL_TABLE_PLAYER_INDEX,
           GET_NAME(ch),
           GET_PASSWD(ch),
           GET_EMAIL(ch),
           escape_quotes(GET_TITLE(ch)),
           escape_quotes(buf)
     );
----8<----8<----8<----8<----8<----8<----8<----
Notice the escape_quotes().  When they are called within the sprintf both
title and description are set to the same value.

The actual char function:
----8<----8<----8<----8<----8<----8<----8<----
char *escape_quotes(char *source) {
   static char dest[MAX_STRING_LENGTH * 2];
   *dest = 0;
   if (!source) {
     return ""; /* Error */
   }  else {
     while (*source) {
       if (*source == '\'')
         strcat(dest, "\\\'");
       else
         strncat(dest, source, 1);
       source++;
     }
   }
   return dest;
}
----8<----8<----8<----8<----8<----8<----8<----

Can anyone help me?

Kind regards,
/Artovil

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/06/01 PST