no software patents - code snippets and programs from josh drew
Sometimes I write some code that I think might benefit fellow programmers. And sometimes I think to list that code here. Sometimes I read something I like, and want to share. The list:
  • [2005-05-25] - Using JavaScript to Disable Enter Key Submission in a Form with Multiple Submit Buttons

    I am ideologically opposed to using JavaScript to change standard browser behavior (like submitting a form when the enter key is pressed in a text input). However, my job requires me to write code for people (users and bosses) who don't share my beliefs. At the end of the day, those people matter more than my opinion (on matters of JavaScript, at least). So, I have a form with multiple submit buttons (which I want to work as usual) and many text input fields. Google turned up many poor, IE-only attempts to prevent the text fields from submitting while allowing the submit buttons to work. Some of these solutions made the submit buttons' values absent from the POSTed data. This will not do... However, this will work in Internet Explorer 6 and Mozilla 1.7.3 (XP and FreeBSD) (it's a bit of kludge, but... it *is* JavaScript):
    <body onkeypress="if (enterWasPressed(event)) { document.submitform = false; }">
    <script type="text/javascript">
       var submitform = false;
       function enterWasPressed(event) {
          return (event &&
                   ((event.which && event.which == 13) ||
                    (event.keyCode && event.keyCode == 13)));
    <form method="post" onsubmit="return document.submitform;">
       <input type="text" name="text1" />
       <input type="text" name="text2" />
       <input type="text" name="text3" />
       <input type="submit" name="sub" onclick="document.submitform = false;" style="border-width: 0px; background-color: transparent;" />
       <input type="submit" name="sub" value="button one" onclick="document.submitform = true;" />
       <input type="submit" name="sub" value="button two" onclick="document.submitform = true;" />
       <input type="submit" name="sub" value="button three" onclick="document.submitform = true;" />
  • [2004-09-24] - MySQL Quote UDF

    A friend of mine is stuck with MySQL 3.23.58 (which does not provide QUOTE()) on RedHat 9. He moved some code from a server with MySQL > 4.0.3 (which provides QUOTE()). That code used the QUOTE() function a lot, and it was easier to write a replacement QUOTE() UDF than to change every usage of QUOTE() in said code. So, I wrote a UDF for him. This function is a little different from the official QUOTE() function in MySQL > 4.0.3 -- the official version escapes apostrophes, backslashes, ctrl-Z, and NUL characters; this version only worries about apostrophes and backslashes. Download my QUOTE() UDF, if you want.
  • [2004-02-06] - MySQL Levenshtein Distance UDF

    Last month, I needed to use the metaphone and edit (Levenshtein) distance algorithms for a fuzzy search of a MySQL table. Of course, neither is available as a built-in MySQL function. So, I had to install them as UDFs. The MySQL source distribution includes a metaphone UDF function in However, I couldn't find a Levenshtein UDF anywhere, so I wrote one, by converting a C implementation by Lorenzo Seidenari. I suspect that other people could benefit from this code, and you can download it from I compared the function's output to that of the PHP levensthein() function for a couple million word pairs; the results agreed completely - that's good enough for me. (this code comes with no warranty whatsoever, but I really hope you find it useful)
  • [2003-12-22] - dbf2mysql v1.15

    After writing some patches for dbf2mysql v1.14, and finding nowhere to contribute them, I decided to go ahead with a dbf2mysql v1.15 release of my own. This release incorporates the dbf2mysql v1.14 patches I've made available (see below) as well as the ideas posted to the "Converters" page of the MySQL manual's "Contributed Programs" appendix. The full details, including credit for the programmers who wrote v1.14 (and prior), are in the README file. If you can get MySQL to update their download page with this version, feel free - or, if you can get in touch with anyone who maintains an "official" dbf2mysql utility (my attempts, last year, went unanswered; so, I gave up). (as with previous versions, this code comes with no warranty whatsoever; neither I nor the other contributors accept any responsibility for any damage caused by your use of this program)
  • [2003-11-28] - mysql2dbf Patch for MySQL 4.X and More

    I've written a patch for the mysql2dbf program which is included in the dbf2mysql v1.14 archive available at the MySQL website. This patch accomplishes three goals:
    • adds support for the -p command-line option which is mentioned in the README file, but was not implemented; this option allows the user to specify the precision used for REAL fields
    • replaces the deprecated mysql_connect() function with mysql_real_connect(), as described by Chris Johnson in his comments in the MySQL manual; I believe the old function was deprecated starting with the 4.x branch
    • adds support for BIGINT attributes; if you've tried to use mysql2dbf on a table containing BIGINTs, you've probably notice the ensuing segmentation fault which is due to the program failing to allocate memory for BIGINT data
  • [2003-11-09] - SuSE Linux Openexhange Server (SLOX) mailto Kludge

    A couple of months ago I wrote some code to allow Windows users to associate mailto links with their SuSE Linux Openexchange Server (SLOX) accounts. SpinWeb said I could release the code; so I did. You can download all the files you need at (note: the client for whom this was developed is using Windows 98 and IE 5 exclusively - the code was not written for other environments, though it may work) (second note: this code is provided without any warranty whatsoever)
  • [2003-07-16] - "Hello World" Program in Brainf*ck

    I've written my first Brainf*ck program. It's a simple "hello world" program. Armed with a table of the eight BF instructions and a table of ASCII values, I coded this in about ten minutes, as an easy BF starting point. (I was going to implement the Bailey Borwein Plouffe pi algorithm, but was quickly dissuaded. It's a cinch in C or Perl, but BF is a different beast.) After I finished, I looked for examples, to see how my program compared; I was a little unimaginative - some "hello world" programs are much shorter. Anyway, my "hello world" source code is available.
  • [2003-03-18] - dbf2mysql Patch for Better Numeric Field Handling

    I wrote a patch for the dbf2mysql utility which (surprise!) converts a DBF file (dBASE, FoxBase) into a MySQL table. The patch is meant to be applied to version 1.14, available as contributed code, from MySQL AB. In version 1.14, all numeric fields from the DBF file are stored as MySQL INTs. Since numeric fields may be up to 20 digits long, clipping is a dangerous possibility (signed INTs hold -2147683648 - 2147483647; larger values will be stored as 2147483647). This patch uses the full range of MySQL integer data types (TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT) where appropriate. Please note that a signed BIGINT can only hold -9223372036854775808 - 9223372036854775807. So, values in the range 9223372036854775808 - 99999999999999999999, inclusive, will be stored as (2^63 - 1).
  • [2003-02-07] - "Text Retrieval Using Linear Algebra" Published

    An extended abstract of my Text Retrieval paper will be published in the Ball State University Mathematics Undergraduate Research Journal.
  • [2002-12-15] - Text Retrieval Thesis with Source Code

    My undergraduate honors thesis, "Text Retrieval Using Linear Algebra," is finished. The document and source code are available by on this site.
  • [2002-09-30] - Patch for Running Blackbox Rootless in OS X

    I wrote a patch to help Blackbox 0.65.0 to run rootless in Apple's OS X. With this patch applied (and compiled, of course), a middle mouse click on the Blackbox toolbar will produce the Blackbox menu.

"The Feynman Problem-Solving Algorithm: (1) write down the problem; (2) think very hard; (3) write down the answer." - Murray Gell-Mann