Solved

Escaping double quotes

Posted on 2000-03-16
15
502 Views
Last Modified: 2010-04-15
The following code escapes double quotes. I works fine execpt if the quote is already escaped (\) it adds another back slash. How do I make it so the code looks for a back slash before adding one.

int escape_quotes_db(char *buffer) {
      char temp_string[MAX_QUERY_LENGTH];
      char *p, *q;

      for (p=buffer,q=temp_string; *q=*p; *q++=*p++)
            if (*p=='"') {
                  *q++='\\';

            }
      strcpy(buffer, temp_string);
      return;
}
0
Comment
Question by:mdlittle
  • 7
  • 4
  • 3
  • +1
15 Comments
 
LVL 1

Expert Comment

by:ntdragon
Comment Utility
i think that i didn't understand your question right
explain it to me
0
 

Author Comment

by:mdlittle
Comment Utility
This routine scans a string for a double quote ("). If it finds one it 'escapes' it by prepending a backslash (\) in front of the double quote.

The problem is that if double quote is alread escaped it adds another back slash. so you end up with:

\\"

I would like to fix the code so it checks for a \ in front of the ". If it does not find one then go ahead and add the \. otherwise leave it alone
0
 
LVL 2

Expert Comment

by:DKostov
Comment Utility
int escape_quotes_db(char *buffer) {
char temp_string[MAX_QUERY_LENGTH];
char *p, *q;

for (p=buffer,q=temp_string; *q=*p; *q++=*p++)
if (*p=='"' && *(p-1) != '\\') {
*q++='\\';

}
strcpy(buffer, temp_string);
return;
}
0
 
LVL 1

Expert Comment

by:ntdragon
Comment Utility
i think you got some logic mistakes

1)what do you mean by *q=*p you set *q to be *p???

do you really understand why does it work
'cause as i understand in the if statment you delete the '\"' and put instead of it '\\' and before it enters the next for you put the '\"'after the
'\\' or something like that

why won't you try this code:

int escape_quotes_db(char *buffer) {
char temp_string[MAX_QUERY_LENGTH];
char *p, *q,*end;
end=buffer+strlen(buffer)+1;


for (p=buffer,q=temp_string; p==end; p++){
if ((*p=='"') && (*(p-1) != '\\')) {
  *q='\\';
  *(++q)='"';
}
else *q=*p;
q++;
}

strcpy(buffer, temp_string);
return;
}




0
 

Expert Comment

by:C_Dreamer
Comment Utility
Undefined behaviour if the first character in the string is a quotation mark. In other words, no cigar for you.
0
 

Author Comment

by:mdlittle
Comment Utility
Adjusted points from 200 to 300
0
 

Author Comment

by:mdlittle
Comment Utility
C Dreamer - How would you fix that?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Expert Comment

by:ntdragon
Comment Utility
mdlittle do you accept the answer/coments that you got or not
or tell us what problem do you have and we"ll try to help you
0
 

Author Comment

by:mdlittle
Comment Utility
No I don't C Dragon raised a good point and it is consistant with the problem I am having. Do you know how to fixt it? If so I will award points.

Thanks
0
 
LVL 1

Expert Comment

by:ntdragon
Comment Utility
explain to me and the others what didn't you like in our comments
'cause maybe we don't understand exectlly your problem 'cause i think we answered you question
so please tell us the problem
<take for example my comment and tell me why it's not helping you 'cause maybe i didn't understood what did you want>
0
 

Author Comment

by:mdlittle
Comment Utility
Look at C Dreamers message and you will get your answer.
0
 

Expert Comment

by:C_Dreamer
Comment Utility
Here ya go:

int escape_quotes_db(char *buffer) {
char temp_string[MAX_QUERY_LENGTH];
char *p, *q;
int escape = 0;

p = buffer;
q = temp_string;

while(*p)
{
  /* If this is an escape char */
  if(*p == '\\')
  {
    /* Remember that fact */
    escape = 1;
  }
  else
  {
    if(escape)
    {
      /* Time to forget it was an escape */
      escape = 0;
    }
    else if(*p == '"')
    {
      /* unescaped quote */
      *q++ = '\\';
    }
  }
  /* copy character */
  *q++ = *p++;
}

strcpy(buffer, temp_string);
return;
}

I haven't bothered testing it, so take care. But it should work fine.

C_D

0
 

Author Comment

by:mdlittle
Comment Utility
C Dreamer, I am testing the code now. It looks good. I will award the point when I finish in the morning. Thanks for all your help
0
 

Author Comment

by:mdlittle
Comment Utility
C_Dreamer - please post your solution as an answer so I can award the points..

Mike
0
 

Accepted Solution

by:
C_Dreamer earned 300 total points
Comment Utility
Sure, not a problem. Here you are:

#include <string.h>
#define MAX_QUERY_LENGTH 256 /* or whatever you like */

int escape_quotes_db(char *buffer) {
char temp_string[MAX_QUERY_LENGTH];
char *p, *q;
int escape = 0;

p = buffer;
q = temp_string;

while(*p)
{
  /* If this is an escape char */
  if(*p == '\\')
  {
    /* Remember that fact */
    escape = 1;
  }
  else
  {
    if(escape)
    {
      /* Time to forget it was an escape */
      escape = 0;
    }
    else if(*p == '"')
    {
      /* unescaped quote */
      *q++ = '\\';
    }
  }
  /* copy character */
  *q++ = *p++;
}

strcpy(buffer, temp_string);
return 0;
}

Regards

C_D

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now