123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- #include <iostream>
- #include <cstring>
- #include <fstream>
- #include "XMLRawParser.h"
- #include "Arguments.h"
- const std::string version= "0.1.0";
- // ----------------------------------------------------------------------------
- namespace gpxtools
- {
- class GPXPath : public XMLParserHandler
- {
- public:
- // -- Constructor -----------------------------------------------------------
- GPXPath() :
- _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."),
- _value(_arguments, true, 'v', "value", "VALUE", "set path to a value", ""),
- _xmlParser(this)
- {
- }
- // -- Deconstructor ---------------------------------------------------------
- virtual ~GPXPath()
- {
- }
- // -- Properties ------------------------------------------------------------
- // -- Parse arguments -------------------------------------------------------
- bool processArguments(int argc, char *argv[])
- {
- std::vector<std::string> filenames;
- if (!_arguments.parse(argc,argv, filenames))
- {
- return false;
- }
- else if (!checkArguments(filenames))
- {
- return false;
- }
- else if (filenames.empty())
- {
- return _xmlParser.parse(std::cin);
- }
- else
- {
- return parseFile(filenames.front());
- }
- }
- // -- Check arguments ---------------------------------------------------------
- bool checkArguments(std::vector<std::string> &filenames)
- {
- if (filenames.empty())
- {
- std::cerr << "Missing path to look for." << std::endl;
- return false;
- }
- _path = filenames.front();
- filenames.erase(filenames.begin());
- if (filenames.size() > 1)
- {
- std::cerr << "Too many input files." << std::endl;
- return false;
- }
- return true;
- }
- // -- Parse a file ----------------------------------------------------------
- bool parseFile(const std::string &filename)
- {
- bool ok =false;
- std::ifstream file(filename);
- if (file.is_open())
- {
- ok = _xmlParser.parse(file);
- file.close();
- }
- else
- {
- std::cerr << "Unable to open: " << filename << std::endl;
- }
- return ok;
- }
- private:
- void store(const std::string &text)
- {
- std::cout << text;
- }
- public:
- // -- Callbacks -------------------------------------------------------------
- virtual void unhandled(const std::string &, const std::string &text)
- {
- }
- virtual void startElement(const std::string &path, const std::string &text, const std::string &, const Attributes &)
- {
- if (std::equal(_path.begin(), _path.end(), path.begin()))
- {
- std::cerr << "Found:" << path << std::endl;
- }
- }
- virtual void text(const std::string &path, const std::string &text)
- {
- }
- virtual void endElement(const std::string &path, const std::string &text, const std::string &)
- {
- }
- private:
- // Members
- arg::Arguments _arguments;
- arg::Argument _value;
- XMLRawParser _xmlParser;
- std::string _path;
- };
- }
- // -- Main program ------------------------------------------------------------
- int main(int argc, char *argv[])
- {
- gpxtools::GPXPath gpxPath;
- return gpxPath.processArguments(argc, argv) ? 0 : 1;
- }
|