DumpRenderTreeCommon.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "config.h"
  2. #include "DumpRenderTree.h"
  3. #include <algorithm>
  4. #include <ctype.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string>
  8. class CommandTokenizer {
  9. public:
  10. explicit CommandTokenizer(const std::string& input)
  11. : m_input(input)
  12. , m_posNextSeparator(0)
  13. {
  14. pump();
  15. }
  16. bool hasNext() const;
  17. std::string next();
  18. private:
  19. void pump();
  20. static const char kSeparator = '\'';
  21. const std::string& m_input;
  22. std::string m_next;
  23. size_t m_posNextSeparator;
  24. };
  25. void CommandTokenizer::pump()
  26. {
  27. if (m_posNextSeparator == std::string::npos || m_posNextSeparator == m_input.size()) {
  28. m_next = std::string();
  29. return;
  30. }
  31. size_t start = m_posNextSeparator ? m_posNextSeparator + 1 : 0;
  32. m_posNextSeparator = m_input.find(kSeparator, start);
  33. size_t size = m_posNextSeparator == std::string::npos ? std::string::npos : m_posNextSeparator - start;
  34. m_next = std::string(m_input, start, size);
  35. }
  36. std::string CommandTokenizer::next()
  37. {
  38. ASSERT(hasNext());
  39. std::string oldNext = m_next;
  40. pump();
  41. return oldNext;
  42. }
  43. bool CommandTokenizer::hasNext() const
  44. {
  45. return !m_next.empty();
  46. }
  47. NO_RETURN static void die(const std::string& inputLine)
  48. {
  49. fprintf(stderr, "Unexpected input line: %s\n", inputLine.c_str());
  50. exit(1);
  51. }
  52. TestCommand parseInputLine(const std::string& inputLine)
  53. {
  54. TestCommand result;
  55. CommandTokenizer tokenizer(inputLine);
  56. if (!tokenizer.hasNext())
  57. die(inputLine);
  58. std::string arg = tokenizer.next();
  59. result.pathOrURL = arg;
  60. while (tokenizer.hasNext()) {
  61. arg = tokenizer.next();
  62. if (arg == std::string("--timeout")) {
  63. std::string timeoutToken = tokenizer.next();
  64. result.timeout = atoi(timeoutToken.c_str());
  65. } else if (arg == std::string("-p") || arg == std::string("--pixel-test")) {
  66. result.shouldDumpPixels = true;
  67. if (tokenizer.hasNext())
  68. result.expectedPixelHash = tokenizer.next();
  69. } else
  70. die(inputLine);
  71. }
  72. return result;
  73. }