Code::Blocks Forums

Developer forums (C::B DEVELOPMENT STRICTLY!) => Development => Topic started by: ollydbg on August 06, 2012, 10:43:29 am

Title: what's the best and quick way to remove all the wx_str and c_str preprocessor
Post by: ollydbg on August 06, 2012, 10:43:29 am
We have many many code snippet like below:
Code
                #if wxCHECK_VERSION(2, 9, 0)
                Codef( _T("%AAppend(%t)"), ArrayChoices[i].wx_str());
                #else
                Codef( _T("%AAppend(%t)"), ArrayChoices[i].c_str());
                #endif
I believe someone can create a simple find/replace script which can quickly replace those code snippet to:
Code
                Codef( _T("%AAppend(%t)"), ArrayChoices[i].wx_str());



Which tool can be used to do this? I found that manually delete those lines are some dirty work. ;)

EDIT:
The match condition should be:
Code
                #if wxCHECK_VERSION(2, 9, 0)
                blablabla1
                #else
                blablabla2
                #endif
The content of "blablabla1" and "blablabla2" should be the same only except the "wx_str" and "c_str".

Title: Re: what's the best and quick way to remove all the wx_str and c_str preprocessor
Post by: Jenna on August 06, 2012, 11:02:38 am
Should be doable with regexes.
Title: Re: what's the best and quick way to remove all the wx_str and c_str preprocessor
Post by: dmoore on August 06, 2012, 02:47:19 pm
Yes, replace in files with regex will do this

Make sure you have "use advanced regexes" checked in editor settings.

Link to syntax is in my sig. Use \n where n is an integer corresponding to your unnamed groups in the replace string. I never got around to adding support for substituting named groups in C::B (but maybe someone else did)

Something like this (untested)

Code
#if WXCHECK_VERSION\(2\s*,9,\s*0\).*?\n(.*?)#else.*?#endif.*?\n

replace with

Code
\1

Edit: placement of spacing.
Title: Re: what's the best and quick way to remove all the wx_str and c_str preprocessor
Post by: ollydbg on August 06, 2012, 03:02:22 pm
Thanks!!
There are some answers, but I'm going to test under C::B's own editor right now.
how to use regex to strip the preprocessor directive (http://stackoverflow.com/questions/11826016/how-to-use-regex-to-strip-the-preprocessor-directive)

Edit:
It looks like the same regex replacement rule works under notepad++ does not work under c::b. :(
Title: Re: what's the best and quick way to remove all the wx_str and c_str preprocessor
Post by: dmoore on August 06, 2012, 04:24:50 pm
For some reason the ".*" doesn't operate through line endings (this may have been intentional), so I had to use this:

Code
 *#if wxCHECK_VERSION\(2\,\s*9\,\s*0\).*?\n(.*?)\n.*?#else\r?\n.*?\n.*?#endif(.*?)\n

replacing with

Code
\1

This allows at most one line of code between the #if, #else, and #endif lines.
Title: Re: what's the best and quick way to remove all the wx_str and c_str preprocessor
Post by: dmoore on August 06, 2012, 05:34:33 pm
For what it's worth the patch to enable ".*" to match through newlines is pretty simple. Attached is an incomplete version (incomplete because it does not add UI to enabled/disable matching through newlines -- may actually be better to include the option in the editor dialog than the editor settings in any case.)
Title: Re: what's the best and quick way to remove all the wx_str and c_str preprocessor
Post by: stahta01 on August 06, 2012, 06:00:16 pm
I wish you luck.

I also wish the patches had been accepted with the #else; since, it was not ever needed.

Tim S.
Title: Re: what's the best and quick way to remove all the wx_str and c_str preprocessor
Post by: MortenMacFly on August 06, 2012, 06:44:58 pm
it was not ever needed.
It was needed (for a short period tough) when we were using an old wxWidgets 2.8.x version which did not have wx_str(). I don't recall which that was, but IMHO we also sticked to this version with the whole core that time.
Title: Re: what's the best and quick way to remove all the wx_str and c_str preprocessor
Post by: Jenna on August 06, 2012, 07:11:53 pm
it was not ever needed.
It was needed (for a short period tough) when we were using an old wxWidgets 2.8.x version which did not have wx_str(). I don't recall which that was, but IMHO we also sticked to this version with the whole core that time.
If I see the wxWidgets logs correctly, wx_str() has returned exactly the same as c_str() for a real long time and has always been there in 2.8, but was not documented and included only for wx1.6 (!) compatibility.

The differences to c_str() have been intriduced with wx2.9.

But it is as it is, and changing it even manually is not too hard.
Title: Re: what's the best and quick way to remove all the wx_str and c_str preprocessor
Post by: MortenMacFly on August 06, 2012, 07:13:45 pm
but was not documented and included only for wx1.6 (!) compatibility.
Al-right - didn't know that. I was always only looking at the official API (a.k.a CHM file). Well then - we are wx1.6 compatible, isn't it? ;D
Title: Re: what's the best and quick way to remove all the wx_str and c_str preprocessor
Post by: ollydbg on August 07, 2012, 06:46:44 am
OK, now the below regrex works(I did it under notepad++)

match:
Code
\r\n[\t ]*#if wxCHECK_VERSION\(2\, 9\, 0\)\r\n([\t ]*.*?)wx_str(.*)\r\n[\t ]*#else\r\n(\1)c_str(\2)\r\n[\t ]*#endif

replace:
Code
(\r\n)\1wx_str\2

This only handle "one line" statement, I strip more than 200 places in our c::b source code, see the patches:

Any objection to apply this patch?

[attachment deleted by admin]
Title: Re: what's the best and quick way to remove all the wx_str and c_str preprocessor
Post by: MortenMacFly on August 07, 2012, 08:00:23 am
Any objection to apply this patch?
Not from my side... I've removed some of these portions anyways already... Nice to see them all gone at once.
Title: Re: what's the best and quick way to remove all the wx_str and c_str preprocessor
Post by: ollydbg on August 07, 2012, 09:20:07 am
Ok, I have done in rev8202, and I think other parts of the preprocessor directives which can't matches by regex can be manually fixed later.
Title: Re: what's the best and quick way to remove all the wx_str and c_str preprocessor
Post by: ollydbg on September 03, 2012, 10:07:03 am
I see two kind of issues in wxsmith code:

Issue one:
Code
#if wxCHECK_VERSION(2, 8, 0)
    WX_DEFINE_ARRAY_INT(bool,wxArrayBool);
#else
    WX_DEFINE_ARRAY(bool,wxArrayBool);
#endif
Can we just strip them to:
Code
WX_DEFINE_ARRAY_INT(bool,wxArrayBool);

I think currently no one was using wx 2.7.x.

Issue two:
Still a lot of usage of c_str(), like:
Code
Code = RebuildCode(BaseIndentation,Code.c_str(),(int)Code.Length(),EOL);
or
Code
for ( const wxChar* Ch = Base.c_str(); *Ch; Ch++ )

Should they all convert to wx_str() ?