131 lines
3.0 KiB
C++
Executable File
131 lines
3.0 KiB
C++
Executable File
// TJP COPYRIGHT HEADER
|
|
|
|
#pragma once
|
|
|
|
#include <string>
|
|
#include <tjp/core/exception/Exception.hpp>
|
|
|
|
namespace tjp {
|
|
namespace core {
|
|
namespace internet {
|
|
|
|
/**
|
|
* a URL is defined as a protocal specifying string + a server string + a directory
|
|
* + a (optional) file. Such as ftp://myserver/mydirectory/myfile.
|
|
*
|
|
* For the purposes of this application a URL may be created using a parent URL, in
|
|
* which case, if the protocal is NOT specified, the URL is assumed to be a RELATIVE
|
|
* URL. In this case, only the directory and file are used.. So, the URL:
|
|
* "/directory/myfile" created with the parent URL - "ftp://myserver/mydirectory"
|
|
* on construction would expand to: "ftp://myserver/mydirectory/directory/myfile"
|
|
*
|
|
* I am not sure whether this is exactly the standard or not, perhaps it should
|
|
* be modified later.
|
|
*/
|
|
class URL
|
|
{
|
|
public:
|
|
/**
|
|
* The single exception thrown by the url class on errors
|
|
*/
|
|
DECLARE_EXCEPTION (Exception);
|
|
|
|
protected:
|
|
std::string protocol;
|
|
std::string server;
|
|
std::string directory;
|
|
std::string file;
|
|
|
|
bool isAbsoluteURL (const std::string &);
|
|
void parseAbsoluteURL (const std::string &);
|
|
void parseRelativeURL (const std::string &);
|
|
|
|
public:
|
|
/**
|
|
* default constructor
|
|
*/
|
|
URL ();
|
|
|
|
/**
|
|
* throws an exception if the protocal is unknown, or if
|
|
* the path somehow is invalid, such as "ftp://me.com/../SystemData"
|
|
* because that dot.dot my cause the resource to point to our files.
|
|
*/
|
|
URL (const std::string &);
|
|
|
|
/**
|
|
* when given a parent URL, if the child URL does not have a
|
|
* protocal or server it is interpreted as a relative URL
|
|
*
|
|
* relative URL's are resolved on construction, pointers are not
|
|
* kept hanging around
|
|
*/
|
|
URL (const URL &parent, const std::string &);
|
|
|
|
/**
|
|
* copy constructor
|
|
*/
|
|
URL (const URL &);
|
|
|
|
/**
|
|
* destructor
|
|
*/
|
|
virtual ~URL ();
|
|
|
|
/**
|
|
* returns the string name of the protocal in string form
|
|
*/
|
|
const std::string &getProtocol () const;
|
|
|
|
/**
|
|
* returns the server segment on the string (server:port)
|
|
*/
|
|
const std::string &getServer() const;
|
|
|
|
/**
|
|
* returns the file segment of the string (file_path)
|
|
*/
|
|
std::string getPath () const;
|
|
|
|
/**
|
|
* returns the directory portion of the path
|
|
*/
|
|
const std::string &getDirectory () const;
|
|
|
|
/**
|
|
* returns the file portion of the path
|
|
*/
|
|
const std::string &getFile () const;
|
|
|
|
/**
|
|
* returns the file portion of the path
|
|
*/
|
|
static URL join(const URL &, const std::string &);
|
|
|
|
/**
|
|
* converts the URL into a string
|
|
*/
|
|
operator std::string() const;
|
|
|
|
/**
|
|
* comparison operators for container classes
|
|
*/
|
|
bool operator <(const URL &right) const;
|
|
bool operator ==(const URL &right) const;
|
|
bool operator !=(const URL &right) const;
|
|
|
|
//==========================================================
|
|
|
|
/**
|
|
* for comparisons
|
|
*/
|
|
static const URL Empty;
|
|
} ;
|
|
|
|
std::ostream &operator <<(std::ostream &, const URL &);
|
|
|
|
} // namespace utilities
|
|
} // namespace utilities
|
|
} // namespace
|
|
|