gpxfetch.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include <cstring>
  2. #include <fstream>
  3. #include <iostream>
  4. #include "Arguments.h"
  5. #include "XMLRawParser.h"
  6. const std::string version= "0.1.0";
  7. // ----------------------------------------------------------------------------
  8. namespace gpxtools
  9. {
  10. class GPXFetch : public XMLParserHandler
  11. {
  12. public:
  13. // -- Constructor -----------------------------------------------------------
  14. GPXFetch() :
  15. _arguments("gpxfetch [OPTION].. [FILE]\nFetch a waypoint, track or route from a GPX-file.\n", "gpxfetch v0.1", "Fetch a waypoint, track or route from a GPX-file and display the GPX-file with this waypoint, track or route on standard output."),
  16. _waypoint(_arguments, true, 'w', "waypoint", "NAME", "fetch the waypoint with NAME", ""),
  17. _track (_arguments, true, 't', "track", "NAME", "fetch the track with NAME", ""),
  18. _route (_arguments, true, 'r', "route", "NAME", "fetch the route with NAME", ""),
  19. _xmlParser(this),
  20. _inWaypoint(false),
  21. _inRoute(false),
  22. _inTrack(false)
  23. {
  24. }
  25. // -- Deconstructor ---------------------------------------------------------
  26. virtual ~GPXFetch()
  27. {
  28. }
  29. // -- Properties ------------------------------------------------------------
  30. // -- Parse arguments -------------------------------------------------------
  31. bool processArguments(int argc, char *argv[])
  32. {
  33. std::vector<std::string> filenames;
  34. if (!_arguments.parse(argc,argv, filenames))
  35. {
  36. return false;
  37. }
  38. else if (!checkArguments(filenames))
  39. {
  40. return false;
  41. }
  42. else if (filenames.empty())
  43. {
  44. return _xmlParser.parse(std::cin);
  45. }
  46. else
  47. {
  48. return parseFile(filenames.front());
  49. }
  50. }
  51. // -- Check arguments ---------------------------------------------------------
  52. bool checkArguments(const std::vector<std::string> &filenames)
  53. {
  54. if (filenames.size() > 1)
  55. {
  56. std::cerr << "Too many input files." << std::endl;
  57. return false;
  58. }
  59. return true;
  60. }
  61. // -- Parse a file ----------------------------------------------------------
  62. bool parseFile(const std::string &filename)
  63. {
  64. bool ok =false;
  65. std::ifstream file(filename);
  66. if (file.is_open())
  67. {
  68. ok = _xmlParser.parse(file);
  69. file.close();
  70. }
  71. else
  72. {
  73. std::cerr << "Unable to open: " << filename << std::endl;
  74. }
  75. return ok;
  76. }
  77. private:
  78. void store(const std::string &text)
  79. {
  80. if (_inWaypoint || _inRoute || _inTrack)
  81. {
  82. _currentText.append(text);
  83. }
  84. else
  85. {
  86. std::cout << text;
  87. }
  88. }
  89. public:
  90. // -- Callbacks -------------------------------------------------------------
  91. virtual void unhandled(const std::string &, const std::string &text)
  92. {
  93. store(text);
  94. }
  95. virtual void startElement(const std::string &path, const std::string &text, const std::string &, const Attributes &)
  96. {
  97. if (path == "/gpx/wpt")
  98. {
  99. _inWaypoint = true;
  100. _currentText.clear();
  101. _currentName.clear();
  102. }
  103. else if (path == "/gpx/rte")
  104. {
  105. _inRoute = true;
  106. _currentText.clear();
  107. _currentName.clear();
  108. }
  109. else if (path == "/gpx/trk")
  110. {
  111. _inTrack = true;
  112. _currentText.clear();
  113. _currentName.clear();
  114. }
  115. store(text);
  116. }
  117. virtual void text(const std::string &path, const std::string &text)
  118. {
  119. if (path == "/gpx/wpt/name" || path == "/gpx/rte/name" || path == "/gpx/trk/name")
  120. {
  121. _currentName.append(text);
  122. }
  123. store(text);
  124. }
  125. virtual void endElement(const std::string &path, const std::string &text, const std::string &)
  126. {
  127. store(text);
  128. XMLRawParser::trim(_currentName);
  129. if (path == "/gpx/wpt")
  130. {
  131. if (_inWaypoint && (_currentName == _waypoint.value()))
  132. {
  133. std::cout << _currentText;
  134. }
  135. _inWaypoint = false;
  136. }
  137. else if (path == "/gpx/rte")
  138. {
  139. if (_inRoute && (_currentName == _route.value()))
  140. {
  141. std::cout << _currentText;
  142. }
  143. _inRoute = false;
  144. }
  145. else if (path == "/gpx/trk")
  146. {
  147. if (_inTrack && (_currentName == _track.value()))
  148. {
  149. std::cout << _currentText;
  150. }
  151. _inTrack = false;
  152. }
  153. }
  154. private:
  155. // Members
  156. arg::Arguments _arguments;
  157. arg::Argument _waypoint;
  158. arg::Argument _track;
  159. arg::Argument _route;
  160. XMLRawParser _xmlParser;
  161. bool _inWaypoint;
  162. bool _inRoute;
  163. bool _inTrack;
  164. std::string _currentText;
  165. std::string _currentName;
  166. };
  167. }
  168. // -- Main program ------------------------------------------------------------
  169. int main(int argc, char *argv[])
  170. {
  171. gpxtools::GPXFetch gpxFetch;
  172. return gpxFetch.processArguments(argc, argv) ? EXIT_SUCCESS : EXIT_FAILURE;
  173. }