Exclude all characters that need an escape Topic is 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.
Post Reply
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Exclude all characters that need an escape

Post by Wanderer82 »

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
Earned some good credits
Earned some good credits
Posts: 114
Joined: Sun Oct 08, 2017 9:36 am

Re: Exclude all characters that need an escape

Post by tuk1 »

wxWidgets(v3.2.2.1) - Vs2022(v143) - Win10(x64) - DialogBlocks(v5.16.5_Unicode)
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Re: Exclude all characters that need an escape

Post by Wanderer82 »

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
Earned some good credits
Earned some good credits
Posts: 114
Joined: Sun Oct 08, 2017 9:36 am

Re: Exclude all characters that need an escape

Post by tuk1 »

Do you mean in c++ generally or?

https://en.cppreference.com/w/cpp/language/escape
wxWidgets(v3.2.2.1) - Vs2022(v143) - Win10(x64) - DialogBlocks(v5.16.5_Unicode)
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Re: Exclude all characters that need an escape

Post by Wanderer82 »

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: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Exclude all characters that need an escape

Post by doublemax »

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
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Re: Exclude all characters that need an escape

Post by Wanderer82 »

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
Earned some good credits
Earned some good credits
Posts: 114
Joined: Sun Oct 08, 2017 9:36 am

Re: Exclude all characters that need an escape

Post by tuk1 »

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.2.2.1) - Vs2022(v143) - Win10(x64) - DialogBlocks(v5.16.5_Unicode)
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Exclude all characters that need an escape

Post by doublemax »

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!
Post Reply