« February 2010 | Main | April 2010 »

March 24, 2010

PEAR's Configuration Object class and commas

PHP's PEAR library of packages has a nice configuration file parsing tool called, aptly enough, Config.php. (It appears to not be well updated--it hasn't been reported touched since 2007.) For most uses, it's pretty convenient and quick to use, especially if you are using and maintaining different types of configuration files, like Windows INI files, for your applications. I seem to have found an interesting quirk in how it processes lines with commas in the text. I have a configuration file with a stanza like:
[files]
fileroot="."
filetypes="Type1,Type 2,Type3,Type4"
Note that there are no spaces around the commas.

What I would like to have happen is that I parse that using regular INIFile rules:

$configRoot =& $config->parseConfig($configFile,"IniFile");
$filesConfig =& $configRoot->getItem("section","files");
$fileRoot=$filesConfig->getItem("directive","fileroot")->getContent();
$fileTypes=$filesConfig->getItem("directive","filetypes")->getContent();
(This isn't meant to be the best example of PHP code extant, only code that is able to elicit the bug I want to show.)

If I then echo out the content of $fileTypes, I'll get:
Type1,Type 2,Type3,Type4
Note that the spaces in "Type 2" are preserved. If I change the filetypes line to read
filetypes="Type1,○Type2,Type3,Type4"
Where "○" represents a space (ASCII 32), I get
Type 2,Type3,Type4
The first element has been lost! If, however, I type
filetypes="Type1○,Type 2,Type3,Type4"
(note that there are spaces in the "Type 2" entry, but the 2 abuts a comma, as does the capital T) I get:
Type1○,Type 2,Type3,Type4
The entire entry is there, including the space before the comma! (This is made clear by doing a
var_dump(split(",",$fileTypes))
and getting
array
  0 => string 'Type1' (length=5)

  1 => string 'Type 2' (length=6)
  2 => string 'Type3' (length=5)

  3 => string 'Type4' (length=5)
in the first case,
array
  0 => string 'Type 2' (length=6)

  1 => string 'Type3' (length=5)
  2 => string 'Type4' (length=5)
in the second, and
array
  0 => string 'Type1 ' (length=6)

  1 => string 'Type 2' (length=6)
  2 => string 'Type3' (length=5)

  3 => string 'Type4' (length=5)
in the third. (Formatting courtesy of xdebug).

March 17, 2010

On travelling from New York to Ottawa, and a screwdriver.

Recently (last week) I flew up to Ottawa on Porter Air. I went EWR→YTZ→YOW.

I was really hoping to gate check my bags rather than regular-check them, it's easier and all, so I made sure to leave my frightening Leatherman tool at home this time.

I forgot that in my backpack were two 6" screwdrivers.

In Newark, nobody blinked at anything, I got to gate-check my bag.

In Toronto City Centre, when I went through security, they got scared because of a keychain screwdriver (it's less than 2" long--see here except mine is black). "It's a tool. Sorry, you can't bring that on board." Um, what about the screwdrivers in my bag? They didn't even see or care about those.

Go figure. At least they let me go back and check through my bag rather than summarily confiscating it.

Actually, though, I do recommend Porter if you have to fly to Canada, they're quite nice.

March 14, 2010

In honor of π day

My wife baked the family a pie:
in honor of π day (3/14), which we ate around 1:59 p.m. Mmm mmm apple π.