gpxpath.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <iostream>
  2. #include <cstring>
  3. #include <fstream>
  4. #include "XMLRawParser.h"
  5. #include "Arguments.h"
  6. const std::string version= "0.1.0";
  7. // ----------------------------------------------------------------------------
  8. namespace gpxtools
  9. {
  10. class GPXPath : public XMLParserHandler
  11. {
  12. public:
  13. // -- Constructor -----------------------------------------------------------
  14. GPXPath() :
  15. _arguments("gpxrm [OPTION].. PATH [FILE]\nRead or set a path in a GPX-file.\n", "gpxpath v0.1", "Read or set a path in a GPX-file and display the result on standard output."),
  16. _value(_arguments, true, 'v', "value", "VALUE", "set path to a value", ""),
  17. _xmlParser(this)
  18. {
  19. }
  20. // -- Deconstructor ---------------------------------------------------------
  21. virtual ~GPXPath()
  22. {
  23. }
  24. // -- Properties ------------------------------------------------------------
  25. // -- Parse arguments -------------------------------------------------------
  26. bool processArguments(int argc, char *argv[])
  27. {
  28. std::vector<std::string> filenames;
  29. if (!_arguments.parse(argc,argv, filenames))
  30. {
  31. return false;
  32. }
  33. else if (!checkArguments(filenames))
  34. {
  35. return false;
  36. }
  37. else if (filenames.empty())
  38. {
  39. return _xmlParser.parse(std::cin);
  40. }
  41. else
  42. {
  43. return parseFile(filenames.front());
  44. }
  45. }
  46. // -- Check arguments ---------------------------------------------------------
  47. bool checkArguments(std::vector<std::string> &filenames)
  48. {
  49. if (filenames.empty())
  50. {
  51. std::cerr << "Missing path to look for." << std::endl;
  52. return false;
  53. }
  54. _path = filenames.front();
  55. filenames.erase(filenames.begin());
  56. if (filenames.size() > 1)
  57. {
  58. std::cerr << "Too many input files." << std::endl;
  59. return false;
  60. }
  61. return true;
  62. }
  63. // -- Parse a file ----------------------------------------------------------
  64. bool parseFile(const std::string &filename)
  65. {
  66. bool ok =false;
  67. std::ifstream file(filename);
  68. if (file.is_open())
  69. {
  70. ok = _xmlParser.parse(file);
  71. file.close();
  72. }
  73. else
  74. {
  75. std::cerr << "Unable to open: " << filename << std::endl;
  76. }
  77. return ok;
  78. }
  79. private:
  80. void store(const std::string &text)
  81. {
  82. std::cout << text;
  83. }
  84. public:
  85. // -- Callbacks -------------------------------------------------------------
  86. virtual void unhandled(const std::string &, const std::string &text)
  87. {
  88. }
  89. virtual void startElement(const std::string &path, const std::string &text, const std::string &, const Attributes &)
  90. {
  91. if (std::equal(_path.begin(), _path.end(), path.begin()))
  92. {
  93. std::cerr << "Found:" << path << std::endl;
  94. }
  95. }
  96. virtual void text(const std::string &path, const std::string &text)
  97. {
  98. }
  99. virtual void endElement(const std::string &path, const std::string &text, const std::string &)
  100. {
  101. }
  102. private:
  103. // Members
  104. arg::Arguments _arguments;
  105. arg::Argument _value;
  106. XMLRawParser _xmlParser;
  107. std::string _path;
  108. };
  109. }
  110. // -- Main program ------------------------------------------------------------
  111. int main(int argc, char *argv[])
  112. {
  113. gpxtools::GPXPath gpxPath;
  114. return gpxPath.processArguments(argc, argv) ? 0 : 1;
  115. }