<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>The Brush Blog</title>
	
	<link>http://blog.brush.co.nz</link>
	<description>software, electronics, web</description>
	<pubDate>Fri, 10 Jul 2009 03:25:46 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/micropledge/blog" type="application/rss+xml" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">micropledge/blog</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://fusion.google.com/add?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2Fmicropledge%2Fblog" src="http://buttons.googlesyndication.com/fusion/add.gif">Subscribe with Google</feedburner:feedFlare><item>
		<title>Easy IP-to-country lookup in Python</title>
		<link>http://blog.brush.co.nz/2009/07/geoip/</link>
		<comments>http://blog.brush.co.nz/2009/07/geoip/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 03:25:46 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
		
		<category><![CDATA[news]]></category>

		<guid isPermaLink="false">http://blog.brush.co.nz/?p=391</guid>
		<description><![CDATA[We&#8217;re branching into the U.S. market with our wedding registry website, Gifty. So first we grabbed a .com domain name, but we also had to make sure the price shows correctly in USD or NZD depending on where you&#8217;re from.

There are a number of tools available for geo-locating someone based on their IP address, including [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re branching into the U.S. market with our wedding registry website, <a href='http://giftyweddings.com/'>Gifty</a>. So first we grabbed a <code>.com</code> domain name, but we also had to make sure the price shows correctly in USD or NZD depending on where you&#8217;re from.</p>

<p>There are a number of tools available for geo-locating someone based on their IP address, including some free ones. <a href='http://www.maxmind.com/'>MaxMind</a> is pretty popular and nice to use, and their free <a href='http://www.maxmind.com/app/geolitecountry'>GeoLite Country</a> database did the trick for me.</p>

<p>Gifty runs on Python, so I wanted something I could just use in pure Python. It turns out that <a href='http://code.google.com/p/pygeoip/'>pygeoip</a> is a nice Python replacement for MaxMind&#8217;s C-based API.</p>

<p>However, I was only interested in the country-code lookup, so I decided to strip it down and release the two-pages-of-Python version I&#8217;m using. Just grab MaxMind&#8217;s <a href='http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz'>database</a> and put the code in Python&#8217;s <code>Lib/site-packages</code> directory:</p>

<p style='text-align: center; font-size: 150%; font-weight: bold;'><a href='http://blogstatic.micropledge.com/2009/07/geoip.py.txt'>get geoip.py</a></p>

<p>And then to use it, simply type:</p>

<pre class='prettyprint'><code>&gt;&gt;&gt; import geoip
&gt;&gt;&gt; geoip.country('202.21.128.102')
'NZ'
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.brush.co.nz/2009/07/geoip/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Blast from the demoscene past</title>
		<link>http://blog.brush.co.nz/2009/06/scene/</link>
		<comments>http://blog.brush.co.nz/2009/06/scene/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 22:03:17 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
		
		<category><![CDATA[news]]></category>

		<guid isPermaLink="false">http://blog.brush.co.nz/?p=358</guid>
		<description><![CDATA[Do you like the demoscene? Or do you just like smaller, faster, or embedded code? Read on.

When I was 14, I started learning how to program, for at least two reasons:


  My dad could code, and he made some neat stuff, like a code-generating pentomino puzzle solver and a really tiny pop-up editor.
  [...]]]></description>
			<content:encoded><![CDATA[<p><i>Do you like the demoscene? Or do you just like smaller, faster, or embedded code? Read on.</i></p>

<p>When I was 14, I started learning how to program, for at least two reasons:</p>

<ul>
  <li>My dad could code, and he made some neat stuff, like a code-generating <a href="http://www.complang.tuwien.ac.at/forth/pentomino.fs">pentomino puzzle solver</a> and a <a href="http://blog.brush.co.nz/2008/06/snappy-software/#comment-2442">really tiny pop-up editor</a>.</li>
  <li>Because I really liked the <a href="http://en.wikipedia.org/wiki/Demoscene">demoscene</a>.</li>
</ul>

<p>Scratch that. I <i>still like</i> the demoscene. I mean, who else can make incredible 3D <a href="http://www.pouet.net/prod.php?which=3397">tube</a> or <a href="http://www.pouet.net/prod.php?which=4659">lattice</a> demos in a <b>256-byte executable?</b> Try them &#8212; both still run fine under Windows XP.</p>

<p><a href="http://blogstatic.micropledge.com/2009/06/phyure_stars.zip"><img class="border right" src="http://blogstatic.micropledge.com/2009/06/phyure_stars.png" width="195" height="158" alt="Fire effect and starfield" title="Click to download the source code for my old fire effect and starfield" /></a>So I read diskmags and tutes to learn how to program the VGA hardware, push pixels to <code>0xA000:0000</code>, and use <a href="http://en.wikipedia.org/wiki/Mode_X">Mode X</a>. Oh, and I learnt about <code>sin</code> and <code>cos</code> before I learnt at school &#8212; for basic 2D and 3D rotation. Then there were effects: the fire effect, plasma, starfields, wormholes, etc, etc. (Click on the piccy to the right to download some of my old source.)</p>

<p>Anyway, back from <a href="http://www.youtube.com/watch?v=8G_aUxbbqWU">Second Reality</a> to the real thing &#8230;</p>

<p>As I&#8217;ve noted before, I&#8217;m <a href="http://blog.brush.co.nz/2008/07/adobe-reader-9/">not exactly in favour</a> of bloatware. But in today&#8217;s &#8220;a GB here, a GB there&#8221; world, <b>is small still beautiful?</b> I think so, for two reasons:</p>

<h4>Embedded programming</h4>

<p>In the embedded world, size still matters a lot. Microcontrollers are getting bigger and faster, sure, but in electronic products there&#8217;s often a place for the small ones (say 64KB flash, 2KB RAM). Just the other day, I cut our code size by 900 bytes, which was a significant percentage of the total &#8212; less code to download, test, and maintain.</p>

<p>And it&#8217;s not only important for small micros, but also to limit download time and cost for in-field updates. If you want to update code for 1000 units over a fairly slow and costly radio link, small is good.</p>

<p><b>Binary diffs</b> or deltas are really good for this. My brother Berwyn has developed a proof-of-concept binary diffing algorithm which is designed for tiny embedded systems &#8212; <a href="http://brush.co.nz/contact">contact us</a> if you&#8217;re keen to hear more.</p>

<p>Binary diffing isn&#8217;t new, of course &#8212; <a href="http://www.daemonology.net/bsdiff/">bsdiff</a> already does something similar for Firefox&#8217;s updates, so you only need to download a small update. But bsdiff doesn&#8217;t work on small embedded systems, because it uses a compression program which requires a fair amount of RAM (bzip2).</p>

<h4>To go fast, do less</h4>

<p>Yep, as <a href="http://asserttrue.blogspot.com/2009/03/how-to-write-fast-code.html">the guy said</a>: <i>To go fast, do less.</i></p>

<p>And KISS. Keeping it Short and Simple means less code to test, and if you&#8217;re using basically the right approach and algorithm, it usually also means <i>faster</i> code. And to follow my own advice, I&#8217;m keeping this section short.</p>

<h4>Conclusion</h4>

<p>In a word, if you&#8217;re a budding hacker, or the parent of a budding hacker, teach them that small is still beautiful. And get &#8216;em started with the demoscene. There is still a pretty active &#8217;scene community, and here are some starting points:</p>

<ul>
 <li><a href="http://en.wikipedia.org/wiki/Demoscene">Wikipedia article on the demoscene</a></li>
 <li><a href="http://www.pouet.net/">Pouet.net</a>, a great place to download popular demos</li>
 <li><a href="http://www.scene.org/">Scene.org</a>, another good &#8217;scene resource</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.brush.co.nz/2009/06/scene/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Pilot ships through Google Earth</title>
		<link>http://blog.brush.co.nz/2009/05/ships/</link>
		<comments>http://blog.brush.co.nz/2009/05/ships/#comments</comments>
		<pubDate>Thu, 21 May 2009 04:35:02 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
		
		<category><![CDATA[news]]></category>

		<guid isPermaLink="false">http://blog.brush.co.nz/?p=351</guid>
		<description><![CDATA[Paul van Dinther, one of the folks we work with, has released a ship simulation game that uses Google Earth for its &#8220;terrain&#8221; data:


PlanetInAction.com released a new simulation game called Ships. In Ships you take the helm from a choice of 3D ships. What is special about this game is that it makes use of [...]]]></description>
			<content:encoded><![CDATA[<p>Paul van Dinther, one of the folks we work with, has released a ship simulation game that uses Google Earth for its &#8220;terrain&#8221; data:</p>

<blockquote>
<p><i><a href="http://planetinaction.com/">PlanetInAction.com</a> released a new simulation game called <a href="http://planetinaction.com/ships.php">Ships</a>. In Ships you take the helm from a choice of 3D ships. What is special about this game is that it makes use of the rich 3D data present in Google Earth. The entire world is your playground.</i></p>
<p><i>&#8220;Ships&#8221; is a graphically rich environment with intricate visual effects that runs right inside your web-browser. All you need is a small Google Earth plugin. Take control now of the majestic Queen Mary 2 and hit the authentic fog horn as you leave the port of Rotterdam in the Netherlands. If water is not your thing then why not climb aboard the airship Hindenburg and check-out the Swiss alps.</i></p>
</blockquote>

<p>Have fun sailing around the (real) world!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.brush.co.nz/2009/05/ships/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Python’s Ellipsis explained</title>
		<link>http://blog.brush.co.nz/2009/05/ellipsis/</link>
		<comments>http://blog.brush.co.nz/2009/05/ellipsis/#comments</comments>
		<pubDate>Fri, 08 May 2009 03:54:53 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
		
		<category><![CDATA[news]]></category>

		<guid isPermaLink="false">http://blog.brush.co.nz/?p=333</guid>
		<description><![CDATA[As others note, using Python&#8217;s Ellipsis object in slices is rather obscure, and there are hardly any good examples out there. So I thought I&#8217;d do my bit with a nice, simple example, complete with comments:

# Portable way to get the You-Know-Which object without naming it
class __:
 def __getitem__(__, _):
  return _
___ = __()[...]

# [...]]]></description>
			<content:encoded><![CDATA[<p>As <a href="http://stackoverflow.com/questions/118370/how-do-you-use-the-ellipsis-slicing-syntax-in-python">others note</a>, using Python&#8217;s <code>Ellipsis</code> object in slices is rather obscure, and there are hardly any good examples out there. So I thought I&#8217;d do my bit with a nice, simple example, complete with comments:</p>

<pre class='prettyprint'><code># Portable way to get the You-Know-Which object without naming it
class __:
 def __getitem__(__, _):
  return _
___ = __()[...]

# An Ellipsobinary-to-ASCII convertor
class __:
 def __getitem__(__, _):
  return chr(sum(1&lt;&lt;i if _[-i-1] is ___ else 0 for i in range(len(_))))
_ = __()

# Finally, use the That-Which-Must-Not-Be-Named object
print (
 _[...,_,_,...,_,_,_] +
  _[...,...,_,_,...,_,...] +
   _[...,...,_,...,...,_,_] +
    _[...,...,_,...,...,_,_] +
     _[...,...,_,...,...,...,...] +
      _[...,_,...,...,_,_] +
       _[...,_,_,_,_,_] +
        _[...,...,...,_,...,...,...] +
         _[...,...,_,...,...,...,...] +
          _[...,...,...,_,_,...,_] +
           _[...,...,_,...,...,_,_] +
            _[...,...,_,_,...,_,_] +
             _[...,_,_,_,_,...])
</code></pre>

<p>And you thought Python code couldn&#8217;t be <a href="http://en.wikipedia.org/wiki/IOCCC">obfuscated</a>?</p>

<p>Seriously, though &#8230; do any non-<a href="http://www.scipy.org/Tentative_NumPy_Tutorial#line-487">NumPy</a> programmers actually use <code>Ellipsis</code>? I have yet to find other (non-NumPy) uses of it in the wild.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.brush.co.nz/2009/05/ellipsis/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Knuth, goto, Python, and OOP</title>
		<link>http://blog.brush.co.nz/2009/04/knuth/</link>
		<comments>http://blog.brush.co.nz/2009/04/knuth/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 07:21:39 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
		
		<category><![CDATA[news]]></category>

		<guid isPermaLink="false">http://blog.brush.co.nz/?p=310</guid>
		<description><![CDATA[The year was 1973. Real programmers were still using punch-cards and Pascal. C had barely been invented, and object-oriented programming was hardly a twinkle even in the eyes of top computer scientists. Whether or not to use goto was the hot topic of the day.

Knuth

It was then that Donald Knuth wrote his famous essay Structured Programming [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.amazon.com/Literate-Programming-Center-Language-Information/dp/0937073806"><img class="border right" src="http://blogstatic.micropledge.com/2009/04/lp.jpg" alt="Literate Programming by Donald Knuth" width="150" height="224" /></a>The year was 1973. Real programmers were still using punch-cards and Pascal. C had barely been invented, and object-oriented programming was hardly a twinkle even in the eyes of top computer scientists. Whether or not to use <code>goto</code> was the hot topic of the day.</p>

<h4>Knuth</h4>

<p>It was then that Donald Knuth wrote his famous essay <i>Structured Programming with go to Statements</i>. And some essay it is: he covers everything from the current trends on structured programming to premature optimization being the root of all evil. (I read it in <a href="http://www.amazon.com/Literate-Programming-Center-Language-Information/dp/0937073806"><i>Literate Programming</i></a>, but it&#8217;s also available as a <a href="http://pplab.snu.ac.kr/courses/adv_pl05/papers/p261-knuth.pdf">scanned PDF here</a>.)</p>

<p>He&#8217;s responding to Edsger Dijkstra&#8217;s well-known letter <a href="http://ctp.di.fct.unl.pt/lei/lap/papers/e.w.%20dijkstra%20[1968]%20-%20go%20to%20statement%20considered%20harmful.pdf"><i>Go To Statement Considered Harmful</i></a>, but (in typical Knuth fashion) he covers so much related ground it&#8217;s not funny.</p>

<p>What was striking to me is the <i>context</i> of his discussion. It&#8217;s clear that structured programming &#8212; which we use every day and think is &#8220;common sense&#8221; &#8212; had to be invented, discussed, and refined. Like most inventions, it&#8217;s obvious &#8230; 36 years later.</p>

<h4>goto</h4>

<p>It&#8217;s also obvious that <code>goto</code> (or &#8220;go to&#8221; as Knuth calls it) was much more widely used and abused than it is today. This is probably partly because assembly language was so much more common, but also because &#8220;they&#8221; had to learn that <code>goto</code> isn&#8217;t usually the right abstraction &#8212; in fact, it isn&#8217;t much of an abstraction at all.</p>

<p>Now it&#8217;s 2009, and <code>goto</code> is pretty rare. It&#8217;s still used, of course, but I&#8217;ve usually seen it only in the cases Knuth is talking about: for efficiency, error exits, and for breaking out of certain kinds of loops.</p>

<p>In C you still occassionally need it for cleaning up before error exits, or for breaking out of efficient nested loops, or in generated code, but these days we also have other constructs and other languages that solve 1973&#8217;s problems most of the time.</p>

<p>In C, you have the invaluable <code>break</code> as well as the ability to <code>return</code> early. Knuth advocated the equivalent of C&#8217;s <code>break</code>, implying also that most languages at the time didn&#8217;t have it.</p>

<p>Compilers are also somewhat better at producing optimized code from non-<code>goto</code>ed source: for example, I can program my virtual machine&#8217;s opcode dispatcher as a bunch of <code>case</code> statements, knowing the compiler will probably optimize it into a jump table.</p>

<p>And in most modern high-level languages (C++ and up) you have <i>exceptions</i>, which eliminate the need for error-exit <code>goto</code>s, as well as solve several other problems in a really tidy way.</p>

<h4>Python</h4>

<p>Python is important in this discussion not only because Knuth is keen on beautiful code, but because Knuth &#8220;predicted&#8221; its arrival in several different ways. Here&#8217;s a quote from the last section of his essay:</p>

<blockquote>
<p><i>It seems clear that languages somewhat different from those in existence today would enhance the preparation of structured programs. We will perhaps eventually be writing only small modules which are identified by name as they are used to build larger ones, so that <b>devices like indentation, rather than delimiters,</b> might become feasible for expressing local structure in the source language.</i></p>
</blockquote>

<p>Of course, many languages now have &#8220;small modules which are identified by name as they are used to build larger ones&#8221;, but Python really took Knuth seriously about using indentation as a delimiter.</p>

<p>What&#8217;s more, you can always <a href="http://entrian.com/goto/">add <code>goto</code> to Python</a> if you really need it. :-)</p>

<h4>OOP</h4>

<p>And it gets even more interesting when he goes on to say:</p>

<blockquote>
<p><i>Although our examples don&#8217;t indicate this, it turns out that a given level of abstraction often involves <b>several related routines and data definitions;</b> for example, when we decide to represent a table in a certain way, we simultaneously want to specify the routines for storing and fetching information from that table. The next generation of languages will probably take into account such related routines.</i></p>
</blockquote>

<p>Correct me if I&#8217;m wrong, but doesn&#8217;t that sound awfully like OOP? So in a single essay apparently about <code>goto</code> statements, Knuth predicted modules, Python&#8217;s use of indentation as delimiters, and object-oriented programming. :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.brush.co.nz/2009/04/knuth/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A browser-agnostic plugin system?</title>
		<link>http://blog.brush.co.nz/2009/03/proxymoron/</link>
		<comments>http://blog.brush.co.nz/2009/03/proxymoron/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 08:51:16 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
		
		<category><![CDATA[news]]></category>

		<guid isPermaLink="false">http://blog.brush.co.nz/?p=265</guid>
		<description><![CDATA[[Update: I've wrongly been calling extensions "plugins". So this post is actually talking about extensions, not plugins.]

Lately I&#8217;ve been browsing with Google Chrome.It looks and works great, and it&#8217;s quick. But it doesn&#8217;t have plugins.

One of the few plugins I can hardly do without is the Blank Canvas Gmail Signatures add-on. Gmail doesn&#8217;t allow a [...]]]></description>
			<content:encoded><![CDATA[<p><small><i>[Update: I've <a href="http://news.ycombinator.com/item?id=514671">wrongly been calling</a> extensions "plugins". So this post is actually talking about extensions, not plugins.]</i></small></p>

<p><img class="right" style="margin-left: 30px;" src="http://blogstatic.micropledge.com/2009/03/chrome-plugins.jpg" width="175" height="190" alt="Chrome: Where are my plugins?" title="Oh where ... are my plugins?" />Lately I&#8217;ve been browsing with <a href="http://www.google.com/chrome">Google Chrome</a>.<br/>It looks and works great, and it&#8217;s quick. <i>But it doesn&#8217;t have plugins.</i></p>

<p>One of the few plugins I can hardly do without is the <a href="http://blankcanvasweb.com/pages/id_9/n_gmail_signatures/">Blank Canvas Gmail Signatures</a> add-on. Gmail doesn&#8217;t allow a signature per email address, which is a pity, but this add-on handles that nicely. It also allows you to use HTML in your signatures.</p>

<p>Anyway, while cycling to work the other day I thought of an idea for <b>a plugin system that would work in all browsers</b>. My idea was a web proxy running on your local machine that injects HTML or JavaScript into pages from certain URLs, <a href="http://www.greasespot.net/">Greasemonkey</a>-style. With some clever regex-matching and JavaScript injection you could do just about whatever you needed.</p>

<p>But of course &ndash; a deflated ego later &ndash; similar things have already been done. :-)</p>

<h4>Privoxy</h4>

<p>First, I searched for &#8220;browser-agnostic plugin manager&#8221; and discovered <a href="http://www.privoxy.org/">Privoxy</a>. It&#8217;s geared for privacy and blocking ads, but it can do content replacement based on regexes. For instance, there&#8217;s a built-in filter called <code>tiny-textforms</code> that makes your <code>&lt;textarea&gt;</code>s bigger, a bit like <a href="https://addons.mozilla.org/en-US/firefox/addon/3818">this Firefox plugin</a>.</p>

<p>Privoxy looks good and is pretty complete, but it&#8217;s not really geared for plugins (though one of the Privoxy developers <a href="http://osdir.com/ml/web.privoxy.user/2006-11/msg00027.html">mentioned</a> you could probably do Greasemonkey-like stuff with it). Also, Privoxy&#8217;s got a pretty nerdy configuration interface that just wouldn&#8217;t cut it for normal plugin users.</p>

<h4>Proxomitron, Proximodo and BFilter</h4>

<p><a href="http://proxomitron.info/">Proxomitron</a>, <a href="http://proximodo.sourceforge.net/">Proximodo</a> and <a href="http://bfilter.sourceforge.net/">BFilter</a> are three similar web proxy filters that might well do the job. Again, they&#8217;re mainly used for blocking ads, and their GUIs aren&#8217;t really set up as &#8220;plugin managers&#8221;, so I&#8217;m not sure they&#8217;d work for ordinary users.</p>

<h4>Plugins in Chrome</h4>

<p>Google is <a href="http://dev.chromium.org/developers/design-documents/extensions">talking seriously</a> about extension support in Chrome &ndash; perhaps it&#8217;ll happen in the next few months.</p>

<p><i>Stop press!</i> Apparently Chromium already supports <a href="http://dev.chromium.org/developers/design-documents/extensions/userscripts">user scripts</a> &ndash; again, Greasemonkey-style) &ndash; but only in the <a href="http://dev.chromium.org/developers/design-documents/user-scripts">latest developer builds</a>.</p>

<h4>Greasemetal</h4>

<p>In the meantime, there&#8217;s also <a href="http://greasemetal.31tools.com/">Greasemetal</a>, which is a Greasemonkey-compatible tool that uses Chrome&#8217;s inter-process <code>AutomationProxy</code> communications channel. So it&#8217;s not browser-agnostic, but good work Kazuho Oku! I&#8217;ll probably start using this if Chrome doesn&#8217;t hurry up with their extension interface.</p>

<h4>But what about browser-agnostic?</h4>

<p>But back to <i>browser-agnostic</i>. Imagine if all your favourite plugins or Greasemonkey scripts worked in IE, Firefox, Safari, Opera, Chrome, <i>and</i> Lynx.</p>

<p>I don&#8217;t think it would take too much hacking to package up Privoxy as a plugin manager with a decent UI, or perhaps turn it into a browser-agnostic version of Greasemonkey. </p>

<p><b>Any takers, hackers?</b></p>

<p>Or maybe I&#8217;ll get around to it someday. I&#8217;ve got a name for my plugin manager already: Proxymoron.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.brush.co.nz/2009/03/proxymoron/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Some regular expression libraries</title>
		<link>http://blog.brush.co.nz/2009/02/regex-libs/</link>
		<comments>http://blog.brush.co.nz/2009/02/regex-libs/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 22:55:33 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
		
		<category><![CDATA[news]]></category>

		<guid isPermaLink="false">http://blog.brush.co.nz/?p=248</guid>
		<description><![CDATA[While I&#8217;m on the lookout for small libraries anyway, I thought I&#8217;d also post a list of a few regular expression libraries I&#8217;ve found recently (again, for possible use in an embedded Linux system).

So if you need a small or medium sized regex library for one of your projects, here&#8217;s a start:


SLRE (Super Light Regular [...]]]></description>
			<content:encoded><![CDATA[<p>While I&#8217;m on the <a href="http://blog.brush.co.nz/2009/02/inih/">lookout for small libraries</a> anyway, I thought I&#8217;d also post a list of a few regular expression libraries I&#8217;ve found recently (again, for possible use in an embedded Linux system).</p>

<p>So if you need a small or medium sized regex library for one of your projects, here&#8217;s a start:</p>

<ul>
<li><a href="http://slre.sourceforge.net/">SLRE (Super Light Regular Expression library)</a>: 15KB of C, but still a decent subset of Perl regexes. Simple API, doesn&#8217;t use heap.</li>
<li><a href="http://tiny-rex.sourceforge.net/">T-Rex</a>: Again, very light at 17KB of C, but good subset of Perl regexes. Uses heap, has a C++ wrapper class.</li>
<li><a href="http://laurikari.net/tre/">TRE</a>: C, lightweight but powerful and POSIX-compliant, looks very good.</li>
<li><a href="http://www.pcre.org/">PCRE</a>, C (with C++ wrapper), heavyish, fully-fledged, Unicode support, industry-standard (used in Apache etc).</li>
<li><a href="http://www.boost.org/doc/libs/1_38_0/doc/html/xpressive.html">Boost.Xpressive</a>: C++ header only, heavyish, API okay, quite hard to extract from Boost.</li>
<li><a href="http://www.boost.org/doc/libs/1_38_0/libs/regex/doc/html/index.html">Boost.Regex</a>: C++, heavyish, API looks good, hard to extract from Boost.</li>
</ul>

<p>Being a <a href="http://blog.brush.co.nz/2008/07/adobe-reader-9/">bloat despiser</a> myself, I love the idea of using just 15KB of source code to handle your <a href="http://www.perl.com/pub/a/2002/06/04/apo5.html">regexen</a>. If you need something more powerful, however, TRE does look very good. Somehow reminds me of <a href="http://bellard.org/tcc/">TCC (Tiny C Compiler)</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.brush.co.nz/2009/02/regex-libs/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Cracking an INI file with a jackhammer</title>
		<link>http://blog.brush.co.nz/2009/02/inih/</link>
		<comments>http://blog.brush.co.nz/2009/02/inih/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 09:46:41 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
		
		<category><![CDATA[news]]></category>

		<guid isPermaLink="false">http://blog.brush.co.nz/?p=190</guid>
		<description><![CDATA[All I was after was a simple .INI file reader in C or C++. You know, to parse [section] and name=value lines for config files. We needed it for an embedded Linux project, so it had to be small, portable and only dependent on the C and C++ standard libraries.

Maybe it&#8217;s my embedded background that [...]]]></description>
			<content:encoded><![CDATA[<p><img class="right border" style="margin-top: 0px;" src="http://blogstatic.micropledge.com/2009/02/ini.png" alt="INI File Snippet" title="A snippet of a win.ini file" width="155" height="142" />All I was after was a simple <a href="http://en.wikipedia.org/wiki/INI_file">.INI file</a> reader in C or C++. You know, to parse <code>[section]</code> and <code>name=value</code> lines for config files. We needed it for an embedded Linux project, so it had to be small, portable and only dependent on the C and C++ standard libraries.</p>

<p>Maybe it&#8217;s my embedded background that makes me defensive about my mild case of Not-Invented-Here syndrome. Then again, perhaps I&#8217;m in <a href="http://www.joelonsoftware.com/articles/fog0000000007.html">good company</a>.</p>

<p>But surely there are tons of INI parsing libraries around, right? Well, kind of.</p>

<p>I found no fewer than 15 in under an hour. But why is it that things like this are either way off the mark (bloated or non-portable) or they&#8217;re very close, but just not <i>quite</i> what you want?</p>

<p>Below is the list of INI file readers that I found, from bad to better. Some of the ones that weren&#8217;t for me might well suit your application, so treat this as something of <b>a non-exhaustive list of INI file readers</b>:</p>

<h4>Not for me</h4>

<ul>
<li><a href="http://student.agh.edu.pl/~koshmaar/SDL_Config/news.php">SDL_Config</a>: Rather bloated &#8230; somehow 246KB of source code just to read an INI file doesn&#8217;t do it for me. I also admit to being a bit icked out by five levels of pointer indirection, for example: <code>file-&gt;internal_file-&gt; global_group.multi_value_iterator = ...</code></li>
<li><a href="http://c.snippets.org/code/ini.c">ini.c by Carey Bloodworth</a>: Small and in C, but has a funny API and is ripe with buffer overrun goodness.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/ms724353(VS.85).aspx">GetPrivateProfileString API</a>: Fine API, but of course Win32 only.</li>
<li><a href="http://www.codeproject.com/KB/cpp/IniReader.aspx">CIniReader by Xiangxiong Jian</a>: C++ class, nice and simple, but Win32 only (uses <code>GetPrivateProfileString</code>).</li>
<li><a href="http://www.codeproject.com/KB/cpp/CIniFile.aspx">CIniFile by Ludvik Jerabek</a>: C++ class, but uses loads of non-portable MFC helper classes. <i>Update: Ludvik&#8217;s now added a portable version to this page.</i></li>
</ul>

<h4>Half way there</h4>

<ul>
<li><a href="http://code.jellycan.com/simpleini/">SimpleIni by Brodie Thiesfield</a>: C++, cross platform, and not really bad, but it just shows that &#8220;simple&#8221; is in the eye of the beholder. Does one really want 110KB of heavy duty, template-driven C++ code just to read an INI file?</li>
<li><a href="http://www.codeproject.com/KB/files/config-file-parser.aspx">Config by freejack</a>: Tantalising &#8230; a C++ class with a nice API, only 5KB of code (as it should be), and optional environment variable expansion. The problem? What it parses are not quite INI files, but his own special &#8220;structured config files&#8221;. Which is all very nice, but not exactly INI-compatible. (Admittedly this could easily be hacked into an INI reader.)</li>
<li><a href="http://www.boost.org/doc/libs/1_36_0/doc/html/program_options.html">Boost.Program_options</a>: C++, and not bad code, but it&#8217;s more of a fully-fledged &#8220;program configuration library&#8221; than just an INI file reader. Plus, we weren&#8217;t yet sure we wanted a dependency on Boost.</li>
<li><a href="http://www.codeproject.com/KB/tips/ini_file_parser_spirit.aspx">CIniFile by Silviu Simen</a>: C++ class, a little better, but relies on the Boost.Spirit parsing library.</li>
<li><a href="http://sourceforge.net/projects/minip/">M&#8217;s INI parser</a>: C++, not bad, small, portable and uses the STL, but relies on the <a href="http://re2c.org/">re2c scanner generator</a>, giving it another dependency and making the code harder to read (and modify).</li>
</ul>

<h4>So very close</h4>

<ul>
<li><a href="http://www.compuphase.com/minini.htm">minIni by CompuPhase</a>: So very close, does just what you want (also writes INI files), and in only 17KB of portable C source, great for embedded systems &#8230; but re-reading the INI file for every <code>name=value</code> pair you need somehow just doesn&#8217;t sit right.</li>
<li><a href="http://www.codeguru.com/cpp/data/data-misc/inifiles/article.php/c4583/">CDataFile by Gary McNickle</a>: This one looks decent &#8212; small, C++, uses the STL instead of MFC or its own fancy dictionary type &#8230; I probably would have used this had I seen it sooner.</li>
<li><a href="http://it.bmc.uu.se/andlov/proj/libinifile/">libinifile by Anders Lövgren</a>: This one&#8217;s quite good too. Minimal, portable, low memory footprint, but a slightly unusual API (partly to give it the low memory footprint). I might well have used this one too, had I found it sooner.</li>
<li><a href="http://sourceforge.net/projects/libini">libini by Simon White</a>: Plain C, SWIGgable, fairly small, though has a bit of an odd API (and I&#8217;m struggling to see why a simple INI file reader needs a 665KB configuration script :-).</li>
<li><a href="http://ndevilla.free.fr/iniparser/">iniParser by Nicolas Devillard</a>: This is the one we ended up using. Small (about 32KB of source) and fast. The only minor drawback is that it implements its own dictionary type in C, and we&#8217;re using the STL which already has one. (Still, in C, what else can you do?) Also, it looks like it&#8217;s been around forever and is well tested.</li>
</ul>

<h4>INI Not Invented Here (INIH)</h4>

<p>Of course, in the time it took to investigate all these, I could have easily written my own. And, being unable to help myself, I did. :-) So I present you with my own offering: <b>INI Not Invented Here</b>, a.k.a. <b>INIH</b> or simply <code>ini.h</code>.</p>

<p>It contains a small C function, <code>ini_parse()</code>, that parses an INI file and executes the given callback function for each <code>name=value</code> pair parsed (think <a href="http://en.wikipedia.org/wiki/Simple_API_for_XML">SAX</a>).</p>

<p>The reason I used the callback style is so you don&#8217;t have to load the whole file into memory if you don&#8217;t need to &#8212; good for embedded systems. Plus, I wanted to be able to use the parser easily in C, but not implement a dictionary-like structure in C.</p>

<p>For a more user-friendly, lookup style API if you&#8217;re using C++, I&#8217;ve wrapped <code>ini_parse()</code> in a class called <code>INIReader</code>, which has <code>Get()</code> and <code>GetInteger()</code> methods (all I needed). And it&#8217;s easy to sub-class <code>INIReader</code> if you need fancier <code>GetXYZ()</code> functions.</p>

<h4>Show us the code</h4>

<p>For what it&#8217;s worth, I hereby put my code into the public domain.</p>

<p>The C code: <a href="http://blogstatic.micropledge.com/2009/02/ini.h.txt">ini.h</a>, <a href="http://blogstatic.micropledge.com/2009/02/ini.c.txt">ini.c</a>, <a href="http://blogstatic.micropledge.com/2009/02/ini_dump.c.txt">ini_dump.c</a>, <a href="http://blogstatic.micropledge.com/2009/02/test.ini.txt">test.ini</a></p>

<p>The C++ code: <a href="http://blogstatic.micropledge.com/2009/02/INIReader.h.txt">INIReader.h</a>, <a href="http://blogstatic.micropledge.com/2009/02/INIReader.cpp.txt">INIReader.cpp</a>, <a href="http://blogstatic.micropledge.com/2009/02/INIReaderTest.cpp.txt">INIReaderTest.cpp</a></p>

<p>Or get it all in a single 5KB zip file: <a href="http://blogstatic.micropledge.com/2009/02/ini.zip">ini.zip</a></p>

<p>Have fun, and feel free to send any feedback my way!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.brush.co.nz/2009/02/inih/feed/</wfw:commentRss>
		</item>
		<item>
		<title>bitchecker: Binary Irony</title>
		<link>http://blog.brush.co.nz/2009/02/bitchecker/</link>
		<comments>http://blog.brush.co.nz/2009/02/bitchecker/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 00:22:39 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
		
		<category><![CDATA[news]]></category>

		<guid isPermaLink="false">http://blog.brush.co.nz/?p=147</guid>
		<description><![CDATA[Inspired by this article on a website called The Daily TLA, I wrote a fully-fledged bit verifier, which checks not only the ordinal values of all the bits on your hard drive, but also their Quantumness&#160;(TM).

At the heart of the Highly Patentable Algorithm are the following functions, which return true if there&#8217;s something wrong with [...]]]></description>
			<content:encoded><![CDATA[<p><img class='border right' alt='Bits' src='http://blogstatic.micropledge.com/2009/02/bits.png' width='110' height='122'/>Inspired by <a href='http://thedailywtf.com/Articles/Some-Crazy-Reason.aspx'>this article</a> on a website called The Daily <abbr title='Otherwise known as The Daily WTF'>TLA</abbr>, I wrote a fully-fledged bit verifier, which checks not only the ordinal values of all the bits on your hard drive, but also their Quantumness&nbsp;(TM).</p>

<p>At the heart of the Highly Patentable Algorithm are the following functions, which return true if there&#8217;s something wrong with either the value or the quantum nature of a given bit, but false if the bit is okay:</p>

<pre class='prettyprint'><code>// Return true if bit's numericity is wrong (bit is neither zero nor one).
int invalid_numericity(int bit)
{
    return bit != 0 &amp;&amp; bit != 1;
}

// Return true if bit's quantum-ness is wrong (i.e., bit is both zero and
// one at the same time).
int invalid_quantumness(int bit)
{
    return bit == 0 &amp;&amp; bit == 1;
}
</code></pre>

<p>Download and run <a href='http://blogstatic.micropledge.com/2009/02/bitchecker.exe'>bitchecker.exe</a>, or have a look at the <a href='http://blogstatic.micropledge.com/2009/02/bitchecker.c.txt'>full C source code</a>. It will check your entire hard drive, displaying the files it&#8217;s checking, and showing you any bit errors as it finds them.</p>

<p>Note that I haven&#8217;t ported it to Linux, because open source software tends to have fewer problems with Quantum Bit Integrity than proprietary software.</p>

<p>If you have non-technical friends, point them to this handy utility &#8212; it will help them identify the worst bit errors on their computers in no time!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.brush.co.nz/2009/02/bitchecker/feed/</wfw:commentRss>
		</item>
		<item>
		<title>RAII, AC/DC, and the “with” statement</title>
		<link>http://blog.brush.co.nz/2009/02/raii-acdc/</link>
		<comments>http://blog.brush.co.nz/2009/02/raii-acdc/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 21:08:17 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
		
		<category><![CDATA[news]]></category>

		<guid isPermaLink="false">http://blog.brush.co.nz/?p=104</guid>
		<description><![CDATA[Once upon a time there was a programming idiom called Resource Acquisition Is Initialization (RAII), and it almost deserved it. (Apologies to C. S. Lewis.)

First, a bit of context

Recently I wanted to figure out exactly what this RAII thing was that C++ and a few other languages have. With such a name, I figured it [...]]]></description>
			<content:encoded><![CDATA[<p>Once upon a time there was a programming idiom called Resource Acquisition Is Initialization (RAII), and it almost deserved it. (Apologies to C. S. Lewis.)</p>

<h4>First, a bit of context</h4>

<p>Recently I wanted to figure out exactly what this RAII thing was that C++ and a few other languages have. With such a name, I figured it must be both extremely powerful and rather complicated.</p>

<p>What does &#8220;resource acquisition is initialization&#8221; even mean? Does it matter if you call it &#8220;initialization is resource acquisition&#8221;? And why <i>initialization</i> when everyone seems to think it&#8217;s mostly about what to do on <i>destruction</i>?</p>

<p>Well, it turns out it&#8217;s badly named. Even the <a href="http://www.parashift.com/c++-faq-lite/big-picture.html#faq-6.18">C++ FAQ agrees</a>:</p>

<blockquote><p>However, if you dissect &#8220;RAII&#8221; as an acronym, and if you look (too?) closely at the words making up that acronym, you will realize that the words are not a perfect match for the concept. Who cares?!? The concept is what&#8217;s important; &#8220;RAII&#8221; is merely a moniker used as a handle for that concept.</p>
</blockquote>

<p>Who cares? Maybe just me, but usually an acronym tells you what something is. What&#8217;s called RAII is actually a fairly simple concept, and perhaps more of us would use and understand it if it had a better name. So I hereby propose one:</p>

<h4>Enter AC/DC</h4>

<p><b>AC/DC &#8212; Acquire in Constructor, Destructor does Cleanup.</b></p>

<p>First AC/DC was two types of electrical current, then a heavy-metal band. And now it&#8217;s a C++ programming idiom. :-) The idea is to acquire or allocate all the resources you need in a class&#8217;s constructor, and clean them up or free them in the destructor.</p>

<p>My first thought when I&#8217;d figured out what RAII meant was, &#8220;Is that all? Isn&#8217;t that what everyone does anyway?&#8221; Well, yes and no.</p>

<p>For a start, this works in C++ because destructors are called in a deterministic way. The destructor is called when an object goes out of scope &#8212; and this is one of the key points &#8212; <i>even when an exception occurs</i>.</p>

<p>So when you use AC/DC, your resources are tied to objects, and when and however the objects die, the resources are freed too.</p>

<p>Well, how do you use it, and what&#8217;s it good for? Let&#8217;s see an example:</p>

<h4>The old FILE-wrapper example</h4>

<pre class='prettyprint'><code>#include &lt;cstdio&gt;
#include &lt;exception&gt;

class CharReader {
public:
    class Exception : public std::exception {
    public:
        virtual const char* what() const throw() { return "Error!"; }
    };

    static const int EndOfFile = EOF;

    CharReader(const char* name) {
        f = fopen(name, "r");
        if (!f)
            throw Exception();
    }

    ~CharReader() {
        printf("Closing file\n");
        fclose(f);
    }

    int Read() {
        int c = fgetc(f);
        if (c == EOF &amp;&amp; ferror(f))
            throw Exception();
        return c;
    }

private:
    FILE* f;
};

int main() {
    int c;

    try {
        CharReader reader("CharReader.cpp");

        while ((c = reader.Read()) != CharReader::EndOfFile) {
            if (c == 'z') {
                printf("'z' byte found in file!\n");
                return 2;
            }
            printf("%02X ", c);
        }
    } catch (CharReader::Exception&amp; e) {
        printf("Error: %s\n", e.what());
        return 1;
    }

    return 0;
}
</code></pre>

<h4>How it works</h4>

<p>As soon as you instantiate the <code>reader</code> it opens the file. The object owns the file until it goes out of scope and the destructor is called. However the program exits (with an early return or via an exception) the reader&#8217;s destructor is called and the file closed.</p>

<p>In this case it&#8217;s not a big deal (the OS will close files for you anyway), but when you&#8217;re writing a file and haven&#8217;t flushed, or when you&#8217;re connecting to a database and need to commit, or when you&#8217;ve disabled interrupts and need to re-enable them regardless of where the function <code>return</code>s &#8212; these are great uses for the AC/DC idiom.</p>

<h4>Python&#8217;s &#8220;with&#8221; statement</h4>

<p>Python (and Java, I think) doesn&#8217;t really do AC/DC, because when an object goes out of scope, its <code>__del__</code> method <a href="http://docs.python.org/reference/datamodel.html#object.__del__">isn&#8217;t necessarily called</a>. <code>__del__</code> is only called when the object&#8217;s reference count goes down to zero or when the object is collected as garbage.</p>

<p>The old-school way of coping with this was <code>try ... finally</code>, for example:</p>

<pre class='prettyprint'><code>def log(s):
    f = open('output.log', 'a+')
    try:
        f.write(s + '\n')
    finally:
        f.close()
</code></pre>

<p>That way, whether or not a write exception occurred, your file would be closed and the log flushed. But with the introduction of the <a href='http://www.python.org/dev/peps/pep-0343/'><code>with</code> statement</a> in Python 2.5 you can do what amounts to AC/DC using <code>with</code>:</p>

<pre class='prettyprint'><code>def log(s):
    with open('output.log', 'a+') as f:
        f.write(s + '\n')
</code></pre>

<p>The difference is that it&#8217;s explicit, rather than implicit based on the scope of the object. (The <code>with</code> protocol uses the special functions <code>__enter__</code> and <code>__exit__</code> rather than the constructor and destructor.)</p>

<h4>References</h4>

<p>Some further reading if you&#8217;re interested:</p>

<ul>
<li><a href="http://sourceforge.net/docman/display_doc.php?docid=8673&#038;group_id=9028">An explanation of RAII from the Firebird docs</a></li>
<li><a href="http://www.research.att.com/~bs/except.pdf"><i>Exception Safety</i>, a paper by Stroustrup that talks about RAII [pdf]</a></li>
<li><a href="http://www.amazon.com/gp/product/0321334876"><i>Effective C++</i>, a book by Scott Meyers that discusses RAII in depth</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.brush.co.nz/2009/02/raii-acdc/feed/</wfw:commentRss>
		</item>
	</channel>
</rss><!-- Dynamic Page Served (once) in 0.616 seconds --><!-- Cached page generated by WP-Super-Cache on 2009-07-10 16:04:44 --><!-- Compression = gzip -->
