The class should be declared in fileparser.h and implemented in
fileparser.cxx. You can use the following header code for class
declaration.
/*************************************************************** ** Name: fileparser.h ** Author: Leo Liberti ** Source: GNU C++ ** Purpose: www exploring topologizer - file parser (header) ** History: 060820 work started ****************************************************************/ #ifndef _WETFILEPARSERH #define _WETFILEPARSERH #include<string> #include<vector> class FileParserException { public: FileParserException(); ~FileParserException(); }; class FileParser { public: FileParser(); FileParser(std::string theFileName, std::string theParseTag); ~FileParser(); void setFileName(std::string theFileName); std::string getFileName(void) const; void setParseTag(std::string theParseTag); std::string getParseTag(void) const; // parse the file and build the list of parsed strings void parse(void) throw(FileParserException); // get the number of parsed strings after parsing int getNumberOfParsedStrings(void) const; // get the i-th parsed string std::string getParsedString(int i) const throw(FileParserException); protected: std::string fileName; std::string parseTag; // compare two strings (case insensitive), return 0 if equal int compareCaseInsensitive(const std::string& s1, const std::string& s2) const; // return true if s2 is the tail (case insensitive) of string s1 bool isTailCaseInsensitive(const std::string& s1, const std::string& s2) const; private: std::vector<std::string> parsedString; }; #endifPay attention to the following details:
int FileParser::compareCaseInsensitive(const std::string& s1, const std::string& s2) const { using namespace std; string::const_iterator p1 = s1.begin(); string::const_iterator p2 = s2.begin(); while(p1 != s1.end() && p2 != s2.end()) { if (toupper(*p1) < toupper(*p2)) { return -1; } else if (toupper(*p1) > toupper(*p2)) { return 1; } p1++; p2++; } if (s1.size() < s2.size()) { return -1; } else if (s1.size() > s2.size()) { return 1; } return 0; } bool FileParser::isTailCaseInsensitive(const std::string& s1, const std::string& s2) const { using namespace std; int s2len = s2.size(); if (s1.size() >= s2.size() && compareCaseInsensitive(s1.substr(s1.size() - s2len, s2len), s2) == 0) { return true; } return false; }
#include<iostream> #include<fstream> #include<istream> #include<sstream> #include<iterator> [...] // opens the file for input string fileName("myfilename.ext"); ifstream is(fileName.c_str()); if (!is) { // can't open file, throw exception } // save all characters to a string buffer char nextChar; stringstream buffer; while(!is.eof()) { is.get(nextChar); buffer << nextChar; } // output the buffer cout << buffer.str() << endl; // erase the buffer buffer.str("");
#include<iostream> #include "fileparser.h" int main(int argc, char** argv) { using namespace std; if (argc < 3) { cerr << "missing 2 args on cmd line" << endl; return 1; } FileParser fp(argv[1], argv[2]); fp.parse(); for(int i = 0; i < fp.getNumberOfParsedStrings(); i++) { cout << fp.getParsedString(i) << endl; } return 0; }Make a small HTML file as follows, and call it myfile.html:
<html> <head> <title>MyTitle</title> </head> <body> My <a href="http://mywebsite.com/mywebpage.html">absolute link</a> and my <a href="otherfile.html">relative link</a>. </body> </html>Now run the fpdriver code as follows:
http://mywebsite.com/mywebpage.html otherfile.html