Code::Blocks Forums

User forums => Help => Topic started by: ValeV on July 19, 2021, 03:31:33 pm

Title: Wizard script and variables expansion
Post by: ValeV on July 19, 2021, 03:31:33 pm
Hello,

I am creating new wizard script for generating C header files. I want to add date to the top of the file (where [DATE] is in the code below). I tried to use variable expansion $(TODAY), but CodeBlocks crashes on creating new such header file.

wizard.script:
Code
////////////////////////////////////////////////////////////////////////////////
//
// Code::Blocks new file wizard script
//
// Project: C/C++ header file
//
////////////////////////////////////////////////////////////////////////////////

header_contents <- @"/**************************************************************************//**
 * \file
 *
 * \brief
 *
 * \author
 *
 * \version
 *
 * \date [DATE]
 *
 *
 ******************************************************************************/

#ifndef IG_[GUARD]
#define IG_[GUARD]

/*========================== function prototypes =================================*/



#endif // IG_[GUARD]
";

function BeginWizard()
{
    // this is the text that will appear in the start (intro) page
    local intro_msg = _T("Welcome to the new C/C++ header file wizard!\n" +
                         "This wizard will guide you to create a new C/C++ header file.\n\n" +
                         "When you 're ready to proceed, please click \"Next\"...");

    // add builtin pages
    //Wizard.AddInfoPage(_T("HFileIntro"), intro_msg); // intro
    Wizard.AddFilePathPage(true); // select filename (header guard required for header files)

    Wizard.SetFilePathSelectionFilter(_T("C/C++ header files (*.h;*.hpp;*.hxx;*.hh)|*.h;*.hpp;*.hxx;*.hh"));
}

function CreateFiles()
{
    local fname = Wizard.GetFileName();
    local ed    = GetEditorManager();
    if (IsNull(ed))
    {
        ShowError(_T("The wizard could not locate the editor manager."));
    }

    local ed_new = ed.New(fname);
    if (IsNull(ed_new))
    {
        ShowError(_T("The wizard could not create a new file.\n" +
                     "Maybe the target folder is write-protected?"));
    }
    else
    {
        // succeeded -> add header guard
        local guard = Wizard.GetFileHeaderGuard();
        local text = _T(header_contents);
        local auto_text = ed_new.GetText();

        text.Replace(_T("[DATE]"), "$(TODAY)");

        text.Replace(_T("[GUARD]"), guard);
        //text.Replace(_T("AUTO_GENERATED_CONTENTS"), auto_text);
        ed_new.SetText(text);

        // succeeded -> add file to project if needed
        if (Wizard.GetFileAddToProject())
        {
            AddFileToTargets(Wizard, fname);
        }
    }
    return fname;
}

Is there a different way for inserting current date?

Thanks!
Title: Re: Wizard script and variables expansion
Post by: oBFusCATed on July 19, 2021, 09:17:01 pm
Where does it crash (backtrace/callstack)?
What version are you using?
What is the exact wizard script and steps to reproduce?
Title: Re: Wizard script and variables expansion
Post by: ValeV on July 20, 2021, 07:40:24 am
Where does it crash (backtrace/callstack)?

When I try to create new header file with this custom script at the last step, when I click "Finish" in the wizard.

What version are you using?

Latest, CodeBlocks 20.03.

What is the exact wizard script and steps to reproduce?

I copy pasted the exact wizard script to the first message. Steps to reproduce is 1. create new custom script, 2. add it to global registration script, 3. create new header file with this custom script.

If I remove
Code
text.Replace(_T("[DATE]"), "$(TODAY)");
from my script it works fine, so it looks clear where the problem is. So my question is how to insert current date to wizard script.
Title: Re: Wizard script and variables expansion
Post by: oBFusCATed on July 20, 2021, 07:35:35 pm
Using a night build gives this error:
Code
Extracting 'PK8wxString' in 'wxString_Replace' failed for index 3
And to decipher - this means that you've passed a string where wxString is expected. To fix it you have to use the _T("...") wrapper.
But even if you do fix this, it won't do want you want, because you need to use the macro manager to replace "$(today)" with the actual date.
You have to use something like:
Code
text.Replace(_T("[DATE]"), ReplaceMacros(_T("$(TODAY)")));
No idea, if it works in 20.03.

Title: Re: Wizard script and variables expansion
Post by: ValeV on July 21, 2021, 08:27:43 am
Thank you! Your code works perfect on 20.03 also.  :)