1 | 1 | simandl | <?php |
2 | | | // Sample Pluggable datasource for PHP Weathermap 0.9 |
3 | | | // - read a pair of values from a database, and return it |
4 | | | |
5 | | | // TARGET dbplug:databasename:username:pass:hostkey |
6 | | | |
7 | | | class WeatherMapDataSource_tabfile extends WeatherMapDataSource { |
8 | | | |
9 | | | function Recognise($targetstring) |
10 | | | { |
11 | | | if(preg_match("/\.(tsv|txt)$/",$targetstring,$matches)) |
12 | | | { |
13 | | | return TRUE; |
14 | | | } |
15 | | | else |
16 | | | { |
17 | | | return FALSE; |
18 | | | } |
19 | | | } |
20 | | | |
21 | | | // function ReadData($targetstring, $configline, $itemtype, $itemname, $map) |
22 | | | function ReadData($targetstring, &$map, &$item) |
23 | | | { |
24 | 85 | simandl | $data[IN] = NULL; |
25 | | | $data[OUT] = NULL; |
26 | 1 | simandl | $data_time=0; |
27 | | | $itemname = $item->name; |
28 | | | |
29 | | | $matches=0; |
30 | | | |
31 | | | $fd=fopen($targetstring, "r"); |
32 | | | |
33 | | | if ($fd) |
34 | | | { |
35 | | | while (!feof($fd)) |
36 | | | { |
37 | | | $buffer=fgets($fd, 4096); |
38 | | | # strip out any Windows line-endings that have gotten in here |
39 | | | $buffer=str_replace("\r", "", $buffer); |
40 | | | |
41 | | | if (preg_match("/^$itemname\t(\d+\.?\d*[KMGT]*)\t(\d+\.?\d*[KMGT]*)/", $buffer, $matches)) |
42 | | | { |
43 | 85 | simandl | $data[IN]=unformat_number($matches[1]); |
44 | | | $data[OUT]=unformat_number($matches[2]); |
45 | 1 | simandl | } |
46 | | | } |
47 | | | $stats = stat($targetstring); |
48 | | | $data_time = $stats['mtime']; |
49 | | | } |
50 | | | else { |
51 | | | // some error code to go in here |
52 | | | debug ("TabText ReadData: Couldn't open ($targetstring). \n"); } |
53 | 85 | simandl | |
54 | | | debug ("TabText ReadData: Returning (".($data[IN]===NULL?'NULL':$data[IN]).",".($data[OUT]===NULL?'NULL':$data[OUT]).",$data_time)\n"); |
55 | | | |
56 | | | return( array($data[IN], $data[OUT], $data_time) ); |
57 | 1 | simandl | } |
58 | | | } |
59 | | | |
60 | | | // vim:ts=4:sw=4: |
61 | | | ?> |