Formatting C++ code to html (mac, linux)

Hmm, it turns out turning C++ code into html is both harder (nothing obvious) and easier (a great tool) then you’d expect. For the previous post on expanding $var I just used:

enscript -B -E -u "" -T "" --language=html --color Strings.cpp -p ~/Strings.cpp.html

and copied the resulting html into the post.
The coolest thing I found was GeSHi (Generic Syntax Highlighter) which is written in php and so more suitable for web sites.
If I were using WordPress, there’s a
plugin
that use the “<pre lang=”C++”/>” tag

Expanding $VAR in a string (C++)

Here’s some sample code that expands strings like “$VAR”. There’s a typedef needed and I have it wrapped in a “namespace path” since this code fragment is part of a bigger project. Feel free to use this code however you like.

typedef std::map StringMap
namespace path
{
    /**
     * Expand any $VAR by looking up VAR in vars and using
     * the return value.  If VAR is not found, then the
     * empty string is used.  VAR can contain letters, digits, and
     * underscore ('_') (the usual).  To escape a dollar sign, use two
     * dollar signs ('$$').  If you include the variable name in parenthesis,
     * then any characters are ok: $(A $ %).  '[]' and '{}' work
     * if you need more characters.
     *
     * Variables are recursively expanded.  So if the expansion includes
     * a variable, that variable is also expanded.
     *
     * @param str The String to be expanded
     * @param vars A std::map from std::string to std::string
     * @param tilde True if expand ~ to $HOME (at start)
     * @return a string with all $VARs expanded.
     */
    std::string expand(const std::string &str, const StringMap &vars, bool tilde)
    {
        std::string     newstr;
        const char      intro = '$';
        const char      tilde_char = '~';
        for (std::string::const_iterator iter = str.begin(); iter != str.end();)
        {
            if (tilde)
            {
                tilde = false;  // only at the very start
                if (*iter == tilde_char)
                {
                    ++iter;
                    newstr += expand("$HOME", vars, false);
                    continue;
                }
            }
            // Search for a '$'
            if (*iter != intro)
            {
                newstr += *iter++;
                continue;
            }
            // We have a '$'
            std::string var;
            ++iter;
            // Treat $$ as an escape for a single '$'
            if (iter != str.end() && *iter == intro)
            {
                newstr += *iter++;
                continue;
            }
            // Get the actual variable
            bool start = true;
            bool domatch = false;
            char match = ')'; // for matching brace/parenthesis
            while (iter != str.end())
            {
                if (start)
                {
                    start = false;
                    switch (*iter)
                    {
                        case '(':
                            match = ')';
                            domatch = true;
                            ++iter;
                            continue;
                        case '{':
                            match = '}';
                            domatch = true;
                            ++iter;
                            continue;
                        case '[':
                            match = ']';
                            domatch = true;
                            ++iter;
                            continue;
                        default:
                            break;
                    }
                }
                if (domatch)
                {
                    if (*iter == match)
                    {
                        ++iter;
                        domatch = false;
                        break;
                    }
                    else
                    {
                        var += *iter++;
                    }
                }
                else if (isalnum(*iter) || *iter == '_')
                {
                    var += *iter++;
                }
                else
                {
                    break;
                }
            }
            if (!domatch)
            {
                StringMap::const_iterator variter = vars.find(var);
                // If we added an else, we could have non-matches expand
                if (variter != vars.end())
                    newstr += expand(variter->second, vars, false);
            }
        }
        return newstr;
    }
}

Home Prices Across the Nation

Here’s a chart showing year-over-year home price changes in Manhattan. Prices
are starting to fall. I’m not sure what the basis is (average selling price or
median price) But these interactive graphics that let you get specific data on
your specific city in an easily digestible form (for free!) are amazing.
Of course, sometimes you get what you pay for. The data for New York doesn’t seem to be updating so I think this is really for Atlanta.
From Home Prices Across the Nation:

SafariScreenSnapz001.png

And here’s the inflation adjusted numbers (i.e. inflation is subtracted):

SafariScreenSnapz002.png

What good reporting looks like

I think Scott McClellan’s new book is just re-hashing old news and buying it just rewards someone that failed miserably at serving the country. But part of what’s happening is the many media notables claiming they did a good job on reporting the runnup to the Iraq war. They didn’t!
One of the few news agencies that did a good job reporting, Knight Ridder (since purchased by McClatchy), has this to say:
From Blog: Nukes Spooks:

The Bush administration was gunning for Iraq within days of the 9/11 attacks, dispatching a former CIA director, on a flight authorized by Deputy Defense Secretary Paul Wolfowitz, to find evidence for a bizarre theory that Saddam Hussein was responsible for the first World Trade Center attack in 1993. (Note: See also Richard Clarke and former Treasury Secretary Paul O’Neill on this point).”

MAMP: Mac – Apache – MySQL – PHP

Previously, I’d found WAMP a pretty nice setup. Well, there’s a comparable installation for Mac, as well. It offers a “Pro” version that gives you more flexibility as far as managing several virtual servers but the free version is pretty good for my needs.
From MAMP: Mac – Apache – MySQL – PHP:

The abbreviation MAMP stands for: Macintosh, Apache, Mysql and PHP. With just a few mouse-clicks, you can install Apache, PHP and MySQL for Mac OS X!
MAMP installs a local server environment in a matter of seconds on your Mac OS X computer, be it PowerBook or iMac. Like similar packages from the Windows- and Linux-world, MAMP comes free of charge.”