4 messages in com.mysql.lists.plusplusRe: escape_string() buggy
FromSent OnAttachments
Sinisa Milivojevic31 Oct 2003 02:57 
Matthias Dahl31 Oct 2003 03:02 
Matthias Dahl31 Oct 2003 03:22 
Sinisa Milivojevic01 Nov 2003 02:54 
Subject:Re: escape_string() buggy
From:Sinisa Milivojevic (sin@beotel.yu)
Date:11/01/2003 02:54:47 AM
List:com.mysql.lists.plusplus

On Fri, 31 Oct 2003 12:23:01 +0100 Matthias Dahl <mdml@designassembly.de> wrote:

It works OK if used as instructed.

Maybe I am wrong, but...

As you can clearly see, the '"' characters have been wrongfully escaped to "\ instead of \".

Please correct me if I am totally loosing my mind here. :-)

Again, use escape manipulater instead of calling a function directly. That is
why it does not work.

If you prefer a function, use this one:

void escape_string (string& s) { if (!s.size()) return; for (int i = 0;i<s.size();i++) { switch (s[i]) { case '\0': /* Must be escaped for "mysql" */ s[i] = '\\'; s.insert(i,"0",1); i++; break; case '\n': /* Must be escaped for logs */ s[i] = '\\'; s.insert(i,"n",1); i++; break; case '\r': s[i] = '\\'; s.insert(i,"r",1); i++; break; case '\\': s[i] = '\\'; s.insert(i,"\\",1); i++; break; case '\"': s[i] = '\\'; s.insert(i,"\"",1); i++; break; case '\'': /* Better safe than sorry */ s[i] = '\\'; s.insert(i,"\'",1); i++; break; case '\032': /* This gives problems on Win32 */ s[i] = '\\'; s.insert(i,"Z",1); i++; break; default: break; } } }

Sincerely,