Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 513
  • Last Modified:

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;
}
0
mdlittle
Asked:
mdlittle
  • 7
  • 4
  • 3
  • +1
1 Solution
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
C_DreamerCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now