00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef RAUL_URI_HPP
00019 #define RAUL_URI_HPP
00020
00021 #include <string>
00022 #include <cstring>
00023 #include <exception>
00024 #include <ostream>
00025 #include <glib.h>
00026 #include "raul/Atom.hpp"
00027
00028 namespace Raul {
00029
00030
00036 class URI {
00037 public:
00038 class BadURI : public std::exception {
00039 public:
00040 BadURI(const std::string& uri) : _uri(uri) {}
00041 ~BadURI() throw() {}
00042 const char* what() const throw() { return _uri.c_str(); }
00043 private:
00044 std::string _uri;
00045 };
00046
00052 URI(const std::basic_string<char>& uri="nil:0") : _str(g_intern_string(uri.c_str())) {
00053 if (!is_valid(uri))
00054 throw BadURI(uri);
00055 }
00056
00062 URI(const char* uri) : _str(g_intern_string(uri)) {
00063 if (!is_valid(uri))
00064 throw BadURI(uri);
00065 }
00066
00067 static bool is_valid(const std::basic_string<char>& uri) {
00068 return uri.find(":") != std::string::npos;
00069 }
00070
00072 inline const std::string chop_start(const std::string& str) const {
00073 return substr(find(str) + str.length());
00074 }
00075
00077 std::string chop_scheme() const { return chop_start(":"); }
00078
00080 inline std::string scheme() const { return substr(0, find(":")); }
00081
00082 inline const std::string str() const { return _str; }
00083 inline const char* c_str() const { return _str; }
00084
00085 inline std::string substr(size_t start, size_t end=std::string::npos) const {
00086 return str().substr(start, end);
00087 }
00088
00089 inline bool operator<(const URI& uri) const { return strcmp(_str, uri.c_str()) < 0; }
00090 inline bool operator<=(const URI& uri) const { return (*this) == uri || (*this) < uri; }
00091 inline bool operator==(const URI& uri) const { return _str == uri._str; }
00092 inline bool operator!=(const URI& uri) const { return _str != uri._str; }
00093
00094 inline size_t length() const { return str().length(); }
00095 inline size_t find(const std::string& s) const { return str().find(s); }
00096 inline size_t find_last_of(char c) const { return str().find_last_of(c); }
00097
00098 inline operator Raul::Atom() const { return Raul::Atom(_str, 12345); }
00099
00100 private:
00101 const char* _str;
00102 };
00103
00104 static inline
00105 std::ostream&
00106 operator<<(std::ostream& os, const URI& uri)
00107 {
00108 return (os << uri.c_str());
00109 }
00110
00111 }
00112
00113 #endif // RAUL_URI_HPP
00114