#ifndef URL_H_ #define URL_H_ 1 // Oldham, Jeffrey D. // 2000Apr18 // CS1321 // A URL Class // Each object represents a URL. The following operations are // supported: // 1. construction - the URL's location is specified // 2. getContents() - returns the URL's contents // 3. getName() - returns the URL's name // 4. ==, != - comparisons returning true if both URLs have the same // (for ==) or different (for !=) contents. Note that the // comparison is between contents, not name. // 5. a conversion to a string // 6. an output operator // Implementation Strategy: // Each URL is actually the name of a file in the local directory. // An alternative would be to actually obtain a WWW document using // sockets. #include #include #include class URL { public: URL(const string & nm) : name(nm) {} string getContents(void) const { ifstream in(name.c_str()); string answer; if (in.fail()) return answer; char c; while (in.get(c)) answer += c; in.close(); return answer; } operator string(void) const { return name; } string getName(void) const { return name; } // Return true iff the URLs' CONTENTS, not names, are the same. friend bool operator==(const URL & u1, const URL & u2) { ifstream in1(u1.name.c_str()), in2(u2.name.c_str()); if (in1.fail()) return false; if (in2.fail()) return false; char c1, c2; while (in1.get(c1) && in2.get(c2) && c1 == c2); in2.get(c2); // force next read if not already done bool answer = (!in1 && !in2); in1.close(); in2.close(); return answer; } friend bool operator!=(const URL & u1, const URL & u2) { return !(u1 == u2); } friend ostream& operator<<(ostream& out, const URL & u) { return out << u.name; } private: string name; // URL's name }; #endif // URL_H_