PageRenderTime 34ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/apps/todo/domain/TodoListParser.php

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