<?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</title>
	<atom:link href="http://marknelson.us/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>C++11: Range-based for and auto</title>
		<link>http://marknelson.us/2012/04/07/c11-range-based-for-and-auto/</link>
		<comments>http://marknelson.us/2012/04/07/c11-range-based-for-and-auto/#comments</comments>
		<pubDate>Sat, 07 Apr 2012 18:18:02 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Puzzles]]></category>

		<guid isPermaLink="false">http://marknelson.us/?p=1511</guid>
		<description><![CDATA[Two really handy features in C++11 are the range-based for statement and the auto type specifier. The former allows you iterate over collections using a much more compact form of expression, and the latter takes some of the headache out of the complex type declarations encountered in the standard library. Both of these features have [...]]]></description>
			<content:encoded><![CDATA[<p>Two really handy features in C++11 are the <i>range-based for statement</i> and the <i>auto type specifier</i>. The former allows you iterate over collections using a much more compact form of expression, and the latter takes some of the headache out of the complex type declarations encountered in the standard library. Both of these features have been available in g++ since release 4.6, and are now present in Visual Studio 11, so you can start using them today. (auto typed variables are available in earlier versions of both compilers.) In this post I&#8217;ll give you a description of how these new features works, and show you a concrete example of the positive effects they can have on your programs.<br />
<span id="more-1511"></span></p>
<h4>The value of containers</h4>
<p>It&#8217;s hard to overstate the value of the containers in the C++ standard library. With the addition of the hash-based containers in TR1, I rarely if ever find myself tempted to roll my own, or use a third party library. The flexibility and power of the library created by Alexander Stepanov does everything I need.</p>
<p>Despite the technical merit of the container classes, newcomers are often hesitant about completely embracing them. One of the main reasons has to be the conceptual drag imposed by the use of iterators as the primary means of accessing the objects they contain. It&#8217;s not that there is anything complicated about the concept, but the syntax can be more than just a little annoying. Let me illustrate it with an example.</p>
<h4>Anagramania</h4>
<p>The listing below is a C++ program that reads through the Scrabble dictionary and determines which set of letters generates the most anagrams. I&#8217;m using C++ circa TR1, in which I have access to the unordered associative containers, but I don&#8217;t take any shortcuts to try to simplify the syntax. (The fact that I can write this program in one screen of simple code is a nice testament to the quality of the container library.)</p>
<p>The logic for the program is simple. I use an <code>unordered_multimap</code> called <code>counts</code> to hold the count of all anagram families in the dictionary, with its key being the sorted value of the scrabble word. This means that all words that are anagrams of one another will have the same key. I use an <code>unordered_multimap</code> called <code>words</code> to hold the list of all words that are anagrams of that key. Each time I process a word, I increment a value in <code>counts</code> and I add a new value to <code>words</code>.</p>
<p>After the input processing is done, I can just iterate over <code>counts</code> from top to bottom, looking for the highest count. When I have gone through the entire map, I have the sorted key that generates the most anagrams. Using that key, I query an <code>unordered_multimap</code> for a range of results. It returns two iterators in a <code>pair<T1,T2></code> object, which I then use to iterate over the result set. </p>
<p>Even if you are familiar with the type system used by the containers and don&#8217;t make too many mistakes, just the magnitude of how much you have to type to get this to work is a bit of a downer. And the length of those type definitions doesn&#8217;t help make the concepts being used any clearer.</p>
<pre>
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;iterator&gt;
#include &lt;algorithm&gt;
#include &lt;unordered_map&gt;

int main(int argc, char* argv[])
{
    std::ifstream data( &quot;sowpods.txt&quot; );
    std::unordered_map&lt;std::string,int&gt; counts;
    std::unordered_multimap&lt;std::string,std::string&gt; words;

    std::string s;
    while ( data &gt;&gt; s ) {
        std::string temp = s;
        std::sort(temp.begin(), temp.end() );
        counts[temp]++;
        words.insert( std::make_pair(temp,s) );
    }

    int max_count = -1;
    std::string max_string = &quot;&quot;;
    for ( std::unordered_map&lt;std::string,int&gt;::iterator ii = counts.begin();
          ii != counts.end();
          ii++ )
    {
        if ( ii-&gt;second &gt; max_count ) {
            max_count = ii-&gt;second;
            max_string = ii-&gt;first;
        }
    }
    std::cout &lt;&lt; &quot;The maximum anagram family has &quot; &lt;&lt; max_count &lt;&lt; &quot; members:\n&quot;;
    std::pair&lt; std::unordered_multimap&lt;std::string,std::string&gt;::iterator,
	       std::unordered_multimap&lt;std::string,std::string&gt;::iterator&gt; range;
    range = words.equal_range( max_string );
    for ( std::unordered_multimap&lt;std::string,std::string&gt;::iterator ii = range.first;
          ii != range.second;
          ii++ )
        std::cout &lt;&lt; ii-&gt;second &lt;&lt; &quot; &quot;;
    std::cout &lt;&lt; std::endl;
    return 0;
}
</pre>
<p><center>Anagram finder circa TR1</center><br />
Now let&#8217;s look at the two features that make major improvements to this program in C++11.</p>
<h4>The auto Type Specification</h4>
<p>The hard working committee members who hammered out the standard last year clearly listened to the millions of C++ programmers out there. While they were charting new waters for the language with things like move semantics and rvalue references, they were also making a lot of small changes that simply make the language a lot easier to work with. Maybe even a little more fun. The two things I find at the top of my list are the the use of auto type specifier and the for-range statement.</p>
<p>The auto keyword can be used in a number of different contexts, but in general it means that you can declare variables without having to enter a complete type. This solves some tricky problems for template programming, and it provides a convenience for awkward variable declarations. Most notably, it allows you to replace these two wordy lines of code:</p>
<pre>
    std::pair&lt; std::unordered_multimap&lt;std::string,std::string&gt;::iterator,
	       std::unordered_multimap&lt;std::string,std::string&gt;::iterator&gt; range;
    range = words.equal_range( max_string );
</pre>
<p>with this much simpler single line:</p>
<pre>
    auto range = words.equal_range( max_string );
</pre>
<p>In both cases, the type of <code>range</code> is the same &#8211; but by using the auto type specifier, we let the compiler replace all that typing with a bit of simple hand waving.</p>
<p>Bjarne Stroustrup has a good, concise explanation of <a href="http://www2.research.att.com/~bs/C++0xFAQ.html#auto" class="newpage">auto</a> on his C++11 FAQ, I recommend you spend the time to read it.</p>
<h4>The Range-based for Statement</h4>
<p>When working with standard library containers, one of the most common things we do is iterate over some or all of the container. This generally is done using a for or while loop with an iterator loop variable.</p>
<p>C++11 makes this type of iteration easier with new syntax injected into the <code>for</code> statement that has been around since 1969. The range-based for looks like this:</p>
<pre>
    for ( declaration : expression ) statement
</pre>
<p>In this new statement, <code>expression</code> can be an initializer list, an array, or an object that implements container semantics. This means that the object returns an iterator-like object from a <code>begin()</code> and <code>end()</code> methods, or via a call to <code>begin()</code> and <code>end()</code> functions in the current or std namespace.</p>
<p>The variable declaration is either a reference or value of the type of variable held in the container, array, or initializer list. The for loop is executed from the beginning of the container to the end, with <code>statement</code> executed once per value returned by the iterator.</p>
<p>Although this is a completely new language feature, I think most C++ programmers will be comfortable with it from the first time they are able &#8211; it makes those iterations over containers clean and concise.</p>
<h4>Putting it to Use</h4>
<p>Although it didn&#8217;t really cut down on my code size in a big way, I first made use of the range-based for in the loop that reads in the data from the scrabble dictionary. My new version of the loop is shown here:</p>
<pre>
for ( const std::string &#038;s : std::istream_iterator&lt;std::string&gt;( data ) )
{
    std::string temp = s;
    std::sort(temp.begin(), temp.end() );
    counts[temp]++;
    words.insert( std::make_pair(temp,s) );
}
</pre>
<p>The only big improvement here is that I was able to declare my string variable on first use, which is always my preference.</p>
<p>However, looking at this code, you might be wondering how it compiles. After all, the <code>istream_iterator</code> doesn&#8217;t have <code>begin()</code> or <code>end()</code> member functions.</p>
<p>That&#8217;s correct, and the reason it works is that I added a couple of convenience functions to my program that enable the use of this iterator type with the range-based for:</p>
<pre>
template&lt;class T&gt;
std::istream_iterator&lt;T&gt; begin(std::istream_iterator&lt;T&gt; &amp;ii_stream)
{
    return ii_stream;
}

template&lt;class T&gt;
std::istream_iterator&lt;T&gt; end(std::istream_iterator&lt;T&gt; &amp;ii_stream)
{
    return std::istream_iterator&lt;T&gt;();
}
</pre>
<p>I made use of a similar set of template functions to enable the use of the new for statement in my final output statement. I now iterate over the discovered members of the anagram family with two easy-to-read lines:</p>
<pre>
for ( const auto &#038;map_entry : words.equal_range( ii-&gt;first ) )
    std::cout &lt;&lt; map_entry.second &lt;&lt; &quot; &quot;;
</pre>
<p>Compare this to the TR1 code that does the same thing, and I think you will see the real value of both auto and range-based for.</p>
<p>Iterating over the values returned from a multimap is a common task, enabled it by these convenient template functions:</p>
<pre>
template&lt;class ITERATOR&gt;
ITERATOR begin( std::pair&lt;ITERATOR,ITERATOR&gt; &amp;range )
{
    return range.first;
}

template&lt;class ITERATOR&gt;
ITERATOR end( std::pair&lt;ITERATOR,ITERATOR&gt; &amp;range )
{
    return range.second;
}
</pre>
<p>When I first implemented the functions for my C++11 program, I was halfway expecting to find that this functionality had already been added to the standard library &#8211; they really make a big improvement for a small investment. But no, I couldn&#8217;t find them, so we will be using our own versions for the time being.</p>
<h4>The Final Product</h4>
<p>My much improved anagram finder is shown below. In addition to the use of range-based for and auto type declarations, I changed the way I find the maximum element in the container. Now that lambdas are part of the language, there is no excuse for not using the standard library algorithms, and this code gives an illustration of how that works as well. </p>
<pre>
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;iterator&gt;
#include &lt;algorithm&gt;
#include &lt;unordered_map&gt;

template&lt;class ITERATOR&gt;
ITERATOR begin( std::pair&lt;ITERATOR,ITERATOR&gt; &amp;range )
{
    return range.first;
}

template&lt;class ITERATOR&gt;
ITERATOR end( std::pair&lt;ITERATOR,ITERATOR&gt; &amp;range )
{
    return range.second;
}

template&lt;class T&gt;
std::istream_iterator&lt;T&gt; begin(std::istream_iterator&lt;T&gt; &amp;ii_stream)
{
    return ii_stream;
}

template&lt;class T&gt;
std::istream_iterator&lt;T&gt; end(std::istream_iterator&lt;T&gt; &amp;ii_stream)
{
    return std::istream_iterator&lt;T&gt;();
}

int main(int argc, char* argv[])
{
    std::ifstream data( &quot;sowpods.txt&quot; );
    std::unordered_map&lt;std::string,int&gt; counts;
    std::unordered_multimap&lt;std::string,std::string&gt; words;

    for ( const std::string &amp;s : std::istream_iterator&lt;std::string&gt;( data ) )
    {
        std::string temp = s;
        std::sort(temp.begin(), temp.end() );
        counts[temp]++;
        words.insert( std::make_pair(temp,s) );
    }
    auto ii = std::max_element( counts.begin(),
                                counts.end(),
                                [](const std::pair&lt;std::string,int&gt; &amp;v1,
                                   const std::pair&lt;std::string,int&gt; &amp;v2)
                                {
                                    return v1.second &lt; v2.second;
                                }
                              );
    std::cout &lt;&lt; &quot;The maximum anagram family has &quot; &lt;&lt; ii-&gt;second &lt;&lt; &quot; members:\n&quot;;
    for ( const auto &#038;map_entry : words.equal_range( ii-&gt;first ) )
        std::cout &lt;&lt; map_entry.second &lt;&lt; &quot; &quot;;
    std::cout &lt;&lt; std::endl;
    return 0;
}
</pre>
<p><center>Anagram finder in C++11</center><br />
If I move the four convenience functions into a utility header file, I think you&#8217;ll agree that the new version of the code implements my algorithm in a very clean and concise way. The new language improvements make a huge difference in readability and convenience.</p>
<p>Of course, these two features are just one small part of a huge new standard, but for right now, they are the ones I turn to the most. How about you? Let me know!</p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2012/04/07/c11-range-based-for-and-auto/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Visual Studio 11 and Modern C++</title>
		<link>http://marknelson.us/2012/03/13/visual-studio-11-and-modern-c/</link>
		<comments>http://marknelson.us/2012/03/13/visual-studio-11-and-modern-c/#comments</comments>
		<pubDate>Tue, 13 Mar 2012 13:13:08 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Standards]]></category>

		<guid isPermaLink="false">http://marknelson.us/?p=1467</guid>
		<description><![CDATA[Despite some harsh words about Visual Studio 11, I&#8217;m finding that it makes my heart go pitter-pat every time I use it. Why? Because this early release is finally incorporating a decent set of long-awaited C++11 features. In this article I&#8217;ll show you how a little thing like a lambda can make a big difference [...]]]></description>
			<content:encoded><![CDATA[<p>Despite some <a href="http://drdobbs.com/windows/232602205" class="newpage">harsh words</a> about Visual Studio 11, I&#8217;m finding that it makes my heart go pitter-pat every time I use it. Why?  Because this early release is finally incorporating a decent set of long-awaited C++11 features. In this article I&#8217;ll show you how a little thing like a lambda can make a big difference in your coding style.<br />
<span id="more-1467"></span></p>
<h4>Microsoft and C++ &#8211; We Have History</h4>
<p>Microsoft has a cyclic relationship with C++. In the early MFC days, the love was there big time &#8211; you had access to most of the system API using C++. However, around the turn of the millennium, Microsoft came under the Rasputin-like influence of Anders Hejlsberg and his beloved offspring, C#. Now it appears that maybe the pendulum is swinging back a bit, and C++ is no longer viewed as an afterthought. Great news.</p>
<p>Although Visual Studio 11 is a developer&#8217;s preview, Microsoft is <a href="http://herbsutter.com/2012/02/29/vc11-beta-on-feb-29/" class="newpage">saying</a> that it is production ready &#8211; you can use this to create programs that are ready for release. In addition to touting a complete implementation of the C++11 standard library, an <a href="http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx" class="newpage">impressive list</a> of language features have been turned on as well. (N.B. the path ahead is still long and arduous.)</p>
<h4>Modern C++</h4>
<p>Before even using Visual Studio C++ 11 to test a single line of code, I really appreciated reading <a href="http://msdn.microsoft.com/en-us/library/hh279654(v=vs.110).aspx" class="newpage">Welcome Back to C++ (Modern C++)</a>, a manifesto that includes the following bullet points:</p>
<blockquote><p>
Modern C++ emphasizes:</p>
<ul>
<li>Stack-based scope instead of heap or static global scope.</li>
<li>Auto type inference instead of explicit type names.</li>
<li>Smart pointers instead of raw pointers.</li>
<li>std::string and std::wstring types instead of raw char[] arrays.</li>
<li>Standard template library (STL) containers—for example, vector, list, and map—instead of raw arrays or custom containers.</li>
<li>STL algorithms instead of manually coded ones.</li>
<li>Exceptions, to report and handle error conditions.</li>
<li>Inline lambda functions instead of small functions implemented separately.</li>
</blockquote>
<p>I feel that all of these changes result in safer code that is easier to read and maintain, without giving up the type-safety and efficiency that we love so much. Fully implementing these features either leans heavily on C++11 or requires it outright.</p>
<h4>A Simple Example Using Naive C++98</h4>
<p>It&#8217;s interesting to watch the evolution of code from C++ 98 to C++11 and see how it affects your code. You&#8217;ll see that the transformation can make it look like you are literally using a new programming language.</p>
<p>In this simple program, I&#8217;m taking a Scrabble rack of tiles and whipping through the Scrabble dictionary to find matches. Since it is a one-time call, I&#8217;m not storing the words, just doing a quick online comparison. In C++ 98, my code might have looked like this:</p>
<pre>
void find_matches( std::string rack, const std::string &amp;filename )
{
    std::sort( rack.begin(), rack.end() );
    std::ifstream sowpods( filename.c_str() );
    std::string word;
    while ( sowpods &gt;&gt; word ) {
        std::string sorted = word;
        std::sort( sorted.begin(), sorted.end() );
        if ( sorted == rack )
            std::cout &lt;&lt; word &lt;&lt; &quot; &quot;;
    }
}

int main(int argc, char* argv[])
{
    find_matches( &quot;etaionsr&quot;, &quot;sowpods.txt&quot; );
    return 0;
}
</pre>
<p>This works properly and I get what looks like correct output:</p>
<pre>
anoestri arsonite notaries notarise rosinate senorita
</pre>
<h4>Classes Good, Templates Better</h4>
<p>As people started to get more comfortable with templates and iterators, algorithms like this were commonly rewritten to take an range of iterators as input &#8211; much as the standard library algorithm functions do. This meant changing the function to a template function, but it did make it a lot more flexible. I could now call the function to operate on data from a file, just as before, but I can also now use any other container, or even an array as input:</p>
<pre>
template&lt;typename ITERATOR&gt;
void find_matches( std::string rack, ITERATOR ii, ITERATOR jj )
{
    std::sort( rack.begin(), rack.end() );
    for ( ; ii != jj ; ii++ ) {
        std::string sorted = *ii;
        std::sort( sorted.begin(), sorted.end() );
        if ( sorted == rack )
            std::cout &lt;&lt; *ii &lt;&lt; &quot; &quot;;
    }
}

int main(int argc, char* argv[])
{
    std::ifstream sowpods( &quot;sowpods.txt&quot; );
    find_matches( &quot;etaionsr&quot;,
                  std::istream_iterator&lt;std::string&gt;( sowpods ),
                  std::istream_iterator&lt;std::string&gt;() );
    return 0;
}
</pre>
<p>More or less the same number of lines of code, but it is now generic.</p>
<p>Of course, just like with OOP, you need to take some care with template programming. Generic programming can&#8217;t be beat when it makes sense, but programmers have a particularly strong susceptibility to <a href="http://en.wikipedia.org/wiki/Pro-innovation_bias" class="newpage">pro-innovation bias</a>. </p>
<h4>Using the Algorithms Library</h4>
<p>Again, prodded by changing styles in the C++ world, my next step is to use a standard library algorithm to do the work. We&#8217;re told over and over that turning to the algorithms library allows you to use code that has been optimized to the n-th degree by the clever library teams. </p>
<p>In order to make this work, I have to call an algorithm with a predicate functor, seen below as class <code>sorted_not_equal</code>. Note also that I can&#8217;t use the logical function for this, which would be <code>copy_if()</code>. Why not? The committee forgot to put it in back in 1998, 2003, and 2005, a mistake that was fortunately remedied in C++11. So I have to use the inverse function, <code>remove_copy_if()</code>, and invert the logical sense of my functor:</p>
<pre>
class sorted_not_equal {
    std::string str;
public :
    sorted_not_equal( const std::string &amp; test )
    {
        str = test;
        sort( str.begin(), str.end() );
    }
    bool operator()( std::string test )
    {
        sort( test.begin(), test.end() );
        return ( str != test );
    }
};

template&lt;typename INPUT_ITERATOR, typename OUTPUT_ITERATOR&gt;
void find_matches( std::string rack,
                   INPUT_ITERATOR ii,
                   INPUT_ITERATOR jj,
                   OUTPUT_ITERATOR kk )
{
    std::sort( rack.begin(), rack.end() );
    std::remove_copy_if( ii, jj, kk, sorted_not_equal( rack ) );
}

int main(int argc, char* argv[])
{
    std::ifstream sowpods( &quot;sowpods.txt&quot; );
    find_matches( &quot;etaionsr&quot;,
                  std::istream_iterator&lt;std::string&gt;( sowpods ),
                  std::istream_iterator&lt;std::string&gt;(),
                  std::ostream_iterator&lt;std::string&gt;(std::cout,  &quot;\n&quot; ) );
    return 0;
}
</pre>
<h4>Functors Not So Hot</h4>
<p>So this new approach is supposed to soup up my code by taking advantage of the algorithms that come with the standard library. But if you look around at the code people have been writing for the past 10 years, you&#8217;ll find that this style is pretty common in textbooks and magazine articles, but no so much in the real world.</p>
<p>Why not? Well, it&#8217;s pretty obvious. The generic algorithms in the library need lots of predicate glue to make them useful, and the work to create these predicates is just a pain. My code is almost twice as long, and the functionality that took two lines of code earlier is now bloated into a complete class definition. It pollutes my namespace, takes up a lot of space, and has to be defined somewhere distant from where it is actually used. Not a win.</p>
<p>This is obviously a problem when you look at the history of Linux, C, and C++. An entire family of technologies and infrastructure was developed with the implicit goal of reducing the number of keystrokes programmers had to enter. (I&#8217;m kidding, but only somewhat.) Functors are a step in the wrong direction.</p>
<h4>Lambda to the Rescue</h4>
<p>So it is with much relief that C++11 delivers lambdas, which allow us to write short sweet predicates exactly where we need them, as shown in this C++11 version of the example:</p>
<pre>
template&lt;typename INPUT_ITERATOR, typename OUTPUT_ITERATOR&gt;
void find_matches( std::string rack,
                   INPUT_ITERATOR ii,
                   INPUT_ITERATOR jj,
                   OUTPUT_ITERATOR kk )
{
    std::sort( rack.begin(), rack.end() );
    std::copy_if( ii, jj, kk,
                  [&amp;rack](std::string str) -&gt;bool
                  {
                      std::sort( str.begin(), str.end() );
                      return rack == str;
                  }
                );
}

int main(int argc, char* argv[])
{
    std::ifstream sowpods( &quot;sowpods.txt&quot; );
    find_matches( &quot;etaionsr&quot;,
                  std::istream_iterator&lt;std::string&gt;( sowpods ),
                  std::istream_iterator&lt;std::string&gt;(),
                  std::ostream_iterator&lt;std::string&gt;(std::cout,  &quot;\n&quot; ) );
	return 0;
}
</pre>
<p>Yes, I now have to get used to a new syntax for writing lambda functions &#8211; I think that was unavoidable. But my lambda function is short, it is quite easy to see exactly what it is doing, and it replaces a gangly and awkward functor class. </p>
<p>Best of all, I use the lambda exactly where I need it &#8211; as the predicate parameter to an algorithm used in the standard library. Locality rules.</p>
<h4>Using Lambdas</h4>
<p>Visual C++ 11 provides a great framework for experimenting with lambdas, as they are supporting the 1.1 definition that was ratified as part of the standard. If you want the gory details, I believe the <a href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2009/n2927.pdf" class="newpage">working group&#8217;s</a> proposal has essentially the same wording that went into the standard. For a detailed tutorial, <a href="http://herbsutter.com/2011/05/20/my-lambdas-talk-nwcpp-is-now-online/" class="newpage">Herb Sutter&#8217;s talk</a> can&#8217;t be beat.</p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2012/03/13/visual-studio-11-and-modern-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C++ &#8211; Where&#8217;s the Hate?</title>
		<link>http://marknelson.us/2012/02/27/c-wheres-the-hate/</link>
		<comments>http://marknelson.us/2012/02/27/c-wheres-the-hate/#comments</comments>
		<pubDate>Tue, 28 Feb 2012 05:23:09 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Complaining]]></category>
		<category><![CDATA[Culture]]></category>

		<guid isPermaLink="false">http://marknelson.us/?p=1453</guid>
		<description><![CDATA[One thing I&#8217;ve become accustomed to over the years is that there are a lot of C++ haters. They have their reasons &#8211; some good, some bad &#8211; but they are never afraid of sharing their opinions. An article on Slashdot this week touting the release of the C++11 standard should have been a hotbed [...]]]></description>
			<content:encoded><![CDATA[<p>One thing I&#8217;ve become accustomed to over the years is that there are a lot of C++ haters. They have their reasons &#8211; some good, some bad &#8211; but they are never afraid of sharing their opinions. An <a href="http://developers.slashdot.org/story/12/02/24/1954225/stroustrup-reveals-whats-new-in-c-11" class="newpage">article on Slashdot</a> this week touting the release of the C++11 standard should have been a hotbed of language trash talk &#8211; instead, it was kind of a low key discussion of both the new language features and some retrospection about the language itself. Where have the haters gone?<br />
<span id="more-1453"></span></p>
<h4>The Epitome of C++ Hate</h4>
<p>There really is no better example of C++ hate than the <a href="http://lwn.net/Articles/249460/" class="newpage">screed</a> arising from the poison keyboard of Linus Torvalds back in 2007. One paragraph sums it up well:</p>
<blockquote><p>
C++ is a horrible language. It&#8217;s made more horrible by the fact that a lot of substandard programmers use it, to the point where it&#8217;s much much easier to generate total and utter crap with it. Quite frankly, even if the choice of C were to do *nothing* but keep the C++ programmers out, that in itself would be a huge reason to use C.
</p></blockquote>
<p>Linus asserts that C++ is actually sort of a honey-trap, pulling in the substandard programmers and keeping them out of decent C development.</p>
<p>Reading through the most coherent parts of his rant, it is my opinion that much of his ire is directed against a couple of real problems:</p>
<ul>
<li/>The early adopter&#8217;s fondness for Object Oriented Programming (OOP), which leads to objects everywhere &#8211; and particularly in places where they aren&#8217;t needed.
<li/>The more sophisticated user&#8217;s fondness for advanced library and language features found in places like boost.
<p>My personal observations are obviously very subjective, but I feel like the excesses of OOP peaked back in the early part of the last decade. It&#8217;s been a while since I saw a program were a user did integer math using a type called Integer derived from something called Object.</p>
<p>These days I see OOP being heavily used for large libraries, and used internally in projects where it makes sense. Often &#8220;making sense&#8221; is as simple as using objects for RAII, which is really not OOP, just good resource management. Things like RAII are really just better C &#8211; providing some guarantees on resource usage that can&#8217;t be made when programming in straight C.</p>
<p>And best of all, the boost language features that Linus didn&#8217;t like in 2007 made their way into TR1, and finally into C++11. No longer will you need to worry about rebuilding boost for your compiler every time there is a new release of either product. From this point on, it should just work.</p>
<h4>The Slashdot Crowd</h4>
<p>So instead of seeing a bunch of comments in the vein of Linus&#8217;s, the recent Slashdot article contained virtually no blanket dismissals of C++ as something a sane programmer would use. The negative comments were much more constrained, maybe aimed at a pet peeve instead of the language as a whole:</p>
<blockquote><p>
printf() isn&#8217;t typesafe, but it&#8217;s a fuckton more readable than all that cout formatting stuff. Also, the fact that it&#8217;s not typesafe isn&#8217;t really an issue if you don&#8217;t suck &#8211; trivial unit testing will pretty much show any problems immediately. Besides, gcc/g++ is nice enough to warn you about egregious ones now.
</p></blockquote>
<blockquote><p>
I don&#8217;t think you&#8217;ll see a lot of people flaming C++, there just aren&#8217;t that many people that care one way or the other anymore.</p>
<p>I think some of the new features look nice but mainstream use has been shifting away from C++ for a while and I&#8217;m not sure I see these new features drawing many back&#8230;
</p></blockquote>
<blockquote><p>
Are the features useful? Yes, but they&#8217;re taking a complex language and slapping on yet more functionality. Some new C++ code syntax doesn&#8217;t even *look* like C++ anymore it&#8217;s so different. Not everyone is a C++ guru and the language is bad enough supporting so many different paths to the same implementation outcomes. This is just going to make staffing, testing, training, and code review that much worse trying to juggle yet another barrel-full of C++ &#8220;improvements&#8221;.
</p></blockquote>
<h4>Why the Dropoff?</h4>
<p>I think there are two reasons for the drop in C++ antagonism. </p>
<p>The first is the increasing diversity of languages in use today. Over the past 10 years, the most popular programming languages like Java, C, C++, and VB have all experienced drops in adoption. In 2002, the top four languages might have claimed almost 70% of the <a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html" class="newpage">Tiobe Language Index</a>. Today the top four represent less than 50%. Language selection has been democratized as the bottom tier of the top 10 gains representation in large markets where it makes sense.</p>
<p>In other words, it was a lot easier to focus your wrath on C++ when it represented 20% of the projects out there instead of the 8% of today.</p>
<p>The second is inevitable rationalization of the earlier adopters. C++ programmers in 1998 saw objects behind every tree. In 2003 they were building insanely complicated template metaprogramming frameworks.</p>
<p>All this crazy stuff settles down over the course of a few years as the C++ mainstream settles into paradigms for things that actually work sensibly. Templates for container classes? Sure. Templates for mathematical computation? Uh, no thanks.</p>
<h4>Final Analysis</h4>
<p>There is a lot of nice stuff in C++11, and nothing that is going to take years to figure out. A lot of it will lead to more readable code, and that&#8217;s always good.</p>
<p>The pace of change is pretty comfortable for me. These language features will be rolling out over the next few years, and as they drop into our favorite compilers, we will pick them up quickly.</p>
<p>I, for one, welcome our new ISO overlords.</p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2012/02/27/c-wheres-the-hate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Streambuf Iterators Are a Big Help</title>
		<link>http://marknelson.us/2012/02/05/streambuf-iterators-are-a-big-help/</link>
		<comments>http://marknelson.us/2012/02/05/streambuf-iterators-are-a-big-help/#comments</comments>
		<pubDate>Mon, 06 Feb 2012 00:34:45 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://marknelson.us/?p=1441</guid>
		<description><![CDATA[A few weeks back I was looking at the choice of whether to use iterators or streaming operations for I/O on my data compression code. I was bemoaning the fact that the C++ iterators that perform stream I/O use the insertion and extraction operators, making them unsuitable for binary data compression. It looks like I [...]]]></description>
			<content:encoded><![CDATA[<p>A few weeks back I was <a href="http://marknelson.us/2011/12/24/streams-or-iterators/" class="newpage">looking at the choice</a> of whether to use iterators or streaming operations for I/O on my data compression code. I was bemoaning the fact that the C++ iterators that perform stream I/O  use the insertion and extraction operators, making them unsuitable for binary data compression.</p>
<p>It looks like I spoke too fast.<br />
<span id="more-1441"></span></p>
<h4>The Problem</h4>
<p>The canonical way to use iterators to go over an iostream is to use <code>istream_iterator</code> or its partner, <code>ostream_iterator</code>. In this short sample, I create a binary file with values 0 to 256, then try to read back the data using this iterator type:</p>
<pre>
#include <iostream>
#include <fstream>
#include <iterator>
using namespace std;

int main(int argc, char* argv[])
{
    ofstream temp_out("temp.dat", std::ios_base::binary );
    for ( int i = 0 ; i < 256 ; i++ )
        temp_out.put( (char) i );
    temp_out.close();
    ifstream temp_in("temp.dat", std::ios_base::binary );
    char last = -1;
    int count = 0;
    istream_iterator<char> ii( temp_in );
    while ( ii != std::istream_iterator<char>() ) {
        char c = *ii++;
        count++;
        if ( c != char(last+1) )
            cout << "Error on character number: " << (int) last << endl;
        last = c;
    }
    cout << "Count: " << count << endl;
    return 0;
}
</pre>
<p>Running this program gives the following output:</p>
<pre>
Error on character number: 8
Error on character number: 31
Count: 250
</pre>
<p>Due to the fact that the extraction operator uses whitespace as a delimiter, we get errors trying to read tab, line feed, vertical tab, form feed, carriage return, and space - which means we simply don't see six characters in the input file. This is not too bad when parsing ascii text, but when trying to do compression on binary data, it just won't work.</p>
<p>I figured this was the end of it until reader Fred Jardon chimed in with:</p>
<blockquote><p>
Isn’t istreambuf_iterator the iterator you’re looking for ?</p>
<p><a href="http://cplusplus.com/reference/std/iterator/istreambuf_iterator/" class="newpage">http://cplusplus.com/reference/std/iterator/istreambuf_iterator/</a></p>
<p>It directly reads from the inner streambuf without using the extraction operator.</p>
<p>The opposite iterator exists: ostreambuf_iterator
</p></blockquote>
<p>Well, Fred is quite right, and it only serves to show my lack of depth when it comes to iostreams. The two classes, <code>istreambuf_iterator</code> and <code>ostreambuf_iterator</code> read directly from the underlying buffer, and don't use the extraction operator, which means they don't have the whitespace issues seen above. Changing just two lines fixes the problem:</p>
<pre>
    istreambuf_iterator&lt;char&gt; jj(temp_in.rdbuf());
    while ( ii != std::istreambuf_iterator&lt;char&gt;() ) {
</pre>
<p>Running that I get the pristine output I long for:</p>
<pre>
Count: 256
</pre>
<p>I'd like to tell you the ramifications of diving down into the object and working on the buffer, but I'm afraid this implementation details still elude me. There's a lot more going on in iostreams than in good old <code>&lt;stdio.h&gt;</code>, and half of what I learned about it was wiped out in the standardization process. So be it.</p>
<p>In any case, I think the proper use of <code>istreambuf_iterator<char></code> tips the scale in favor of iterators for me, which reverses my previous thinking. </p>
<p>And many thanks to Fred for straightening out the mess.</p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2012/02/05/streambuf-iterators-are-a-big-help/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I&#8217;m In the Money</title>
		<link>http://marknelson.us/2012/02/01/im-in-the-money/</link>
		<comments>http://marknelson.us/2012/02/01/im-in-the-money/#comments</comments>
		<pubDate>Wed, 01 Feb 2012 16:36:28 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[Data Compression]]></category>
		<category><![CDATA[Humor]]></category>

		<guid isPermaLink="false">http://marknelson.us/?p=1437</guid>
		<description><![CDATA[It looks like all my long years of studying data compression might be ready to pay off: Hello Good Day, This is Troop Emonds With regards to your Company i am sending this email Regards to order some( Compression Machine )I will like to know the type and sizes you have in stock and get [...]]]></description>
			<content:encoded><![CDATA[<p>It looks like all my long years of studying data compression might be ready to pay off:</p>
<blockquote><p>Hello Good Day,</p>
<p>This is Troop Emonds With regards to your Company i am sending this email Regards to order some( Compression Machine )I will like to know the type and sizes you have in stock and get me the sales price of one so that i will tell you the quantity i will be ordering, and if you accept credit card as a form of payment..</p>
<p>Hope to read from you soon about my order request&#8230;&#8230;<br />
With Kind Regards.<br />
Troop</p></blockquote>
<p>I just need to put together some compression machines, and then I&#8217;m set.</p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2012/02/01/im-in-the-money/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Mark&#8217;s Travel Guide to New Zealand</title>
		<link>http://marknelson.us/2012/01/28/marks-travel-guide-to-new-zealand/</link>
		<comments>http://marknelson.us/2012/01/28/marks-travel-guide-to-new-zealand/#comments</comments>
		<pubDate>Sat, 28 Jan 2012 22:37:16 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://marknelson.us/?p=1428</guid>
		<description><![CDATA[I recently spent a little over two weeks touring New Zealand. It was a self-driving trip, which meant we got to cover a lot of ground, although certainly the coverage was very shallow. Before this trip, I had not set foot on foreign soil more than one mile from the US border, so the experience [...]]]></description>
			<content:encoded><![CDATA[<p>I recently spent a little over two weeks touring New Zealand. It was a self-driving trip, which meant we got to cover a lot of ground, although certainly the coverage was very shallow.</p>
<p>Before this trip, I had not set foot on foreign soil more than one mile from the US border, so the experience of going to a foreign country was in itself new. This means I am compelled to share it with you.</p>
<p>Overall New Zealand made me feel very welcome. I would like to move to New Zealand. Barring that, visiting New Zealand as a tourist was a great experience.</p>
<p>I could write a detailed photoblog of our eighteen day journey, but this would be a lot like going to your Uncle&#8217;s house and watching his 90-minute DVD compilation of his trip to Norway &#8211; a bit tedious.</p>
<p>One thing I noticed as a tourist is that it is kind of hard to notice things that are there, but very easy to notice the things that are missing. So my detailed summary of the trip will give you the list of things that I noticed were <i>not</i> in New Zealand &#8211; at least not on my self-driven tour. (I&#8217;d give a link to the fine touring company if they would work out some sort of affiliate program, then you could do it yourself.)</p>
<p><b>Things that don&#8217;t appear to exist in New Zealand:</b></p>
<ul>
<li/>Stop signs
<li/>Iced tea
<li/>Pennies
<li/>Nickels
<li/>Dollar bills
<li/>Insane airport security (domestic only)
<li/>Pickup trucks
<li/>The purported monoculture of sheep
<li/>Moas
<li/>Cosmopolitans, or more generally, grown-up cocktails
<li/>Hot weather
<li/>Air-Conditioned hotel rooms
<li/>Free Wireless
<li/>Reasonably priced Wireless
<li/>Horrifying public restrooms
<li/>4 lane highways
<li/>Any sense of realism about building a country in a volcano/earthquake/tsunami free-fire zone
</ul>
<h4>Pictures and Movies</h4>
<p>A bunch of photos <a href="http://www.flickr.com/photos/snorkel58/sets/72157628844439217/" class="newpage">here</a>.<br/><br />
Some very short videos <a href="http://www.flickr.com/photos/snorkel58/sets/72157628843998801/" class="newpage">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2012/01/28/marks-travel-guide-to-new-zealand/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Visit With Tim Bell</title>
		<link>http://marknelson.us/2012/01/21/a-visit-with-tim-bell/</link>
		<comments>http://marknelson.us/2012/01/21/a-visit-with-tim-bell/#comments</comments>
		<pubDate>Sun, 22 Jan 2012 02:22:50 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Compression]]></category>
		<category><![CDATA[People]]></category>

		<guid isPermaLink="false">http://marknelson.us/?p=1407</guid>
		<description><![CDATA[I was in Christchurch, New Zealand, recently and had a chance to meet Tim for the first time in person. Tim teaches at the <a href=" http://www.canterbury.ac.nz/" class="newpage">University of Canterbury in Christchurch</a>, and is <a href="http://www.cosc.canterbury.ac.nz/tim.bell/" class="newpage">Deputy Head of the Computer Science and Software Engineering</a> department. I got a chance to ask him about his work in data compression as well as one of his new areas of interest, Computer Science education.]]></description>
			<content:encoded><![CDATA[<p><img src="/attachments/2012/bell/TimBell2.jpg" alt="Dr. Timothy Bell" align="right" style="margin-left:15px;border-style:solid;border-width:2px"><br />
In my early years of learning about data compression, the book <a href="http://books.google.com/books/about/Text_compression.html?id=sdZQAAAAMAAJ" class="newpage">Text Compression</a> by Timothy Bell, John Cleary, and Ian Witten was my resource of first resort. I was in Christchurch, New Zealand, recently and had a chance to meet Tim for the first time in person. Tim teaches at the <a href=" http://www.canterbury.ac.nz/" class="newpage">University of Canterbury in Christchurch</a>, and is <a href="http://www.cosc.canterbury.ac.nz/tim.bell/" class="newpage">Deputy Head of the Computer Science and Software Engineering</a> department. I got a chance to ask him about his work in data compression as well as one of his new areas of interest, Computer Science education.<br />
<span id="more-1407"></span></p>
<hr/>
MN: Tim, it seems like there has been a lot of interest in data compression in the Antipodes. Names that come to mind include you, John Cleary, and Peter Fenwick in New Zealand, and Ross Williams in Australia. Is this just coincidence, or is compression in the air down there?</p>
<p>TB: I’ve sometimes wonder about this myself&#8230; during the early days of computing and especially personal computers, it took some time for the latest technology to reach us “down under”, so perhaps we were motivated to get more out of what we had rather than wait some months for a larger disk or new memory to arrive from overseas. When the Internet arrived we started with a very small pipe, so a good compression algorithm could do the equivalent to laying a second cable from NZ to the US – who can resist getting something for free?</p>
<p>MN: Since you wrote Text Compression back in the early 90s, I&#8217;d say the biggest development in lossless compression has been the Burrows-Wheeler transform. Is lossless text compression basically done? Are we left with just incremental improvements as processor resources increase?</p>
<p>TB: That seems to be the case; the only big improvements we’ve seen have turned out to be frauds &#8212; we even had one in NZ recently, where a Nelson man raised NZ$5.3 million for an impressive sounding method; he was <a href="http://www.stuff.co.nz/nelson-mail/news/3892853/Whitley-found-guilty-of-fraud" class="newpage">convicted of fraud</a> last year. The main indicator we have that we’re running out of steam (apart from a lack of new discoveries) is Shannon’s experiments on predicting text which gave a bound in the order of 1 bit per character for English text, and current methods are approaching this. Of course, there’s plenty of room for dealing with new kinds of data (for example, bioinformatics deals with massive amounts of data that we’re still trying to understand) and for finding better data structures and algorithms for performing the compression and decompression. Lossy compression is a whole different story&#8230;</p>
<h4>A Change In Focus</h4>
<p>MN: It looks like you are now dedicating a large amount of your time to establishing computer science as part of the basic curriculum in high school education, for students in the 15-18 age range. In many ways, this is as much a bureaucratic problem as an academic one. What motivated you to take it on?</p>
<p>TB: It’s been a problem that we’ve complained about for decades, and it’s been getting worse and worse as computing in schools has focussed increasingly on using computers and not preparing students to be developers. A lot of this can be attributed to bureaucracy – it’s hard to explain to government officials that putting word processors in every classroom isn’t the same as building a computationally literate society. As a result of some strategic lobbying done by others, a small window of opportunity opened for me to be on a group to advise our Ministry of Education, just over 3 years ago. The group managed to convince the officials that something useful could be done, and then we had to work very quickly to come up with a concrete proposal before the enthusiasm died down.  This has happened rapidly; the advisory group first met in November 2008, and Computer Science started being taught in schools in February 2011.</p>
<p>MN: What have you been able to accomplish in New Zealand so far?</p>
<p>TB: Computer science (including programming, but also topics the involve understanding the importance of things like algorithms, HCI, programming languages and even compression) is currently available as part of computing courses for two of the three final years of our main high school graduation qualification, with all three years being covered from 2013. After that we would expect some of the introductory material to start filtering down to earlier classes, and for wider offerings as teachers become more confident in the subject. One of the biggest challenges has been preparing teachers, few of whom have significant experience in Computer Science. Many have embraced it enthusiastically, and the universities and others have done a lot of work to help them get up to  speed. It’s been a wild ride doing it so quickly, but there have been some very pleasing outcomes.</p>
<p>MN: And how do things look in the rest of the world? Are there any obvious winners and losers at this point? Do you have any concise advice for the world?</p>
<p>TB: Computing in schools is a hot topic around the world; the UK have just announced a strong drive to introduce this sort of material to schools, and the US has people working hard to make it available to students. Israel and Korea have had computer science in schools for some time. We’re learning a lot about what is worth teaching, and what the best pedagogy is for the general classroom (most of our experience is for specialist students who have chosen the subject!) The New Zealand path of getting something going quickly with grass-roots support seems to be more effective than waiting for a top-down approach which could take years to develop and prepare teachers for, although it does make for a bumpy ride as problems are ironed out as we go along!</p>
<p>MN: This might be straying out of your area a bit, but do you see CS in a K-12 education setting having an effect on the representation of women in the STEM fields?</p>
<p>TB: Attitudes that affect representation definitely start at school, and to me the biggest goal of teaching CS in high school is not so much to prepare students for further study, but to enable them to find out what the subject is! School students rarely know what CS is, and even worse, it’s common for them to assume that it must be advanced word processing or some other dull area, and hence they avoid it. It’s particularly important for female students to have the opportunity to find out if it’s something that they might be good at, as the stereotypes associated with computing can make them assume that they shouldn’t consider it as a career.</p>
<p>MN: One final question, Tim. The whole world has seen the devastating damage Christchurch has suffered from the earthquakes in the last year. How has the University of Canterbury held up? Have you managed to maintain continuity in your academic calendar?</p>
<p>TB: It’s been quite a year! Thankfully our university has escaped the brunt of the earthquakes (most of the damage is some distance from the university), and we’ve managed to keep a full programme going despite being closed for three weeks for safety checks. Many students joined the  “student volunteer army”, who helped with the cleanup in the damaged parts of town, and that was probably one of the most valuable experiences of their career! It hasn’t been without disruption as buildings need to be checked carefully, and some are still under repair, but with a bit of resourcefulness we managed to keep going (for a while I even delivered my classes in a restaurant while lecture theatres were being inspected) The city is now going through a massive program of redevelopment with some pretty creative ideas, and it’s an exciting time to be part of these changes.</p>
<hr/>
<p>
<img src="/attachments/2012/bell/New_Zealand.png" alt="New Zealand" align="left" style="margin-right:15px;border-style:solid;border-width:2px">Thanks to Dr. Bell for taking the time to share all this with us. My visit to his amazing homeland was a real treat, and the short time I got to spend with Tim in Christchurch was worth the trip all in itself.</p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2012/01/21/a-visit-with-tim-bell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Streams or Iterators?</title>
		<link>http://marknelson.us/2011/12/24/streams-or-iterators/</link>
		<comments>http://marknelson.us/2011/12/24/streams-or-iterators/#comments</comments>
		<pubDate>Sat, 24 Dec 2011 18:21:11 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Data Compression]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://marknelson.us/?p=1393</guid>
		<description><![CDATA[When I updated my LZW reference code to use the latest C++ features, I abstracted my input and output functions using templates. Data was read and written using the iostreams paradigm, which requires simple classes that implement just a few functions. Would I have been better off using the iterator paradigm instead? The C++ algorithms [...]]]></description>
			<content:encoded><![CDATA[<p>When I updated my <a href="http://marknelson.us/2011/11/08/lzw-revisited/" class="newpage">LZW</a> reference code to use the latest C++ features, I abstracted my input and output functions using templates. Data was read and written using the iostreams paradigm, which requires simple classes that implement just a few functions. Would I have been better off using the iterator paradigm instead? The C++ algorithms library favors that method of processing data, and it can be both elegant and powerful. Which of the two paradigms is the right one for data compression?<br />
<span id="more-1393"></span></p>
<h4>The Conflict</h4>
<p>General purpose data compression routines tend to be used on binary streams of data, either from files or in-memory objects. So what is the best general paradigm for input and output when compressing data? </p>
<p>You might analyze this problem by imagining that you need to write a binary copy routine. </p>
<pre>
template&lt;class INPUT_ITERATOR, class OUTPUT_ITERATOR&gt;
void bcopy( INPUT_ITERATOR input, INPUT_ITERATOR eof, OUTPUT_ITERATOR output )
{
    while ( input != eof )
        *output++ = *input++;
}
</pre>
<p>This routine is particularly nice when you are performing a simple copy using pointers to memory &#8211; the generated code should be really efficient.</p>
<p>However, the iterator paradigm doesn&#8217;t work quite as well when you want to perform a binary copy of data in a file. I can make use of iterators that almost do the job:</p>
<pre>
 std::ifstream in( &quot;input.txt&quot;, std::ios_base::binary );
 std::ofstream out(&quot;output.txt&quot;, std::ios_base::binary );
 bcopy( std::istream_iterator(in),
        std::istream_iterator(),
	std::ostream_iterator(out) );
</pre>
<p>But the bad news is that both <code>istream_iterator</code> and <code>ostream_iterator</code> use the insertion and extraction operators, which are really meant for whitespace-delimited textual data, not binary data. The copy routine shown here will not make a binary byte-for-byte copy of the input file.</p>
<p>So when using files, the stream approach seems to be the way to go:</p>
<pre>
template&lt;class INPUT_STREAM, class OUTPUT_STREAM&gt;
void bcopy( INPUT_STREAM in, OUTPUT_STREAM out )
{
    char c;
    while ( in.get(c) )
        out.put(c);
}
</pre>
<p>If my files have been opened using the <code>iostream</code> classes, you can use this binary copy function without having to write any glue code &#8211; they already support the <code>get</code> and <code>put</code> methods, so this works right out of the box.</p>
<h4>My Choice</h4>
<p>If I&#8217;ve made up my mind that my data compression routine is going to use one of these two paradigms, it means I am going to have to write some glue code. If I choose the iterator-based approach, I need the equivalent of <code>istream_iterator</code> and <code>ostream_iterator</code> for binary files &#8211; and these aren&#8217;t in the standard library. If I choose the stream-based approach, I need efficient <code>put()</code> and <code>get()</code> members for blocks of memory. In some cases <code>basic_stringstream</code> might do the job, but not in all cases.</p>
<p>After dithering around with various solutions, I tentatively opted for the stream paradigm. I found the implementation for various sources of data to be fairly simple, and the interface is easy to understand. I don&#8217;t know if it&#8217;s the perfect choice, and I&#8217;ll keep experimenting, but for now it works for me. My abstraction of the LZW code still needs a lot of work, so it&#8217;s always possible I could rethink this at a later date.</p>
<p>I&#8217;d like to hear your thoughts &#8211; is there an obvious right answer to this question?</p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2011/12/24/streams-or-iterators/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<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[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[<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>Sendmail on Linux &#8211; the Easy Way</title>
		<link>http://marknelson.us/2011/12/09/sendmail-on-linux-the-easy-way/</link>
		<comments>http://marknelson.us/2011/12/09/sendmail-on-linux-the-easy-way/#comments</comments>
		<pubDate>Fri, 09 Dec 2011 16:11:05 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Networking]]></category>

		<guid isPermaLink="false">http://marknelson.us/?p=465</guid>
		<description><![CDATA[This summer I'm teaching a graduate class, Principles of UNIX, which is more or less a crash course in the Mother of All Operating Systems. One of our early topics is email on UNIX, in which I try to impart to the class just how transformative email was back in the day. For early Internet [...]]]></description>
			<content:encoded><![CDATA[<p>This summer I'm teaching a graduate class, Principles of UNIX, which is more or less a crash course in the Mother of All Operating Systems. One of our early topics is email on UNIX, in which I try to impart to the class just how transformative email was back in the day. For early Internet users (mostly UNIX users), this was an incredibly big deal.</p>
<p>Unfortunately, setting up email on a Linux or UNIX system is not quite as automatic as it once was. In our class we use mailx and sendmail as tools to send files from background processes or cron jobs - but mailx will typically not work out of the box. In this post I'll discuss how to get it working on an Ubuntu 11 system.<br />
<span id="more-465"></span></p>
<h4>Things Have changed</h4>
<p>Back in the day if you wanted to send mail, you simply found a handy <a href="http://www.webhostingsearch.com/dedicated-server.php" class="newpage">dedicated server</a> that was accepting incoming SMTP connections. There were thousands, and they were undiscriminating, so this was no big deal.</p>
<p>The invention of <a href="http://en.wikipedia.org/wiki/Spam_(electronic)" class="newpage">spam</a> ruined that. </p>
<p>Now any SMTP server you find is going to require you to go through a bit of a dance in order to authenticate and prove you are not a spammer. I started off this post with the intention of showing you how to use your gmail account to access Google's SMTP servers. The process was fairly arduous, as it involved creating a certificate authority, your own certificates, and then setting up the mail server to use this authentication.</p>
<p>While working on this, my son <a href="http://wlrs.net/" class="newpage">Joey</a> recommended that I just set up a free account on one of several email gateway providers, such as <a href="http://sendgrid.com" class="newage">SendGrid</a> or <a href="http://mailjet.com" class="newpage">MailJet</a>. Both services will let you access their servers and send up to 200 emails a day for free.</p>
<p>I took him up on it and found the process to be much simpler than using gmail, so I'll pass along the setup procedure here.</p>
<h4>Getting an Account</h4>
<p>Obviously, SendGrid is in business to get you to purchase a commercial account so you can send thousands of emails a day from your web site. Accordingly, the don't go out of their way to advertise their free plan. If you go to their <a href="https://Sendgrid.com/pricing.html" class="newpage">pricing page</a>, you will find a little tiny link to the <a href="https://sendgrid.com/user/signup" class="newpage">free plan</a> hidden at the bottom.</p>
<p>Setting up an account is easy, but SendGrid insists that you have a web site. For automatic verification they will need to find your email address on the site. I opted for an alternate provisioning plan in which I created a page on my site with the phrase "Sendgrid". </p>
<p>Once you have an account, you have free access to the SendGrid SMTP servers for up to 200 outbound messages a day. So you are ready to configure your UNIX system to take advantage of it.</p>
<h4>Ubuntu Configuration</h4>
<p>Configuring Ubuntu 11 to send email is fairly painless. Using the Ubuntu Software Center, you can locate and install two packages: postfix and bsd-mailx. During the install of postfix, you will get dropped into a debconf window asking you some basic configuration questions:<br />
<center></p>
<table border="0" width="100%">
<tr>
<td><center><img src="/attachments/2011/smtp/PostfixConfig.png"  width="90%"><center></td>
</tr>
<tr>
<td><center>Figure 1 - The initial configuration screen</center></td>
</tr>
</table>
<p></center><br />
I entered the following answers to the two questions I got hit up with:</p>
<ul>
<li/>General configuration: Internet
<li/>System mail name: dogma.net
</ul>
<p>That seemed to be all I needed for basic configuration.</p>
<h4>Postfix Configuration</h4>
<p>To configure postfix to use SendGrid was just a matter of adding a few lines to /etc/postfix/main.cf, using your SendGrid user name and password. Note that the file probably has an existing <code>relayhost</code> line, this one should replace it:</p>
<pre>
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = static:username:password
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = may
relayhost = [smtp.sendgrid.net]:587
</pre>
<p>After making the changes you should restart postfix so it reads the new config options. I also start watching the mail log file so I can see if there are any problems on first use:</p>
<pre>
sudo /etc/init.d/postfix restart
sudo tail -f /var/log/mail.log
</pre>
<p>A test message sent to my cell phone arrived as a text message in just one or two seconds, with the following log messages:</p>
<pre>
Jun 26 17:02:08 ubuntu postfix/pickup[21145]: 51A5E5E1DA6: uid=1000 from=&lt;mark&gt;
Jun 26 17:02:08 ubuntu postfix/cleanup[21336]: 51A5E5E1DA6: message-id=&lt;20110627000208.51A5E5E1DA6@ubuntu&gt;
Jun 26 17:02:08 ubuntu postfix/qmgr[21146]: 51A5E5E1DA6: from=&lt;mark@dogma.net&gt;, size=273, nrcpt=1 (queue active)
Jun 26 17:02:08 ubuntu postfix/smtp[21338]: 51A5E5E1DA6: to=&lt;xxxxxxxx@txt.att.net&gt;, relay=smtp.sendgrid.net[174.36.32.204]:587, delay=0.33, delays=0.04/0.02/0.23/0.04, dsn=2.0.0, status=sent (250 Delivery in progress)
Jun 26 17:02:08 ubuntu postfix/qmgr[21146]: 51A5E5E1DA6: removed
</pre>
<h4>Moving On to Better Things</h4>
<p>Now that postfix is properly configured, I can really start taking advantage of the mail infrastructure on my system. The next obvious step is to create a <code>.forward</code> in my home directory, and give it my external gmail address. That external address will now be the recipient of output from <code>cron</code> jobs, or from <code>at</code> or <code>batch</code>. It's nice to have the mail set up as in integral part of the O/S, and if you can just make it through a little bit of setup, it's all yours.</p>
<p>With a limit of 200 messages a day you can still make extensive use of outbound email for system monitoring - whether it is via text to your phone or huge messages being sent to an account used for storing log files. Either way, integral email is still a great feature, almost forty years after it first showed up in UNIX.</p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2011/12/09/sendmail-on-linux-the-easy-way/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

