• C

Escaping double quotes

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;
}
mdlittleAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
C_DreamerConnect With a Mentor Commented:
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
 
ntdragonCommented:
i think that i didn't understand your question right
explain it to me
0
 
mdlittleAuthor Commented:
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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
DKostovCommented:
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
 
ntdragonCommented:
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
 
C_DreamerCommented:
Undefined behaviour if the first character in the string is a quotation mark. In other words, no cigar for you.
0
 
mdlittleAuthor Commented:
Adjusted points from 200 to 300
0
 
mdlittleAuthor Commented:
C Dreamer - How would you fix that?
0
 
ntdragonCommented:
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
 
mdlittleAuthor Commented:
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
 
ntdragonCommented:
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
 
mdlittleAuthor Commented:
Look at C Dreamers message and you will get your answer.
0
 
C_DreamerCommented:
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
 
mdlittleAuthor Commented:
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
 
mdlittleAuthor Commented:
C_Dreamer - please post your solution as an answer so I can award the points..

Mike
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.