June 30, 2001 tools

Codename TextBox

The Need for Code Generation

Have you ever wanted to generate code like the wizards do, i.e. start with a template, mix in some symbols and boom, out comes the code? If you’re building a custom AppWizard, you define code like so:

    int WINAPI WinMain(HINSTANCE hinst, HINSTANCE, LPSTR, int nShow) {
    $$IF(coinit)
        // Initialize COM
        CoInitialize(0);

    $$ENDIF
        // Initialize the ATL module
        _Module.Init(0, hinst);
    $$IF(axhost)

        // Initialize support for control containment
        AtlAxWinInit();
    $$ENDIF

        // Create and show the main window
        HMENU   hMenu = LoadMenu(_Module.GetResourceInstance(),
                       MAKEINTRESOURCE(IDR_$$ROOT$$));
    ...
    

This is fine if you’ve got the MFC-based interpreter building your code and you’re willing to live within the boundaries of a very small set of features. The ATL Object Wizard-style of generation is similar, i.e.

    class [!ClassName] : 
     public CAxDialogImpl<[!ClassName]>
    {
    public:
     [!ClassName]()
     {
     }
    [!crlf]
    ...
    

Again, only good if you’re running under the ObjectWizard and again, somewhat limited. What you really want is to be able to do things ASP-style, e.g.

    <%@ language=vbscript %>
    <% ' test.cpp.asp %>
    <%
        greeting = Request.QueryString("greeting")
        if len(greeting) = 0 then greeting = "Hello, World."
    %>
    // test.cpp

    <% if Request.QueryString("iostream") <> "" then %>
    #include <iostream>
    using namespace std;
    <% else %>
    #include <stdio.h>
    <% end if %>

    int main()
    {
    <% if Request.QueryString("iostream") <> "" then %>
        cout << "<%= greeting %>" << endl;
    <% else %>
        printf("<%= greeting %>\n");
    <% end if %>
        return 0;
    }

In this case, you get the same effect as the other two, but you’ve got the full power of a scripting language. However, for this to work, you had to run under ASP… until now…

TextBox

TextBox is a ASP-like script host that will process any file you give it looking for:

  • text blocks
  • script blocks (<% script %>)
  • output blocks (<%= output %>)
  • An optional language block as the first line in the file only (<%@ language= language %>)
    (TextBox defaults to vbscript and currently only works with vbscript and jscript).

TextBox pre-processes the text to turn the whole thing into into script and hands it to the scripting engine for execution, outputting the result to standard out. Whatever features of the scripting language you want to use, feel free.

Usage

To provide for I/O, TextBox emulates ASP somewhat. It provides two intrinsics, the request object and the response object. The Request object has a single property, QueryString, that works just like ASP. The Response object has a single property, Write, just like ASP. In fact, if you only use Request.QueryString and Response.Write, you should be able to test your script files using ASP.

To set name/value pairs for use by the script via the Request object, the usage of TextBox is like so:

    usage: textbox <file> [name=value]

For example, to interpret the file above, any of the following command lines would work:

    textbox test.cpp.asp
    textbox test.cpp.asp greeting="Double Wahoo!"
    textbox test.cpp.asp iostream=true greeting="Double Wahoo!"
    

The first would yield the following output:

    // test.cpp

    #include <stdio.h>

    int main()
    {
        printf("Hello, World.\n");
        return 0;
    }
    

while the last would yield the following:

    // test.cpp

    #include <iostream>
    using namespace std;

    int main()
    {
        cout << "Double Wahoo!" << endl;
        return 0;
    }
    

Errors and Debugging

If the scripting engine finds an error, it will notify TextBox, who will notify you. However, if you’ve got script debugging enabled on your machine, the scripting engine will ask you if you’d like to fire up the debugger, showing you exactly the offending code.

Not Just Code

Of course, TextBox is good for the generation of any text, not just code.

Download

TextBox is available for download. It’s just a prototype, so please adjust your expectations accordingly. If you have any comments, please send them to csells@sellsbrothers.com.

Copyright

Copyright (c) 1998-2001, Chris Sells All rights reserved. NO WARRANTIES EXTENDED. Use at your own risk.