473,624 Members | 2,685 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dequote a string, unescaping escaped quotes

Hi,

I am trying to write a C function which will dequote the string in a
char * variable, and unescape any escaped quotes, so that, for example:

"hello"

would become:

hello

and:

"\"hello\""

would become:

"hello"

much in the same way as char *argv command-line arguments are treated
when passed to function main.

Please can you help.

Regards,
Jonny
Dec 19 '05 #1
7 9391
Jonny wrote:
I am trying to write a C function which will dequote the string in a
char * variable, and unescape any escaped quotes, so that, for example:

"hello"

would become:

hello

and:

"\"hello\""

would become:

"hello"


Dear Jonny!

I think the best way is to go through the string character-by-character;
then you simply ignore the opening quote und continue until you find its
contra-piece.

Inside the loop you copy every character which isn't a quote nor a
backslash to your output; if you find a backslash, read the next
character and output it, regardless of its meaning.

Maybe you might want some further checks inside that, but in general
this should implement what you expect.

Example code (without error-checking or something like that):

void unescape(const char* in, char* out)
{
// Opening quote expected.
assert(*in=='\" ');

// Loop until closing quote is found.
for(++in; *in!='\"'; ++in)
{

// Character to copy?
if(*in!='\\')
{
*(out++)=*in;
continue;
}

// No. Copy next one.
*(out++)=*(++in );
}
}

Yours,
Daniel
Dec 19 '05 #2
Daniel Kraft wrote:
<snip>
Example code (without error-checking or something like that):

void unescape(const char* in, char* out)
{
// Opening quote expected.
C99 style comments can be misleading on usenet.
assert(*in=='\" ');
Note that the assert macro should be used to make debugging
easier and not to catch errors, let alone perform error handling.
Example:
if (*in != '\"') {
/* You can issue an error message if wished
fprintf(stderr, "useful error message pointing to line,"
" function, and file\n");
* maybe implemented by macro
*/
return;
}
This is quite enough. If you feel sure that you need to abort
the program on an error, use
if (*in != '\"') {
assert(0 && "Unexpected character, expected \'\"\'");
return;
}
That works even if _NDEBUG is defined i.e. when assert does
nothing.
// Loop until closing quote is found.
for(++in; *in!='\"'; ++in)
{

// Character to copy?
if(*in!='\\')
{
*(out++)=*in;
continue;
}

// No. Copy next one.
*(out++)=*(++in );
An if/else construction may be clearer than if/continue.
Note that the above will also "ruin" explicitly wanted
backslashes.
}
}

Yours,
Daniel

--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Dec 19 '05 #3
Jonny wrote:
Hi,

I am trying to write a C function which will dequote the string in a
char * variable, and unescape any escaped quotes, so that, for example:

"hello"

would become:

hello

and:

"\"hello\""

would become:

"hello"

much in the same way as char *argv command-line arguments are treated
when passed to function main.


Do you want to copy the source string to a destination string
or replace everything in the source string? You can do both with
one function but be sure to think it through before.

Finding:
Just use strchr() to find quotes. If the number of backslashes
immediately preceding the quote is odd, you can 'replace' \" by ",
otherwise you replace " by nothing.
Note that you must not run to a position before the beginning
of the string, so use strrchr() to count backwards or take
appropriate precautions. Restart strchr() and your counting such
that you do not change already changed parts of the string.

Bring your best shot at it for us to see and we will help you
by pointing out problems and answering C questions.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Dec 19 '05 #4
Jonny wrote:
Hi,

I am trying to write a C function which will dequote the string in a
char * variable, and unescape any escaped quotes, so that, for example:
<snip examples>

Then go ahead and write it.
Please can you help.


Yes, you've missed off a semi-colon on line 42.

In other words, if you want help with your code then post it together
with information about what specific problems you have with it. We are
not going to write your code for you.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Dec 19 '05 #5
Michael Mair <Mi**********@i nvalid.invalid> wrote:
This is quite enough. If you feel sure that you need to abort
the program on an error, use
if (*in != '\"') {
assert(0 && "Unexpected character, expected \'\"\'");
return;
I suspect that OP will want

exit( EXIT_FAILURE );

if he does in fact want to abort the program on an error in the
presence of _NDEBUG.
}


--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Dec 19 '05 #6
Jonny <ww******@ntlwo rld.com> writes:
I am trying to write a C function which will dequote the string in a
char * variable, and unescape any escaped quotes, so that, for example:

"hello"

would become:

hello

and:

"\"hello\""

would become:

"hello"

much in the same way as char *argv command-line arguments are treated
when passed to function main.


You'll need to define the problem more precisely. The language
doesn't define any particular treatment of command-line arguments;
they're just made available to the program by some system-specific
means.

If you were on a Unix-like system, I'd say that you're probably trying
to duplicate what the shell does with command-line arguments before
invoking a program; for example

echo "\"hello\""

translates each \" to " and drops the outer " characters. This is
often very similar to the way string literals are treated within a C
program, but it's not identical. It can also vary from one shell to
another, and even within some shells depending on what options are
specified.

That's just one example of what you might be talking about. It's
likely to be different on other operating systems.

Once you've defined the problem, try implementing a solution in C. If
you have problems with it, post here again and we can try to help.

If this is a homework assignment, please say so.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 19 '05 #7
I wrote:
I am trying to write a C function which will dequote the string in a
char * variable, and unescape any escaped quotes, so that, for example:

"hello"

would become:

hello

and:

"\"hello\""

would become:

"hello"

much in the same way as char *argv command-line arguments are treated
when passed to function main.

Please can you help.


Many thanks to you all for your help and advice. I wasn't sure where to
start, but you have given me enough information to have a go at this.

Regards,
Jonny
Dec 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2174
by: Julia Briggs | last post by:
Hello, trying to create logic tests against a string allowing someone to enter a certain character once or twice into a form consecutively, but not twice seperated between other characters.... so, hello##there (ok) hello#there (ok) #hello#there (no) ##hello#there (no)
1
1325
by: Yodai | last post by:
Hi all... I got this script where I have to declare a string thats as wide as a whole webpage. The problem is that it takes several lines to finish the whole string and, when I try to load it, I get an error saying that there was a caracter expected at the end of the line... Does anybody know how can I make notice that my variable is several lines wide? <html>
3
38816
by: Ryan Folstad | last post by:
Hello i have a problem with c# string handling. Hopefully someone can explain what im doing wrong. this code: string windowsdir = "c:\\windows"; string installdir ="c:\\install"; string path= String.Format("{0}\\system32\\cscript.exe {1}\\filename.vbs", windowsdir, installdir); The inspector shows:
12
9628
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" & Chr(34) & PopUpWindowTitle & Chr(34) & ", " & Chr(34) & CurrentEventDetails & ")" strTemp += "<BR><A HREF='#' onClick='" & PopupLink & "'>" & EventName & "</A><BR>" The problem I have is that when the string variables or contain a string with an...
2
8419
by: Vance Kessler | last post by:
We are trying write a new ASP.NET page to work with an existing stateless ASP application. The ASP application creates a cookie and of course stores the cookie values as escaped strings (using the vbscript escape function). I am have a terrible time 'unescaping' that string with C# under ASP.NET. I have tried the following: HttpUtility.UrlDecode HttpUtility.HtmlDecode (this should not work but I tried it)...
4
2334
by: Robert | last post by:
Hello. I have tried to remove the char "\" from a string that I am building in codebehind. to be used in a script tag. I have tried adding (char)34 to the string instead of the escape character, as well as @then double quotes. when i watch the string in the command window or the watch window it has the escape character in the string allways \"
1
5811
by: Mark Rae | last post by:
Hi folks, Apologies - am having a bit of a "senior moment"... Is there a way to convert a string containing escaped characters into a verbatim string literal? I.e. converting something like "This string contains \"double quotes\" and \'single quotes\'"
3
2876
by: John Nagle | last post by:
Here's a URL from a link on the home page of a major company. <a href="/adsk/servlet/index?siteID=123112&amp;id=1860142">About Us</a> Yes, that "&amp;" is in the source text of the page. This is, in fact, correct HTML. See http://www.htmlhelp.com/tools/validator/problems.html#amp
4
12743
by: Michael Yanowitz | last post by:
Hello: If I have a long string (such as a Python file). I search for a sub-string in that string and find it. Is there a way to determine if that found sub-string is inside single-quotes or double-quotes or not inside any quotes? If so how? Thanks in advance: Michael Yanowitz
0
8251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8352
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6115
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4085
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2614
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1496
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.