Author Topic: wxsmith code generate tab or spaces issue  (Read 8331 times)

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5910
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
wxsmith code generate tab or spaces issue
« on: August 27, 2012, 09:17:24 am »
Firstly, I do not familiar with wxsmith's source code, but I do see an issue:

Code
wxString wxsCoder::RebuildCode(wxString& BaseIndentation,const wxChar* Code,int CodeLen,wxString& EOL)
{
    wxString Tab;
    bool UseTab = Manager::Get()->GetConfigManager(_T("editor"))->ReadBool(_T("/use_tab"), false);
    int TabSize = Manager::Get()->GetConfigManager(_T("editor"))->ReadInt(_T("/tab_size"), 4);
    if ( !UseTab )
    {
        Tab.Append(_T(' '),TabSize);
    }

    if ( EOL.IsEmpty() )
    {
        int EolMode = Manager::Get()->GetConfigManager(_T("editor"))->ReadInt(_T("/eol/eolmode"), 0);
        switch ( EolMode )
        {
            case 1:  EOL = _T("\r"); break;
            case 2:  EOL = _T("\n"); break;
            default: EOL = _T("\r\n");
        }
    }

    BaseIndentation.Prepend(EOL);

    wxString Result;
    Result.reserve(CodeLen+10);

    while ( *Code )
    {
        switch ( *Code )
        {
            case _T('\n'):
                {
                    while (Result.Last() == _T(' ') || Result.Last() == _T('\t'))
                        Result.RemoveLast();
                    Result << BaseIndentation;
                    break;
                }
            case _T('\t'): if ( UseTab ) { Result << Tab; break; }
            default:       Result << *Code;
        }
        Code++;
    }

    return Result;
}

The Value "Tab" is either "empty string" or "four spaces".
There is something wrong with the line:
Code
case _T('\t'): if ( UseTab ) { Result << Tab; break; }

I think it should be:
Code
case _T('\t'): if ( !UseTab ) { Result << Tab; break; }

Otherwise, the '\t' is always added to the code, right?
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 oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: wxsmith code generate tab or spaces issue
« Reply #1 on: August 27, 2012, 09:33:35 am »
And what is the test case to reproduce the problem?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5910
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: wxsmith code generate tab or spaces issue
« Reply #2 on: August 27, 2012, 09:59:24 am »
And what is the test case to reproduce the problem?
There is an option: Configure Eidtor->General setting->Use Tab chars.
Whenever you select this option or not, the generate xrc/wxs file always use "Tabs". I would like use "4 spaces" in the xrc/wxs file, but I can't find a way.

I suspect the source code above cause the problem(I'm going to debug the code myself), I think the wxsmith should follow those options, but in-fact, it was not.

EDIT:
The code snippet in my original post was related to generate the source in h/cpp files, they are not related to code generation for xrc/wxs. but what the logic seems still wrong. :)
« Last Edit: August 27, 2012, 10:27:22 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.