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.”

The Democratic Surge

Here are some more statistic supporting my previous post that Republicans are toast. The turnout in Democratic primaries has been phenominal:
From The Democratic Party :

– Turnout increases ranged from 18 percent in Arkansas to an astronomical 2,549 percent in Kansas.
– In contrast, comparing 2008 Republican turnout to the last contested Republican primary in 2000, Republican turnout either stayed relatively stagnant or decreased. Sinking turnout throughout the country for Republicans shows the contrast between Democrats and Republicans this primary season.
– In fact, for the 30 states for which comparable data is available, 27 of them saw more Democratic than Republican voters this year.

And even though John McCain is the presumptive Republican nominee, one quarter of Republicans voted for a candidate not even in the race anymore:

As Democrats continue to see unprecedented enthusiasm for our candidates, the
news continues to be worrisome for John McCain. Even after locking up the
Republican nomination, 27 percent of Republican voters voted for another
Republican candidate in North Carolina, and 23 percent of Republican voters
voted for another Republican candidate in Indiana. [abcnews.com, accessed
5/7/08]

What’s New in Qt? — Trolltech

Someday, I’ll get back to using Qt in my professional existance. That and the iPhone. Anyway, webkit looks like a great addition.
What’s New in Qt?

Incorporate online content and services such as maps, music stores and instant messaging into your native applications. Qt now integrates with WebKit, the open source browser engine inside Apple’s Safari browser, the Apple iPhone and millions of Nokia smartphones. The integration allows developers to blend web and native content and functionality, create innovative user interfaces, and deliver a consistent, web-enriched user

Mercurial and darcs for distributed version control

In my work world, I use clearcase for doing version control. At NYSE, we used a flexible but complex model of several vobs for shared pieces, major branches for specific versions (.e.g. product.11), minor branches (product.11.03) and each developer creating one or more further branches as needed (product.11.03.4538). Lots of merging going on. It took a while to get used to but it works well.
At Credit Suisse, we have a much simpler model for clearcase where everyone creates a local branch off the main view and when they are ready just commits that. It freaks me out but it works.
On my personal projects, I’ve been evolving over the years. Many years ago, I
used RCS.
I thought CVS was a huge improvement over RCS. Then Subversion held sway over me up until
about a year ago when I started experimenting with more distributed version
control systems.
When I wanted to work with someone else, we both had laptops, were frequently
working without a network connection (commuting by train) and we were doing a
lot of experimental stuff. It made sense to use distributed version control. I
started off with Darcs. The concept is great;
everything works as expected. I had repositories under Linux, Windows, and
MacOS. Tres cool. Darcs got a little annoying with constantly prompting me if
I really wanted to included this. Sure, I used “-a” or answered “a” a lot
(accept all changes), but it was frustrating.
Recently, I decided to try Mercurial and I’m totally impressed. I’m not to far into my evaluation but the first few things work like I expect.

  1. Creating a new repository and importing all the changes from darcs worked like a charm
  2. Creating repositories on several machines via ssh works well.
  3. Simple changes and updates is intuitive
  4. Performance is great

More info

Here are some other articles