Exclude all characters that need an escape  [SOLVED]

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Wanderer82
Earned some good credits
Earned some good credits
Posts: 130
Joined: Tue Jul 26, 2016 2:00 pm

Exclude all characters that need an escape

Postby Wanderer82 » Sat Jun 23, 2018 12:15 pm

If in a program you have user input from a TextCtrl that you need further on inside a string, there's always the problem of characters that need an escape (\). So my question is: Is there an easy way (function or so) to exclude user input of all characters that need a \ or to automatically replace all these characters with an \ ?

In my specific case... I have text files with product names and other information. The administrator of the program can create new products and thus has to chose product names. This input is then saved in a new text file. The product name is used in the program for the shopping cart. So the product names are read into a string. As these names are read from the text files, all the characters that need a \ have to be written like this into the text file.

Thanks,
Thomas

tuk1
Experienced Solver
Experienced Solver
Posts: 59
Joined: Sun Oct 08, 2017 9:36 am

Re: Exclude all characters that need an escape

Postby tuk1 » Sat Jun 23, 2018 12:46 pm

wxWidgets(v3.0.3) - Vs2015(v14) - Win7(x64) - DialogBlocks(v5.15.3_Unicode)

Wanderer82
Earned some good credits
Earned some good credits
Posts: 130
Joined: Tue Jul 26, 2016 2:00 pm

Re: Exclude all characters that need an escape

Postby Wanderer82 » Sat Jun 23, 2018 3:05 pm

Thanks, didn't know about this functionality. Very helpful!

As I'm not familiar with ASCII and non-ASCII characters... is there an overview of all characters that need an escape \? I mean, I have the option to check the string for alphabetic characters or non-ASCII characters. But I also want the user to be able to use for example / or : and such. So I have to know which ones fall into the category "need a \".

tuk1
Experienced Solver
Experienced Solver
Posts: 59
Joined: Sun Oct 08, 2017 9:36 am

Re: Exclude all characters that need an escape

Postby tuk1 » Sat Jun 23, 2018 3:08 pm

wxWidgets(v3.0.3) - Vs2015(v14) - Win7(x64) - DialogBlocks(v5.15.3_Unicode)

Wanderer82
Earned some good credits
Earned some good credits
Posts: 130
Joined: Tue Jul 26, 2016 2:00 pm

Re: Exclude all characters that need an escape

Postby Wanderer82 » Sat Jun 23, 2018 3:33 pm

Yes actually in C++.

But the website that you linked doesn't show this "/" character as an escape sequence. But if I want to use this in a string, it won't work. I need it doubled "//".

User avatar
doublemax
Moderator
Moderator
Posts: 12680
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Exclude all characters that need an escape

Postby doublemax » Sat Jun 23, 2018 3:39 pm

I don't know any public method in wxWidgets that does this. There are methods in wxFileConfig, but they're not public.

But i still don't know exactly why you (think you) need this. How do you save the strings into a file and how do you read them back?
Use the source, Luke!

Wanderer82
Earned some good credits
Earned some good credits
Posts: 130
Joined: Tue Jul 26, 2016 2:00 pm

Re: Exclude all characters that need an escape

Postby Wanderer82 » Sat Jun 23, 2018 3:58 pm

Well, for example in the text file it says "Paper/Sheets" without the quotes.

Then in the program I use an ifstream variable to read the text into a string. It just doesn't work if I write "Paper/Sheets", I have to write "Paper//Sheets" to make it work.

EDIT:

After I had written this post it dawned on me. The problem was in the program code and not in the / character. So I can be sure that the characters from the "escape sequences" table from a link above is complete and I only have to exclude these?

tuk1
Experienced Solver
Experienced Solver
Posts: 59
Joined: Sun Oct 08, 2017 9:36 am

Re: Exclude all characters that need an escape

Postby tuk1 » Sat Jun 23, 2018 4:23 pm

Wanderer82 wrote: So I can be sure that the characters from the "escape sequences" table from a link above is complete and I only have to exclude these?


As long as your talking about 'escape sequences' a'la the basic c++ standard.

But that table doesn't include the non standard, eg:

Code: Select all

\e = \x1B = escape (non-standard GCC extension)


But, as DM says, I would think carefully why/if you really need this.

Wanderer82 wrote:Well, for example in the text file it says "Paper/Sheets" without the quotes.

Then in the program I use an ifstream variable to read the text into a string. It just doesn't work if I write "Paper/Sheets", I have to write "Paper//Sheets" to make it work.

I did something similar recently and didn't come across your problem, but I used this:
https://en.cppreference.com/w/cpp/filesystem/path
Last edited by tuk1 on Sat Jun 23, 2018 4:29 pm, edited 1 time in total.
wxWidgets(v3.0.3) - Vs2015(v14) - Win7(x64) - DialogBlocks(v5.15.3_Unicode)

User avatar
doublemax
Moderator
Moderator
Posts: 12680
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Exclude all characters that need an escape  [SOLVED]

Postby doublemax » Sat Jun 23, 2018 4:27 pm

If only string literals in the source code are the problem, the only character you have to escape is "\" itself (not "/").
Use the source, Luke!


Return to “C++ Development”

Who is online

Users browsing this forum: No registered users and 20 guests