1 <?php rcs_id('$Id: Units.php,v 1.1 2007-01-03 21:22:48 rurban Exp $');
3 * Interface to man units(1), /usr/share/units.dat
6 * Definition: 9.6347558e+08 m^2
7 * $ units "372.0 mi2" m^2
8 * Definition: 9.6347558e+08 m^2
10 * Windows requires the cygwin /usr/bin/units.
11 * CHECK: All successfully parsed unit definitions might be stored in the wikidb,
12 * so that subsequent expansions will not require /usr/bin/units be called again.
13 * So far even on windows (cygwin) the process is fast enough.
17 function Units ($UNITSFILE = false) {
20 elseif (defined("UNITS_EXE")) // ignore dynamic check
23 exec("units m2",$o,$this->errcode);
26 function Definition ($query) {
27 static $Definitions = array();
28 if (isset($Definitions[$query])) return $Definitions[$query];
31 $query = preg_replace("/,/","", $query);
32 $def = $this->_cmd("\"$query\"");
33 if (preg_match("/Definition: (.+)$/",$def,$m))
34 return ($Definitions[$query] = $m[1]);
36 trigger_error("units: ". $def, E_USER_WARNING);
41 function basevalue($query, $def = false) {
42 if (!$def) $def = $this->Definition($query);
44 if (is_numeric($def)) // e.g. "1 million"
46 if (preg_match("/^([-0-9].*) \w.*$/",$def,$m))
52 function baseunit($query, $def = false) {
53 if (!$def) $def = $this->Definition($query);
55 if (preg_match("/ (.+)$/",$def,$m))
61 function _cmd($args) {
62 if ($this->errcode) return $args;
63 if (defined("UNITS_EXE")) {
64 $s = UNITS_EXE ." $args";
68 $result = `units $args`;