PageRenderTime 27ms CodeModel.GetById 18ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/apps/todo/domain/TodoListParser.php

http://zoop.googlecode.com/
PHP | 72 lines | 58 code | 14 blank | 0 comment | 1 complexity | c5331504b2d827cc908c831221ec50b0 MD5 | raw file
 1<?php
 2class TodoListParser
 3{
 4	public $filename;
 5	private $file;
 6	private $root;
 7	private $stack;
 8	private $lookAheadLine;
 9	
10	function __construct($filename)
11	{
12		$parts = explode('/', $filename);
13		$this->filename = array_pop($parts);
14		$this->file = fopen($filename, "r");
15	}
16	
17	function parse()
18	{
19		$this->root = new TodoListItem($dummy = NULL, '*', '');
20		$this->stack = array();
21		$this->stack[0] = &$this->root;
22		$this->lookAheadLine = NULL;
23		
24		while(!$this->done())
25		{
26			$nextItem = $this->getNextItem();
27		}
28		
29		fclose($this->file);
30		
31		$todoList = new TodoList($this->root);
32		
33		return $todoList;
34	}
35	
36	function getNextItem()
37	{
38		$line = $this->getNextLine();
39		
40		$parsed = $this->parseFirstLine($line);
41		$parent = &$this->stack[$parsed['tabLevel']];
42		
43		$thisItem = new TodoListItem($parent, $parsed['status'], $parsed['line']);
44		
45		$this->stack[$parsed['tabLevel'] + 1] = &$thisItem;
46	}
47	
48	function parseFirstLine($line)
49	{
50		ereg("([\t]*)([+-/])(.*)", $line, $regs);
51		$return['tabLevel'] = strlen($regs[1]);
52		$return['status'] = $regs[2];
53		$return['line'] = $regs[3];
54		return $return;
55	}
56	
57	function getNextLine($value='')
58	{
59		if($this->lookAheadLine)
60		{
61			$this->lookAheadLine = NULL;
62			return $this->lookAheadLine;
63		}
64		
65		return fgets($this->file, 4096);
66	}
67	
68	function done()
69	{
70		return feof($this->file);
71	}
72}