Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Escaping double quotes

Posted on 2000-03-16
15
Medium Priority
?
512 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
ID: 2625333
i think that i didn't understand your question right
explain it to me
0
 

Author Comment

by:mdlittle
ID: 2625419
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
ID: 2625506
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Expert Comment

by:ntdragon
ID: 2625611
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
ID: 2630769
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
ID: 2631418
Adjusted points from 200 to 300
0
 

Author Comment

by:mdlittle
ID: 2631419
C Dreamer - How would you fix that?
0
 
LVL 1

Expert Comment

by:ntdragon
ID: 2631434
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
ID: 2631438
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
ID: 2632021
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
ID: 2632040
Look at C Dreamers message and you will get your answer.
0
 

Expert Comment

by:C_Dreamer
ID: 2632078
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
ID: 2634853
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
ID: 2656043
C_Dreamer - please post your solution as an answer so I can award the points..

Mike
0
 

Accepted Solution

by:
C_Dreamer earned 1200 total points
ID: 2656169
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 recursion in the C programming language.

963 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