Technology

C# and GC (Garbage Collection)

Good overview of diagnosing memory related problems

Technology

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

Technology

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;
    }
}
Technology

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

Technology

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

Technology

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

Technology

Software Design Thoughts

Constructors should be simple, predictable, and never fail.

Simple

Keep your constructors simple in the sense that:

  • Few parameters. Ever hear that a “function should do one thing and do it
    well”? It applies to constructors, too.
  • Few overrides. Don’t try to get around the previous dictum by having a bunch
    of overrides on the constructors. It’s confusing for someone to understand
    the initial state if there are a bunch of initial states.
  • Leave the work to public methods. If you think everything the constructor
    needs to be able to provide the ultimate configuration for an object you are
    trying to control (constrict) some developers too much.

Predictable

We write code like:

int  days = 13;

and don’t expect initialization to be doing very much work. Developers are used to that. Same with:

MyType val = new MyType(arg1, arg2);

should just be doing initialization comparable to assigning that integer. If
you want to have the constructor do a bunch of database work, factor that out
into a separate method:

val.LoadDB();

Many times people with constructor fever think have an underlying mindset that
thinks an object should be completely initialized and everything handled at the
constructor level. If you make every member function “const” then I’d believe
you. Otherwise, it’s just being a control freak.

Never fail

A constructor shouldn’t fail except under the most exceptional of circumstances
(out of memory). Examples of things that are not exceptional: a file doesn’t
exist, a network connection doesn’t succeed, input is poorly formatted, etc.