Author Topic: what's the best and quick way to remove all the wx_str and c_str preprocessor  (Read 10984 times)

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5916
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
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".

« Last Edit: August 06, 2012, 10:45:48 am by ollydbg »
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Should be doable with regexes.

Offline dmoore

  • Developer
  • Lives here!
  • *****
  • Posts: 1576
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.
« Last Edit: August 06, 2012, 04:33:28 pm by dmoore »

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5916
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
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

Edit:
It looks like the same regex replacement rule works under notepad++ does not work under c::b. :(
« Last Edit: August 06, 2012, 03:39:32 pm by ollydbg »
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline dmoore

  • Developer
  • Lives here!
  • *****
  • Posts: 1576
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.

Offline dmoore

  • Developer
  • Lives here!
  • *****
  • Posts: 1576
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.)

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
I wish you luck.

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

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
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.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
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.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
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
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5916
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
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]
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
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.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5916
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
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.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5916
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: what's the best and quick way to remove all the wx_str and c_str preprocessor
« Reply #13 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() ?

If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.