<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mark Nelson &#187; Magazine Articles</title>
	<atom:link href="http://marknelson.us/category/magazine-articles/feed/" rel="self" type="application/rss+xml" />
	<link>http://marknelson.us</link>
	<description>Programming, mostly.</description>
	<lastBuildDate>Fri, 13 Apr 2012 19:25:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Automating Putty</title>
		<link>http://marknelson.us/2011/12/10/automating-putty/</link>
		<comments>http://marknelson.us/2011/12/10/automating-putty/#comments</comments>
		<pubDate>Sat, 10 Dec 2011 12:11:15 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Magazine Articles]]></category>
		<category><![CDATA[Serial Communications]]></category>

		<guid isPermaLink="false">http://marknelson.us/?p=776</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2011/12/10/automating-putty/' addthis:title='Automating Putty' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div>Windows users who need a command line connection to another system via telnet or SSH are big fans of PuTTY. It&#8217;s free, it has every feature you need, and it&#8217;s reliable. One thing many people would like to do is use PuTTY as a component in their program. Apparently this comes up so often enough [...]]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2011/12/10/automating-putty/' addthis:title='Automating Putty' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div><p>Windows users who need a command line connection to another system via telnet or SSH are big fans of <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/" class="newpage">PuTTY</a>. It's free, it has every feature you need, and it's reliable. </p>
<p>One thing many people would like to do is use PuTTY as a component in their program. Apparently this comes up so often enough that there is a <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/faq.html#faq-embedding" class="newpage">FAQ entry</a> dedicated to the topic. Alas, PuTTY does not have any sort of automation interface, so this goal has always been out of reach.</p>
<p>In this article I will show you how to work around this minor shortcoming. Creating a version of PuTTY that can be driven from a Windows program turns out to be an easy task. I'll demonstrate this with a small C++ program that shows exactly how to get this versatile program to do your bidding. My solution works for C++, but the changes I make should work well with any Windows software that can properly process a few messages.<br />
<span id="more-776"></span></p>
<h4>Putting Together the Project</h4>
<p>I'm using Visual Studio 2010 to build both my program and the modified version of Putty. I created the basic outline as follows:</p>
<ol>
<li/>Use the <em>File|New|Project</em> menu item to bring up the list of available project wizards.
<li/>Select <em>MFC project</em>, and enter a project name (I used the uninspired name <em>PuttyDriver</em>.)
<li/>I don't want the default MFC settings, so in the MFC App Wizard, select the <em>Next</em> button.
<li/>On the <em>Application Type</em> page of the wizard, change the Application Type to <em>Dialog Based</em>.
<li/>The project is ready to go at this point, you can click the <em>Finish</em> button and then build your initial project.
</ol>
<p>My driver program is only going to do one thing: direct putty to connect to the host of my choice, then log in using canned credentials. The resulting UI is shown below, and I am going to leave the very minor details of creating it up to the reader.</p>
<table border="0" width="100%">
<tr>
<td><center><img src="/attachments/2011/putty/Figure01.png"></center></td>
</tr>
<tr>
<td><center>The driver program - a simple dialog-based MFC app</center></td>
</tr>
</table>
<h4>Adding Putty to the Project</h4>
<p>The next step in this process is to add the Putty components to the project. I downloaded version 0.61 of the PuTTY source from the <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html" class="newpage">download page</a> and extracted it to a separate folder. I then used Visual Studio's <em>File|Add|Existing Project</em> to add the compatible project file, <code>Putty.dsp</code>, found in <code>/Windows/MSVC/Putty</code>. Visual Studio has to convert this project to a version 10 project file, but it should do so with no problems.</p>
<p>I then right-clicked on the Putty project in Solution Explorer and renamed it to <em>AutoPutty</em>. Since this version of PuTTY will have some slightly different behavior, I don't want to confuse the executable I am creating with the real thing.</p>
<p>From Project|Project Dependencies, I set the PuttyDriver project to depend on AutoPutty - this insures that both projects get built when I build the entire solution.</p>
<p>My final change to the project is to modify the output directory for both Debug and Release versions of AutoPutty. I set the project to build the executable in the root directory of my PuttyDriver project - this will make it easy to find the executable when I need to launch it. I had to make this change in two places: <em>Properties|Configuration Properties|General|Output Directory</em> and <em>Properties|Linker|Outuput File</em>.</p>
<p>When you finally build the project, you'll find that current version of Microsoft's C++ compiler complain quite a bit about the use of functions like <code>strcpy</code> - Microsoft would like you to use safer replacement functions. You may choose to turn those errors off by defining <code>_CRT_SECURE_NO_WARNINGS</code> in the project file. While you are there, you should define <code>SECURITY_WIN32</code> as well - it is required by Windows header <code>sspi.h</code>.</p>
<p>After a successful build you should find a copy of <code>AutoPutty.exe</code> in the root directory of your project, and it should run on your system and behave just like PuTTY.</p>
<h4>Launching AutoPutty</h4>
<p>If I'm going to have a PuTTY component in my PuttyDriver program, one of the first things I need is to be able to start and stop AutoPutty. So my first step in this project is to create the code that launches the program from PuttyDriver. The code below is inserted into the handler for the Start button:</p>
<pre>
UpdateData( true );
char path[MAX_PATH];
GetCurrentDirectory(MAX_PATH, path);
if ( path[ strlen(path) - 1 ] != '\\' )
    strcat_s( path, MAX_PATH, &quot;\\&quot; );
strcat_s( path, MAX_PATH, &quot;AutoPutty.exe -ssh &quot; );
strcat_s( path, MAX_PATH, m_HostName.GetBuffer() );
PROCESS_INFORMATION pi;
ZeroMemory(&amp;pi, sizeof(pi) );
STARTUPINFO si;
ZeroMemory(&amp;si, sizeof(si) );
si.cb = sizeof(si);
if ( CreateProcess( NULL, path, NULL, NULL, NULL, NULL, NULL, NULL, &amp;si, &amp;pi ) )
{
    Sleep( 1000 );
    BringWindowToTop();
}
</pre>
<p>This code assumes that <code>AutoPutty.exe</code> is in the current directory, and launches it with a command line telling it to connect to the host named in the dialog using <a href="http://www.ietf.org/rfc/rfc4251.txt" class="newpage">ssh</a>. Assuming that you have the project set up properly, pushing the start button should now start an independent copy of AutoPutty, which will behave identically to classic PuTTY.</p>
<h4>Taking Ownership of AutoPutty</h4>
<p>At this point I can successfully launch AutoPutty, but I can't really start calling this an integrated part of my main program, PuttyDriver. All I have done is set up a launcher for a separate executable. </p>
<p>The next step in the integration process is to establish PuttyDriver as the owner of AutoPutty's main window. Most Windows programmers are familiar with the traditional parent/child relationship between windows. That relationship is well understood, but I can't use it here - it doesn't work for two top level windows.</p>
<p>Setting PuttyDriver to be the <em>owner</em> (as opposed to the parent) of AutoPutty has the following effects, as explained <a href="http://msdn.microsoft.com/en-us/library/ms632599(v=VS.85).aspx#owned_windows" class="newpage">here</a> by Microsoft:</p>
<ul>
<li/>The owned window will always be above its owner in the z-order.
<li/>The system automatically destroys the owned window when the owner is destroyed.
<li/>The owned window is hidden when the owner is minimized.
</ul>
<p>The most straightforward way to set ownership of the window is to pass the owner's handle in the call to <code>CreateWindow()</code>, which means I will now make my first modifications to the PuTTY source code. </p>
<p>There are a number of ways to pass the owner handle to AutoPutty for use in the call to <code>CreateWindow()</code>, with the most obvious being to pass it on the command line. In the interest of minimizing changes to the existing PuTTY code base, I elected to pass it by creating an environment variable that holds the owner window handle. Since a child process inherits the parent's environment, this is a no-fuss way to get the data to AutoPutty.</p>
<p>I added the following code to the end of <code>InitDialog()</code> in PuttyDriver:</p>
<pre>
CString hwnd_text;
hwnd_text.Format( &quot;%d&quot;, m_hWnd );
SetEnvironmentVariable(&quot;PUTTY_OWNER&quot;, hwnd_text );
</pre>
<p>This sets the environment variable for AutoPutty to find when it gets launched.</p>
<p>Now I come to the point where I am actually making changes to the PuTTY code. Fortunately, all of the changes needed for this program are confined to two files: <code>terminal.c</code> and <code>windows/window.c</code>. My first change is to <code>window.c</code>. This file contains the WndProc for the PuTTY window, and thus most of the rendering and control code for the GUI.</p>
<p>In order to establish the Owner/Owned relationship, I need to modify the code that calls <code>CreateWindow()</code>. I hoisted the function call into a block, added code to get the owner window handle, and inserted the handle into the call to <code>CreateWindow()</code>:</p>
<pre>
{
    HWND owner_hwnd = 0;
    char buffer[ 132 ];
    if ( GetEnvironmentVariable( &quot;PUTTY_OWNER&quot;, buffer, 132 ) )
        sscanf( buffer, &quot;%d&quot;, &amp;owner_hwnd );
    if ( owner_hwnd == 0 )
        MessageBox( NULL,
                    &quot;AutoPutty did not find the handle for the &quot;
                    &quot;owner window, this is not going to work&quot;,
                    &quot;Fail&quot;,
                    MB_OK );
    hwnd = CreateWindowEx(exwinmode, appname, appname,
                          winmode, CW_USEDEFAULT, CW_USEDEFAULT,
                          guess_width, guess_height,
                          owner_hwnd, NULL, inst, NULL);
}
</pre>
<p>At this point I've only modified one small block of code in the PuTTY source, but I'm well on my way to having it behave more like a component of PuttyDriver and less like an independent program. The ownership status means that the two programs only appear once on the taskbar, and will only appear once when you are pressing ALT-TAB to select a new active process. And they only produce a single entry in the Applications Tab of Task Manager.</p>
<h4>The Communications Link</h4>
<p>In order to achieve the automation that I am seeking, I also need to have two way communications between AutoPutty and the driver program. Since this is Windows, a natural choice for communications is to use native Windows messages. In order to do this, both programs need the Window handle of their opposite number.</p>
<p>I've already solved half of that problem through the ownership relationship established when I created the main window for AutoPutty. Now that it has set PuttyDriver as its owner window, I can get this window handle any place in the program through a simple function call:</p>
<pre>
HWND parent = GetWindow(hwnd, GW_OWNER);
</pre>
<p>But the reverse is not true - PuttyDriver does not know have a copy of the window handle for AutoPutty. </p>
<p>To remedy this situation, I added code to <code>window.c</code> that notifies its owner when it s created, and when it is destroyed. First I add this statement immediately after the call to <code>CreateWindow()</code>:</p>
<pre>
if ( owner_hwnd )
   PostMessage( owner_hwnd, WM_APP, 0, (LPARAM) hwnd );
</pre>
<p>This tells PuttyDriver that the window is created, and gives it the handle to use for communications.</p>
<p>I also need to know when the window is closed, and I have to add that code two places in <code>window.c</code> - because Putty can be shut down two different ways. </p>
<p>Normally AutoPutty will shut down in response to a windows message. When this happens, I can count on a <code>WM_CLOSE</code> message being sent to the Windows Procedure. I add this code the existing handler for <code>WM_CLOSE</code>:</p>
<pre>
if (!cfg.warn_on_close || session_closed ||
    MessageBox(hwnd,
               &quot;Are you sure you want to close this session?&quot;,
               str, MB_ICONWARNING | MB_OKCANCEL | MB_DEFBUTTON1)
    == IDOK) {
    HWND parent = GetWindow(hwnd, GW_OWNER);
    if ( parent )
        SendMessage( parent, WM_APP, 0, 0 );
    DestroyWindow(hwnd);
}
</pre>
<p>This lets PuttyDriver know that the window has been destroyed.</p>
<p>The original PuTTY code has an alternative method of shutdown. When it receives one of several possible network events, such as a telnet connection being broken, it calls <code>PostQuitMessage()</code>. When a program shuts down this way, it doesn't issue messages to destroys its windows - it relies on the O/S to destroy the windows when the process exists. As a result, I have to make a change in <code>WinMain()</code>, the main window procedure for PuTTY. This procedure extracts the messages sent to it using <code>PeekMessage</code>, and I add some code to handle the processing when a <code>WM_QUIT</code> message is sent:</p>
<pre>
if (msg.message == WM_QUIT) {
    HWND parent = GetWindow(hwnd, GW_OWNER);
    if ( parent )
        SendMessage( parent, WM_APP, 0, 0 );
    goto finished;	       /* two-level break */
}
</pre>
<h4>Handling the AutoPutty Lifecycle Events</h4>
<p>To keep track of the state of AutoPutty, I have to add a handler for <code>WM_APP</code> to PuttyDriver. It does two things when handling the incoming<code> WM_APP</code> event.</p>
<p>First, then handler stores the handle of the AutoPutty window - or sets the value to 0 when the window has been destroyed.</p>
<p>Second, it either enables or disables the button used to start up AutoPutty. Since this program can only manage one window at a time, I don't want to allow any inadvertent button pushes:</p>
<pre>
afx_msg LRESULT CPuttyDriverDlg::OnWmApp(WPARAM wParam, LPARAM lParam)
{
    m_PuttyWindow = (HWND) lParam;
    m_StartButton.EnableWindow( !m_PuttyWindow );
    return 0;
}
</pre>
<p>One final piece of bookkeeping is to make sure that the AutoPutty window is shut down when PuttyDriver shuts down. (The Windows documentation claims this happens automatically to owned windows, but it doesn't seem to be the case.)</p>
<pre>
void CPuttyDriverDlg::OnDestroy()
{
    CDialogEx::OnDestroy();
    if ( m_PuttyWindow )
        ::SendMessage( m_PuttyWindow, WM_CLOSE, 0, 0 );
}
</pre>
<h4>Monitoring Input Traffic</h4>
<p>Now that I have control over the lifetime of my AutoPutty window, it's time to take the next step in automation. My driver program needs to watch all the data coming in from the remote end so that it can take action on various types of input.</p>
<p>Depending on how you set up your connection, PuTTY can receive input data from a serial port, a Telnet connection, or an SSH connection. Fortunately the Windows version of PuTTY uses a standard handle-based interface to all three types of connections. The routine <code>term_data()</code> in <code>terminal.c</code> is called as data arrives, regardless of the source.</p>
<p>Since we are using the Windows API to communicate between processes, it makes sense to use the <code>WM_COPYDATA</code> message to send data to the parent program as it arrives. <code>WM_COPYDATA</code> is a good choice, as it takes care of marshalling the data between the two processes, which can add some complication to other solutions. The modified routine is shown below:</p>
<pre>
int term_data(Terminal *term, int is_stderr, const char *data, int len)
{
    HWND parent = GetWindow(hwnd, GW_OWNER);
    if ( parent ) {
        COPYDATASTRUCT cd;
        cd.dwData = (ULONG_PTR) 0xDEADBEEF;
        cd.cbData = len;
        cd.lpData = (PVOID) data;
        SendMessage( parent, WM_COPYDATA, (WPARAM) hwnd, (LPARAM) &amp;cd );
    }
</pre>
<h4>Receiving the Data</h4>
<p>To receive this messages in PuttyDriver, I simply create a handler for <code>WM_COPYDATA</code> and start grabbing the data as it arrives. One important thing to note is that because AutoPutty has to use <code>SendMessage()</code> to send the data to its parent, it has to wait for PuttyDriver to finish processing the data until it can continue. This dictates a certain style of behavior on my part.</p>
<p>There are quite a few ways to skin this cat, and I'm keeping it very simple here. I'm using a <code>deque&lt;char&gt;</code> container to hold the last 64 characters I've received. After each <code>WM_COPYDATA</code> message I received, I check to see if the current output snapshot ends in one of my trigger messages. If it does, I post the message number to myself for later processing, then return so that AutoPutty can continue its work.</p>
<p>The code I'm using here is doing something fairly simple: automating the login process by using the credentials that I've entered into the dialog box. That means the two strings I'm looking for are the login and password prompts. The resulting code is shown here:</p>
<pre>
BOOL CPuttyDriverDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
    char *p = (char *) pCopyDataStruct-&gt;lpData;
    int len = pCopyDataStruct-&gt;cbData;
    if ( len &gt;= 64 ) {
        p += len - 64;
        len = 64;
        m_Snapshot.clear();
    }
    while ( len-- )
        m_Snapshot.push_front(*p++);
    m_Snapshot.resize(64);
    static const char *needles[2] = { &quot;login as: &quot;, &quot;password: &quot; };
    for ( int i = 0 ; i &lt; 2 ; i++ ) {
        int len = strlen( needles[i] );
        int j;
        for ( j = 0 ; j &lt; len ; j++ ) {
            if ( needles[i][j] != m_Snapshot[len-1-j] )
                break;
        }
        if ( j == len )
            PostMessage( WM_APP+1, i, 0 );
    }
    return TRUE;
}
</pre>
<p>There is plenty of room for improvement in this routine, much of it depending on what type of automation you are going to be using in your program. Some obvious items would include the ability to add and remove triggers as the program progresses, and regular expression matching for triggers. </p>
<h4>Driving PuTTY</h4>
<p>This login program is now complete save for one detail: I need a way to send my responses back to AutoPutty. </p>
<p>The first part of this is pretty obvious - I just need to read the data from the dialog box and post it to AutoPutty with my useful <code>WM_COPYDATA</code> command. This happens in my <code>WM_APP+1</code> handler:</p>
<pre>
afx_msg LRESULT CPuttyDriverDlg::OnWmAppPlusOne(WPARAM wParam, LPARAM lParam)
{
    UpdateData(TRUE);
    CString msg;
    switch ( wParam ) {
    case 0 :
        msg = this-&gt;m_UserId + '\r'; break;
    case 1:
        msg = this-&gt;m_Password + '\r'; break;
    }
    if ( this-&gt;m_PuttyWindow ) {
        COPYDATASTRUCT cd;
        cd.dwData = (ULONG_PTR) 0xF00DFACE;
        cd.cbData = msg.GetLength();
        cd.lpData = (PVOID) (const char *) msg;
        ::SendMessage( this-&gt;m_PuttyWindow,
                       WM_COPYDATA,
                       (WPARAM) this-&gt;m_hWnd,
                       (LPARAM) &amp;cd );
    }
    return 0;
}
</pre>
<p>Sending this data to AutoPutty is fine, but right now the program doesn't do anything with that message. The final piece of work is to add a <code>WM_COPYDATA</code> handler to window.c. </p>
<p>Simply grabbing the data is easy enough - the data structure that accompanies the message contains a pointer to the data and a value indicating its length. However, I have two problems I have to solve before the data is actually sent out to the to whatever device AutoPutty is connected to.</p>
<p>First, I have to take into account the fact that PuTTY was written to use wide characters. My driver program was built using MultiByte characters, so we have a mismatch. This means I have to do a conversion of the data from one domain to the other. This is a two step process - I call <code>MultiByteToWideChar()</code> once to determine how much space I need, then I allocate a buffer and call it again.</p>
<p>The second thing I need to do is determine what to do with the data once I've converted it. PuTTY takes all terminal input and eventually passes through a function called <code>luni_send()</code>. Calling this function directly from the Windows procedure seems to work just fine. </p>
<p>The <code>WM_COPYDATA</code> handler I created looks like this:</p>
<pre>
case WM_COPYDATA :
{
    COPYDATASTRUCT *cd = (COPYDATASTRUCT *) lParam;
    int wsize = MultiByteToWideChar( CP_ACP,
                                     MB_PRECOMPOSED,
                                     (LPCSTR) cd-&gt;lpData,
                                     cd-&gt;dwData,
                                     NULL,
                                     0 );
    wchar_t *buf = (wchar_t *) calloc( wsize+1, sizeof(wchar_t) );
    MultiByteToWideChar( CP_ACP,
                         MB_PRECOMPOSED,
                         (LPCSTR) cd-&gt;lpData,
                         cd-&gt;dwData,
                         buf,
                         wsize + 1 );
    if (term-&gt;ldisc)
        luni_send(term-&gt;ldisc, buf, wsize, 0);
    free( buf );
}
</pre>
<p>At this point I have a working program - it connects to my designated <a href="http://www.webhostingsearch.com/" class="newpage">host</a>, and sends the username and password of my choice to the host, connecting me to the system.</p>
<p>I should add a note of caution here. Automating logins is a tempting time saver, but in general this is a really bad idea. Any time you hard code credentials into a program, you open the door to all sorts of new attacks on your system.</p>
<p>In my demo program, the user has to enter a name and password, so nothing is hardcoded, but even this adds security holes to a system. I encourage you to think of this as a demonstration only.</p>
<p><center></p>
<table border="0">
<tr>
<td><center><iframe width="500" height="281" src="http://www.youtube.com/embed/3O4t9KzpKbo?fs=1&#038;feature=oembed" frameborder="0" allowfullscreen></iframe></center></td>
</tr>
<tr>
<td><center>Demo of the program in action<br/>For a better view, go to full screen and select 720p</td>
</tr>
</table>
<p></center></p>
<h4>Source Code</h4>
<p>I've included the complete source code for PuttyDriver, the MFC project that controls AutoPutty. It was built with Visual Studio 2010, so you may have a little work to do if you backport it to earlier versions. My use of language features and classes should be compatible with much earlier versions - this is all very simple code.</p>
<p>Because PuTTY is always changing, I am not redistributing a snapshot of the version I used. Instead, I'm including before and after copies of the two source files I modified: <code>window.c</code> and <code>terminal.c</code>. If you build with Putty 0.61, you should be able to drop these two files right on top of the files included with the distribution and be on your way. With later versions of PuTTY you will have to perform an intelligent merge of the changes, which I hope will be a fairly effortless process.</p>
<h4>Downloads</h4>
<ul>
<li><a href="/attachments/2011/putty/PuttyDriver.zip">PuttyDriver.zip</a>. The PuttyDriver source and project. You will need to add the PuTTY project to this solution as described in the article.
<li><a href="/attachments/2011/putty/putty.zip">putty.zip</a>. This contains the two PuTTY source files modified for this project. Both the original 0.61 source and my modified source are supplied. Executables are supplied as well, which may or may not work on your system.
</ul>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2011/12/10/automating-putty/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>The Byzantine Generals Problem</title>
		<link>http://marknelson.us/2007/07/23/byzantine/</link>
		<comments>http://marknelson.us/2007/07/23/byzantine/#comments</comments>
		<pubDate>Mon, 23 Jul 2007 12:11:48 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Magazine Articles]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">/2006/08/13/byzantine/</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2007/07/23/byzantine/' addthis:title='The Byzantine Generals Problem' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div>This article presents the algorithm that solves the Byzantine General’s Problem, as first described by Lamport, Pease, and Shostak in 1982 <a href="http://research.microsoft.com/users/lamport/pubs/byz.pdf" class="newpage">[1]</a>. While Lamport’s algorithm is not particularly complex, programmers who aren’t used to working on distributed computation might find it difficult to implement. To accompany the explanation of the algorithm, I have included a C++ program designed for experimentation with the solution.]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2007/07/23/byzantine/' addthis:title='The Byzantine Generals Problem' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div><p>This article presents the algorithm that solves the Byzantine General’s Problem, as first described by Lamport, Pease, and Shostak in 1982 <a href="http://research.microsoft.com/users/lamport/pubs/byz.pdf" class="newpage">[1]</a>. While Lamport’s algorithm is not particularly complex, programmers who aren’t used to working on distributed computation might find it difficult to implement. To accompany the explanation of the algorithm, I have included a C++ program designed for experimentation with the solution.</p>
<h4>Introduction</h4>
<p>The Byzantine General’s Problem is one of many in the field of agreement protocols. In 1982, Leslie Lamport described this problem in a paper written with Marshall Pease and Robert Shostak. Lamport framed his paper around a story problem after observing what he felt was an inordinate amount of attention received by Dijkstra’s Dining Philosophers problem <a href="http://research.microsoft.com/users/lamport/pubs/pubs.html#byz" class="newpage">[2]</a>.</p>
<p>This problem is built around an imaginary General who makes a decision to attack or retreat, and must communicate the decision to his lieutenants. A given number of these actors are traitors (possibly including the General.) Traitors cannot be relied upon to properly communicate orders; worse yet, they may actively alter messages in an attempt to subvert the process.</p>
<p>When we’re not dwelling in storybook land, the generals are collectively known as <i>processes</i>, the general who initiates the order is the <i>source process</i>, and the orders sent to the other processes are <i>messages</i>. Traitorous generals and lieutenants are <i>faulty processes</i>, and loyal generals and lieutenants are <i>correct processes</i>. The order to retreat or attack is a message with a single bit of information: a one or a zero.</p>
<p>In general, a solution to an agreement problem must pass three tests: <i>termination</i>, <i>agreement</i>, and <i>validity</i>. As applied to the Byzantine General’s problem, these three tests are:</p>
<ol>
<li>A solution has to guarantee that all correct processes eventually reach a decision regarding the value of the order they have been given.</li>
<li>
All correct processes have to decide on the same value of the order they have been given.</li>
<li>If the source process is a correct process, all processes have to decide on the value that was original given by the source process.</li>
</ol>
<p>Note that one interesting side effect of this is that if the source process is faulty, all other processes still have to agree on the same value. It doesn’t matter what value they agree on, they simply all have to agree. So if the General is subversive, all lieutenants still have to come to a common, unanimous decision.<br />
<span id="more-86"></span></p>
<h4>Difficulties</h4>
<p>This agreement problem doesn’t lend itself to an easy naïve solution.  Imagine, for example, that the source process is the only faulty process. It tells half the processes that the value of their order is zero, and the other half that their value is one.</p>
<p>After receiving the order from the source process, the remaining processes have to agree on a value that they will all decide on. The processes could quickly poll one another to see what value they received from the source process.</p>
<p>In this scenario, imagine the decision algorithm of a process which receives an initial message of zero from the source process, but sees that one of the other processes says that the correct value is one. Given the conflict, the process knows that either the source process is faulty, having given different values to two different peers, or the peer is faulty, and is lying about the value it received from the source process. </p>
<p>It’s fine to reach the conclusion that someone is lying, but making a final decision on who is the traitor seems to be an insurmountable problem. And in fact it can be proven that it is impossible to decide in some cases. The classic example used to show this is when there are only three processes: one source process and two peer processes. </p>
<p>In the two configurations shown in Figure 1 and Figure 2, the peer processes attempt to reach consensus by sending each other their proposed value after receiving it from the source process. In Figure 1, the source process (P<sub>1</sub>) is faulty, sending two different values to the peers. In Figure 2, P<sub>3</sub> is faulty, sending an incorrect value to the peer.</p>
<p>You can see the difficulty P<sub>2</sub> faces in this situation. Regardless of which configuration it is in, the incoming data is the same. He has no way to distinguish between the two configurations, and no way to know which of the two other processes to trust.<br />
<center><br />
<img src="http://marknelson.us/attachments/2007/byzantine/Figure1.gif"><br />
Figure 1<br />
The case in which the source process is faulty</p>
<p><img src="http://marknelson.us/attachments/2007/byzantine/Figure2.gif"><br />
Figure 2<br />
The case in which P<sub>3</sub> is faulty<br />
</center><br />
This situation doesn’t necessarily get better just by throwing more non-faulty processes at the problem. A naïve algorithm as shown in Figure 1 and Figure 2 might have each process tell every other process what it received from P<sub>1</sub>. A process would then decide on the correct value by taking a simple majority of the values in its incoming messages.</p>
<p>Under the rules of this approach, it is easy to show that regardless of how many processes are in the system, a subversive source process with one collaborator can cause half the processes to choose to attack, while half the processes elect to retreat, leading to maximum confusion. </p>
<h4>The Lamport, Pease and Shostak Algorithm</h4>
<p>In 1982, Lamport, Pease, and Shostak published a fairly simple solution to this problem. The algorithm assumes that there are <i>n</i> processes, with <i>m</i> faulty processes, where <i>n > 3m</i>. Thus, for a scenario such as that in Figure 1 and 2 with 1 faulty process, there would have to be a minimum of 4 processes in the system to come to agreement. (For the rest of this article, <i>n</i> will always refer to the count of processes, and <i>m</i> will always refer to the number of faulty processes.)</p>
<p>The definition of the algorithm in the original paper is short and succinct, but at least in my experience, is somewhat confusing for programmers without a lot of experience in distributed algorithms.</p>
<p>Lamport’s algorithm is a recursive definition, with a base case for <i>m=0</i>, and a recursive step for m <i>> 0</i>:</p>
<p><center></p>
<table border="0">
<tr>
<td width="10%">&nbsp;</td>
<td>
<strong>Algorithm OM(0)</strong></p>
<ol>
<li>The general sends his value to every lieutenant.</li>
<li>Each lieutenant uses the value he receives from the general.</li>
</ol>
<p><strong>Algorithm OM(m), m > 0</strong></p>
<ol>
<li>The general sends his value to each lieutenant.</li>
<li>For each i, let v<sub>i</sub> be the value lieutenant i receives from the general. Lieutenant i acts as the general in Algorithm OM(m-1) to send the value v<sub>i</sub> to each of the n-2 other lieutenants.</li>
<li>For each i, and each j ≠ i, let v<sub>i</sub> be the value lieutenant i received from lieutenant j in step 2 (using Algorithm (m-1)). Lieutenant i uses the value majority (v<sub>1</sub>, v<sub>2</sub>, … v<sub>n</sub>).</li>
</ol>
</td>
</tr>
</table>
<p>Lamport’s Algorithm Definition<br />
</center><br />
To most programmers, this is going to look like a conventional recursive function definition, but it doesn’t quite fit into the mold you learned when studying the example of <i>factorial( n )</i>. </p>
<p>Lamport’s algorithm actually works in two stages. In the first step, the processes iterate through <i>m+1</i> rounds of sending and receiving messages. In the second stage of the algorithm, each process takes all the information it has been given and uses it to come up with its decision.</p>
<p>I found this to be non-obvious without quite a bit of study, which is the reason for this article.</p>
<h4>The First Stage</h4>
<p>The first stage of the algorithm is simply one of data gathering. The algorithm defines <i>m+1</i> rounds of messaging between all the processes. </p>
<p>In round 0, the General sends the order to all of its lieutenants. Having completed his work, the General now retires and stands by idly waiting for the remaining work to complete. Nobody sends any additional messages to the General, and the General won’t send any more messages.</p>
<p>In each of the remaining rounds, each lieutenant composes a batch of messages, each of which is a tuple containing a value and a path. The value is simply a 1 or a 0. The path is a string of process ids, &lt;ID<sub>1</sub>, ID<sub>2</sub>, …, ID<sub>n</sub>>. What the path means in this context is that in Round N,  PID<sub>N</sub> is saying that was told in round N-1 that P<sub>IDN-1</sub> was told by… P<sub>ID1</sub> that the command value was <i>v</i>. (This is very much like the classic party game in which a message is whispered from ear to ear through a chain of players, becoming slightly mangled along the way.) No path can contain a cycle. In other words, if ID1 is 1, no other ID in the string of process IDs will be a 1.</p>
<p>The message definition is easy in round 1. Each process broadcasts a message to all the other processes, including itself, but excluding the General, with the value it received from the General and its own process ID. </p>
<p>In subsequent rounds, things get more complicated. Each process takes all the messages it received from the previous round, appends its process ID where allowed, and sends those messages to all other processes, including itself. (The "where allowed"  just means that the process skips any messages where adding its process ID to the list would create a cycle in the string of process IDs.)</p>
<p>For example, let’s suppose that in Round 0 that P<sub>1</sub>, a faulty general told P<sub>2</sub>, P<sub>3</sub>, and P<sub>4</sub> that the command value was 0, and told P<sub>5</sub>, P<sub>6</sub>, and P<sub>7</sub> that the command value was 1. In round 1, the following messages would be sent:<br />
<center></p>
<table border="1" cellpadding="3">
<tr>
<td colspan="2">Sender=P<sub>2</sub></td>
<td colspan="2">Sender=P<sub>3</sub></td>
<td colspan="2">Sender=P<sub>4</sub></td>
<td colspan="2">Sender=P<sub>5</sub></td>
<td colspan="2">Sender=P<sub>6</sub></td>
<td colspan="2">Sender=P<sub>7</sub></td>
</tr>
<tr>
<td>Dest</td>
<td>Msg</td>
<td>Dest</td>
<td>Msg</td>
<td>Dest</td>
<td>Msg</td>
<td>Dest</td>
<td>Msg</td>
<td>Dest</td>
<td>Msg</td>
<td>Dest</td>
<td>Msg</td>
</tr>
<tr>
<td>P<sub>2</sub></td>
<td>{0,12}</td>
<td>P<sub>2</sub></td>
<td>{0,13}</td>
<td>P<sub>2</sub></td>
<td>{0,14}</td>
<td>P<sub>2</sub></td>
<td>{1,15}</td>
<td>P<sub>2</sub></td>
<td>{1,16}</td>
<td>P<sub>2</sub></td>
<td>{1,17}</td>
</tr>
<tr>
<td>P<sub>3</sub></td>
<td>{0,12}</td>
<td>P<sub>3</sub></td>
<td>{0,13}</td>
<td>P<sub>3</sub></td>
<td>{0,14}</td>
<td>P<sub>3</sub></td>
<td>{1,15}</td>
<td>P<sub>3</sub></td>
<td>{1,16}</td>
<td>P<sub>3</sub></td>
<td>{1,17}</td>
</tr>
<tr>
<td>P<sub>4</sub></td>
<td>{0,12}</td>
<td>P<sub>4</sub></td>
<td>{0,13}</td>
<td>P<sub>4</sub></td>
<td>{0,14}</td>
<td>P<sub>4</sub></td>
<td>{1,15}</td>
<td>P<sub>4</sub></td>
<td>{1,16}</td>
<td>P<sub>4</sub></td>
<td>{1,17}</td>
</tr>
<tr>
<td>P<sub>5</sub></td>
<td>{0,12}</td>
<td>P<sub>5</sub></td>
<td>{0,13}</td>
<td>P<sub>5</sub></td>
<td>{0,14}</td>
<td>P<sub>5</sub></td>
<td>{1,15}</td>
<td>P<sub>5</sub></td>
<td>{1,16}</td>
<td>P<sub>5</sub></td>
<td>{1,17}</td>
</tr>
<tr>
<td>P<sub>6</sub></td>
<td>{0,12}</td>
<td>P<sub>6</sub></td>
<td>{0,13}</td>
<td>P<sub>6</sub></td>
<td>{0,14}</td>
<td>P<sub>6</sub></td>
<td>{1,15}</td>
<td>P<sub>6</sub></td>
<td>{1,16}</td>
<td>P<sub>6</sub></td>
<td>{1,17}</td>
</tr>
<tr>
<td>P<sub>7</sub></td>
<td>{0,12}</td>
<td>P<sub>7</sub></td>
<td>{0,13}</td>
<td>P<sub>7</sub></td>
<td>{0,14}</td>
<td>P<sub>7</sub></td>
<td>{1,15}</td>
<td>P<sub>7</sub></td>
<td>{1,16}</td>
<td>P<sub>7</sub></td>
<td>{1,17}</td>
</tr>
</table>
<p>Table 1<br />
Messages sent by all six lieutenant processes in round 1<br />
</center><br />
The number of messages goes up in in the second round. From the previous iteration, we know that each process now has six values that it received in the previous round – one message from each of the six other non-source processes – and it needs to send each of those messages to all of the other processes, which might mean each process would send 36 messages out.</p>
<p>In the previous table I showed the messages being sent to all six processes, which is fairly redundant, since the same messages are broadcast to all processes. For round 2, I’ll just show you the set of messages that each process sends to all of its neighbors.</p>
<p><center></p>
<table border="1" cellpadding="3">
<tr>
<td>Sender=P<sub>2</sub></td>
<td>Sender=P<sub>3</sub></td>
<td>Sender=P<sub>4</sub></td>
<td>Sender=P<sub>5</sub></td>
<td>Sender=P<sub>6</sub></td>
<td>Sender=P<sub>7</sub></td>
</tr>
<tr halign="center">
<td>{0,132}<br/>{0,142}<br/>{1,152}<br/>{1,162}<br/>{1,172}</td>
<td>{0,123}<br/>{0,143}<br/>{1,153}<br/>{1,163}<br/>{1,173}</td>
<td>{0,124}<br/>{0,134}<br/>{1,154}<br/>{1,164}<br/>{1,174}</td>
<td>{0,125}<br/>{0,135}<br/>{0,145}<br/>{1,165}<br/>{1,175}</td>
<td>{0,126}<br/>{0,136}<br/>{0,146}<br/>{1,156}<br/>{1,176}</td>
<td>{0,127}<br/>{0,137}<br/>{0,147}<br/>{1,157}<br/>{1,167}</td>
</tr>
</table>
<p>Table 2<br />
Messages sent by all six processes in round 2<br />
</center><br />
The six messages that P<sub>2</sub> received in round 1 were {0,12}, {0,13}, {0,14}, {1,15}, {1,16}, and {1,17}. According to the earlier definition, P<sub>2</sub> will append its process ID to the path and forward each resulting message to all other processes. The possible messages it could broadcast in round 2 are {0,122}, {0,132}, {0,142}, {1,152}, {1,162}, and {1,172}. The first message, {1,122} contains a cycle in the path value of the tuple, so it is tossed out, leaving five messages to be sent to all processes.</p>
<p>The first message that P<sub>2</sub> is sending in round 2, {0,132}, is equivalent to saying "P<sub>2</sub> is telling you that in round 1 P<sub>3</sub> told it that in round 0 that P<sub>1</sub> (the General) told it that the value was 0". The five messages shown in P<sub>2</sub>’s column in the table are sent to all six lieutenant processes, include itself.</p>
<p>It’s easy to see that as the number of processes increases, the number of messages being exchanged starts to go up rapidly. If there are N processes, each process sends N-1 messages in round 1, then (N-1)*(N-2) in round 2, (N-1)*(N-2)*(N-3) in round 3. That can add up to a lot of messages in a big system.</p>
<h4>The Second Stage</h4>
<p>While sending messages in each round, processes are also accumulating incoming messages. The messages are stored in a tree format, with each round of messages occupying one rank of the tree. Figure 3 shows the layout of the tree for a simple configuration with six processes, one of which can be faulty. Since m=1, there are just two rounds of messaging: the first, in which the general sends a value to each lieutenant process, and a second, in which each process broadcasts its value to all the other processes. Two rounds of messaging are equivalent to two ranks in the tree.</p>
<p>Each node in the tree has three elements: an input value, a path, and an output value. The input value and path are defined in the first stage of the algorithm - they are simply the messages received from the peer processes. The output value is left undetermined until the second stage of the algorithm, which I am defining here. Note that in the figure below, the output values are initially set to '?', indicating that they are presently unknown.<br />
<center><br />
<img src="http://marknelson.us/attachments/2007/byzantine/Figure3.gif" alt="" /><br />
Figure 3<br />
The Tree Layout for 5 processes with 1 faulty process<br />
</center><br />
In Figure 3, there are six processes, and the General (P<sub>1</sub>) is faulty – sending a 1 to the first three lieutenants and 0 to the last two. The subsequent round of messaging results in P<sub>2</sub> having an information tree that looks just like that shown in Figure 3. (Because only the General is faulty, in this case all other processes will have an identical tree.)</p>
<p>Once a process has completed building its tree, it is ready to decide on a value. It does this by working its way up from the leaves of the tree, calculating the majority value at each rank and assigning it to the rank above it. The output value at each level is the third item in the data structure attached to each node, and those values are all undefined during the information gathering stage.</p>
<p>Calculating the output values is a three step process:</p>
<ol>
<li>Each leaf node in the tree (all values at rank m) copies its input value to the output value.</li>
<li>Starting at rank m-1 and working down to 0, the output value of each internal node is set to be the majority of the output values of all its children. In the event of a tie, an arbitrary tie-breaker is used to assign a default value. The same default value must be used by all processes.</li>
<li>When complete, the process has a decision value in the output of the sole node at rank 0.</li>
</ol>
<p>In Figure 3, step 1 of the process assigns the initial values to the leaf nodes. In the next step, the majority value of { 1, 1, 1, 0, 0 } is evaluated and returns a value of 1, which is assigned to the output value in rank 0. Because that is the top rank, the process is done, and P<sub>1</sub> decides on a value of 1.</p>
<p>Every lieutenant value in a given exercise will have the same paths for all its nodes, and in this case, since only the General is faulty, we know that all lieutenants will have the same input values on all its leaves. As a result, all processes will agree on the same value, 1, which fulfills the agreement property.</p>
<h4>A More Complicated Example</h4>
<p>Getting a good understanding of the algorithm really requires walking through an example that has at least three ranks. (Examples on the web, mostly extracted from lecture notes, nearly always have the simple two-rank example.) For this example, consider an example with <i>n=7</i> and <i>m=2</i>. We’ll continue with the convention that the General is P<sub>1</sub>, and instead of having a faulty general, we’ll have P<sub>6</sub> and P<sub>7</sub> be faulty processes. After the initial three rounds of information exchange, we have the three-ranked tree shown in Figure 4:<br />
<center><br />
<img src="http://marknelson.us/attachments/2007/byzantine/Figure4.gif" alt="" /><br />
Figure 4<br />
A tree with <i>n=7</i>, <i>m=2</i>, and faulty processes P<sub>6</sub> and P<sub>7</sub><br />
</center><br />
The important thing to note in these trees is that I’ve inserted the value 'X' for the input values of any input value that comes from the two faulty processes. We don’t know what P<sub>6</sub> and P<sub>7</sub> might send in any given round, so in general, we’ll try to work through the algorithm without constricting their incorrect messages to any specific values.</p>
<p>You’ll see that at rank 1, the values from path 17 and 16 are both set to X. In the first round the two faulty processes communicated possibly false values to all other processes, and may have arbitrarily changed the values sent to different processes in order to skew the results.</p>
<p>As a result of those bad values in rank 1, we see their frequent occurrence in rank 2. The incorrect values show up not only in direct messages from the faulty processes, but also in any message from a correct process that includes a faulty process earlier in its path.</p>
<p>All in all, at the leaf nodes, we have 18 deceptive values at the leaf nodes, and only 12 accurate messages that trace their way all the way back to the general through nothing but correct processes. Obviously, if we just voted on the majority of the messages we had received, we would be susceptible to falling for the wrong value.</p>
<p>Fortunately, the layout of the tree guarantees that we will actually get a correct value. In Figure 4, the roll up of the output values hasn’t occurred yet, so every node has a question mark in the output value. In Figure 5, the output values are shown. The leaf rank has the output values set to the input values, with X used to indicate unknown values from faulty processes.</p>
<p>When the leaf rank is rolled up to the second rank, the nodes with paths 12, 13, 14, and 15 all have clear majority values of 0 for their output values, with 16 and 17 set to X, as their values are uncertain.</p>
<p>The final roll up to the top rank successfully sets the output value to 0, as four of the inputs are set to 0 and only 2 are set to X. Mission accomplished. And because of the way this was calculated, we know that the correct result will be achieved regardless of what deceptive values are sent by the two faulty processes.<br />
<center><br />
<img src="http://marknelson.us/attachments/2007/byzantine/Figure5.gif" alt="" /><br />
Figure 5<br />
The tree after calculating the output values<br />
</center></p>
<h4>The Sample Code</h4>
<p>I’ve included a simple C++ program that implements this algorithm, with extensive internal documentation. It has a <span class="inline_code">Process </span>class that is used to send and receive messages, as well as to roll up the decision tree. A <span class="inline_code">Traits </span>class is used to define the number of processes, the number of faulty processes, the source process, and what values the faulty processes send in various rounds.</p>
<p>To help with visualization, the program will output the tree for a given process in the format used by <i>dot</i>, part of the free Graphviz program. You can then use dot to create a nice picture of the output graph – all the figures in this article were created that way. I find that using the SVG format for the output produces very readable results.</p>
<p>As supplied, the program is set for values of <i>n=7</i> and <i>m=2</i>. Good exercises to perform while experimenting with it include:</p>
<ul>
<li>Attempt to invalidate the program or the algorithm by getting incorrect results with some particular combination of faulty messages.</li>
<li>Add a third faulty process and show that it is relatively easy to get invalid output when <i>n=7</i> and <i>m=2</i>.</li>
<li>Reduce <i>n</i> to 6 and show that it is relatively easy to get invalid output with two faulty processes.</li>
<li>Move up to <i>m=3</i> and <i>n=10</i>. Experiment with various combinations of faulty Generals and lieutenants and see if you can create incorrect results.</li>
</ul>
<h4>Note</h4>
<p>Most implementations of this algorithm include logic designed to deal with the case in which a faulty process fails to send a message. This is equivalent to simply having the faulty process send an arbitrary value, so I don’t treat it as a separate case.</p>
<h4>Source Code</h4>
<p><a href="/attachments/2007/byzantine/source.zip">source.zip</a>, which contains:</p>
<ul>
<li>main.cpp</li>
<li>README.TXT</li>
<li>VS2003/byzantine.sln
<li>
<li>VS2003/byzantine.vcproj
<li>VS2005/byzantine.sln
<li>
<li>VS2005/byzantine.vcproj
</ul>
<h4>References</h4>
<p>[1]  The Byzantine Generals Problem  (with Marshall Pease and Robert Shostak)<br />
ACM Transactions on Programming Languages and Systems 4, 3 (July 1982), 382-401.<br />
<a href="http://research.microsoft.com/users/lamport/pubs/byz.pdf" class="newpage">http://research.microsoft.com/users/lamport/pubs/byz.pdf</a></p>
<p>[2] The Writings of Leslie Lamport: <a href="http://research.microsoft.com/users/lamport/pubs/pubs.html#byz" class="newpage">http://research.microsoft.com/users/lamport/pubs/pubs.html#byz</a></p>
<p>Lynch, Nancy A. Distributed Algorithms. San Francisco, CA: Morgan Kaufmann, 1997. ISBN: 1558603484.</p>
<p>Graphviz – Graph Visualization Software. <a href="http://www.graphviz.org/" class="newpage">http://www.graphviz.org/</a></p>
<p>CS 6378: Advanced Operating Systems Section 081 Notes on Agreement Protocols (lecture notes by Neeraj Mittal, University of Texas at Dallas)  <a href="http://www.utdallas.edu/~neerajm/cs6378su07/agreement.pdf" class="newpage">http://www.utdallas.edu/~neerajm/cs6378su07/agreement.pdf</a></p>
<p><i>Update 01-November-2007</i><br />
<em>Distributed Computing With Malicious Processors w/o Crypto or Private Channels</em>, <a href="http://www.youtube.com/watch?v=xXhnJl4AVg0" class="newpage">YouTube Video of Google TechTalk</a>, <a href="http://webhome.cs.uvic.ca/~val/" class="newpage">Valerie King</a>, Department of Computer Science, University of Victoria, Victoria, BC, Canada</p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2007/07/23/byzantine/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Louis, Louis &#8211; The Preternatural Jukebox</title>
		<link>http://marknelson.us/2002/10/01/louis-louis-the-preternatural-jukebox/</link>
		<comments>http://marknelson.us/2002/10/01/louis-louis-the-preternatural-jukebox/#comments</comments>
		<pubDate>Wed, 02 Oct 2002 05:01:55 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[Magazine Articles]]></category>

		<guid isPermaLink="false">/2002/10/01/louis-louis-the-preternatural-jukebox/</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2002/10/01/louis-louis-the-preternatural-jukebox/' addthis:title='Louis, Louis &#8211; The Preternatural Jukebox' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div>This article shows how to embed Microsoft's Windows Media Player in an ATL-based C++ program to create an MP3 player that adapts to your personal preferences. I use it to run a home jukebox that creates random playlists based on your personal preferences.]]></description>
			<content:encoded><![CDATA[











<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2002/10/01/louis-louis-the-preternatural-jukebox/' addthis:title='Louis, Louis &#8211; The Preternatural Jukebox' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div><table border="0">
<tr>
<td style="width: 33%"><img alt="DDJ Cover from October, 2002" id="image32" src="http://marknelson.us/attachments/louis-louis-the-preternatural-jukebox/cover.gif" /></td>
<td style="width: 66%"><strong>Dr. Dobb's Journal</strong> October, 2002</p>
<p><a id="p33" href="http://marknelson.us/attachments/louis-louis-the-preternatural-jukebox/LouisLouis.zip">Source code and install program for Louis, Louis</a></td>
</tr>
</table>
<p>Wouldn't it be nice to have a radio station that always played your favorite music? A JukeBox that could read your mind - mixing in your old favorites with new music that you really like?</p>
<p>Mental telepathy is still a bit out of reach, but in this article I'm going to show you how to build a Windows-based MP3 player that keeps track of your musical tastes and does its best to accommodate them. By giving you the opportunity to rate artists, albums, and individual songs, the MP3 player can apply a bias towards the music you like and away from the music you don't.</p>
<p>So that you can build an MP3 Player like this, I'll first describe the steps you need to embed Microsoft's Windows Media Player (WMP) in a Visual C++ program as an ActiveX control, and what you need to do to control it, and respond to events from it.</p>
<p>With that infrastructure in place, I'll then show you how to keep a music database and use the ratings it contains to select the music you like.<br />
<span id="more-26"></span></p>
<h4>The Demo Program: Louis, Louis</h4>
<p>My demo program is named <em>Louis, Louis</em>, in honor of Louis Glass, a man who is credited by many with being the inventor of the modern Juke Box. In 1889 Louis introduced the first coin-operated phonograph at the Palais Royal saloon in San Francisco.</p>
<p>Figure One shows Louis, Louis in action. The main window of the program is divided into three parts. At the top, there is information about the track currently being played, which includes the <em>Artist</em>, <em>Album</em>, and <em>Song</em> name, along with your current ratings for each.</p>
<p><center><img alt="Figure1.gif" id="image29" src="http://marknelson.us/attachments/louis-louis-the-preternatural-jukebox/Figure1.gif" /><br />
Figure 1<br />
Louis, Louis at Work</center>The center section of the dialog contains the embedded copy of Windows Media Player (WMP). This includes an area for the display of the current WMP visualization, a caption area for information about what's playing, and a set of player controls. This entire section is part of a single monolithic instance of the WMP OCX.</p>
<p>The bottom section of the dialog contains buttons that are used for the various functions I've added to the program.</p>
<h4>A Few Details</h4>
<p>Louis, Louis is an MP3 player that has two different modes of operation. <em>Random Play Mode</em> is enabled by checking the box of the same name in the top section of the Dialog. In this mode, the program randomly selects tracks from the master catalog. As it selects each song, it determines how likely you are to want to hear it. The selection algorithm first checks to see if you've rated the song. If you haven't, it then checks to see if you've rated the album, and finally the artist. Your chances of hearing the song range from 0 to 100 percent, depending on your rating on a score of 0 to 10. If you haven't applied any rating at all, the probability is fixed at 25%.</p>
<p>If you uncheck the Random Play box, Louis, Louis operates more or less like a conventional MP3 player. You can set it to start playing tracks anywhere in your playlist, and it will work its way sequentially through all your music. You can navigate through your playlist by clicking the <em>Playlist</em> button on the main screen. That brings up a tree view of all your music, as shown in Figure 2.</p>
<p><center><img alt="Figure 2" id="image30" src="http://marknelson.us/attachments/louis-louis-the-preternatural-jukebox/Figure2.gif" /><br />
Figure 2<br />
The Tree View of the Master Playlist</center>You can add tracks to the database at any time by clicking the <em>Add Tracks</em> button on the main window. It brings up a dialog that lets you select a folder and scan it for new material. If you keep all your music in the same folder you can simply rescan every time you add new tracks. A scan in progress is shown in Figure 3.</p>
<p><center><img alt="Figure 3" id="image31" src="http://marknelson.us/attachments/louis-louis-the-preternatural-jukebox/Figure3.gif" /><br />
Figure 3<br />
Scanning for New Songs</center>After you scan your hard drive for files and start using Louis, Louis, you can start entering ratings for Artists, Albums, and Songs. All it takes to do that is a quick movement of the appopriate slider control and you're done! Every time you enter in a new rating, you'll find that Louis, Louis becomes better and better at playing the music you want to hear.</p>
<h4>Building an MP3 Player</h4>
<p>If you want to build your own MP3 player, there are many avenues you can take. Just as an example, the folks at <a href="http://www.javazoom.net/">javazoom.net</a> have developed a free MP3 player component written in pure Java, suitable for developing an app that can play on multiple platforms. There are many other freeware MP3 players that you can modify to suit your needs.</p>
<p>When I set out on the Louis, Louis project I was hoping to avoid the work of completely implementing an MP3 player from scratch. After a little research, I saw that both WinAmp (from Nullsoft at <a href="http://www.winamp.com">winamp.com</a>) and Microsoft's Windows Media Player had SDKs that would allow me to control the player while taking advantage of the existing user interface, playlist features, and so on.</p>
<p>Both of these programs were good candidates for a personal jukebox, but in the end I chose the Microsoft solution. The fact that I could embed WMP as a control in my program gave me a somewhat cleaner solution than I would have controlling Winamp as an external program. Just as importantly, I found out that WMP has an additional bonus: Microsoft's license to use MP3 technology is passed on to users of the WMP SDK. Thus, if Louis, Louis were ever to be distributed, the headache of acquiring a license to use MP3 patented technology would be avoided.</p>
<h4>Embedding WMP in Louis, Louis</h4>
<p>Microsoft's SDK for Windows Media Player can be found by navigationg to their <a href="http://msdn.microsoft.com">developer support site</a>. From the <em>Downloads</em> link you can navigate down through <em>Graphics and Multimedia</em> to <em>Windows Media Technologies</em> and finally download the <em>Windows Media Player 7.1 SDK</em>. When you download and install the SDK you will have to agree to an End User License Agreement which will describe the terms under which you can use WMP. For PC based MP3 player you don't have to worry about royalties or other payments; you can even distribute the WMP components as long as you follow the rules.</p>
<p>Besides the license, the most important thing you get from the SDK is a set of documentation. The help pages describing the WMP object model are essential, including all the functions and events needed to develop an app.</p>
<p>Although the SDK ships with a decent amount of sample code, C++ programmers should prepare for disappointment. There is a grand total of one C++ example, which is basically an API exerciser. It might help you perform a few experiments, but there is no way the sample is going to show you how to write an application like Louis, Louis.</p>
<p>Since the single C++ example was an ATL-based app, I decided to create Louis, Louis as a dialog-based ATL application, embedding the WMP OCX as a control in the dialog. ATL allows you to do this without writing too much code, but you will have to do a substantial amount of work without the benefit of the wizard-driven coding used for MFC development.</p>
<h4>The ATL App</h4>
<p>Microsoft's AppWizard doesn't have an option for creating a simple ATL Window-based application, so I had to wing it. My initial app was created following a <a href="http://www.codeguru.com/atl/atlWindows.html">fairly simple recipe from Andrew Whitechapel</a> on the invaluable <a href="http://www.codeguru.com/">CodeGuru</a> site. I made a couple of modifications needed to change my main Window class to be Dialog-based, and had an app that was ready to go.</p>
<p>At this point I had a main window class, <span class="inline_code">CLouisDialog</span>, and was ready to start working with the WMP OCX. The first task was to create an instance of the OCX and embed it in the dialog.</p>
<p>ATL has a Window class called <span class="inline_code">CAxWindow</span> that is specifically designed to contain ActiveX controls, so the first thing I did was add a member of this class to <span class="inline_code">CLouisDialog</span>, then create the window as a child in <span class="inline_code">CLouisDialog::OnInitDialog()</span>. Once the window is created, it's a simple step to create an instance of the Windows Medial Player OCX in that window. The code extracted from <span class="inline_code">OnInitDialog()</span> is shown below:</p>
<div class="igBar"><span id="lcpp-1"><a href="#" onclick="javascript:showPlainTxt('cpp-1'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-1">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1">m_PlayerHostWindow.<span class="me1">Create</span><span class="br0">&#40;</span> m_hWnd,</div>
</li>
<li class="li2">
<div class="de2">rect,</div>
</li>
<li class="li1">
<div class="de1"><span class="st0">"LouisPlayerHost"</span>,</div>
</li>
<li class="li2">
<div class="de2">WS_CHILD | WS_VISIBLE |</div>
</li>
<li class="li1">
<div class="de1">WS_CLIPCHILDREN,</div>
</li>
<li class="li2">
<div class="de2">WS_EX_CLIENTEDGE <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">CComPtr pHost;</div>
</li>
<li class="li2">
<div class="de2">HRESULT hr = m_PlayerHostWindow.<span class="me1">QueryHost</span><span class="br0">&#40;</span> &amp;pHost <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">char</span> *guid = <span class="st0">"{6BF52A52-394A-11d3-B153-00C04F79FAA6}"</span>;</div>
</li>
<li class="li2">
<div class="de2">hr = pHost-&gt;CreateControl<span class="br0">&#40;</span> CComBSTR<span class="br0">&#40;</span>guid<span class="br0">&#41;</span>, m_PlayerHostWindow, <span class="nu0">0</span><span class="br0">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h4>Working With the Control</h4>
<p>After instantiating the WMP control, the next step is setting up the program elements needed to call its methods and respond to its events. The first step in this is to use Visual C++'s nifty import facility to pull in a complete definition of the COM objects in the OCX. This is accomplished by adding a single line of code to <span class="inline_code">STDAFX.H</span>:</p>
<div class="igBar"><span id="lcpp-2"><a href="#" onclick="javascript:showPlainTxt('cpp-2'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-2">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="co2">#import &quot;WMP.OCX&quot; </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>I then added a set of member variables to class <span class="inline_code">CLouisDialog</span>:</p>
<div class="igBar"><span id="lcpp-3"><a href="#" onclick="javascript:showPlainTxt('cpp-3'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-3">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">protected</span>:</div>
</li>
<li class="li2">
<div class="de2">CComPtr m_Player;</div>
</li>
<li class="li1">
<div class="de1">CComPtr m_Playlist;</div>
</li>
<li class="li2">
<div class="de2">CComPtr m_Controls;</div>
</li>
<li class="li1">
<div class="de1">CComPtr m_MediaCollection; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>These four COM objects represent the interfaces by which I call methods on the various components of the player. They are initialized in <span class="inline_code">CLouisDialog::OnInitiDialog()</span> right after the WMP OCX is created:</p>
<div class="igBar"><span id="lcpp-4"><a href="#" onclick="javascript:showPlainTxt('cpp-4'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-4">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1">hr = m_PlayerHostWindow.<span class="me1">QueryControl</span><span class="br0">&#40;</span> &amp;m_Player <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">hr = m_Player-&gt;get_currentPlaylist<span class="br0">&#40;</span> &amp;m_Playlist <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">hr = m_Player-&gt;get_controls<span class="br0">&#40;</span> &amp;m_Controls <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">hr = m_Player-&gt;get_mediaCollection<span class="br0">&#40;</span> &amp;m_MediaCollection <span class="br0">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>With these objects in place I now have the ability to insert and remove songs into playlist, skip from one song to the next, and so on. All I need now is the ability to receive events.</p>
<h4>Receiving Events</h4>
<p>To receive events from the OCX, I need to create a separate event sink class. The ATL has a template class called <span class="inline_code">IDispEventImpl</span> that is designed to do just this. Following Microsoft's cookbook for this class, I created an event sink that had support for the only event I need, the CurrentItemChange event. The resulting class definition looks like this:</p>
<div class="igBar"><span id="lcpp-5"><a href="#" onclick="javascript:showPlainTxt('cpp-5'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-5">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1">class CLouisEventSink</div>
</li>
<li class="li2">
<div class="de2">: <span class="kw2">public</span> IDispEventImpl&lt;<span class="nu0">1</span>,</div>
</li>
<li class="li1">
<div class="de1">CLouisEventSink,</div>
</li>
<li class="li2">
<div class="de2">&amp;DIID__WMPOCXEvents,</div>
</li>
<li class="li1">
<div class="de1">&amp;LIBID_WMPOCX,</div>
</li>
<li class="li2">
<div class="de2"><span class="nu0">1</span>,</div>
</li>
<li class="li1">
<div class="de1"><span class="nu0">0</span>&gt;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span>:</div>
</li>
<li class="li2">
<div class="de2">CLouisEventSink<span class="br0">&#40;</span> CLouisDialog &amp;frame <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">virtual</span> ~CLouisEventSink<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">CLouisDialog &amp;m_Frame;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">BEGIN_SINK_MAP<span class="br0">&#40;</span>CLouisEventSink<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">SINK_ENTRY_EX<span class="br0">&#40;</span> <span class="nu0">1</span>, DIID__WMPOCXEvents, 0x16ae, CurrentItemChange <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">END_SINK_MAP<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw4">void</span> _stdcall CurrentItemChange<span class="br0">&#40;</span>IDispatch * pdispMedia <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Adding new events to this map is simply a matter of creating a new entry in the header file sink map, then adding a new member function to handle the event. Note that the <em>dispid</em> that uniquely identifies the event (0x16ae in this case) and the function prototype for the event handler are found in the <span class="inline_code">WMP.TLI</span> file created when processing the <span class="inline_code">#import</span> statement.</p>
<p>To actually connect to the event mechanism, I create a CLouisEventSink member in CLouisDialog, then connect it to the OCX in the InitDialog() function:</p>
<div class="igBar"><span id="lcpp-6"><a href="#" onclick="javascript:showPlainTxt('cpp-6'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-6">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1">hr = m_EventSink.<span class="me1">DispEventAdvise</span><span class="br0">&#40;</span> m_Player <span class="br0">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>A corresponding <span class="inline_code">Unadvise</span> function call has to be made when the program is shutting down.</p>
<h4>Database</h4>
<p>With the control in place, the other major component needed for full functionality is the database. For this program, I created a simple Access database with three tables: <em>Tracks</em>, <em>Albums</em>, and <em>Artists</em>. Each contains the name and rating for the specific items. The Tracks table also contains a file name that denotes the actual location of the specfic song.</p>
<p>I created a single query that groups all of this data into a single sorted master playlist - this is what the program uses to decide what to play during normal operations. The three primary tables are only accessed directly when new tracks are being added.</p>
<p>Communicating with this database is nearly trivial when using OLE DB access as provided through ATL. For each of the three tables and the single query I simply used the ATL Object Wizard to create a consumer class that gives me full access to that object.</p>
<p>I had some concern about the speed with which the program can access items in the database. At program startup, the single query is executed and all of its information is loaded into memory. On my development system, processing around 8,000 songs seems to take only a second or two, which is acceptable. However, users with significantly slower machines or significantly larger music libraries might have to rethink this strategy.</p>
<h4>Adding tracks</h4>
<p>Adding tracks to the database is a two-step operation. As Figure 3 shows, the first step is to identify a root folder, then execute a file search for files with the MP3 extension. This is cookie-cutter code and needs no elaboration.</p>
<p>Finding the file is the simple part - but once you have the file, you have to determine the title of the song, the album, and artist. Readers who remember my DDJ article of January 2000, The Ultimate Home Jukebox, know that I keep my personal music organized in a directory structure that automatically gives you the artist, album, title, and track number of any given file.</p>
<p>But it would be unrealistic to expect everyone to follow this convention, particularly when a better solution is available: ID3 tags. The ID3 tag is a conventional way of embedding information about a track directly in the MP3 file itself. A nice explanation of how this works can be found at <a href="http://www.id3.org/">http://www.id3.org</a>, the ID3v2 web site.</p>
<p>Rather than writing my own ID3 decoder class, I went to one of my favorite programming web sites, <a href="http://www.codeproject.com/">The Code Project</a>, and found the <span class="inline_code">CMP3Info</span> class by Roman Nurik. I dropped it into my project and it worked perfectly with no muss, no fuss!</p>
<p>As nice as Roman's class is, I still elected to perform a bit of surgery on it. Roman was getting some detailed information about the encoding of each block in the MP3 file, which took a considerable amount of time. I cut that code out, limiting the class to just the ID3 info I cared about.</p>
<p>The resulting code seems pretty efficient. On my sub-GHz development computer, I can scan my entire library of 8300 tracks, create track, artist, and album records for each, and update the database in under two minutes, or roughly 75 tracks per second. Given that you will do updates of this magnitude infrequently, this seems pretty good to me.</p>
<h4>Overall Operations</h4>
<p>The actual operation of Louis, Louis is essentially identical whether in random shuffle or normal jukebox mode. In both cases, tracks are selected from the master playlist, which is sorted by artist, then album, then track number. In random play mode the list undergoes a single shuffle operation, and as songs are selected they may be skipped if they fail the internal coin toss.</p>
<p>The WMP component is fed a playlist of five songs, allowing for a current song being played, two previous songs, and two upcoming songs. This insures that that user can use the skip buttons on WMP to immediately go ahead to the next song or back up to listen to one that was previously heard.</p>
<p>Each time a song completes, a <span class="inline_code">CurrentItemChange</span> event is sent to the event sink. Each time we move forward, Louis, Louis adds a new song to the playlist on the upcoming end, and removes one from the already-been-played side. This virtual playlist makes random play mode easy to implement.</p>
<h4>Requirements and a Quirk</h4>
<p>To build and work with Louis, Louis, you will need to have Visual C++ 6, and it's probably a good idea to bring it up to Service Pack 5. In addition, you'll need to install Windows Media Player 7.1. Odds are you'll want to install the Windows Media Player SDK as well, if only for the help file.</p>
<p>One additional point that you'll need to be aware of before using Louis, Louis. By default, Windows Media Player will not allow external apps to insert tracks into its playlist, which means programs like Louis, Louis are disabled by default. To enable external control of the playlist, you will need to start Windows Media Player, select the <em>Tools|Options</em> menu item, switch to the <em>Media Library tab</em>, and select <em>Full Access</em> for Internet Site rights.</p>
<p>In summary, creating Louis, Louis has been a lot of fun. To Microsoft's credit, Windows Media Player gives me a powerful tool to make this happen, in combination with ATL for the UI and OLE DB for database access. I started this project with little knowledge of all three topics, and was able to fumble my way through to a finished product that I'm pretty happy with. If only all programming assignments could follow this path!</p>
<p><i>&copy; 2002 Mark Nelson</i></p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2002/10/01/louis-louis-the-preternatural-jukebox/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Star-Encoding in C++</title>
		<link>http://marknelson.us/2002/08/01/star-encoding-in-cpp/</link>
		<comments>http://marknelson.us/2002/08/01/star-encoding-in-cpp/#comments</comments>
		<pubDate>Fri, 02 Aug 2002 05:00:51 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[Data Compression]]></category>
		<category><![CDATA[Magazine Articles]]></category>

		<guid isPermaLink="false">http://test.kewby.com/?p=25</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2002/08/01/star-encoding-in-cpp/' addthis:title='Star-Encoding in C++' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div>Star-encoding is a transform that can be applied to text before compression, giving significant improvement in performance. This article discusses how Star-encoding works, and provides you with working code to create dictionaries, compress, and uncompress data using Star Encoding.]]></description>
			<content:encoded><![CDATA[











<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2002/08/01/star-encoding-in-cpp/' addthis:title='Star-Encoding in C++' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div><p/>
<table border="0" width="300px">
<tbody>
<tr>
<td width="33%">
      <img src="http://marknelson.us/attachments/star-encoding-in-cpp/cover.gif" alt="DDJ Cover from August, 2002" />
      </td>
<td>
        <b>Dr. Dobb's Journal</b></p>
<p>August, 2002</p>
</td>
</tr>
</tbody>
</table>
<p>This article describes an interesting data compression technique called star-encoding. I'll explain what star-encoding is, describe a simple C++ implementation, and show you what sort of results you can expect to see when using it.</p>
<h4>Pre-Compression Transformations</h4>
<p>One interesting class of data compression techniques consists of transformation algorithms. It is often possible to perform a reversible transformation on a data set that increases its susceptibility to other compression techniques.</p>
<p>A great example of this is the <em>Burrows-Wheeler Transform</em> (BWT), which I described in an <a href="/1996/09/01/data-compression-with-the-burrows-wheeler-transform/">article</a> in Dr. Dobb's Journal in 1996. The BWT is a completely reversible sorting algorithm that tends to create long runs of identical characters in the output text. Using a Move To Front algorithm followed by Huffman or arithmetic coding gives compression that outperforms all but the best algorithms.</p>
<p>A similar example is that of the <em>Discrete Cosine Transform</em> (DCT). In the JPEG compression algorithm, 8X8 blocks of pixels are run through the DCT, which transforms the spatial data points to the frequency domain. At that point, lossy compression algorithms can then be effectively applied.</p>
<p>Of course, the transformations I'm describing here generally don't compress data at all; they merely prep the data before compression is applied. Thus, a key factor in using any transform is understanding what compression technique can be effectively be used on the output. With the Burrows-Wheeler transform, the best results come from a three-stage compressor. The output of the BWT transform is usually piped through a move-to-front stage, then a run length encoder stage, and finally an entropy encoder, normally arithmetic or Huffman coding. The actual command line to perform this sequence will look something like this:</p>
<p><code>BWT < input-file | MTF | RLE | ARI > output-file</code></p>
<h4>Star-Encoding</h4>
<p>The Burrows-Wheeler transform is a fairly complicated sorting algorithm that radically changes the nature of an input file. Star-encoding is much simpler to understand, and can be implemented easily with the help of the standard C++ library.</p>
<p>Star-encoding works by creating a large dictionary of commonly used words expected in the input files. The dictionary must be prepared in advance, and must be known to the compressor and decompressor.</p>
<p>Each word in the dictionary has a star-encoded equivalent, in which as many letters a possible are replaced by the '*' character. For example, a commonly used word such as <em>the</em> might be replaced by the string <em>t**</em>. The star-encoding transform simply replaces every occurrence of the word the in the input file with <em>t**</em>.</p>
<p>Ideally, the most common words will have the highest percentage of '*' characters in their encodings. If done properly, this means that transformed file will have a huge number of '*' characters. This ought to make the transformed file more compressible than the original plain text.</p>
<p>As an example, a section of text from Project Gutenberg's version of Romeo and Juliet looks like this in the original text:</p>
<p><code>But soft, what light through yonder window breaks?<br />
It is the East, and Iuliet is the Sunne,<br />
Arise faire Sun and kill the enuious Moone,<br />
Who is already sicke and pale with griefe,<br />
That thou her Maid art far more faire then she</code></p>
<p>Running this text through the star-encoder yields the following text:</p>
<p><code>B** *of*, **a* **g** *****g* ***d*r ***do* b*e***?<br />
It *s *** E**t, **d ***i** *s *** *u**e,<br />
A***e **i** *un **d k*** *** e****** M****,<br />
*ho *s a****** **c*e **d **le ***h ****fe,<br />
***t ***u *e* *ai* *r* f*r **r* **i** ***n s**</code></p>
<p>You can clearly see that the encoded data has exactly the same number of characters, but is dominated by stars. It certainly looks as though it is more compressible. But before you can test the compressibility of the output data, you need to build the three programs this system requires:</p>
<ul>
<li>A program to create the dictionary.</li>
<li>An encoder which star-encodes a file using a dictionary.</li>
<li>A decoder which decodes the file using an identical dictionary.</li>
</ul>
<p>I'll discuss each of these in turn in the remainder of this article.</p>
<h4>Creating the Dictionary</h4>
<p>The program that creates the dictionary, <span class="inline_code">MakeDictA.cpp</span>, is show in Listing 1. This program is a testament to the power of the C++ standard library classes. It would literally triple in size if it were written in C, and would have been a nightmare to debug.</p>
<p><span class="inline_code">MakeDictA</span> is invoked with a list of file names as command line arguments. It scans through each file looking for tokens, adds them to the dictionary if needed, and updates the token's frequency count.</p>
<p>After all of the tokens have been counted, the list is sorted by frequency. The program then goes through the list, starting with the most frequent tokens, and assigns them star-codes. As a final step, the dictionary is written to standard output.</p>
<h4>Tokenizing and scanning</h4>
<p>Unfortunately, the C++ standard library doesn't have a stream tokenizer. If my needs were more complicated, I might use the Boost Tokenizer found at the <a href="http://boost.org/libs/tokenizer/index.html">boost.org Tokenizer page</a>, but in this case I spurned reuse and wrote just a few lines:</p>
<div class="igBar"><span id="lcpp-7"><a href="#" onclick="javascript:showPlainTxt('cpp-7'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-7">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1">string get_token<span class="br0">&#40;</span> ifstream &amp;file <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; string token;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span> <span class="kw4">file</span> <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">char</span> c;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; file&gt;&gt; c;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> <span class="kw3">isalpha</span><span class="br0">&#40;</span> c <span class="br0">&#41;</span> || c == <span class="st0">'<span class="es0">\'</span>'</span> <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; token += c;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span> token.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> token;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> token;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This function is called repeatedly from <span class="inline_code">main()</span>. The <span class="inline_code">map<string,int></span> object named <span class="inline_code">frequency</span> is updated as each token is encountered. If the string is not found in <span class="inline_code">frequency</span>, it is inserted with a count of 1. Otherwise the count is incremented:</p>
<div class="igBar"><span id="lcpp-8"><a href="#" onclick="javascript:showPlainTxt('cpp-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-8">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1">string token = get_token<span class="br0">&#40;</span> infile <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> token.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span> == <span class="nu0">0</span> <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; map&lt;string, int&gt;::<span class="me2">const_iterator</span> ii = frequency.<span class="me1">find</span><span class="br0">&#40;</span> token <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> ii == frequency.<span class="me1">end</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; frequency<span class="br0">&#91;</span> token <span class="br0">&#93;</span> = <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">else</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; frequency<span class="br0">&#91;</span> token <span class="br0">&#93;</span>++; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h4>Sorting and Assigning Codes</h4>
<p>After all the tokens from all the input files have been scanned, you have a map with an appearance count for every word encountered in the input text. The next step requires that you iterate through all the words starting at the most frequent and going all the way to the least.</p>
<p>Unfortunately, the frequency map is keyed on the word name, not the frequency. So before code assignment can start, you need to reorder the data. You can do this with just a few lines of code by inserting all the string/integer pairs into a <span class="inline_code">map<int,string> </span>object called <span class="inline_code">counts</span>. Since this map is keyed on the integer value instead of the string, the values are automatically sorted in the form you want.</p>
<div class="igBar"><span id="lcpp-9"><a href="#" onclick="javascript:showPlainTxt('cpp-9'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-9">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1">multimap&lt;int, string&gt; counts;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span> map&lt;string, int&gt;::<span class="me2">iterator</span> ii = frequency.<span class="me1">begin</span><span class="br0">&#40;</span><span class="br0">&#41;</span> ; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ii != frequency.<span class="me1">end</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ii++ <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; counts.<span class="me1">insert</span><span class="br0">&#40;</span> pair&lt;int,string&gt;<span class="br0">&#40;</span> <span class="br0">&#40;</span>*ii<span class="br0">&#41;</span>.<span class="me1">second</span>, <span class="br0">&#40;</span>*ii<span class="br0">&#41;</span>.<span class="me1">first</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>With the data sorted correctly, the remaining piece of hard work is to iterate over the <span class="inline_code">counts</span> map and assign a code to each one:</p>
<div class="igBar"><span id="lcpp-10"><a href="#" onclick="javascript:showPlainTxt('cpp-10'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-10">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1">set&lt;string&gt; used_codes;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">for</span> <span class="br0">&#40;</span> multimap&lt;int, string&gt;::<span class="me2">reverse_iterator</span> jj = counts.<span class="me1">rbegin</span><span class="br0">&#40;</span><span class="br0">&#41;</span> ; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; jj != counts.<span class="me1">rend</span><span class="br0">&#40;</span><span class="br0">&#41;</span> ; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; jj++ <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; string code = create_star_code<span class="br0">&#40;</span> <span class="br0">&#40;</span>*jj<span class="br0">&#41;</span>.<span class="me1">second</span>, used_codes <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; used_codes.<span class="me1">insert</span><span class="br0">&#40;</span> code <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; codes<span class="br0">&#91;</span> <span class="br0">&#40;</span>*jj<span class="br0">&#41;</span>.<span class="me1">second</span> <span class="br0">&#93;</span> = code;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">cout</span> &lt;&lt;<span class="br0">&#40;</span>*jj<span class="br0">&#41;</span>.<span class="me1">second</span> &lt;&lt;<span class="st0">" "</span> &lt;&lt;code &lt;&lt;<span class="st0">" "</span> &lt;&lt;<span class="br0">&#40;</span>*jj<span class="br0">&#41;</span>.<span class="me1">first</span> &lt;&lt;endl;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>A couple of interesting things of note show up in this loop. First, each time a new code is assigned to a token, the code value is stored in a <span class="inline_code">set<string></span> called <span class="inline_code">used_codes</span>. You have to keep track of the used code values during the assignment process so that no star code is inadvertently used twice.</p>
<p>Second, you can see that the value of each token and its code is being written to standard output. (I write the frequency count as well, although this is technically not needed. It helps me to keep an eye on the process and ensure it is working as desired.)</p>
<h4>The Code Assignment Algorithm</h4>
<p>In <span class="inline_code">MakeDictA</span>, I do star code assignment in a routine called <span class="inline_code">create_star_code()</span>. This algorithm uses a greedy heuristic to assign codes, attempting to use the highest possible number of '*' characters to each token.:</p>
<div class="igBar"><span id="lcpp-11"><a href="#" onclick="javascript:showPlainTxt('cpp-11'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-11">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">for</span> <span class="br0">&#40;</span> <span class="kw4">int</span> star_count = token.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span> ; star_count&gt; <span class="nu0">0</span> ; star_count-- <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; vector&lt;char&gt; pattern<span class="br0">&#40;</span> token.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, <span class="st0">'*'</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span> <span class="kw4">int</span> i = star_count ; i &lt;token.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span> ; i++ <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; pattern<span class="br0">&#91;</span> i <span class="br0">&#93;</span> = <span class="st0">'-'</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw1">do</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; string test<span class="br0">&#40;</span> token <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span> <span class="kw4">int</span> j = <span class="nu0">0</span> ; j &lt;token.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span> ; j++ <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> pattern<span class="br0">&#91;</span> j <span class="br0">&#93;</span> == <span class="st0">'*'</span> <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; test<span class="br0">&#91;</span> j <span class="br0">&#93;</span> = <span class="st0">'*'</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; set&lt;string&gt;::<span class="me2">const_iterator</span> kk = used_codes.<span class="me1">find</span><span class="br0">&#40;</span> test <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> kk == used_codes.<span class="me1">end</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> test;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span> <span class="kw1">while</span> <span class="br0">&#40;</span> next_permutation<span class="br0">&#40;</span> pattern.<span class="me1">begin</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, pattern.<span class="me1">end</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">return</span> token; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>For a token of length <em>N</em>, the assignment routine first attempts to replace all characters in the token with stars, then all but one, then all but two, and so one. Note that the often-overlooked <span class="inline_code">next_permutation</span> function from the standard library helps out quite a bit with this.</p>
<h4>Executing MakeDictA</h4>
<p>To test this program, I went to Project Gutenberg and procured the text of eight of Shakespeare's plays. (The text is included in the source zip file for this project.) With allowances for line breaks, the text below shows the command line I used to create the dictionary file:</p>
<p><code>[c:\star] MakeDictA text\AsYouLikeIt.txt<br />
                    text\Hamlet.txt<br />
                    text\JuliusCaesar.txt<br />
                    text\KingLear.txt<br />
                    text\Macbeth.txt<br />
                    text\RomeoAndJuliet.txt<br />
                    text\TheTamingOfTheShrew.txt<br />
		    text\TheTempest.txt<br />
		  > DictA.txt<br />
</code></p>
<p>The program creates 16,927 codes in fairly short order, producing <span class="inline_code">DictA.txt,</span> a file that is shown below in somewhat abridged form:</p>
<p><code>the *** 4556<br />
I * 4390<br />
and **d 3676<br />
to ** 3218<br />
of *f 2899<br />
you **u 2775<br />
a a 2714<br />
.<br />
.[skipping 8500 lines]<br />
.<br />
vexations ****t**n* 1<br />
vestall **s*a** 1<br />
versall **r**l* 1<br />
verities ***i***s 1<br />
verge v*r** 1<br />
.<br />
. [skipping 8500 lines]<br />
.<br />
'Twentie *T****** 1<br />
'Saue *S*** 1<br />
'Prethee *P****** 1<br />
'Pre *P** 1<br />
'Boue *B***</code></p>
<p>With this somewhat limited vocabulary, you can see that even words that appear very rarely are replaced with a very high percentage of stars. The most frequent words are replaced completely.</p>
<h4>Compressing and Decompressing the Transformed Data</h4>
<p>Listing 2 and 3 show <span class="inline_code">StarEncode.cpp</span> and <span class="inline_code">StarDecode.cpp</span>, the short programs used to transform text files to and from star-encoded format. I won't go into the details of their operation - if you understand <span class="inline_code">MakeDictA.cpp</span>, you won't have any trouble with these two programs.</p>
<p>Both programs are invoked with the name of the dictionary file on the command line. The programs then transform standard input to standard output. Internally, they simply read the dictionary file into a <span class="inline_code">map<string,string></span> object that holds all the encodings. They then read in text, passing special characters through unchanged, and translating tokens whenever possible.</p>
<p>Figure 1 shows the encoding program in operation, as it encodes Romeo and Juliet using the dictionary created earlier:</p>
<p><center><br />
<img src="http://marknelson.us/attachments/star-encoding-in-cpp/Figure1.gif"><br />
Figure 1 - Encoding Romeo and Juliet<br />
</center></p>
<p>The result of this encoding should be a file that is now more compressible than the original. To test this theory, I compressed both the original and the encoded file in WinZip, with the results shown in Figure 2. </p>
<p><center><br />
<img src="http://marknelson.us/attachments/star-encoding-in-cpp/Figure2.gif"><br />
Figure 2 - Compressing the encoded file<br />
</center><br />
The results are encouraging. The compressed file decreases in size by almost 12%, with the compression ratio improving from 60% to 64%. Tangible savings with only a small increase in computation.</p>
<h4>Variant B</h4>
<p>While building the encoder, I started wondering about the structure of my star codes. It seemed kind of arbitrary to insist that every star code have the same number of stars as the word it was encoding. For example, the most frequent word, <em>the</em>, was encoded as "***", while the second most frequent word, <em>I</em>, was encoded with a single star. Shouldn't the most frequent word get the fewest number of stars?</p>
<p>To test this theory, I created a second dictionary builder using a new heuristic for assigning codes. In this heuristic, a star code is simply a single star character concatenated with a plain-text code number, with the most frequent token being assigned code 0.</p>
<p>Using this scheme simplified my encoder quite a bit. The main loop that assigns codes to tokens no longer needed to keep track of used codes, and was simple enough to do code assignment inside the loop, instead of in a separate function:</p>
<div class="igBar"><span id="lcpp-12"><a href="#" onclick="javascript:showPlainTxt('cpp-12'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-12">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">int</span> star_code = <span class="nu0">0</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">for</span> <span class="br0">&#40;</span> multimap&lt;int, string&gt;::<span class="me2">reverse_iterator</span> jj = counts.<span class="me1">rbegin</span><span class="br0">&#40;</span><span class="br0">&#41;</span> ; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; jj != counts.<span class="me1">rend</span><span class="br0">&#40;</span><span class="br0">&#41;</span> ; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; jj++ <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; string token = <span class="br0">&#40;</span>*jj<span class="br0">&#41;</span>.<span class="me1">second</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> token.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span>&gt; <span class="nu0">1</span> <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt;token </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;&lt;<span class="st0">" "</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;&lt;<span class="st0">'*'</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;&lt;star_code++ </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;&nbsp; &lt;&lt;<span class="st0">" "</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;&lt;<span class="br0">&#40;</span>*jj<span class="br0">&#41;</span>.<span class="me1">first</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;&lt;endl;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Comparing the output from Variant B to that describe earlier shows how the two strategies create quite different dictionaries:</p>
<p><code>the *0 4556<br />
and *1 3676<br />
to *2 3218<br />
of *3 2899<br />
you *4 2775<br />
.<br />
.[skipping 8500 lines]<br />
.<br />
vexations *8480 1<br />
vestall *8481 1<br />
versall *8482 1<br />
verities *8483 1<br />
verge *8484 1<br />
.<br />
. [skipping 8500 lines]<br />
.<br />
'Twentie *16899 1<br />
'Saue *16900 1<br />
'Prethee *16901 1<br />
'Pre *16902 1<br />
'Boue *16903 1</code></p>
<p>One notable difference in this case is that I don't bother to encode strings of just a single character in length, as their encoded values would always be larger. To test this variant, I compiled <span class="inline_code">MakeDictB.cpp</span>, created <span class="inline_code">DictB.txt</span>, and ran <span class="inline_code">RomeoandJuliet.txt</span> through <span class="inline_code">StarEncode</span>. The five lines shown earlier in this article have now morphed into the following:</p>
<p><code>*43 *1069, *41 *349 *469 *1492 *2583 *12782?<br />
*114 *7 *0 *2550, *1 *417 *7 *0 *931,<br />
*5359 *193 *902 *1 *521 *0 *1548 *616,<br />
*168 *7 *1283 *731 *1 *695 *13 *1042,<br />
*34 *21 *27 *938 *144 *3817 *49 *193 *37 *67</code></p>
<p>The text is somewhat harder to read, but also a bit more compact. Note also that the character set is much more limited here, since every word is star-encoded using only the star character and the integers.</p>
<h4>Comparing A and B</h4>
<p>Figure 3 shows the listing of a Zip file that contains both encoded texts plus the original.</p>
<p><center><br />
<img src="http://marknelson.us/attachments/star-encoding-in-cpp/Figure3.gif"><br />
Figure 3 - A Zip file with both A and B variants<br />
</center></p>
<p>In this case, the difference between a standard Zip compressor and variant B runs nearly 15%, quite a nice savings. You can see that variant B clearly comes out ahead, with a delta that seems worth going for.</p>
<h4>Recommendations and Caveats</h4>
<p>Star-encoding is clearly a handy way to squeeze some additional fluff out of your files. Of course, there is a catch. For star-encoding to be effective, you have to know in advance what the vocabulary of your source files is going to be. There are many times when this will be the case. Typical examples might include:</p>
<ul>
<li>Computer-generated reports</li>
<li>Email or newsgroup archives</li>
<li>Trace or log files</li>
<li>Cached web pages</li>
</ul>
<p>I doubt that star-encoding could be used in a general purpose compressor. A hypothetical archiver called <em>StarZip</em> might attempt to create a good dictionary and tokenizer for various file types, but the task seems difficult.</p>
<p>The code I have written here is typical magazine code: short and simple. It could form the basis for production code, but would need some hardening first. For example, the programs shown here can't cope with something as simple as a star character embedded in the plain text. <em>Caveat emptor</em>.</p>
<p>There is plenty of room for experimentation here. I'd love to hear back from any readers who develop encodings that can outdo the ones shown here. Send me an email if you create something interesting here. Enjoy!</p>
<h4>References</h4>
<p>Robert Franceschini and Amar Mukherjee. Data Compression Using Encrypted Text. Proceedings of the third Forum on Research and Technology, Advances on Digital Libraries, ADL 96, 1996, pp. 130-138. Robert Franceschini, Holger Kruse, Nan Zhang, Raja Iqbal and Amar Mukherjee, <a href="http://vlsi.cs.ucf.edu/datacomp/papers/textcomp.doc">Lossless, Reversible Transformations that Improve Text Compression Ratios</a>.</p>
<p>Mark Nelson, Data Compression with the Burrows-Wheeler Transform, Dr. Dobb's Journal, September 1996.</p>
<h4>Listings</h4>
<p>Listing 1 - <a href="http://marknelson.us/attachments/star-encoding-in-cpp/MakeDictA.cpp">MakeDictA.cpp</a><br />
Listing 2 - <a href="http://marknelson.us/attachments/star-encoding-in-cpp/StarEncode.cpp">StarEncode.cpp</a><br />
Listing 3 - <a href="http://marknelson.us/attachments/star-encoding-in-cpp/StarDecode.cpp">StarDecode.cpp</a><br />
Listing 4 - <a href="http://marknelson.us/attachments/star-encoding-in-cpp/MakeDictB.cpp">MakeDictB.cpp</a><br />
Sample text - <a href="http://marknelson.us/attachments/star-encoding-in-cpp/Shakespeare.zip">Shakespeare.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2002/08/01/star-encoding-in-cpp/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Rendering Transparent GIFs &#8211; Revisited</title>
		<link>http://marknelson.us/2002/07/01/rendering-transparent-gifs-revisited/</link>
		<comments>http://marknelson.us/2002/07/01/rendering-transparent-gifs-revisited/#comments</comments>
		<pubDate>Tue, 02 Jul 2002 04:59:33 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Magazine Articles]]></category>

		<guid isPermaLink="false">http://test.kewby.com/?p=24</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2002/07/01/rendering-transparent-gifs-revisited/' addthis:title='Rendering Transparent GIFs &#8211; Revisited' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div>Rendering GIFs with transparency under Win32. It's gotten a bit easier since I first wrote about it for DDJ in 1998. This article shows a couple of ways to implement transparent GIF rendering: the IPicture interface, and GDI+.]]></description>
			<content:encoded><![CDATA[













<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2002/07/01/rendering-transparent-gifs-revisited/' addthis:title='Rendering Transparent GIFs &#8211; Revisited' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div><p/>
<table border="0" width="400px">
<tbody>
<tr>
<td width="33%">
      <img src="http://marknelson.us/attachments/rendering-transparent-gifs-revisited/wdmag.jpg" alt="Windows Developer Journal, July, 2002" />
      </td>
<td>
        <b>Windows Developer Magazine</b></p>
<p>July, 2002</p>
</td>
</tr>
</tbody>
</table>
<p>Back in March 1998, Tom Armstrong and I published an article in Dr. Dobb's Journal showing a technique for implementing transparent regions in bitmapped images. In those days, Windows developers had to roll their own transparency solution, and the path was quite arduous. Today, Windows programmers can render images with transparency using Microsoft's <span class="inline_code">IPicture</span> COM interface, which is considerably easier than the solution Tom and I suggested. It's an easy way to accomplish the same task - if you watch out for its single notable drawback</p>
<p>The <span class="inline_code">IPicture</span> interface is a good way to draw images with transparency today. In the future, you'll probably want to use GDI+, Microsoft's new GDI interface for C++ programmers. I'll show you how to convert the sample program to write cleaner code with GDI+, and discuss the improvements it brings to the table.</p>
<h4>Transparent GIFs</h4>
<p>When creating a GIF file, you have the option of designating one color in the image as transparent. This transparency feature is normally used to make it appear as if an irregularly shaped image is floating on the background. An example of how this can be useful is shown in Figures 1 and 2.</p>
<p>In Figure 1, two news articles displayed on a Web page have graphical icons that illustrate article topics. The users of this particular web site have the ability to change their personal settings to use a different layout, which changes the background color of the page. In Figure 2, a different theme is used to show the same articles.</p>
<p>In both Figure 1 and Figure 2, the background of the GIF file used to illustrate the article is transparent. Web browsers know that they need to let the background color show through in the transparent regions of the graphic. You can see that the browser deals with the transparency properly in both cases, with nice effect. A beige background in Figure 1, a white background in Figure 2, and in both the topic images float nicely above the background.</p>
<p><center><br />
<img src="http://marknelson.us/attachments/rendering-transparent-gifs-revisited/Figure1.gif"/><br />
Figure 1 - Transparent GIFs on a light brown background<br />
</center></p>
<p><center><br />
<img src="http://marknelson.us/attachments/rendering-transparent-gifs-revisited/Figure2.gif"/><br />
Figure 2 - The same GIFs on a white background<br />
</center></p>
<h4>Loading the GIF File</h4>
<p>Getting an external file into a format Windows can display can be a chore. Tom and I chose to skirt the issue in our original article by working with files stored in Windows' Device Independent Bitmap format. Although not a popular file format, it was one of the few that Windows was able to read and write without help from third-party libraries.</p>
<p>The situation has improved quite a bit since then. You can now use Microsoft's <span class="inline_code">OleLoadPicture()</span> function to load both JPEG and GIF files. Given the popularity of these two file formats, you might find that <span class="inline_code">OleLoadPicture()</span> covers all your needs in this area.</p>
<p><span class="inline_code">OleLoadPicture()</span> loads data from an <span class="inline_code">IStream</span> object and stores it in an <span class="inline_code">IPicture</span> object. You'll find that having the data in <span class="inline_code">IPicture</span> format is quite convenient when it comes time to render it. However, the odds are good that you want to be able to load your data from a file, which is not quite the same thing as an <span class="inline_code">IStream</span> interface.</p>
<p>The easiest way I know of to get from a file to an <span class="inline_code">IStream</span> object is to simply read the whole thing into memory and then use <span class="inline_code">CreateStreamOnHGlobal()</span> to create an <span class="inline_code">IStream</span>. A fragment that accomplishes that is shown here: </p>
<div class="igBar"><span id="lcpp-13"><a href="#" onclick="javascript:showPlainTxt('cpp-13'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-13">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">FILE</span> *f = <span class="kw3">fopen</span><span class="br0">&#40;</span> filename, <span class="st0">"rb"</span> <span class="br0">&#41;</span> ;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">if</span> <span class="br0">&#40;</span> !f <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">FALSE</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw4">long</span> size = _filelength<span class="br0">&#40;</span> fileno<span class="br0">&#40;</span> f <span class="br0">&#41;</span> <span class="br0">&#41;</span> ;</div>
</li>
<li class="li1">
<div class="de1">HGLOBAL hGlobal = GlobalAlloc<span class="br0">&#40;</span> GMEM_MOVEABLE, size <span class="br0">&#41;</span> ;</div>
</li>
<li class="li2">
<div class="de2">LPVOID pvData = GlobalLock<span class="br0">&#40;</span> hGlobal <span class="br0">&#41;</span> ;</div>
</li>
<li class="li1">
<div class="de1">read<span class="br0">&#40;</span> pvData, <span class="nu0">1</span>, size, f <span class="br0">&#41;</span> ;</div>
</li>
<li class="li2">
<div class="de2">GlobalUnlock<span class="br0">&#40;</span>hGlobal<span class="br0">&#41;</span> ;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">fclose</span><span class="br0">&#40;</span> f <span class="br0">&#41;</span> ;</div>
</li>
<li class="li2">
<div class="de2">LPSTREAM pStream = <span class="kw2">NULL</span>;</div>
</li>
<li class="li1">
<div class="de1">HRESULT hr = CreateStreamOnHGlobal<span class="br0">&#40;</span> hGlobal, <span class="kw2">TRUE</span>, &amp;amp;pStream <span class="br0">&#41;</span> ; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>At this point you have an <span class="inline_code">IStream</span> object ready for use, and all that remains is to make the call to load the <span class="inline_code">IPicture</span>object: </p>
<div class="igBar"><span id="lcpp-14"><a href="#" onclick="javascript:showPlainTxt('cpp-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-14">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1">hr = ::<span class="me2">OleLoadPicture</span><span class="br0">&#40;</span> pStream, </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;size, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">FALSE</span>, </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IID_IPicture,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#40;</span>LPVOID *<span class="br0">&#41;</span> &amp;m_pPicture <span class="br0">&#41;</span> ; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Although the code isn't particularly elegant looking, it has accomplished the goal of reading a GIF or JPEG file into memory in just a dozen  lines or so. All that remains is the task of drawing the code on the  appropriate display. </p>
<h4>The Drawing Part</h4>
<p>In our original article, Tom and I presented a straightforward method for displaying bitmaps with transparency. It required the use of three bitmaps:    </p>
<ol>
<li/>The background bitmap.
<li/>The bitmap to be displayed, with a known transparent color.
<li/>A special mask bitmap. The mask bitmap should have all bits set to zero in areas where the display bitmap is opaque, and all bits set to one in the areas where the display bitmap is transparent.
</ol>
<p>Displaying the bitmap is simply a matter of performing the following sequence of operations: </p>
<p><i>Output = Bitmap1 XOR Bitmap2 AND Bitmap3 XOR Bitmap 2</i></p>
<p>With just a little bit of work, you can see that the output will be  a copy of the pixel from Bitmap1 in every position where the mask  (Bitmap3) has a value of all ones. The output will be a copy from  Bitmap2 in every position where the mask has a value of all zeros. </p>
<p>That's all you really need to understand in order to draw bitmaps  with transparency in any sort of graphics environment. What made it  into a long article in 1998 was simply all the plumbing you had to  do in order to get the background bitmap, create the mask, get the  display bitmap, and so on.</p>
<p>The good news is that Microsoft's <span class="inline_code">IPicture</span> interface can take care of all of the messy details here with one call to its <span class="inline_code">Render()</span> function. Once you have loaded the GIF file into an <span class="inline_code">IPicture</span>  object, the snippet of code below is all you need to get it onto your  display:</p>
<div class="igBar"><span id="lcpp-15"><a href="#" onclick="javascript:showPlainTxt('cpp-15'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-15">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">void</span> CChildView::<span class="me2">RenderPicture</span><span class="br0">&#40;</span> CDC &amp;dc,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CMainFrame *frame, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CRect &amp;rect <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; OLE_XSIZE_HIMETRIC width;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; OLE_YSIZE_HIMETRIC height;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; frame-&gt;m_pPicture-&gt;get_Width<span class="br0">&#40;</span> &amp;width <span class="br0">&#41;</span> ;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; frame-&gt;m_pPicture-&gt;get_Height<span class="br0">&#40;</span> &amp;height <span class="br0">&#41;</span> ;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; CSize sz<span class="br0">&#40;</span> width, height <span class="br0">&#41;</span> ;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; dc.<span class="me1">HIMETRICtoDP</span><span class="br0">&#40;</span> &amp;sz <span class="br0">&#41;</span> ;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">int</span> x = <span class="br0">&#40;</span> rect.<span class="me1">Width</span><span class="br0">&#40;</span><span class="br0">&#41;</span> - pt.<span class="me1">x</span> <span class="br0">&#41;</span> / <span class="nu0">2</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw4">int</span> y = <span class="br0">&#40;</span> rect.<span class="me1">Height</span><span class="br0">&#40;</span><span class="br0">&#41;</span> - pt.<span class="me1">y</span> <span class="br0">&#41;</span> / <span class="nu0">2</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; frame-&gt;m_pPicture-&gt;Render<span class="br0">&#40;</span> dc,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rect.<span class="me1">Height</span><span class="br0">&#40;</span><span class="br0">&#41;</span> - y,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pt.<span class="me1">x</span>, pt.<span class="me1">y</span>,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="nu0">0</span>, </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="nu0">0</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;width, </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;height,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">NULL</span> <span class="br0">&#41;</span> ; </div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Note that the code shown here centers the image in the client window, which complicates things just a bit. It also has to deal with the fact that <span class="inline_code">IPicture</span> uses the <span class="inline_code">HIMETRIC</span> mapping mode, which I  have to convert to the more convenient pixel measurements.  </p>
<h4>A First Pass</h4>
<p>Pressing this code into service reveals one striking problem with the <span class="inline_code">IPicture</span> interface. Calling its <span class="inline_code">Render()</span> function with the intention of writing directly to your display window leads to a somewhat unpleasant surprise. It turns out that <span class="inline_code">IPicture</span> uses the standard algorithm I describe earlier to implement transparency. Unfortunately,  it performs each of the three steps directly on the display, so you get  to see each intermediate bitmap in the process. The four screen shots  below show it in all its glory. </p>
<p><center><br />
<img src="http://marknelson.us/attachments/rendering-transparent-gifs-revisited/Figure3a.gif"><br />
Figure 3a - Opening the file<br />
</center></p>
<p><center><br />
<img src="http://marknelson.us/attachments/rendering-transparent-gifs-revisited/Figure3b.gif"><br />
Figure 3b - The background has been XORed with the image<br />
</center></p>
<p><center><br />
<img src="http://marknelson.us/attachments/rendering-transparent-gifs-revisited/Figure3c.gif"><br />
Figure 3c - The resulting bitmap has now been ANDed with the special transparency mask<br />
</center></p>
<p><center><br />
<img src="http://marknelson.us/attachments/rendering-transparent-gifs-revisited/Figure3d.gif"><br />
Figure 3d - After a final XOR, the image is rendered with transparency<br />
</center></p>
<p>This sequence might be interesting to the average DDJ reader, but the end users of your program are hardly going to want to see these three images  flashing in quick succession across their screen. The flickering is barely  noticeable with small images, but on slow machines, big images go through  the painting sequence with painful precision. </p>
<p>The solution to this problem is straightforward. Instead of drawing  directly to the screen, you can create an off-screen bitmap, have <span class="inline_code">Render()</span> draw to that bitmap, then copy the completed product  to the display. Having done that, you'll find that not only is the flicker eliminated, but the drawing operation actually completes much more  quickly.  </p>
<p>An MFC routine that accomplishes this in conjunction with the rendering routine above is shown here:  </p>
<div class="igBar"><span id="lcpp-16"><a href="#" onclick="javascript:showPlainTxt('cpp-16'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-16">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">void</span> CChildView::<span class="me2">DrawOff</span><span class="br0">&#40;</span> CDC &amp;dc,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CRect &amp;rect,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CMainFrame *frame, </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CBrush &amp;brush <span class="br0">&#41;</span> </div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; CDC odc;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; odc.<span class="me1">CreateCompatibleDC</span><span class="br0">&#40;</span> &amp;dc <span class="br0">&#41;</span> ;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; CBitmap obm;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; obm.<span class="me1">CreateCompatibleBitmap</span><span class="br0">&#40;</span> &amp;dc, rect.<span class="me1">Width</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, rect.<span class="me1">Height</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> ;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; odc.<span class="me1">SelectObject</span><span class="br0">&#40;</span> &amp;obm <span class="br0">&#41;</span> ;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; odc.<span class="me1">FillRect</span><span class="br0">&#40;</span> rect, &amp;brush <span class="br0">&#41;</span> ;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; RenderPicture<span class="br0">&#40;</span> odc, frame, rect <span class="br0">&#41;</span> ;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; dc.<span class="me1">BitBlt</span><span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">0</span>, rect.<span class="me1">Width</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, rect.<span class="me1">Height</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, &amp;dc, <span class="nu0">0</span>, <span class="nu0">0</span>, SRCCOPY <span class="br0">&#41;</span> ;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>You can see that it only takes a few lines to create the off-screen  bitmap. The code above creates a bitmap of the correct size, fills it  with the correct background pattern, then calls the rendering routine.  Once that is done, all that is left is a <span class="inline_code">BitBlt()</span> call which  copies the result to the screen.   </p>
<h4>The Sample Program</h4>
<p>To demonstrate this, I created an MFC program named <i>TransparentGif</i>, forgoing the usual document/view architecture. Instead, I just paint the entire client background of the window with a pattern, the better  to view the transparency of the loaded image.    </p>
<p>When the program first starts up, by default it draws directly to the screen. If you are blessed with a sub-Gigahertz CPU, you should be able  to see the flickering effect quite clearly when loading the  sample GIF included  with the source. I added code to force a redraw every time you click on the image, which allows you to see the effect  in all its beauty.</p>
<p>You can then use the <i>Options|Off</i> Screen Draw menu item to switch to the use of the off-screen bitmap.  A little sampling between the two should be enough to convince you of the value of the off-screen drawing technique.</p>
<h4>Switcing to GDI+</h4>
<p>Looking forward to the future, you might find that this task is going to become a bit easier. Microsoft has developed a new interface to GDI for Windows programmers called GDI+. The class libraries and supporting DLL for GDI+ are shipping as part of Windows XP, and can be installed as an add-on to Windows NT, 2000, 98, and ME. </p>
<p>The header files and import libraries required for GDI+ are part of  Microsoft's Platform SDK. To get this SDK, you'll have to have to  subscribe to Microsoft's MSDN program at the Operating System Level  or better, which has a list price of $699.  </p>
<p>You need to perform two installations to develop and test GDI+ programs:  the Win32 Platform SDK and the GDI+ runtime files. Both of these can  be found on your MSDN CDs or can be downloaded directly from the MSDN  site The GDI+ runtime kit can also be distributed with your software, so your  end users need no special licensing.  </p>
<h4>Additions to the project</h4>
<p>I made a copy of the <i>TransparentGif</i> program, renaming all the files and classes so that the new project goes by the name <i>TransparentPlus</i>. I then set to work making the changes needed to use GDI+ as the graphics interface. </p>
<p>The first few changes are the ones you typically have to make when using a new library. I added an include of the master header file,  <i>gdiplus.h</i>, to <i>stdafx.h</i>. This ensures that I have all the correct prototypes and class definitions. To make sure that the compiler was able to find the header file, I had to add the SDK include directory to the preprocessor settings for the C++ compiler. I used the default  installation location for the SDK, so I simply inserted the path:</p>
<p><i>C:\Program Files\Microsoft SDK\Include</i> </p>
<p>into the list of additional  directories for the compiler.</p>
<p>All of the classes and identifiers in GDI+ are placed in the <i>Gdiplus</i> namespace, so I also added a <span class="inline_code">using namespace Gdiplus;</span> declaration to <i>stdafx.h</i>. In a larger project, I probably wouldn't do this; the use of explicit namespaces in the source would add readability to the code. </p>
<p>Likewise, the import library was in the default SDK directory, so all I had  to do was drag:</p>
<p><i>C:\Program Files\Microsoft SDK\lib\GdiPlus.lib</i> </p>
<p>into my workspace pane, thereby adding it to the project.  </p>
<h4>Startup code</h4>
<p>GDI+ programs require a few lines of code for initialization, as well as a cleanup call. I added the following lines to the <span class="inline_code">InitInstance()</span> function in my application class:</p>
<div class="igBar"><span id="lcpp-17"><a href="#" onclick="javascript:showPlainTxt('cpp-17'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-17">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1">GdiplusStartupInput gdiplusStartupInput;</div>
</li>
<li class="li2">
<div class="de2">ULONG_PTR gdiplusToken;</div>
</li>
<li class="li1">
<div class="de1">GdiplusStartup<span class="br0">&#40;</span> &amp;gdiplusToken, &amp;gdiplusStartupInput, <span class="kw2">NULL</span><span class="br0">&#41;</span> ; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This code is lifted cookie-cutter style straight out of the GDI+ documentation. The token initialized by the startup function is passed  to the <span class="inline_code">GdiplusShutdown()</span>method when you are done using the library.  </p>
<h4>File Loading</h4>
<p>As you recall, the file loading code in the standard GDI program was a real rat's nest. The hoops I had to jump through in order to load a file  into an <span class="inline_code">IPicture</span> object gave a classic demonstration of  an ugly Microsoft  API.</p>
<p>I'm happy to say that someone working in API design in Redmond has  seen the light in this area. Loading images is an order of magnitude easier with  GDI+. Instead of loading into an <span class="inline_code">IPicture</span> COM object, GDI+ supports  a C++ <span class="inline_code">Image</span> object. This requires that I change  the type of <span class="inline_code">CMainFrame::m_pPicture</span> in the appropriate header file.  I can then reduce the file loading code from the ugly mess I had before to this picture of simplicity:  </p>
<div class="igBar"><span id="lcpp-18"><a href="#" onclick="javascript:showPlainTxt('cpp-18'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-18">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">void</span> CMainFrame::<span class="me2">LoadPicture</span><span class="br0">&#40;</span>CString filename<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; USES_CONVERSION;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; LPWSTR wname = A2W<span class="br0">&#40;</span> filename <span class="br0">&#41;</span> ;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> m_pPicture <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">delete</span> m_pPicture;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; m_pPicture = Image::<span class="me2">FromFile</span><span class="br0">&#40;</span> wname, <span class="kw2">FALSE</span> <span class="br0">&#41;</span> ;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This is a huge improvement, and by itself is almost worth the cost of the SDK. </p>
<h4>Drawing</h4>
<p>The drawing code with GDI+ is a bit simpler as well, mostly because the <span class="inline_code">Image</span> object uses pixels for measurement instead of the <span class="inline_code">HIMETRIC</span> units from <span class="inline_code">IPicture</span>. Other than that a simple replacement of the <span class="inline_code">Render()</span> function with a call to <span class="inline_code">DrawImage()</span> constitutes most of the work. The updated routine is shown here: </p>
<div class="igBar"><span id="lcpp-19"><a href="#" onclick="javascript:showPlainTxt('cpp-19'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-19">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">void</span> CChildView::<span class="me2">RenderPicture</span><span class="br0">&#40;</span> CDC &amp;dc,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CMainFrame *frame, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CRect &amp;rect <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> frame-&gt;m_pPicture <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> width = frame-&gt;m_pPicture-&gt;GetWidth<span class="br0">&#40;</span><span class="br0">&#41;</span> ;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> height = frame-&gt;m_pPicture-&gt;GetHeight<span class="br0">&#40;</span><span class="br0">&#41;</span> ;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> x = <span class="br0">&#40;</span> rect.<span class="me1">Width</span><span class="br0">&#40;</span><span class="br0">&#41;</span> - width <span class="br0">&#41;</span> / <span class="nu0">2</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> y = <span class="br0">&#40;</span> rect.<span class="me1">Height</span><span class="br0">&#40;</span><span class="br0">&#41;</span> - height <span class="br0">&#41;</span> / <span class="nu0">2</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Graphics graphics<span class="br0">&#40;</span> dc <span class="br0">&#41;</span> ;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; graphics.<span class="me1">DrawImage</span><span class="br0">&#40;</span> frame-&gt;m_pPicture,&nbsp; x, y, width, height <span class="br0">&#41;</span> ;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>GDI+ makes my code look a lot nicer, and informal tests show that GDI+ renders a bit faster as well. Better yet, GDI+ is smart enough to  avoid rendering all three steps directly on to the screen, which  means you won't  see a flash of the XOR'd or masked image.  </p>
<p>Despite the speed of GDI+, I think you'll still find that you need to do this image rendering in an off-screen bitmap. Experimenting with the second test program shows that although you don't see intermediate  images, you still get an annoying flickering as the image as drawn. Rendering off-screen eliminates that problem.  </p>
<h4>Conclusion</h4>
<p>This article gives you a couple of different ways to effectively use transparent GIF files in your Win32 programs. For today, the <span class="inline_code">IPicture</span> interface does a pretty good job. When GDI+ rolls  out as a standard part of Visual Studio, you'll probably want to  switch over to it for your imaging needs. Either way, you've got  good options that make things look a lot nicer  than they did in 1998.</p>
<h4>Links</h4>
<p><a href="http://marknelson.us/attachments/rendering-transparent-gifs-revisited/source.zip">Source for both demo programs</a>    </p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2002/07/01/rendering-transparent-gifs-revisited/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Intel&#8217;s JPEG Library</title>
		<link>http://marknelson.us/2002/07/01/intels-jpeg-library/</link>
		<comments>http://marknelson.us/2002/07/01/intels-jpeg-library/#comments</comments>
		<pubDate>Tue, 02 Jul 2002 04:58:27 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Magazine Articles]]></category>

		<guid isPermaLink="false">/2002/07/01/intels-jpeg-library/</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2002/07/01/intels-jpeg-library/' addthis:title='Intel&#8217;s JPEG Library' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div>An article that takes a look at Intel's free JPEG Library for Win32, and compares it to doing things the Microsoft Way.]]></description>
			<content:encoded><![CDATA[

<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2002/07/01/intels-jpeg-library/' addthis:title='Intel&#8217;s JPEG Library' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div><table border="0" width="400px">
<tbody>
<tr>
<td width="33%">
      <img src="http://marknelson.us/attachments/intels-jpeg-library/cover.gif" alt="Dr. Dobb's Journal, July, 2002" />
      </td>
<td>
        <b>Dr. Dobb's Journal</b></p>
<p>July, 2002</p>
</td>
</tr>
</tbody>
</table>
<p><b>Update:</b> <i>Intel is no longer giving these libraries away for free - be prepared to fork out real money for this code. Does this render the information in this article useless? I don't know, add your comments below!</i></p>
<p>In this article I take a look at Intel's Win32 JPEG Library, and  present a slide-show app I built to test it. I found it quite easy to render JPEG images using this library, and I think the demo code I provide will provide you with a good starting point for your own work. I'll also perform a short comparison of Intel's library against Microsoft's standard <span class="inline_code">IPicture</span> component, both in terms of performance and ease of use.</p>
<h4>Intel's Performance Libraries</h4>
<p>Not too many people are aware of the fact that Intel dedicates a substantial amount of energy to its software products. As you would expect, most of the software Intel creates is designed to pull through more hardware sales, not to generate revenue of its own. One good example of this is Intel's JPG Library.</p>
<p>Intel added the MMX instruction sets to their CPU chips starting with their <strike>486</strike> Pentium MMX processors. The MMX instructions are designed to speed up certain operations needed in multimedia applications. One of these is the <i>Inverse Discrete Cosine Transform</i> (IDCT), which is used to when decoding JPEG images.</p>
<p>Developers typically can't take advantage of extended instruction sets in products designed for a widespread market, because they can't count on their target machines having the correct extensions. Companies like Intel or AMD can still leverage their proprietary processor features by providing device drivers, browser plugins, or other components, but this doesn't completely satisfy their desire for market penetration.</p>
<p>For example, my sample slide-show program doesn't use operating system components to decode JPEG files, meaning I can't take advantage of the MMX instruction set without writing specific routines to detect the target processor type, then branch to one of two different decoders depending on the result. </p>
<p>Intel correctly figures that they can convince me to offer support for their hardware if they dish it up on a silver platter, and that's exactly what they do with their series of Performance Libraries. At this time, Intel is offering the following roster of libraries:</p>
<ul>
<li/>Intel Integrated Performance Primitives - an abstraction layer for multimedia processing.
<li/>Intel Math Kernel Library - linear algebra and FFT routines.
<li/>Intel Recognition Primitives Library - code for speech and character recognition software.
<li/>Intel Signal Processing Library - access to Intel CPU functionality similar to that used in Digital Signal Processors.
<li/>Intel Image Processing Library - image manipulation functions, including filtering, thresholding, morphing, FFT, and DCT.
<li/>Intel JPEG Library - encoding and decoding of JPEG images.
</ul>
<p>All of these libraries have processor specific routines for various families of Intel CPUs, and all have generic code that will also run on any standard Intel Architecture CPU.</p>
<h4>What's the Catch</h4>
<p>In a clear case of enlightened self-interest, Intel is making these libraries freely available to software developers. You will have to go through a licensing procedure to in order to redistribute the libraries with your products, but as far as I can tell there aren't any circumstances under which Intel will require payments or royalties.</p>
<p>Support is another matter, as you might expect. Intel's web site has FAQ material, known issues, and so on. Talking to a person will require subscription to a premium support service.  </p>
<h4>Where to go</h4>
<p>You can find Intel's JPEG library  on their Web site, <a href="http://www.intel.com">www.intel.com</a>, by following the links to<br />
<i>Products/Software/Performance Libraries</i>. At the time this article is going to press, the direct link is at:</p>
<p><a href="http://www.intel.com/cd/software/products/asmo-na/eng/perflib/ipp/index.htm"></p>
<p>http://www.intel.com/cd/software/products/asmo-na/eng/perflib/ipp/index.htm</a>.</p>
<p>The library has many of the niceties that you would expect from Intel. Figure 1 shows one of the first screens from the professional installation program, which naturally allows for a full uninstall as well. </p>
<p><center><br />
<img src="http://marknelson.us/attachments/intels-jpeg-library/Figure01.gif">
<p/>
Figure 1 - Installing the Library<br />
</center></p>
<p>As you install the library, you'll also see that it includes examples, white papers, documentation, and support for C/C++, Visual Basic, and Delphi. </p>
<h4>Using the Library - an Example</h4>
<p>I originally stumbled upon Intel's library while searching for a solution to a pressing problem. My friend Darrick Deel and I were intent on supplying some entertainment to a company shindig at Cisco's development offices in Dallas. We were going to act as roving digital photographers, snapping candid shots of our coworkers as they engaged in atypical mirth and merriment. </p>
<p>Our goal was to use a notebook PC and a projector to randomly display shots of partygoers during the event. What we needed was a slideshow<br />
program that would randomly display JPEG photos from a directory tree. We were hoping for a program that would run in unattended mode, but would still be able to periodically scan the photo archive looking for new shots. This would allow Darrick and I to add photos across the network without having to break into the show.</p>
<p>Slideshow software is easy to come by. As Figure 2 shows, a simple search on a popular download site turns up literally hundreds of possibilities. A surprisingly large percentage of this software is either free or quite cheap.</p>
<p><center><br />
<img src="http://marknelson.us/attachments/intels-jpeg-library/Figure02.gif">
<p/>
Figure 2- Searching for Slide Show Software<br />
</center></p>
<p>Despite the bountiful selection of software, Darrick and I were suffering from the Goldilocks syndrome - nothing we tried out was just right. Following in an age-old tradition among programmers, I decided to reinvent the wheel. Having recently downloaded Intel's JPEG Library, I knew I had just the tool I needed to make a quick<br />
job of it.</p>
<h4>Using Intel's Library</h4>
<p>The library ships from Intel with a pretty decent manual in PDF format, and a nicely fleshed out sample program called JPGView. The manual is full of code fragments that do a good job of illustrating the various features of the library. After reading the manual and examining the sample code, the process of displaying a JPEG image on the screen looked pretty straightforward: </p>
<ol>
<li/>Initialize the library.
<li/>Read the JPEG file header to get the basic image information, including size and color space.
<li/>Set up the various members of the data structure that will hold a Win32 <i>Device Independent Bitmap</i> (DIB.)
<li/>Read the JPEG image data into the DIB.
<li/>Display the DIB.
</ol>
<p>The first four steps here are accomplished in a single routine I wrote called <span class="inline_code">CreateImage</span>. This function takes an input file name as a parameter and returns a pointer to a <span class="inline_code">JPEG_CORE_PROPERTIES</span> structure, which is where the Intel library stores all its information. </p>
<p>The final step is done in the <span class="inline_code">WM_PAINT</span> handler for the program's <span class="inline_code">WndProc</span>. Since the first four steps create a fully functional DIB, all the <span class="inline_code">WM_PAINT</span> handler has to do is set up some parameters and draw the DIB to the output device context. </p>
<h4>SlideShow</h4>
<p>I put this knowledge together in an imaginatively named program called <em>SlideShow</em>. Although the code is built using Visual C++, it doesn't use MFC, so you should have good luck using the program with Brand X C++ compilers.</p>
<p><em>SlideShow</em> starts off as a console program that takes a directory name as a command line argument. That directory name should be the root directory of your album of JPEG files. Slideshow then creates a full-screen main window, starts a five second timer, and proceeds to process the message loop.</p>
<p>Other than a little bit of glue code, the message handling routine for this program really only cares about two messages: <span class="inline_code">WM_PAINT</span> and <span class="inline_code">WM_TIMER</span>.</p>
<h4>WM_TIMER</h4>
<p>The main window of <em>SlideShow</em> gets a timer tick once every five seconds. It has three main jobs to do when this timer tick occurs:</p>
<ol>
<li/>If needed, reload the list of pictures to display.
<li/>Destroy the current picture object.
<li/>Create a new picture object.
</ol>
<p><em>SlideShow </em>keeps a list of all the pictures it finds in the JPEG directory in a C++ <span class="inline_code">vector</span> named <span class="inline_code">WindowData::picture_names</span>. It also keeps an index of the next picture to decode in <span class="inline_code">WindowData::current_picture</span>. As soon as the timer handler is entered, it checks to see if the current_picture value is at or past the end of the picture_names vector. If it is, the current contents of the vector are erased and the picture directories are scanned for a new list of picture names. (Note that this happens naturally the first time the <span class="inline_code">WM_TIMER</span> tick is processed.) The picture names are randomized so the sequences aren't repeated each time the program is executed.</p>
<p>After potential maintenance of the image name list, it's a simple matter to delete the current image and create a new one. <span class="inline_code">CreateImage</span>,<br />
described a bit earlier in this article, does all the processing needed to load the JPEG image into a Device Independent Bitmap. This new image is ready to be painted on the screen, and I force that to happen by calling the Windows API function, <span class="inline_code">InvalidateRect()</span>. This forces a <span class="inline_code">WM_PAINT</span> message to be sent to the main window.</p>
<h4>WM_PAINT</h4>
<p>The <span class="inline_code">WM_PAINT</span> message means it is time to paint the JPEG image onto the screen. I've characterized this as being an easy task since the image is now in a DIB, and examination of the <span class="inline_code">WM_PAINT</span> handler in Listing 1 bears this out.</p>
<p>The actual painting is done via a call to Windows API function <span class="inline_code">StretchDIBits()</span>. This routine not only copies the image to the screen, but scales it as well. There are a dozen or so lines of code that set up the call to this function. I scale the image so that it fills as much of the screen as possible without clipping any bits, filling any unused space with the desktop background color.</p>
<p>Figure 3 shows what this looks like on the screen of a PC. The program quietly paints a new random image on the screen every five seconds. The machine shown here is a modified I-opener, which runs Windows 98 and has full network access. With no cooling fan to create ambient noise, the I-opener is an ideal desktop photo album.</p>
<p><center><br />
<img src="http://marknelson.us/attachments/intels-jpeg-library/Figure3.jpg"><br />
Figure 3 - The Slideshow in Action<br />
</center></p>
<p/>
<h4>Making Windows into Objects</h4>
<p>If you are used to using MFC for all of your C++ projects under Windows, you might find this to-the-API program a bit different. One thing I did to try and make it a little more structured is to encapsulate the Window data in a C struct, which could just as easily be a class.</p>
<p>When the program first starts, I create a <span class="inline_code">WindowData</span> object, which contains all the information regarding the currently selected<br />
picture, the list of file names, and so on. When the window is created, I stuff a pointer to this object into the <span class="inline_code">GWL_USERDATA</span> DWORD owned by the window. From that point on, every time I enter the <span class="inline_code">WndProc</span> I can get a pointer to the structure, eliminating the need for global variables.</p>
<h4>Integrating with Visual Studio</h4>
<p>The download package for this program contains a project file, but you may need to do a bit of modification after you install Intel's JPG Library.</p>
<p>First of all, the <i>SlideShow.cpp</i> file includes Intel's header file, <i>ijl.h</i>, as shown in this section of code from the source file:</p>
<div class="igBar"><span id="lcpp-20"><a href="#" onclick="javascript:showPlainTxt('cpp-20'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-20">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="co2">#include &lt;windows.h&gt;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="co2">#include &quot;ijl.h&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">#include &lt;iostream&gt;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="co2">#include &lt;string&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">#include &lt;vector&gt;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="co2">#include &lt;ctime&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">#include &lt;algorithm&gt;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">using</span> <span class="kw2">namespace</span> std; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Naturally, this file won't be in your normal include path. I take care of this by going to the <i>Project|Settings</i> dialog in Visual C++, selecting the <i>C/C++</i> pane, then the <i>Preprocessor</i> category. You should then enter Intel's header directory in the <i>Additional Include Directories</i> edit box. If you take the default installation, your dialog will look like Figure 4.</p>
<p><center><br />
<img src="http://marknelson.us/attachments/intels-jpeg-library/Figure04.gif"></p>
<p>Figure 4 - Configuring the Include Path<br />
</center></p>
<p>The project file I built for SlideShow includes the static library, <i>ijl5l.lib</i>. If you took the default path for Intel's install, you don't need to make any changes whatsoever. Figure 5 shows the path that is defined in the current project.</p>
<p>If you would like to use Intel's DLL instead of the static library, or if you chose a different install path, you'll need to delete the current library from the project. After deleting, use menu option <i>Project|Add to Project|Files</i> to add the correct library. </p>
<p><center><br />
<img src="http://marknelson.us/attachments/intels-jpeg-library/Figure05.gif"></p>
<p>Figure 5 - The correct properties for the Intel Library in Visual Studio<br />
</center></p>
<h4>Intel vs. Microsoft</h4>
<p>There are any number of reasons you might choose to use Intel's library to display JPEG images. But what about Microsoft's solution? Does the standard distribution of Windows come with components that can perform just as well?</p>
<p>As it happens, Microsoft ships with a standard COM object called <span class="inline_code">IPicture</span> that  shares much of the functionality with Intel's library. It not only handles JPEG files, but works with GIF, BMP, and other formats as well.</p>
<h4>Ease of use</h4>
<p>To get a handle on its ease of use, I reworked my SlideShow.cpp to use an all Micrsoft solution. The resulting program, SlideShowMS.cpp (available for download) shares the same structure, and is probably nearly identical in terms of structure and complexity. The major changes are:</p>
<ul>
<li/>Microsoft's image data is stored in the <span class="inline_code">IPicture</span> component, Intel's is in a <span class="inline_code">JPEG_CORE_PROPERTIES</span> structure.
<li/>Reading the file data requires two steps and a bit of coding for Intel. Microsoft makes reading easier with the simple <span class="inline_code">OleLoadPicture</span> function, but loses points for not reading from standard files. Setting up the <span class="inline_code">IStream</span> object Microsoft needs throws away any advantage they have in this area.
<li/>I used a standard Windows GDI function to draw the Intel object to the screen. <span class="inline_code">IPicture</span> has a <span class="inline_code">Render</span> method that is somewhat easier to use and requires less setup.
</ul>
<p>After examining these points, I think you'll agree that there's no clear winner here. If all that matters to you is how much work you have to perform to solve your imaging problems, you can stick with your personal preference and not worry about making a big mistake.</p>
<h4>Performance</h4>
<p>When it comes to decoding JPEG files and rendering them to the screen, Intel's library stole the show. I ran the Slideshow program through a batch of 20 JPEG files that were mostly 1600x1200 pixels in size. My non-scientific test game the following performance results:</p>
<p><center></p>
<table border=1 cellspacing=2>
<tbody>
<tr>
<th>CPU</th>
<th>Intel Batch Time</th>
<th>Microsoft Test Time</th>
</tr>
<tr>
<td>AMD K6-450</td>
<td>13 seconds</td>
<td>28 seconds</td>
</tr>
<tr>
<td>WinChip 200</td>
<td>50 seconds</td>
<td>130 seconds</td>
</tr>
<tr>
<td>Celeron 600</td>
<td>8 seconds</td>
<td>15 seconds</td>
</tr>
</tbody>
</table>
<p>Table 1 - Benchmark results<br />
</center></p>
<p>Intel's library seems to take Microsoft's time and cut it in half!  It appears that Intel's attention to performance paid off. (Interestingly, I would have expected to see a bigger delta between Intel and Microsoft on the Celeron CPU, but was disappointed.)</p>
<p>I'm sure there are ways to tweak my SlideShowMS program to get better performance from Microsoft's implementation. But the point of this article is to look at an off-the-shelf solution for use by programmers who may not be graphics experts. And for a straightforward implementation, Intel appears to really deliver when it comes to CPU cycles.</p>
<h4>What about IJG?</h4>
<p>The JPEG file format enjoys great acceptance in the software world. This is due in no small part to the availability of the Independent JPEG Group's (IJG) source code. The IJG created a non-commercial source code package that has been used as a reference and/or library by literally hundreds of programs out there. It enjoys a great deal of respect for its flexibility and feature depth.</p>
<p>Intel knows that many developers have already integrated the IJG code into their products, so they have created a 26-page white paper that discusses what you need to perform open-heart library replacement. There are major structural differences between the two sets of code, so this isn't an exercise for the faint of heart. But it can be done.</p>
<h4>Summary</h4>
<p>I found Intel's library quite easy to work with, which is probably evident given the length of my sample application. While I didn't do too much objective performance testing, I was happy with the speed I achieved when decoding and displaying multi-megapixel JPEG files on various PCs. </p>
<p>My satisfaction with the library is certainly only enhanced by the absence of a price tag. Intel's JPG library is definitely part of my image processing toolbox, you might want to add it to yours as well.</p>
<p><a href="http://marknelson.us/attachments/intels-jpeg-library/Source.zip">Article Source Code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2002/07/01/intels-jpeg-library/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>CubeShow</title>
		<link>http://marknelson.us/2002/07/01/cubeshow/</link>
		<comments>http://marknelson.us/2002/07/01/cubeshow/#comments</comments>
		<pubDate>Mon, 01 Jul 2002 13:12:05 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Magazine Articles]]></category>

		<guid isPermaLink="false">http://marknelson.us/2002/07/01/cubeshow/</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2002/07/01/cubeshow/' addthis:title='CubeShow' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div>&#160; PC Magazine July, 2002 Remember when PC Magazine used to give away their utilities for free? Ah yes, those where the days. The earliest PC Magazine utilities were distributed as PC-BASIC programs printed up in the Utilities section, which made good typing accuracy an important skill. Eventually this was supplanted by electronic distribution via [...]]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2002/07/01/cubeshow/' addthis:title='CubeShow' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div><table border="0">
<tr>
<td style="width: 30%"><img src="http://marknelson.us/attachments/2002/cubeshow/pcmag_cover.png" /></td>
<td width="5%">&nbsp;</td>
<td style="width: 65%"><strong>PC Magazine</strong> July, 2002<br />
</tr>
</table>
<p>Remember when <a href="http://pcmag.com" class="newpage">PC Magazine</a> used to give away their utilities for free? Ah yes, those where the days. The earliest PC Magazine utilities were distributed as PC-BASIC programs printed up in the Utilities section, which made good typing accuracy an important skill. Eventually this was supplanted by electronic distribution via BBS and then the Internet. A lot of useful programs were thrown our way for free, and PC Magazine presumably bathed in good will and reflected glory.</p>
<p>Back in July, 2002, I wrote one of those free utilities for PC Magazine. CubeShow was a screensaver that featured a tumbling representation of a 3D cube, adorned with photos of your choice. Having never written a line of DirectX code in my life, it was quite an adventure, but I think I managed to produce something that worked and was popular.</p>
<p>Some of the more interesting problems involved with writing this utility were:</p>
<ul>
<li/>Finding the right rotation parameters to achieve smooth animation
<li/>Figuring out how to change photos on a cube fact only when it was hidden
<li/>Keeping a photo upright as the cube rotated
</ul>
<p>A lot of fun!<br />
<center><br />
<img src="http://marknelson.us/attachments/2002/cubeshow/cubeshow.jpg"><br />
Figure 1 - CubeShow in action<br />
</center><br />
Sadly, PC Magazine's free utility policy bit the dust during some corporate belt-tightening, and now it's a for-profit operation. As a result, they are rather tight-fisted about their rights to CubeShow, so I can't give you a copy, or even give you the source. And worse yet, when they converted their Utilities department to a store, they didn't grandfather previously free utilities. So if you want a copy of CubeShow, you'll either pay $7.97 for a <a href="http://www.pcmag.com/join/0%2C1213%2C%2C00.asp?join_page=8&#038;offers=/offer/ppv/OneDownload&#038;content_id=0" class="newpage">one time download</a>, or $19.97 for a year of unlimited downloads. Not a bad deal, and there are some good utilities on the site. If you download all 140 of them you'll surely feel like you got your money's worth!</p>
<h4>Links</h4>
<p><a href="http://www.pcmag.com/article2/0,4149,138094,00.asp" class="newpage">CubeShow on PCMag.com</a><br />
<a href="http://www.pcmag.com/data_process_yaga_module/0,1320,,00.asp?dr=1&#038;content_id=1380&#038;success_page=http%3A%2F%2Fwww%2Epcmag%2Ecom%2Farticle2%2F0,1759,177062,00%2Easp&#038;success_title=&#038;failure_page=&#038;discuss=" class="newpage">CubeShow download link</a></p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2002/07/01/cubeshow/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Building a URL Scanner With Java 1.3</title>
		<link>http://marknelson.us/2002/04/01/url-scanner/</link>
		<comments>http://marknelson.us/2002/04/01/url-scanner/#comments</comments>
		<pubDate>Tue, 02 Apr 2002 04:56:41 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[Magazine Articles]]></category>

		<guid isPermaLink="false">/2002/04/01/url-scanner/</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2002/04/01/url-scanner/' addthis:title='Building a URL Scanner With Java 1.3' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div>This article describes a Java program I wrote to scan through a URL database, flagging sites that have shut down, moved, or simply can't be reached.]]></description>
			<content:encoded><![CDATA[



<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2002/04/01/url-scanner/' addthis:title='Building a URL Scanner With Java 1.3' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div><table border="0" width="400px">
<tbody>
<tr>
<td width="33%">
      <img src="http://marknelson.us/attachments/url-scanner/cover.jpg" alt="Windows:: Developer, January, 2002" />
      </td>
<td>
        <b>Windows:: Developer</b></p>
<p>January, 2002</p>
</td>
</tr>
</tbody>
</table>
<p>This article describes a URL validation program I wrote in Java, using standard Internet and database language features. I'll show you why Java is the clear choice of languages for this job. Finally, I'll show you how to work around one particularly egregious shortcoming in Sun's JDK 1.3.</p>
<h4>Introduction</h4>
<p>For several years now I've maintained a web site called <a href="http://datacompression.info">DataCompression.info</a>. <i>(Note - this site passed out of my hands in 2003)</i>. I have one steady advertiser who helps me cover my hosting costs, but when you consider the time and energy required to keep the site up you'll understand that it is a labor of love.</p>
<p>The Data Compression Library is simply a collection of links to sites related to data compression. I have a database of over 2000 URLs, categorized with one or more of thirty-some topics. On a periodic basis I run a program that passes over the database and generates a batch of pages I upload to a server. Figure 1 shows the main page for the site, and Figure 2 shows a typical topic page.</p>
<p><center><br />
<img src="http://marknelson.us/attachments/url-scanner/Figure1.gif"></p>
<p>Figure 1<br/>The Front Page of the Data Compression Library<br />
</center></p>
<p><center><br />
<img src="http://marknelson.us/attachments/url-scanner/Figure2.gif"></p>
<p>Figure 2<br/>A Typical Topic Page<br />
</center></p>
<h4>Wish You Were Here</h4>
<p>One of the biggest ongoing problems maintaining this site is dealing with bad links. Over the course of a year, I have to contend with literally hundreds of sites that change URLs, move within an existing domain, or just disappear from the face of the earth. My users tend to become annoyed when every other link ends up with an HTTP 404 error.</p>
<p>When I first started working on the site I was operating under the illusion that dead links could be dealt with through manual labor on my part and faithful error reporting from my users. It turns out that both of these assets are in short supply. After a couple of years I had more bad URLs than Microsoft has lawsuits. It was time for a change.</p>
<h4>An Automated Solution</h4>
<p>I knew exactly what I needed: an automated scanning program that I can run periodically. The scanner could update my database with its results, and generate a report showing me which sites are repeatedly AWOL. </p>
<p>Despite the clear need for this program, I had been avoiding getting down to business for some time. My tool of choice for utility programs has always been Visual C++, and the thought of writing more database code in Microsoft's universe was far from appealing. </p>
<p>The problem in this case isn't C++, Visual Studio, or even Microsoft. I get along with all three on a daily basis. The problem is the unfriendly nature of Microsoft's C++ interface to ODBC, their database API of choice.  At least from my perspective, it seems that every piece of data that is passed back and forth between my C++ code and the database has to go through at least one or two type transformations. The database code is never content to take a C++ string type, or even a simple character array. Instead, every string has to be converted to type <span class="inline_code">_variant_t</span> or <span class="inline_code">_bstr_t</span>, two monstrosities that Microsoft apparently cooked up for Visual Basic programmers. Visual Basic is weakly typed, C++ is strongly typed. It's not fair to make either of them use the other's type system, but the API developers in Redmond decided a Procustean fit was just fine.</p>
<p>Data read in from the database typically comes in as a <span class="inline_code">variant</span> type, which means that before I can even use it, I first have to check the type, then examine the value, leading to lots of code that looks something like this: </p>
<div class="igBar"><span id="lcpp-21"><a href="#" onclick="javascript:showPlainTxt('cpp-21'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-21">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">if</span> <span class="br0">&#40;</span> pal.<span class="me1">vt</span> == VT_BOOL &amp;&amp; pal.<span class="me1">bVal</span> <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; score = pLinks-&gt;lds&gt;Item<span class="br0">&#91;</span> _bstr_t<span class="br0">&#40;</span> <span class="st0">"Score"</span> <span class="br0">&#41;</span> <span class="br0">&#93;</span>-&gt;Value;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> score.<span class="me1">vt</span> != VT_NULL &amp;&amp; score.<span class="me1">lVal</span>&gt;= <span class="nu0">1</span> &amp;&amp; score.<span class="me1">lVal</span> &lt;= <span class="nu0">5</span> <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp;<span class="kw4">char</span> *files<span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="br0">&#123;</span> <span class="st0">"0.gif"</span>, <span class="st0">"1.gif"</span>, <span class="st0">"2.gif"</span>, <span class="st0">"3.gif"</span>, <span class="st0">"4.gif"</span> <span class="br0">&#125;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>To complicate matters more, there was a matter of another familiar complaint I have with Microsoft's COM interfaces: error handling. Working through a database typically involves a fairly long batch of calls to API calls, each of which can return an error. Dealing with deeply nested error conditions leaves you with truly messy code:</p>
<div class="igBar"><span id="lcpp-22"><a href="#" onclick="javascript:showPlainTxt('cpp-22'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-22">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">if</span> <span class="br0">&#40;</span>open_database<span class="br0">&#40;</span><span class="br0">&#41;</span> == S_OK <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> open_table<span class="br0">&#40;</span><span class="br0">&#41;</span> == S_OK <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> read_data<span class="br0">&#40;</span><span class="br0">&#41;</span> == S_OK <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; do_something<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; dispose_of_data<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">else</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt;<span class="st0">"Error reading data"</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">else</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt;<span class="st0">"Error opening table"</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; close_database<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span> <span class="kw1">else</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">cout</span> &lt;&lt;<span class="st0">"Error opening database"</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>I don't like code like this, whether I'm reading it or writing it.</p>
<h4>Java to the Rescue</h4>
<p>Fortunately, before I started writing my scanner in C++, I found myself knee-deep in Java programming at work. To my great pleasure, I saw that database programming in Java using JDBC worked around all of my annoyances with ODBC in a manner that made my job a lot easier.  </p>
<p>Java's standard interface to databases is done through the <i>Java Database Connectivity interface</i>, known by the trademarked name JDBC.  JDBC has two key points that make it look good in comparison to C++/ODBC. First, JDBC uses native types to interface to the database - eliminating the need to convert between variant or non-standard string types. Second, error handling in JDBC is managed via exceptions, which cuts out all those conditional tests I had to use with ODBC.</p>
<p>With these conditions, I found that I was able to implement my scanner in 100 lines of straightforward Java code. The Java code was easier to write, easier to debug, and has been easier to maintain.</p>
<h4>Details</h4>
<p>Figure 3 shows a screenshot from a Microsoft Access view of my URL database. That view shows all of the pertinent data from the URL table. The fields in each row that are updated by the scanner program are the <i>Scan History</i>, the <i>Last Good Scan</i> field, and the <i>Last Scan Result</i> field. </p>
<p><center><br />
<img src="http://marknelson.us/attachments/url-scanner/Figure3.gif"></p>
<p>Figure 3 - A View of the URL Table<br />
</center></p>
<p>The job of the scanner program is to go through all the rows in the URL table. The program attempts to access the web page pointed to by the URL. If the page is found, a hyphen character is appended to the Scan History. If the page isn't found, the character <i>F</i> is appended. (The Scan History is always truncated to a maximum of 16 characters.) </p>
<p>If the page is found, the <i>Last Good Scan</i> field is set to the current date. In the event of a failure, the Java exception code is written to the <i>Last Scan Result</i>. This gives me the information I need to quickly scan through the database and determine which sites are going to give my users trouble.</p>
<p>The Java code to accomplish this is basically divided into two pieces: setup and scanning.  The first step is done in the constructor of the <span class="inline_code">HttpScan</span> object, and the second is done in its member function <span class="inline_code">scan()</span>. I call both of these from the static main function in the <span class="inline_code">HttpScan</span> class:</p>
<div class="igBar"><span id="ljava-23"><a href="#" onclick="javascript:showPlainTxt('java-23'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-23">
<div class="java">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw4">static</span> <span class="kw4">void</span> main<span class="br0">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&#038;bntl=1"><span class="kw3">String</span></a><span class="br0">&#91;</span><span class="br0">&#93;</span> args<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">try</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; HttpScan scanner = <span class="kw2">new</span> HttpScan<span class="br0">&#40;</span> <span class="st0">"jdbc:odbc:CompressionLinks"</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; scanner.<span class="me1">scan</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;<span class="kw2">catch</span> <span class="br0">&#40;</span> <a href="http://www.google.com/search?q=allinurl%3AException+java.sun.com&#038;bntl=1"><span class="kw3">Exception</span></a> e <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&#038;bntl=1"><span class="kw3">System</span></a>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span><span class="st0">"Failed to connect to database. Exception: "</span> + e<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Note that both functions are wrapped in a try/catch block. This means I get to handle fatal errors by simply throwing or propagating an exception, knowing that it will be properly displayed when caught. </p>
<h4>Connecting to the Database</h4>
<p>JDBC ships with an ODBC bridge for Windows, which means the only database setup you need to perform is to set up an ODBC data source. Under Windows 2000, you can do this from the Administrative Tools section. Figure 4 shows part of the process. The text box labeled <i>Data Source Name</i> defines the name that you use to connect to the database with the call to <span class="inline_code">DriverManager.getConnection()</span>.</p>
<p><center><br />
<img src="http://marknelson.us/attachments/url-scanner/Figure4.gif"></p>
<p>Figure 4 - Setting up the ODBC Data Source<br />
</center></p>
<p>The full code for the connection to the database is in the <span class="inline_code">HttpScan</span> constructor. The steps taken sequentially are:</p>
<ol>
<li/>Load the JDBC-ODBC bridge.
<li/>Connect to the database.
<li/>Load the recordset containing all of the http links in my database.
<li/>Ask the database for the count of links in the database.
<li/>Create an update statement that will be used throughout the program.
</ol>
<p>After the constructor executes, I have a count of records in member variable <span class="inline_code">recordCount</span>, a copy of all the records in member variable <span class="inline_code">results</span>, and a member variable called <span class="inline_code">updateStatement</span> that will be used to update the records in the database.</p>
<h4>Scanning the records</h4>
<p>Once the connection has been made and the recordset loaded, the member function <span class="inline_code">scan()</span> is called. Its job is to attempt to connect to every URL in the recordset. In Listing 1, you'll see that I declare that <span class="inline_code">scan()</span> throws an <span class="inline_code">SQLException</span> - which means I don't have to do any fancy recovery when a fatal error is encountered. I rely on the caller, <span class="inline_code">main()</span>, to catch the exception and print out the corresponding information.</p>
<p>At that point, the scanner enters the main loop, which executes as long as I can successfully call <span class="inline_code">results.next()</span>, which moves to the next available database record. In Listing 1, you can see some typical code used to get data from a record with code that looks like this:</p>
<div class="igBar"><span id="ljava-24"><a href="#" onclick="javascript:showPlainTxt('java-24'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-24">
<div class="java">
<ol>
<li class="li1">
<div class="de1">key = results.<span class="me1">getInt</span><span class="br0">&#40;</span> <span class="st0">"Key"</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&#038;bntl=1"><span class="kw3">String</span></a> url = results.<span class="me1">getString</span><span class="br0">&#40;</span> <span class="st0">"Url"</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">history = results.<span class="me1">getString</span><span class="br0">&#40;</span> <span class="st0">"ScanHistory"</span> <span class="br0">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>You can immediately see why I would prefer this code to the equivalent C++ code under Windows. All of the data is returned in native Java types, I pass in parameters in native types, and I allow error handling to be done via exception. The result? Code that's easy to read, easy to write!</p>
<p>Once I have the URL in hand, the next step is to create and open a <span class="inline_code">URLConnection</span> object, part of the standard <span class="inline_code">java.net</span> package. After creating the response code, I just check the <span class="inline_code">responseCode</span> member of<br />
the <span class="inline_code">URLConnection</span> object. If I received a 200, it means that the <span class="inline_code">URLConnection</span> object was able to<br />
locate the web site and read in the page. Anything else means an error of some sort.</p>
<p>Note that the code that the library calls that loads the URL may well throw an exception, which I catch inside the <span class="inline_code">scan()</span> routine. When that happens, the <span class="inline_code">Exception</span> object will typically hold useful information as well.</p>
<h4>Performing the Updates</h4>
<p>The final task that the <span class="inline_code">scan()</span> loop performs is to update the database with the results for the given URL. This is done from one of four different places in the scan loop, where three points represent various failures, and one represents success.</p>
<p>The <span class="inline_code">Update()</span> routine takes four parameters: the record key, the 16 character history string, the status flag, (which is '-' on success and 'F' on failure,) and a string which contains any error message from the attempt to connect. </p>
<p>Again, this is where Java shines in comparison to the Visual C++ solution. As you can see from Listing 1, creating the SQL statement that performs the update is all done using native types and straightforward code.</p>
<h4>Using the Program</h4>
<p>Running the program is straightforward. The program is a simple console app that displays its results as it runs. I invoke the program from the command line with the following command:</p>
<p><em>Java -classpath . HttpScan</em></p>
<p>Figure 5 shows the program in action:</p>
<p><center><img src="http://marknelson.us/attachments/url-scanner/Figure5.gif"></p>
<p>Figure 5 - HttpScan in Action<br />
</center></p>
<p>I try to run the scanner program once a week or so. After each run, I generate a report showing which sites have failed several consecutive scans. From that point I have to go through the laborious process of determining whether the site has moved, gone under, or is just plain flaky. </p>
<h4>A Catch</h4>
<p>When I first developed this program, I assumed that one complete run through the database would take on the order of an hour or two. The program reaches most URLs in a matter of seconds, with some of the more distant sites taking as long as sixty seconds. So I was quite disappointed when I let the program run overnight and found that it was stalled on one particular site after only running through roughly 100 URLs.</p>
<p>A little research into Sun's bug database showed that I was far from the first person to see this problem. For an example, look up bug 4304701, 4143518, or 4283433. Basically the problem is that socket connections used by <span class="inline_code">URLConnection</span> are opened without a timeout value, which means they can hang forever when network problems crop up.</p>
<p>Of course, there are quite a few possible ways to solve this problem, and no shortage of suggestions in the appropriate newsgroups. Some of the proposed solutions seem to be platform dependent, and may not work in all environments. The only simple solution I found that worked properly on Win32 platforms was to put my <span class="inline_code">URLConnection</span> attempts in a separate thread, and then to call <span class="inline_code">Thread.stop()</span> after an unreasonable amount of time with no results. But <span class="inline_code">Thread.stop()</span> is a deprecated function call, and has the ugly possibility of creating resource leaks.</p>
<h4>My Solution</h4>
<p>The solution I implemented in <em>HttpScan</em> was based on a post to the java.sun.com site by a person named Denis Haskin. Denis's workaround replaces the default <span class="inline_code">SocketFactory</span> used by the <span class="inline_code">java.net</span> classes with a slightly modified version. The modified version creates sockets that have a timeout set upon creation. Since classes such as <span class="inline_code">URLConnection</span> rely on the default socket factory for their sockets, inserting a replacement factory is an elegant workaround to the problem.</p>
<p>My version of the factory class is called <span class="inline_code">TimeoutSocketFactory</span>, and is shown in Listing 2. You can see that the class is incredibly simple. All it does is return a copy of a socket class I created called <span class="inline_code">MySocketImpl</span>. This class is derived from the <span class="inline_code">PlainSocketImpl</span> class defined in <span class="inline_code">java.net</span>, and differs in only one function: <span class="inline_code">connect()</span>. My version of the <span class="inline_code">connect()</span> function calls the base class <span class="inline_code">connect()</span>, then sets the connection to have a specified timeout. Since the base class defines all of the remaining behavior, I'm confident that my derived socket class won't cause any other problems.</p>
<p>Once I created this class and integrated it with my <em>HttpScan</em> program, things worked perfectly. I make a single call at the start of the program that creates this new socket factory using this code:</p>
<div class="igBar"><span id="ljava-25"><a href="#" onclick="javascript:showPlainTxt('java-25'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-25">
<div class="java">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">try</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <a href="http://www.google.com/search?q=allinurl%3ASocket+java.sun.com&#038;bntl=1"><span class="kw3">Socket</span></a>.<span class="me1">setSocketImplFactory</span><span class="br0">&#40;</span> <span class="kw2">new</span> TimeoutSocketFactory<span class="br0">&#40;</span> <span class="nu0">30000</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">catch</span> <span class="br0">&#40;</span> <a href="http://www.google.com/search?q=allinurl%3AException+java.sun.com&#038;bntl=1"><span class="kw3">Exception</span></a> e <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&#038;bntl=1"><span class="kw3">System</span></a>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span> <span class="st0">"Hijacking socket factory failed: "</span> + e <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&#038;bntl=1"><span class="kw3">System</span></a>.<span class="me1">exit</span><span class="br0">&#40;</span> -<span class="nu0">1</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>It now takes a bit over an hour to scan through the entire database, and a fair number sites do actually generate a timeout.</p>
<p>There is one  big catch to this technique. I have to derive my special socket class from <span class="inline_code">PlainSocketImpl</span>, the standard socket  class used by the java.net package. Unfortunately, <span class="inline_code">PlainSocketImpl</span> is only visible in the <span class="inline_code">java.net</span> package. So my entire socket factory has to be hoisted into the java.net package, which is clearly coloring outside the lines. </p>
<p>In order to add a class to the package like this, I simply create a <em>java/net</em> directory and put the source file there. I then have to add the following switch to the VM when running the program:</p>
<p><em>-Xbootclasspath/a:.</em></p>
<p>This tells the VM to look the other way while I inject some  additional code into the library space. When running with JBuilder, VM parameters are added to the Project Properties Dialog on the Run tab.  </p>
<p>Sun understands that this is a problem, and documents some of the shortcomings of the current architecture in bug ID 4245730. In fact, the bug documentation is pessimistic enough to say "SocketImplFactories are a dead end." Personally, I'm happy with the workaround.</p>
<h4>The JDK 1.4 Solution</h4>
<p>By the time you read this, Sun may have released version 1.4 of Java. If they have, this program can use a simpler solution to this problem. In the 1.4 JDK, Sun has documented a pair of system properties that allow you to modify the default timeouts used by sockets. These two properties are <span class="inline_code">sun.net.client.defaultConnectTimeout</span> and <span class="inline_code">sun.net.client.defaultReadTimeout</span>, and both specify timeout values in milliseconds. </p>
<p>I was able to modify the static <span class="inline_code">main()</span> method of HttpScan to look likethis with the 1.4 JDK:</p>
<div class="igBar"><span id="ljava-26"><a href="#" onclick="javascript:showPlainTxt('java-26'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-26">
<div class="java">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">try</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&#038;bntl=1"><span class="kw3">System</span></a>.<span class="me1">setProperty</span><span class="br0">&#40;</span> <span class="st0">"sun.net.client.defaultConnectTimeout"</span>, <span class="st0">"30000"</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&#038;bntl=1"><span class="kw3">System</span></a>.<span class="me1">setProperty</span><span class="br0">&#40;</span> <span class="st0">"sun.net.client.defaultReadTimeout"</span>, <span class="st0">"30000"</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; Test test = <span class="kw2">new</span> Test<span class="br0">&#40;</span> <span class="st0">"jdbc:odbc:CompressionLinks"</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; test.<span class="me1">scan</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>I could then run the program successfully with the 1.4 JDK after removing my modified socket factory. Once that JDK is released and working well I will undoubtedly modify my program to use this more conventional method of controlling timeouts.</p>
<h4>Summary</h4>
<p>I was very pleased with how easy this project was developed in Java. As I said at the start of the article, my issues with C++ all melted away when I saw the light with Sun. The two technologies used here, JDBC and java.net, are both powerful and easy to use. I hope that the code presented here helps you with your next project in this area.</p>
<p>Listing 1 - <a href="http://marknelson.us/attachments/url-scanner/HttpScan.java">HttpScan.java</a></p>
<p>Listing 2 - <a href="http://marknelson.us/attachments/url-scanner/java/net/TimeoutSocketFactory.java">java/net/TimeoutSocketFactory.java</a></p>
<p>Download - <a href="http://marknelson.us/attachments/url-scanner/source.zip">source.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2002/04/01/url-scanner/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>C++ Algorithms: next_permutation()</title>
		<link>http://marknelson.us/2002/03/01/next-permutation/</link>
		<comments>http://marknelson.us/2002/03/01/next-permutation/#comments</comments>
		<pubDate>Sat, 02 Mar 2002 04:54:22 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[Magazine Articles]]></category>

		<guid isPermaLink="false">/2002/03/01/next-permutation/</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2002/03/01/next-permutation/' addthis:title='C++ Algorithms: next_permutation()' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div>A quick look at the STL function next_permutation(), with a practical example that demonstrates how I used it to solve a real-world problem.]]></description>
			<content:encoded><![CDATA[





<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2002/03/01/next-permutation/' addthis:title='C++ Algorithms: next_permutation()' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div><table border="0" width="400px">
<tbody>
<tr>
<td width="33%">
      <img src="http://marknelson.us/attachments/next-permutation/cover.jpg" alt="C/C++ Users Journal, March, 2002" />
      </td>
<td>
        <b>C/C++ Users Journal</b></p>
<p>March, 2002</p>
</td>
</tr>
</tbody>
</table>
<p><EM>Note: Thanks to Shawn McGee for pointing out an error in Figure 1. The print edition of this article in C/C++ Users Journal had an unfortunate extra line!</em></p>
<p>My daughter's math teacher at Hockaday School in Dallas wants his sixth-grade students to enjoy their class. He's fond of sending home interesting problems that are meant to be both entertaining and enriching. As most parents probably know, this can only mean trouble!</p>
<p>Last week Mr. Bourek sent home a worksheet containing a set of variations on the traditional magic square. Students were given various shapes, such as triangles, stars, and so on, and asked to fill in a set of consecutive numbers at the vertices. The goal was to come up with an arrangement of numbers such that various rows, columns, and diagonals all added up to a given sum.</p>
<p>Kaitlin worked her way through most of the problems in fairly quick order. But the shape shown in Figure 1 managed to stump her.  </p>
<p><center><br />
<img src="http://marknelson.us/attachments/next-permutation/Figure1.gif"></p>
<p>Figure 1 - Sum = 17<br />
</center></p>
<p>The problem was simple enough. All she had to do was place the numbers 1 through 9 in the nine positions of the figure so that the sum of all the straight lines was 17. Although Kate was able to knock the other problems out quickly, this one was still unsolved after fifteen minutes or so; well past the normal sixth-grade attention span. </p>
<p>Even worse, after another 10 minutes of my help we were no closer to a solution. That's when I decided it was time for a brute force approach. I remembered that the standard C++ library had a handy function, <span class="inline_code">next_permutation()</span>, that would let me iterate through all the possible arrangements of the figure with just a couple of lines of code. All I had to do was check the five different sums for each permutation and I'd have the answer in no time. </p>
<p>The resulting program is shown in Listing 1, and its output is given below:</p>
<p><code><br />
100706 : 3 5 9 8 2 1 6 4 7<br />
114154 : 3 8 6 5 2 4 9 1 7<br />
246489 : 7 1 9 4 2 5 6 8 3<br />
259937 : 7 4 6 1 2 8 9 5 3<br />
362880 permutations were tested<br />
</code></p>
<p>A little quick sketching will show you that the four solutions are simply rotations and mirror images of the one true solution. You can also see that randomly putting down numbers makes the odds almost 100,000:1 against finding a solution. Not quite as bad as the lottery, but it clearly shows that random guessing isn't going to work. (My daughter asked me to give her the center position only, upon which she solved the rest of it in roughly 30 seconds.)</p>
<div class="igBar"><span id="lcpp-27"><a href="#" onclick="javascript:showPlainTxt('cpp-27'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-27">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="co2">#include &lt;iostream&gt;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="co2">#include &lt;algorithm&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">using</span> <span class="kw2">namespace</span> std;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">main<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">int</span> a<span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="br0">&#123;</span> <span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span>, <span class="nu0">4</span>, <span class="nu0">5</span>, <span class="nu0">6</span>, <span class="nu0">7</span>, <span class="nu0">8</span>, <span class="nu0">9</span> <span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw4">int</span> count = <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">do</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> <span class="br0">&#40;</span> a<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> + a<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> + a<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span> <span class="br0">&#41;</span> == <span class="nu0">17</span> &amp;&amp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#40;</span> a<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> + a<span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span> + a<span class="br0">&#91;</span><span class="nu0">6</span><span class="br0">&#93;</span> <span class="br0">&#41;</span> == <span class="nu0">17</span> &amp;&amp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#40;</span> a<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span> + a<span class="br0">&#91;</span><span class="nu0">5</span><span class="br0">&#93;</span> + a<span class="br0">&#91;</span><span class="nu0">8</span><span class="br0">&#93;</span> <span class="br0">&#41;</span> == <span class="nu0">17</span> &amp;&amp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#40;</span> a<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span> + a<span class="br0">&#91;</span><span class="nu0">4</span><span class="br0">&#93;</span> + a<span class="br0">&#91;</span><span class="nu0">6</span><span class="br0">&#93;</span> <span class="br0">&#41;</span> == <span class="nu0">17</span> &amp;&amp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#40;</span> a<span class="br0">&#91;</span><span class="nu0">6</span><span class="br0">&#93;</span> + a<span class="br0">&#91;</span><span class="nu0">7</span><span class="br0">&#93;</span> + a<span class="br0">&#91;</span><span class="nu0">8</span><span class="br0">&#93;</span> <span class="br0">&#41;</span> == <span class="nu0">17</span> <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt;count &lt;&lt; <span class="st0">" : "</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span> <span class="kw4">int</span> i = <span class="nu0">0</span> ; i &lt;<span class="nu0">9</span> ; i++ <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt;a<span class="br0">&#91;</span> i <span class="br0">&#93;</span> &lt;&lt;<span class="st0">" "</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt;<span class="st0">"<span class="es0">\n</span>"</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; count++;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">while</span> <span class="br0">&#40;</span> next_permutation<span class="br0">&#40;</span> a, a + <span class="nu0">9</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt;count &lt;&lt;<span class="st0">" permutations were tested<span class="es0">\n</span>"</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><center>Listing 1 - Magic.cpp</center></p>
<h4>Magic Permutations</h4>
<p>From this program you can see that <span class="inline_code">next_permutation()</span> is a handy function to have in the C++ library. In my case it meant the difference between writing an impulse program versus fiddling around with pencil and paper for another hour. What really makes <span class="inline_code">next_permuation()</span> interesting to me is the fact that it can generate permutations without keeping any additional information beyond the sequence being juggled. I can generate a permutation, go off and do whatever I like with it, even write the numbers out to a file and save them for later. Regardless of what I do, <span class="inline_code">next_permuation()</span> will always be happy to generate the next set in the series given only the previous one as input.</p>
<p>Just writing a function to generate permutations isn't particularly hard. One easy way to tackle the problem is with a recursive approach. If the string you want to permute is <i>n</i> characters long, you execute a loop that makes one pass per character in the string. Each time through the loop you remove character <i>i</i> from the string, and keep it as a prefix. You then print out all the permutations of the remaining substring concatenated with the prefix. How do you get the list of permutations of the substring? By recursively calling the permutation function. The only additional piece of logic you need to include is the test to see if a substring is only one character long. If it is, you don't need to call the permutation function, because you already have the only permutation of the string.</p>
<p>For example, to print the permutations of "abc", you will first strip off the "a" character, and then get the permutations of "bc". To get those permutations, you will first strip off the "b" character, and get a resulting permutation list of "c". You then strip off the "c" character, and get a resulting permutation of "b". The results when combined with the prefix character of "a" give strings "abc" and "acb".</p>
<p>You then repeat the process for prefix "b" and substring "ac", then for prefix "c" and substring "ab". </p>
<p>Using the <span class="inline_code">string</span> class in the C++ standard library makes it fairly easy to implement this logic. Listing 2 shows <code>permute.cpp</code> which implements this algorithm relatively faithfully.</p>
<div class="igBar"><span id="lcpp-28"><a href="#" onclick="javascript:showPlainTxt('cpp-28'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-28">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="co2">#include &lt;iostream&gt;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="co2">#include &lt;string&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">using</span> <span class="kw2">namespace</span> std;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">void</span> permute<span class="br0">&#40;</span> string prefix, string s <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> s.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span> &lt;= <span class="nu0">1</span>&nbsp; <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt;prefix &lt;&lt;s &lt;&lt;<span class="st0">"<span class="es0">\n</span>"</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">else</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span> <span class="kw4">char</span> *p = s.<span class="me1">begin</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; p &lt;s.<span class="me1">end</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; p++ <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">char</span> c = *p;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.<span class="me1">erase</span><span class="br0">&#40;</span> p <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; permute<span class="br0">&#40;</span> prefix + c, s <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.<span class="me1">insert</span><span class="br0">&#40;</span> p, c <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">main<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; permute<span class="br0">&#40;</span> <span class="st0">""</span>, <span class="st0">"12345"</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><center>Listing 2 - Permute.cpp</center></p>
<p>This approach to generating permutations is okay, but its recursive nature makes it unattractive for use in a library. To use this in a library we would have to employ a function pointer that would be invoked from deep inside the chain of function calls. That would work, but it's definitely not the nicest way to do it.</p>
<p><span class="inline_code">next_permutation()</span> manages to avoid this trouble by using a simple algorithm that can sequentially generate all the permutations of a sequence (in the same order as the algorithm I described above) without maintaining any internal state information. The first time I saw this code was in the original STL published by Alexander Stepanov and Ming Lee at Hewlett-Packard. The original code is shown in Listing 3.</p>
<div class="igBar"><span id="lcpp-29"><a href="#" onclick="javascript:showPlainTxt('cpp-29'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-29">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">template</span> &lt;class BidirectionalIterator&gt;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw4">bool</span> next_permutation<span class="br0">&#40;</span>BidirectionalIterator first,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BidirectionalIterator last<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>first == last<span class="br0">&#41;</span> <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; BidirectionalIterator i = first;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; ++i;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>i == last<span class="br0">&#41;</span> <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; i = last;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; --i;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span>;;<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; BidirectionalIterator ii = i--;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>*i &lt;*ii<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BidirectionalIterator j = last;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>!<span class="br0">&#40;</span>*i &lt;*--j<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iter_swap<span class="br0">&#40;</span>i, j<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reverse<span class="br0">&#40;</span>ii, last<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>i == first<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reverse<span class="br0">&#40;</span>first, last<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><center>Listing 3 - Function <i>next_permutation()</i> from the STL</center></p>
<p>Using this function is simple. You call it repetitively, asking it to permute a given sequence. If you start with a sequence in ascending order, <span class="inline_code">next_permutation()</span> will work its way through all possible permutations of the sequence, eventually returning a value of false when there are no more permutations left. </p>
<h4>Internals of next_permutation()</h4>
<p>You don't need to be an STL expert to understand this code, but if you've never been exposed to this new part of the C++ standard library, there are a few things you need to know.</p>
<p>First, iterators (and the <span class="inline_code">BidirectionalIterator</span> type used here) are an STL abstraction of pointers. When looking at this code you can mentally think of the iterators as pointers. The permutation sequence is defined by iterators <span class="inline_code">first</span> and <span class="inline_code">last</span>. <span class="inline_code">first</span> points to the first element in the sequence, while <span class="inline_code">last</span> points one past the last element.</p>
<p>The code shown in Listing 3 also uses two other STL functions. <span class="inline_code">iter_swap()</span> swaps the values ponted to by its two arguments. And <span class="inline_code">reverse()</span> simply reverses the sequence defined by its two arguments. By convention of course, the first argument points to the start of the sequence to be reversed, and the last argument points one past the end of the sequence. </p>
<p>To help illustrate the workings of this algorithm, I've included a listing of a permutation sequence in Figure 2. It contains all 120 permutations of a five digit sequence. With that output example, plus Listing 3, it is fairly easy to see how this code works. </p>
<p>The function first does a cursory check for sequences of length 0 or 1, and returns <span class="inline_code">false</span> if it finds either. Naturally, sequences of those lengths only have one permutation, so they must always return false.</p>
<p>After passing those tests, the algorithm goes into a search loop. It starts at the end of the sequence and works its way towards the front, looking for two consecutive members of the sequence where member n is less than member n+1. These members are pointed to by iterators <span class="inline_code">i</span> and <span class="inline_code">ii</span> respectively. If it doesn't find two values that pass this test, it means all permutations have been generated. You can see this is the case in Figure 2 for the very last value, '54321'.</p>
<p><center><br />
<code><br />
12345 12354 12435 12453 12534 12543 13245 13254 13425 13452<br />
13524 13542 14235 14253 14325 14352 14523 14532 15234 15243<br />
15324 15342 15423 15432 21345 21354 21435 21453 21534 21543<br />
23145 23154 23415 23451 23514 23541 24135 24153 24315 24351<br />
24513 24531 25134 25143 25314 25341 25413 25431 31245 31254<br />
31425 31452 31524 31542 32145 32154 32415 32451 32514 32541<br />
34125 34152 34215 34251 34512 34521 35124 35142 35214 35241<br />
35412 35421 41235 41253 41325 41352 41523 41532 42135 42153<br />
42315 42351 42513 42531 43125 43152 43215 43251 43512 43521<br />
45123 45132 45213 45231 45312 45321 51234 51243 51324 51342<br />
51423 51432 52134 52143 52314 52341 52413 52431 53124 53142<br />
53214 53241 53412 53421 54123 54132 54213 54231 54312 54321<br />
</code></p>
<p>Figure 2 - A sequence generated by next_permutation()</center></p>
<p>Once iterators <span class="inline_code">i</span> and <span class="inline_code">ii</span> have been properly located, there are still a few more steps left. The next step is to again start searching from the end of the sequence for the first member that is greater than or equal to the member pointed to by <span class="inline_code">i</span>. Because of the previous search for <span class="inline_code">i</span> and <span class="inline_code">ii</span>, we know that at worst the search will end at <span class="inline_code">ii</span>, but it might end earlier. Once this member is located, it is pointed to by iterator <span class="inline_code">j</span>.</p>
<p>Once these three iterators are located, there are only two more simple steps. First, a call is made to <span class="inline_code">iter_swap( i, j )</span>. This simply swaps the members pointed to by <span class="inline_code">i</span> and <span class="inline_code">j</span>. Finally, a call is made to <span class="inline_code">reverse( ii, last )</span>. This has the effect of reversing the sequence that starts at <span class="inline_code">ii</span> and ends at the end of the sequence.</p>
<p>The end result is a routine that is short, simple, and runs in linear time. You really can't ask for much more than that.</p>
<h4>Walk through an example</h4>
<p>For a quick look at the algorithm in action, consider what happens when you call <span class="inline_code">next_permutation( "23541" )</span>. After passing through the initial size tests, the algorithm will search for suitable values for iterators <i>i</i> and <i>ii</i>. (Remember that you are searching from the end of the sequence for the first adjacent pair where the value pointed to by <i>i</i> is less than the value pointed to by <span class="inline_code">ii</span>, and <span class="inline_code">i</span> is one less than <span class="inline_code">ii</span>.) The first pair of values that meet the test are seen when <span class="inline_code">i</span> points to 3 and <span class="inline_code">ii</span> points to 5. After that, a second search starts from the end for the first value of <span class="inline_code">j</span> where <span class="inline_code">j</span> points to a greater value than that pointed to by <span class="inline_code">i</span>. This is seen when <span class="inline_code">j</span> points to 4.</p>
<p>Once the three iterators are set, there are only two tasks left to perform. The first is to call <span class="inline_code">iter_swap( i, j )</span>, which swaps the values pointed to by the iterators <span class="inline_code">i</span> and <span class="inline_code">j</span>. After you do this, you are left with the modified sequence "24531". The last step is to call <span class="inline_code">reverse( ii, last )</span>, which reverses the sequence starting at <span class="inline_code">ii</span> and finishing at the end of the sequence. This yields "24135". Examining Figure 2 shows that the result demonstrated here does agree with the output of the program.</p>
<h4>An additional charming attribute</h4>
<p>The algorithm shown here has one additional feature that is quite useful. It properly generates permutations when some of the members of the input sequence have identical values. For example, when I generate all the permutations of "ABCDE", I will get 120 unique character sequences. But when I generate all the permutations of "AAABB", I only get 10. This is because there are 6 different identical permutations of "AAA", and 2 identical permutations of "BB". </p>
<p>When I run this input set through a set of calls to <span class="inline_code">next_permutation()</span>, I see the correct output:</p>
<p><code><br />
AAABB AABAB AABBA ABAAB ABABA ABBAA BAAAB BAABA BABAA BBAAA<br />
</code></p>
<p>This might have you scratching your head a bit. How does the algorithm know that there are 6 identical permutations of "AAA"? The recursive implementation of a permutation generator I showed in Listing 2 treats the permutations of "AAABB" just as it does "ABCDE", obligingly printing out 120 different sequences. It doesn't know or care that there are a huge number of identical permutations in the output sequence.</p>
<p>It's easy to see why the brute force code in Listing 2 doesn't notice the duplicates. It never pays any attention to the contents of the string that it is permuting. It couldn't possibly notice that there were duplicates. It just merrily swaps characters without paying any attention to their value.</p>
<p>The STL algorithm, on the other hand, actually performs comparisons of the elements that it is interchanging, and uses their relative values to determine what interchanging will be done. In the example from the last section, you saw that an input of "24531" will generate a next permutation of "24135". What if the string had a pair of duplicates, as in "24431"? If the algorithm were ignorant of character values, the next permutation would undoubtedly be "24134".</p>
<p>In the early case, iterators <span class="inline_code">i</span> and <span class="inline_code">ii</span> were initially set to offsets of 1 and 2 within the string. But in this case, since the value pointed to by <span class="inline_code">i</span> must be less than the value pointed to by <span class="inline_code">ii</span>, the two iterators have to be decremented to positions 0 and 1. <span class="inline_code">j</span> would again point to position 3. </p>
<p>The subsequent swap operation yields "34421", and the reverse function produces a final result of "31244".  Remember that the algorithm works by progressively bubbling the larger values of the string into position 0, you can see that this permutation has already jumped well ahead of the permutation of "24531" on its way to completion. Thus, the algorithm "knows" how to deal with duplicate values.</p>
<h4>Conclusion</h4>
<p>The addition of the STL to the C++ Standard Library gave us a nice grab bag of functions that automate many routine tasks. <span class="inline_code">next_permuation() </span>turned out to be just what I needed to solve a sixth grade math problem. It might be time for you to look through the declarations in the <code>algorithm</code> header file to see what else standards committee laid on our doorstep.</p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2002/03/01/next-permutation/feed/</wfw:commentRss>
		<slash:comments>41</slash:comments>
		</item>
		<item>
		<title>XML Schema Validation in the Microsoft Universe</title>
		<link>http://marknelson.us/2002/01/01/xml-schema-validation/</link>
		<comments>http://marknelson.us/2002/01/01/xml-schema-validation/#comments</comments>
		<pubDate>Wed, 02 Jan 2002 04:53:17 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[Magazine Articles]]></category>

		<guid isPermaLink="false">/2002/01/01/xml-schema-validation/</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2002/01/01/xml-schema-validation/' addthis:title='XML Schema Validation in the Microsoft Universe' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div>This article shows how to use Microsoft's XML parser to validate XML against schemas. ]]></description>
			<content:encoded><![CDATA[



<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2002/01/01/xml-schema-validation/' addthis:title='XML Schema Validation in the Microsoft Universe' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div><table border="0" width="400px">
<tbody>
<tr>
<td width="33%">
      <img src="http://marknelson.us/attachments/xml-schema-validation/cover.jpg" alt="Windows Developers Journal, January, 2002" />
      </td>
<td>
        <b>Windows Developers Journal</b></p>
<p>January, 2002</p>
</td>
</tr>
</tbody>
</table>
<p><em>Thanks to Shaun Wilde for correcting a couple of mistakes in the code.</em></p>
<hr />
<p>XML is a great way to package data. With the approval of the XML Schema standard by the World Wide Web Consortium, developers now have a good tool for validating the form and content of their XML data. This article gives an example of how to perform this validation by using version 4 of Microsoft's MSXML parser. </p>
<h4>Real World XML</h4>
<p>Over the past few months I've been laboring away at a new book for Cisco Press which has the snappy title <i>Developing Cisco IP Phone Productivity Services</i>. In a nutshell, Cisco IP Phone Services are simply XML files served up to the phone over a standard HTTP link. XML objects can be used to display a wide variety of data on the phone, enabling programs running on a web server to use the phone as a flexible I/O device.  </p>
<p>This means that anybody who can put together a web page can create nifty programs that interact with Cisco's IP phones. Examples that have been created so far include flight tracking apps, appointment books, and joke-a-day services. A well-loved productivity application is shown in Figure 1.</p>
<p><center><br />
<img src="http://marknelson.us/attachments/xml-schema-validation/Figure1.gif"></p>
<p>Figure 1 - An XML Object Rendered on the Phone<br />
</center></p>
<p>When the programmers at Cisco's Dallas engineering center were designing these services, they naturally chose to use XML to wrap up the various objects that were being sent back and forth. Yes, XML was in the air at the time, but there were also practical reasons. First among these was the availability of a compact XML parser that fit in the phone's firmware footprint. Second was the general feel-good vibe that comes with using an accepted standard as an integral part of our product. An additional boon was the support for XML in off-the-shelf tools, such as Microsoft's Internet Explorer or Altova's XML Spy.</p>
<h4>Details</h4>
<p>The menagerie of XML objects that the phone knows how to render and/or execute is fairly small. There are XML definitions for menus, graphics, directory listings, and so on. As an example, the simplest XML object supported by the phone is the CiscoIPPhoneText element. A typical example of this element might look like this:</p>
<div class="igBar"><span id="lxml-30"><a href="#" onclick="javascript:showPlainTxt('xml-30'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">XML:</span>
<div id="xml-30">
<div class="xml">
<ol>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;CiscoIPPhoneText<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="sc3"><span class="re1">&lt;Title<span class="re2">&gt;</span></span></span>Magic Cube<span class="sc3"><span class="re1">&lt;/Title<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;Text<span class="re2">&gt;</span></span></span>When you take the pebble from my hand<span class="sc3"><span class="re1">&lt;/Text<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li2">
<div class="de2"><span class="sc3"><span class="re1">&lt;/CiscoIPPhoneText<span class="re2">&gt;</span></span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Leading to a display on the phone like that shown in Figure 2.</p>
<p><center><br />
<img src="http://marknelson.us/attachments/xml-schema-validation/Figure2.gif"></p>
<p>Figure 2 - The CiscoIPPhoneText Object<br />
</center></p>
<p>Unfortunately, there isn't much to do when the phone gets badly-formed XML. In a typical example, a web designer could inadvertently introduce a typo in the text used in the above example, leading to this XML file on the server:</p>
<div class="igBar"><span id="lxml-31"><a href="#" onclick="javascript:showPlainTxt('xml-31'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">XML:</span>
<div id="xml-31">
<div class="xml">
<ol>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;CiscoIPPhoneText<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="sc3"><span class="re1">&lt;Title<span class="re2">&gt;</span></span></span>Magic Cube<span class="sc3"><span class="re1">&lt;/Title<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;Text<span class="re2">&gt;</span></span></span>When you take the pebble from my hand<span class="sc3"><span class="re1">&lt;/Text</span>.<span class="re2">&gt;</span></span></div>
</li>
<li class="li2">
<div class="de2"><span class="sc3"><span class="re1">&lt;/CiscoIPPhoneText<span class="re2">&gt;</span></span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>We've all had times when a fat finger on the keyboard accidentally introduced an extra character, as shown above. The extra period in the closing tag for the Text element is enough to turn this into a badly-formed XML file. </p>
<p>When the Cisco IP Phone attempts to render this XML object, its parser naturally rejects the input. With not many good options at this point, the phone simply displays a blank screen, as shown in Figure 3.</p>
<p><center><br />
<img src="http://marknelson.us/attachments/xml-schema-validation/Figure3.gif"></p>
<p>Figure 3 - The Cisco IP Phone Responds to Bad XML Input<br />
</center></p>
<p>This is a bad situation for the user of the IP Phone, but it's even worse for the designer of the web page. There are virtually no clues here to identify the problem. In theory we could load up the phone firmware with additional debugging code to identify problems such as this one, but we are up against constraints in memory space, developer time, and parser intelligence.</p>
<p>The good news is that existing tools such as Internet Explorer can help a lot with problems such as this. Looking at the same XML served up by the web server, Internet Explorer produces the display shown in Figure 4.</p>
<p><center><br />
<img src="http://marknelson.us/attachments/xml-schema-validation/Figure4.gif"></p>
<p>Figure 4 - Internet Explorer Finds the Problem<br />
</center></p>
<p>This kind of display is great - it takes you directly to the problem and identifies it clearly. Unfortunately, there is an entire class of XML problems that are not detected by Internet Explorer.  Specifically, Internet Explorer (as of today) can't help diagnose an XML streamed that is well-formed but invalid. </p>
<h4>Valid vs. Well-Formed</h4>
<p>The XML standard defines two different standards for XML documents: well-formed and valid. A well-formed document is one that follows the basic rules of XML regarding basic syntax, such as nesting of tags, placement of attributes, and so on. Creating well-formed documents is fairly easy for developers, because you can simply add new elements and attributes as the need arises, confident that the parser reading the document will know what to do.</p>
<p>On the other hand, a valid document is held to a somewhat higher standard.  A valid document has to adhere to a well-defined set of rules regarding the type, order, and placement of tags that appear in it. In the early days of XML, these rules were typically defined in what was called a Document Type Definition, or DTD. </p>
<p>For various reasons, the DTD format first used with XML proved to be unpopular, and a revised schema definition system called <i>XML Schema</i> was born. XML Schema does all that the original DTDs did, with some enhancements. In addition to knowing about the placement and types of tags, XML Schema allows you to specify information about the text that is embedded inside the markup. So, for example, XML Schema allows me to specify that a value between the &lt;Price&gt; and &lt;/Price&gt;  tags should be a floating-point value between 1.0 and 9.99.</p>
<p>The World Wide Web Consortium ratified XML Schema as an official recommendation in 2001, and we're now seeing the first set of tools that support it. I've been using <i>XML Spy 4.0</i>, both to create schema files and to test them against XML data. Writing a parser that validates against XML Schema is considerably more difficult than a plain-vanilla parser, so it may be some time before a wide selection of tools are available.</p>
<h4>The Invalid XML Problem</h4>
<p>The difference between a well-formed and a valid file is easy to demonstrate with the XML objects used for the Cisco IP Phone. Figure 5 shows Internet Explorer happily parsing a well-formed XML document and displaying it on a PC. A quick look at this document makes it look as if all is well, but oddly enough, the IP Phone fails to parse and  display the data.</p>
<p><center><br />
<img src="http://marknelson.us/attachments/xml-schema-validation/Figure5.gif"></p>
<p>Figure 5 - Well-formed, but not valid<br />
</center></p>
<p>This is the kind of problem that can be pretty tough to spot. In this particular case, the programmer of the web page used a lower case 'p' in the opening and closing tags for element <em>CiscoIPPhoneText</em>. Since XML is case-sensitive, this error tripped up the phone, which determined that this is not a recognized document. The end user was faced with the unfriendly blank screen.</p>
<h4>The Solution</h4>
<p>The solution was to write a validation tool that compared the XML document to the schema defined for the Cisco IP Phones. For this particular project, I chose to use the MSXML parser from Microsoft. Version 4 is currently in an open beta, and will validate XML documents against DTDs and XML Schema. (A popular alternative is the Xerces parser, available for both C and Java.)</p>
<p>My simple version of this tool allows the user to input either a file name or a Universal Resource Locator (URL), then press the <i>Validate</i> button. The data is read from the source, parsed, and the result shown in a dialog box. The XML document from Figure 5 results in the error display in Figure 6.</p>
<p><center><br />
<img src="http://marknelson.us/attachments/xml-schema-validation/Figure6.gif"></p>
<p>Figure 6 - A Validating Parser Finds the Error<br />
</center></p>
<p>As you can see, MSXML does a pretty good job of identifying the offending location in the XML, saving the programmer from quite a bit of head scratching. The next section of this article will show you how to put together a program that accomplishes the same thing as my Validator.</p>
<h4>Using MSXML</h4>
<p>MSXML 4.0 is an ActiveX control, which means it can be used by quite a few different programming languages, ranging from JavaScript to Visual Basic to C++. I chose to write my validation program as a simple MFC dialog application using Visual C++ 6.0.</p>
<p>Visual C++ 6.0 has some language extensions that make working with COM objects a bit easier. (For example, using the <span class="inline_code">#include </span>statement to include a DLL directly, creating type libraries on the fly.) I didn't use these in this application, which means the code you see here should be easily portable to other C++ compilers. Perhaps this philosophy will help when porting the code used here to other languages as well.</p>
<h4>Installing MSXML 4.0</h4>
<p>In October of 2001 Microsoft released MSXML 4.0 as a free download. All versions of the XML parser are found at Microsoft's XML page, <a href="http://www.microsoft.com/XML">http://www.microsoft.com/XML</a>. You will need to download and install the MSXML 4.0 SDK to build programs that use the new parser.</p>
<p>The install procedure for the parser by default copies header and library files to <span class="inline_code">"C:\Program Files\MSXML 4.0"</span>, with the header files in the <span class="inline_code">inc</span> subdirectory and the library files in <span class="inline_code">lib</span>. The include file <span class="inline_code">msxml2.h</span> contains all the definitions you will need in your C++ code, and <span class="inline_code">msxml2.lib</span> contains links to the appropriate parser routines.</p>
<h4>The Project Skeleton</h4>
<p>My Validator project is a simple MFC dialog-based application. A complete listing is available by following the links below, so I won't give you every detail. After creating the project using Microsoft's Wizard, I made two minor modifications to accommodate the use of MSXML.</p>
<ul>
<li/>First, I added <span class="inline_code">"C:\Program Files\MSXML 4.0\inc"</span> to my include path for both the debug and release versions of the program. This is where the header file <span class="inline_code">msxml2.h</span> is located, and it will be included in the main project.
<li>Second, I added the file <span class="inline_code">C:\Program Files\MSXML 4.0\lib\msxml2.lib</span> to the project. This library contains the CLSID and other definitions needed to access the functions in MSXML 4.0.
</ul>
<p>To this skeleton, I added some code to let the user either browse to select an XML file for input, or simply type in a URL. These pieces of infrastructure are shown in action in Figure 7.</p>
<p><center><br />
<img src="http://marknelson.us/attachments/xml-schema-validation/Figure7.gif"><br />
Figure 7 - The I/O portions of Validator<br />
</center></p>
<h4>The Real Work - Validation</h4>
<p>There are two steps to perform in order to actually validate an XML document using MSXML 4.0. After the necessary plumbing needed to instantiate a copy of the MSXML COM Object of type <span class="inline_code">XMLDOMDocument2</span>, I have to create an <span class="inline_code">XMLSchemaCache</span> object, then add a copy of the Cisco IP Phone schema to it. A reference to that schema container is then passed to the DOM Document. The fragment of code that accomplishes  this is shown in Listing 1.</p>
<div class="igBar"><span id="lcpp-32"><a href="#" onclick="javascript:showPlainTxt('cpp-32'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-32">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1">hr = CoCreateInstance<span class="br0">&#40;</span> CLSID_XMLSchemaCache40, </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">NULL</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CLSCTX_SERVER, </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IID_IXMLDOMSchemaCollection, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#40;</span>LPVOID*<span class="br0">&#41;</span><span class="br0">&#40;</span> &amp;pIXMLDOMSchemaCollection<span class="br0">&#41;</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">SUCCEEDED<span class="br0">&#40;</span>hr<span class="br0">&#41;</span> ? <span class="nu0">0</span> : throw hr;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span> <span class="br0">&#40;</span> SUCCEEDED<span class="br0">&#40;</span> hr <span class="br0">&#41;</span> &amp;&amp; pIXMLDOMSchemaCollection <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; hr = pIXMLDOMSchemaCollection-&gt;add<span class="br0">&#40;</span> _bstr_t<span class="br0">&#40;</span> _T<span class="br0">&#40;</span><span class="st0">""</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>, </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _variant_t<span class="br0">&#40;</span> _T<span class="br0">&#40;</span><span class="st0">"CiscoIPPhone.xsd"</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> hr == INET_E_OBJECT_NOT_FOUND <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; throw <span class="st0">"You need to have a copy of CiscoIPPhone.xsd in the <span class="es0">\r</span><span class="es0">\n</span>"</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">"same directory that you are executing Validator in. The <span class="es0">\r</span><span class="es0">\n</span>"</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">"program will not work properly until you rectify this. This<span class="es0">\r</span><span class="es0">\n</span>"</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">"schema file ships on the CD in the same directory<span class="es0">\r</span><span class="es0">\n</span>"</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">"as Validator.exe"</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; SUCCEEDED<span class="br0">&#40;</span>hr<span class="br0">&#41;</span> ? <span class="nu0">0</span> : throw hr;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; varValue.<span class="me1">vt</span> = VT_DISPATCH;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; varValue.<span class="me1">pdispVal</span> = pIXMLDOMSchemaCollection;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; hr = pIXMLDOMDocument2-&gt;putref_schemas<span class="br0">&#40;</span> varValue <span class="br0">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><center>Listing 1 - Loading the schema object and passing it to the DOM object</center></p>
<p>As is usually the case, C++ code that uses COM objects is unattractive, and a bit opaque. But in this simple example you should be able to look past that and see what is actually happening. Most importantly, you can see that this program expects to find a copy of <span class="inline_code">CiscoIPPhone.xsd</span> in the current directory. This file is the file that actually contains the schema for all the Cisco IP Phone XML objects. </p>
<p>Once the DOM Document has a reference to the schema, it is ready to do the actual parsing. Microsoft's DOM Document object is flexible enough to load either a reference to a local file, or to a URL which will supply an HTTP stream. The fragment in Listing 2 does this by simply calling the <span class="inline_code">load</span> method of the object. It then pops up a dialog with one of two possible outcomes: the successfully parsed XML content, or a formatted error message.</p>
<div class="igBar"><span id="lcpp-33"><a href="#" onclick="javascript:showPlainTxt('cpp-33'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-33">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1">CString <span class="kw4">file</span>;</div>
</li>
<li class="li2">
<div class="de2">GetDlgItemText<span class="br0">&#40;</span> IDC_EDIT1, <span class="kw4">file</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">hr = pIXMLDOMDocument2-&gt;load<span class="br0">&#40;</span> _variant_t<span class="br0">&#40;</span> _T<span class="br0">&#40;</span> <span class="kw4">file</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span>, </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;sResult<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span> <span class="br0">&#40;</span> SUCCEEDED<span class="br0">&#40;</span> hr <span class="br0">&#41;</span> &amp;&amp; <span class="br0">&#40;</span> sResult == VARIANT_TRUE <span class="br0">&#41;</span> <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; pIXMLDOMDocument2-&gt;get_xml<span class="br0">&#40;</span> &amp;bstrValue <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; CViewer dlg<span class="br0">&#40;</span> <span class="st0">"Valid XML Input"</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; dlg.<span class="me1">m_Label</span> = <span class="st0">"Parsed XML:"</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; dlg.<span class="me1">m_Text</span> = bstrValue;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; dlg.<span class="me1">DoModal</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; IXMLDOMParseError *pIParseError = <span class="kw2">NULL</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; hr = pIXMLDOMDocument2-&gt;get_parseError<span class="br0">&#40;</span> &amp;IParseError <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; SUCCEEDED<span class="br0">&#40;</span>hr<span class="br0">&#41;</span> ? <span class="nu0">0</span> : throw hr;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; BSTR b;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; hr = pIParseError-&gt;get_reason<span class="br0">&#40;</span> &amp;b <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; ostringstream out;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; out &lt;&lt;<span class="st0">"MS Parser reported this error:<span class="es0">\r</span><span class="es0">\n</span>"</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw4">long</span> code;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; pIParseError-&gt;get_errorCode<span class="br0">&#40;</span> &amp;code <span class="br0">&#41;</span> ;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; out &lt;&lt;<span class="st0">"Code = 0x"</span> &lt;&lt;hex &lt;&lt;code &lt;&lt;<span class="st0">"<span class="es0">\r</span><span class="es0">\n</span>"</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">long</span> line_number;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw4">long</span> line_pos;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; pIParseError-&gt;get_line<span class="br0">&#40;</span> &amp;line_number <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; pIParseError-&gt;get_linepos<span class="br0">&#40;</span> &amp;line_pos <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; out &lt;&lt;<span class="st0">"Source = Line : "</span> &lt;&lt;line_number </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt;<span class="st0">"&nbsp; Char : "</span> &lt;&lt;line_pos &lt;&lt;<span class="st0">"<span class="es0">\r</span><span class="es0">\n</span>"</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; out &lt;&lt;<span class="st0">"Error Description = "</span> &lt;&lt;<span class="br0">&#40;</span><span class="kw4">const</span> <span class="kw4">char</span> *<span class="br0">&#41;</span> _bstr_t<span class="br0">&#40;</span> b <span class="br0">&#41;</span> &lt;&lt;<span class="st0">"<span class="es0">\r</span><span class="es0">\n</span>"</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; hr = pIParseError-&gt;get_srcText<span class="br0">&#40;</span> &amp;b <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> hr == <span class="nu0">0</span> <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; out &lt;&lt;<span class="st0">"<span class="es0">\r</span><span class="es0">\n</span>Source text where the error occured:<span class="es0">\r</span><span class="es0">\n</span><span class="es0">\r</span><span class="es0">\n</span>"</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt;<span class="br0">&#40;</span><span class="kw4">const</span> <span class="kw4">char</span> *<span class="br0">&#41;</span> _bstr_t<span class="br0">&#40;</span> b <span class="br0">&#41;</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt;<span class="st0">"<span class="es0">\r</span><span class="es0">\n</span>"</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span> <span class="kw4">int</span> i = <span class="nu0">1</span> ; i &lt;= line_pos ; i++ <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> i == line_pos <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out &lt;&lt;<span class="st0">'^'</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out &lt;&lt;<span class="st0">"-"</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; out &lt;&lt;<span class="st0">"<span class="es0">\r</span><span class="es0">\n</span>"</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; CViewer dlg<span class="br0">&#40;</span> <span class="st0">"Parser Error"</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; dlg.<span class="me1">m_Text</span> = out.<span class="me1">str</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">c_str</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; dlg.<span class="me1">m_Label</span> = <span class="st0">"Parser errors:"</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; dlg.<span class="me1">DoModal</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; SysFreeString<span class="br0">&#40;</span> b <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;<span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><center>Listing 2 - Parsing the XML and displaying the results.</center></p>
<p>Figure 6 showed an example of what you see when the parser reports a failure. Figure 8 shows what it does upon success, which is to just present a copy of the parsed data found in the xml property of the DOM object.</p>
<p><center><br />
<img src="http://marknelson.us/attachments/xml-schema-validation/Figure8.gif"></p>
<p>Figure 8 - Succesfully parsed XML Data</center></p>
<h4>Wrap-up</h4>
<p>MXSML 4.0 is a great step forward for users of Microsoft's parser. Adding validation to the tool provides a great leap forward for developers, and for end users who benefit from better checking of the data. There is one feature I wish Microsoft had implemented differently. As the control works now, when parsing fails, the document only keeps a copy of one line from the bad input. It would be nice to keep the entire document, or at least a little more context, which  would make interpretation of the error a little bit easier.</p>
<p>But given the price tag and the capabilities the control brings to my programs, I have to give it a better than passing grade.</p>
<h4>Source</h4>
<p>Validator source files are here in <a href="http://marknelson.us/attachments/xml-schema-validation/Source.zip">Source.zip</a>.</p>
<p>You will need to install version 4 of Microsoft's XML parser in order to build and execute the program.</p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2002/01/01/xml-schema-validation/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

