sedScript.cxx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Filename: sedScript.cxx
  2. // Created by: drose (24Oct00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. #include "sedScript.h"
  6. #include "sedCommand.h"
  7. #include "sedContext.h"
  8. ////////////////////////////////////////////////////////////////////
  9. // Function: SedScript::Constructor
  10. // Access: Public
  11. // Description:
  12. ////////////////////////////////////////////////////////////////////
  13. SedScript::
  14. SedScript() {
  15. _quit = false;
  16. }
  17. ////////////////////////////////////////////////////////////////////
  18. // Function: SedScript::Destructor
  19. // Access: Public
  20. // Description:
  21. ////////////////////////////////////////////////////////////////////
  22. SedScript::
  23. ~SedScript() {
  24. Commands::iterator ci;
  25. for (ci = _commands.begin(); ci != _commands.end(); ++ci) {
  26. delete (*ci);
  27. }
  28. }
  29. ////////////////////////////////////////////////////////////////////
  30. // Function: SedScript::add_line
  31. // Access: Public
  32. // Description: Adds the indicated script line to the script.
  33. // Returns true if it is a valid line, false if there is
  34. // an error.
  35. ////////////////////////////////////////////////////////////////////
  36. bool SedScript::
  37. add_line(const string &line) {
  38. size_t p = 0;
  39. SedCommand *command = new SedCommand;
  40. if (!command->parse_command(line, p)) {
  41. // That's an invalid command.
  42. delete command;
  43. return false;
  44. }
  45. _commands.push_back(command);
  46. while (p < line.length()) {
  47. // There's more to the line.
  48. if (line[p] != ';') {
  49. // But it's an error.
  50. cerr << "Invalid character at: " << line.substr(p) << "\n";
  51. return false;
  52. }
  53. p++;
  54. command = new SedCommand;
  55. if (!command->parse_command(line, p)) {
  56. // That's an invalid command.
  57. delete command;
  58. return false;
  59. }
  60. _commands.push_back(command);
  61. }
  62. // Everything parsed ok.
  63. return true;
  64. }
  65. ////////////////////////////////////////////////////////////////////
  66. // Function: SedScript::run
  67. // Access: Public
  68. // Description: Runs the script, modifying the context as
  69. // appropriate. Returns true if the process should
  70. // continue with the next line, or false if we have quit
  71. // and we should terminate.
  72. ////////////////////////////////////////////////////////////////////
  73. bool SedScript::
  74. run(SedContext &context) {
  75. context._deleted = false;
  76. _next_command = _commands.begin();
  77. while (!_quit && _next_command != _commands.end()) {
  78. SedCommand *command = (*_next_command);
  79. ++_next_command;
  80. command->run(*this, context);
  81. }
  82. if (!context._deleted) {
  83. context._out << context._pattern_space << "\n";
  84. }
  85. return !_quit;
  86. }