Putting UTF-8 into C/C++ Source Code

After much googling, I could not find any tools for converting a UTF-8 string into an escaped C/C++ string literal suitable for pasting into an ASCII source file. Therefore I produced this Perl script which seems to provide a fairly readable escaped string:

use strict;
chomp;
print '"';
my $prev_esc = 0;
print map
    {
        if (ord $_ > 0x7f) {
            $prev_esc = 1;
            sprintf('\\x%lx', ord $_);
        } else {
            my $need_break = $prev_esc && /[0-9A-Fa-f]/;
            $prev_esc = 0;
            ($need_break ? '" "' : '') . $_;
        }
    }
    split('', $_);
print '"' . "\n";

Run it with the Perl -n option, and it will output an escaped string literal for each line input:

$perl -n utf8esc.pl
Grüße aus Bärenhöfe
"Gr\xc3\xbc\xc3\x9f" "e aus B\xc3\xa4renh\xc3\xb6" "fe"

Hit Ctrl-D on a blank line to exit.

Unfortunately, C/C++ seems to have the strange rule that all hex characters following a “\x” apply to that escape sequence, even though the maximum value allowed is 0xff. Therefore it is necessary to break the string into separate segments.

Build Boost 1.39 for 64-bit Windows

Building Boost for 64-bit Windows turns out not to be completely straightforward. These instructions assume you’re using Visual Studio 2008 and running from a VS 2008 x64 Command Prompt.

First, running bootstrap.bat appears to fail:

Building Boost.Jam build engine

Failed to build Boost.Jam build engine.
Please consult bjam.log for furter diagnostics.

You can try to obtain a prebuilt binary from

http://sf.net/project/showfiles.php?group_id=7586&package_id=72941

Also, you can file an issue at http://svn.boost.org
Please attach bjam.log in that case.

Actually, it’s not failing to build, it’s just trying to copy the executable from the wrong path. All you need to do is copy tools\jam\src\bin.ntx86_64\bjam.exe to the top-level Boost directory.

Next, you need to install any of the optional libraries you want to support. For Python support, you can install the Windows AMD64 MSI Installer, and then add the install directory (C:\Python26 by default) to your PATH. For Graphviz parsing support, download and unzip Expat 2.0.1 and my VS 2008 x64 project files, then use Batch Build in VS 2008 to build the x64 targets. You’ll also need to rename the Expat library reference in Boost’s libs\graph\build\Jamfile.v2 from “expat” to “libexpat”.

Finally, run the following commands to build Debug and Release DLLs, setting the Expat paths appropriately and the -j option to the number of processor cores you have:

set EXPAT_INCLUDE=C:\expat-2.0.1\lib

set EXPAT_LIBPATH=C:\expat-2.0.1\win64\bin\Debug
bjam -j3 --stagedir=stage64 --without-mpi toolset=msvc address-model=64 variant=debug link=shared threading=multi runtime-link=shared stage > build-shared-multi-debug64.log

set EXPAT_LIBPATH=C:\expat-2.0.1\win64\bin\Release
bjam -j3 --stagedir=stage64 --without-mpi toolset=msvc address-model=64 variant=release link=shared threading=multi runtime-link=shared stage > build-shared-multi-release64.log

With any luck, in about 5-15 minutes, you’ll have a set of DLLs and import libraries in stage64/lib. The build generates lots of output and warnings, which is the reason for redirecting the output to a file, but you’ll want to check the beginning and end of each log to make sure it built everything you expected.

Expat 2.0.1 for 64-bit Windows

I couldn’t find any instructions for building Expat for 64-bit Windows (x64), so I updated the 32-bit projects files myself: expat-vs2008-x64.zip. See my projects page for brief usage instructions.

Windows Update Finally Working

When I got my iPhone 6 months ago, I had to upgrade vile and hated iTunes to activate it. After the iTunes upgrade, it wanted me to restart the computer. When the system booted back up (and on every subsequent reboot), there was a crash in userinit.exe. Unable to find any trace of a surgical solution on the web, I resorted to the blunt solution: reinstall Windows (in place).

After reinstalling, the userinit.exe crash went away, but then a new problem surfaced that has plagued me constantly until today: Windows Update fails on all updates. Back when the problem first occurred, I could find no solution that mentioned the particular problem, which surfaced as error 80246002 or “AUClnt FATAL: Error: 0×80004002. wuauclt handler: failed to spawn COM server” in C:\WINDOWS\WindowsUpdate.log. At long last, thanks to this page, I found the solution, KB943144, posted October 26, 2007. The short answer:

net stop wuauserv
regsvr32 %windir%\system32\wups2.dll
net start wuauserv

myFairTunes

I’m very excited to find that it’s not difficult at all to remove iTunes DRM from songs purchased from the iTunes Music Store. myFairTunes will convert all your purchased music from M4P to M4A in place, add it back to your iTunes library, and optionally back up the M4P files to another folder and remove them from iTunes. Since my primary music computer has trouble syncing my iPhone, it’s very helpful to be able to move the music to another computer that doesn’t have as much sync trouble.

myFairTunes on SourceForge

C++ Functors

People have asked me several times what I think of “functors” in C++, so I decided to form an opinion (and write it down somewhere that myself and others can refer to). I’ve also always meant to include more technical content in this blog…

My reference material on the subject was an article Chuck provided, Callbacks in C++ Using Template Functors. I felt that while it’s true that all of the functor alternatives shown in the article are obviously undesirable according to the criteria listed (and perhaps other more basic criteria, such as code readability), it doesn’t really seem to show an example of the interface-oriented (i.e. pure virtual class) model, which I believe is the purest way to solve the problem. (By pure, I mean satisfying criteria such as being object-oriented, type-safe, and non-coupled, yet not relying on void* casts and template wizardry.) Here is the example that I feel was omitted:

class Notifiable
{
public:
    virtual void notify() = 0;
}

class Button
{
public:
    void click() { if (m_pnOnClick) m_pnOnClick->notify(); }
    void onClick(Notifiable* pn) { m_pnOnClick = pn; }
private:
    Notifiable* m_pnOnClick;
}

class CDPlayer
{
public:
    void play();
    void stop();
}

class CDPlayNotifiable : public Notifiable
{
public:
    CDPlayNotifiable(CDPlayer* cdp) : m_cdp(cdp) { }
    virtual void notify() { m_cdp->play(); }
private:
    CDPlayer* m_cdp;
}

class CDStopNotifiable : public Notifiable
{
public:
    CDStopNotifiable(CDPlayer* cdp) : m_cdp(cdp) { }
    virtual void notify() { m_cdp->stop(); }
private:
    CDPlayer* m_cdp;
}

void main()
{
    CDPlayer cdp;
    CDPlayNotifiable pn(&cdp);
    CDStopNotifiable sn(&cdp);
    Button playButton;
    playButton.onClick(&pn);
    Button stopButton;
    stopButton.onClick(&sn);
    // ...
}

I believe this satisfies all but one of the “Criteria for a Good Callback Mechanism” mentioned in the article: object-oriented, type-safe, non-coupling, non-type-intrusive, and flexible. The remaining criterion, being “generic”, is of questionable validity. Obviously it’s desirable not to require the user to write repetitive support code, but I think there’s some value to explicitly defining interfaces and being able to write custom binding code. Consider, for instance, how most UI frameworks include an argument on their events that identifies the sending object. If the functor of the Button class in the article included this argument, you could no longer directly bind it directly to CDPlayer::play().

However, the code above sucks in C++ for two reasons: 1) There’s no shorthand way to provide implementations of Notifiable. (Anonymous classes in Java are great for this.) 2) Managing the lifetime of the Notifiable objects is difficult. (Again, with GC in Java, this is not an issue.) In my example, I used pointers, which necessitates ensuring that the Notifiable objects exist at least as long as the Buttons referencing them. In the functor article, this is accomplished by the functors being held by value. This makes sense for functors, which are known to be small and non-polymorphic and to implement a default constructor and operator=(). Objects implementing Notifiable may or may not be small, but more importantly, they are polymorphic and therefore cannot be held by value.

Given these difficulties, functors do seem to be a reasonable mechanism for handling callbacks in C++. I’m curious whether the memcpy() on the pointer-to-member-function in FunctorBase is kosher with regard to the C++ standard, but even if it’s not, it’s the sort of thing that always tends to work anyway.

BTW, in Java, the above code directly translates to this, which seems pretty much ideal to me:

interface Notifiable
{
    void notify();
}

class Button
{
    public void click() { if (m_onClick != null) m_onClick.notify(); }
    public void onClick(Notifiable n) { m_onClick = n; }

    private Notifiable m_onClick;
}

class CDPlayer
{
    public void play();
    public void stop();
}

void main()
{
    final CDPlayer cdp = new CDPlayer();
    Button playButton = new Button();
    playButton.onClick(new Notifiable() { public void notify() { cdp.play(); } });
    Button stopButton = new Button();
    stopButton.onClick(new Notifiable() { public void notify() { cdp.stop(); } });
    // ...
}

Jove in EE Times

EE Times printed an article today about Jove. It has several quotes from me, gathered from our 45 minute interview, though as you might expect, they really zeroed in on the few somewhat inflammatory ones:

“It really didn’t seem like Vera was justified as a separate language,” Robinson said. “It had no tools and no libraries, it had a lot of bugs, and it ran pretty slow.” Another issue was limited reporting capabilities for functional coverage. “What took us a couple of weeks in Vera took a day in Java,” Robinson said.

Oh well, I guess that is how I feel about Vera.

Europeans Of The Year

Unless you’re totally xenophobic, you should consider voting for the Europeans of the Year, organized by the European Voice magazine, which “casts a spotlight on 50 individuals who have most influenced the European legislative and political agenda during the preceding year”. (They have to be European, but you don’t.)

I took interest in this poll because one of the nominees, Florian Müller, is the founder of NoSoftwarePatents.com, which is a cause very important to me. In addition to recognizing his efforts, a win for Müller would also do much to promote the cause. Because the poll requires voting in all 10 different areas, I found the NoSoftwarePatents.com voting guide helpful.

Jove Released!

Yesterday saw the culmination of several years of hard work (and corporate arm twisting): Jove was finally released on SourceForge. This was the official announcement:

Newisys is pleased to announce the release of Jove: The Open Verification Environment for the Java (TM) Platform. Jove is a set of Java APIs and tools to enable Verilog hardware design verification of ASICs and FPGAs using the Java programming language. It contains components that accomplish the following:

  • Verilog simulator interaction (via PLI 2.0 aka VPI)
  • Standalone behavioral simulation (i.e. a discrete event simulator)
  • Thread and event synchronization
  • Design Verification abstractions (e.g. clock-relative signal access, mailboxes, semaphores)
  • Constraint-based randomization
  • Dynamic Verilog shell generation

Jove has been tested extensively with Synopsys VCS and to a lesser extent with the GPL version of cver by Pragmatic C Software.

Information regarding the state of the project as well as the motivations behind it is available in the FAQ.

Binary and source code distributions are available at http://jove.sf.net/.

Jove is licensed under the Open Software License 2.0.

Java is a registered trademark of Sun Microsystems, Inc. in the U.S. or other countries.

Jove

Bite My Shiny Metal Casemod

Julian found my ideal computer case, the Bender PC. Apparently, he can even eat CDs while smoking a cigar.