Solved

Escaping double quotes

Posted on 2000-03-16
15
506 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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 300 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
undefined reference to `bswap_128' 9 173
How to configure Digital Ouput in Renesas RH850/FIL Micrcontroller ? 5 201
Test against App 49 138
Coverting 24 hour time to 12 hour in C++ 15 178
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

827 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