Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

URL Class Uniform Resource Locators (URLs) are commonly used in the world wide web as addresses for HTML web pages. URLs can be divided into

URL Class

Uniform Resource Locators (URLs) are commonly used in the world wide web as addresses for HTML web pages. URLs can be divided into distinct segments including the scheme, user, password, host, port, path, query and fragment. You will create a class for storing and manipulating URLs by subclassing your DynamicString class. As before, you are not allowed to use the string header file, cstring header file or C++ strings.

Goals:

The purpose of this project is to continue working with C++ classes and inheritance, design and implement algorithms for parsing strings and begin working on the pieces necessary to implelement a basic web crawler.

Requirements:

The URL class will store absolute URLs. In addition to the inherited methods from the DynamicString class, the class will need to implement the following methods:

  • Constructor that takes a URL as a const DynamicString& and stores the URL. Throws an invalid_argument exception if the string is not a valid URL.
  • Constructor that takes a URL as a const char* and stores the URL. Throws an invalid_argument exception if the string is not a valid URL.
  • getScheme - returns the scheme of the URL as a DynamicString
  • getAuthority - returns the host (including user, password and port if applicable) of the URL as a DynamicString
  • getPath - returns the path of the URL as a DynamicString
  • getQuery - returns the query of the URL as a DynamicString
  • getFragment - returns the fragment of the URL as a DynamicString
  • compare - compares this URL with another URL and returns a value<0 if the other url is lexicographically earlier or smaller than this url, 0 urls are identical and a value>0 if the other URL is lexicographically later or larger than this URL. The compare should take into consideration which parts of the URL are case-sensitive and which parts of the URL are case-insensitive. Each piece is considered separately. For example, if the scheme is equal continue to the authority and so on.
  • compareIgnoreFrag - compares this URL with another URL and returns a value<0 if the other url is lexicographically earlier or smaller than this url, 0 urls are identical and a value>0 if the other URL is lexicographically later or larger than this URL. The compare should take into consideration which parts of the URL are case-sensitive and which parts of the URL are case-insensitive. Each piece is considered separately. For example, if the scheme is equal continue to the authority and so on. Also, the fragment should be ignored.
  • isHTML - returns true if the path points to an html document based on the following assumptions. A path is an html document if it ends in one of the following extensions (.html .htm .shtml .cgi .jsp .asp .aspx .php .pl .cfm .py) or if the path does not have an extension.

Starter code is provided and you will need to modify the files appropriately. Your code should pass all of the test cases in the URLTest.cpp driver file.

Tips and suggestions:

  • You might want to store each of the pieces (path, query, etc) as a private data member.
  • Make sure to comment each method.

//Url.cpp

#include "Url.h" #include

using std::ostream; using std::invalid_argument;

Url::Url(const DynamicString& other):DynamicString(other){ }

Url::Url(const char* url_):DynamicString(url_){ }

DynamicString Url::getUrl() const{ return "a"; }

DynamicString Url::getScheme() const{ return "a"; }

DynamicString Url::getAuthority() const{ return "a"; }

DynamicString Url::getPath() const{ return "a"; }

DynamicString Url::getQuery() const{ return "a"; }

DynamicString Url::getFragment() const{ return "a"; }

int Url::compare(const Url & other) const{ return -1; }

int Url::compareIgnoreFrag(const Url & other) const{ return -1; }

//Url.h

#ifndef URL #define URL /**This is the Url class. Its job is to manage url links * methods are provided to validate parse and resolve urls. * @param */

#include "DynamicString.h" #include

class Url: public DynamicString{

public:

Url(const DynamicString& other); Url(const char* url_);

DynamicString getUrl() const;

DynamicString getScheme() const;

DynamicString getAuthority() const;

DynamicString getPath() const;

DynamicString getQuery() const;

DynamicString getFragment() const;

int compare(const Url & other) const;

int compareIgnoreFrag(const Url & other) const;

}; #endif

//DynamicString.h

#ifndef DYNAMIC_STRING_H #define DYNAMIC_STRING_H

class DynamicString{

public: /** * Calculate the length of the c-string * @param str The c-string whose length will be determined * @return The number of characters in the c-string */ static int myStringLen(const char* str);

/** * c-string constructor. Creates a copy of * the provided c-string, allocating the * required amount of memory. * @param str the c-string to store. */ DynamicString(const char* str);

/** Get the number of chars in the string * Assignment:Part1 */ int len() const;

/** Get a pointer to the char buffer * Assignment:Part1 */ const char* c_str() const;

/** Get a reference to the character at the specified position. * @param position the 0-based index to retreive. * @return the character at the specified position. */ char& char_at(int position);

/** Get a copy of the character at the specified position. * @param position the 0-based index to retreive. * @return the character at the specified position. */ char char_at(int position) const;

/** Get a reference to the character at the specified position. * Assignment:Part1 * @param position the 0-based index to retreive. * @return the character at the specified position. */ char& operator[](int position);

/** Get a copy of the character at the specified position. * @param position the 0-based index to retreive. * @return the character at the specified position. */ char operator[](int position) const;

/** Returns true if other is a prefix of this string. * @param other the string to check as a prefix. * @return bool true if other is a prefix. */ bool isPrefix(const DynamicString& other) const;

/** Returns true if other is a prefix of this string * ignoring character case. * @param other the string to check as a prefix. * @return bool true if other is a prefix. */ bool isIPrefix(const DynamicString& other) const;

/** Returns true if other is a suffix of this string. * @param other the string to check as a suffix. * @return bool true if other is a suffix. */ bool isSuffix(const DynamicString& other) const;

/** Returns true if other is a suffix of this string * ignoring character case. * @param other the string to check as a suffix. * @return bool true if other is a suffix. */ bool isISuffix(const DynamicString& other) const;

/** Compares this string to other. * @param other the string to compare. * @return -1 if this string is smaller, 0 if they are the same, and 1 if this string is larger. */ int compare(const DynamicString& other) const;

/** Compares this string to other ignoring case. * @param other the string to compare. * @return -1 if this string is smaller, 0 if they are the same, and 1 if this string is larger. */ int iCompare(const DynamicString& other) const; /** Converts all characters to lowercase. * Contents are modified "in-place" * @return self (the lowercased string) */ DynamicString& toLower();

/** Converts all characters to uppercase. * Contents are modified "in-place" * @return self (the uppercased string) */ DynamicString& toUpper();

/** * Default constructor. Initializes an empty string. */ DynamicString();

/** * Copy-constructor. Performs a deep-copy. * Assignment:Part2 * @param other the DynamicString to copy */ DynamicString(const DynamicString& other);

/** * Destructor. Releases memory resources" */ ~DynamicString();

/** * Assignment operator. Performs a deep-copy. * @param other the DynamicString to copy * @return self which now contains a deep-copy of other */ DynamicString& operator=(const DynamicString& other);

/** Removes leading and trailing whitespace. * The original DynamicString is not modified. * @return a DynamicString with all leading and * trailing whitespace characters removed. */ DynamicString& trim();

/** Returns a concatenation of this string and other. * The original strings are not modified. * @param other the string to append. * @returns the concatenated DynamicString * @throws out_of_range error if the string does not fit in the buffer. */ DynamicString operator+(const DynamicString& other) const;

/** Returns a concatenation of this string and other. * The original strings are not modified. * @param other the string to append. * @returns the concatenated DynamicString * @throws out_of_range error if the string does not fit in the buffer. */ DynamicString concat(const DynamicString& other) const;

/** Finds the first index of the character in the string * @param start the position of the string to start at * @param c the char to look for * @return the index of the character or -1 if the character is not found. */ int find(int start, char c) const;

/** Finds the last index of the character in the string * @param start the position of the string to end at * @param c the char to look for * @return the index of the character or -1 if the character is not found. * Assignment:Part1 */ int reverseFind(int start, char c) const;

/** Copy a sub-string into the buffer * @param buf the char buffer to copy the substring into * @param start the index to start copying from (inclusive) * @param end the index to stop copying at (exclusive) */ void subStr(char* buf, int start, int end) const;

private: char* cstr; };

#endif

//project link (more information given in readme file and comments in the header file)

https://github.com/sabitris/url.git

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Demonstrate three aspects of assessing group performance?

Answered: 1 week ago