RAUL
0.8.0
|
00001 /* This file is part of Raul. 00002 * Copyright (C) 2007-2009 David Robillard <http://drobilla.net> 00003 * 00004 * Raul is free software; you can redistribute it and/or modify it under the 00005 * terms of the GNU General Public License as published by the Free Software 00006 * Foundation; either version 2 of the License, or (at your option) any later 00007 * version. 00008 * 00009 * Raul is distributed in the hope that it will be useful, but WITHOUT ANY 00010 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00011 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. 00012 * 00013 * You should have received a copy of the GNU General Public License along 00014 * with this program; if not, write to the Free Software Foundation, Inc., 00015 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 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 00037 class URI { 00038 public: 00039 class BadURI : public std::exception { 00040 public: 00041 BadURI(const std::string& uri) : _uri(uri) {} 00042 ~BadURI() throw() {} 00043 const char* what() const throw() { return _uri.c_str(); } 00044 private: 00045 std::string _uri; 00046 }; 00047 00053 URI(const std::basic_string<char>& uri="nil:0") : _str(g_intern_string(uri.c_str())) { 00054 if (!is_valid(uri)) 00055 throw BadURI(uri); 00056 } 00057 00063 URI(const char* uri) : _str(g_intern_string(uri)) { 00064 if (!is_valid(uri)) 00065 throw BadURI(uri); 00066 } 00067 00068 static bool is_valid(const std::basic_string<char>& uri) { 00069 return uri.find(":") != std::string::npos; 00070 } 00071 00073 inline const std::string chop_start(const std::string& str) const { 00074 return substr(find(str) + str.length()); 00075 } 00076 00078 std::string chop_scheme() const { return chop_start(":"); } 00079 00081 inline std::string scheme() const { return substr(0, find(":")); } 00082 00083 inline const std::string str() const { return _str; } 00084 inline const char* c_str() const { return _str; } 00085 00086 inline std::string substr(size_t start, size_t end=std::string::npos) const { 00087 return str().substr(start, end); 00088 } 00089 00090 inline bool operator<(const URI& uri) const { return strcmp(_str, uri.c_str()) < 0; } 00091 inline bool operator<=(const URI& uri) const { return (*this) == uri || (*this) < uri; } 00092 inline bool operator==(const URI& uri) const { return _str == uri._str; } 00093 inline bool operator!=(const URI& uri) const { return _str != uri._str; } 00094 00095 inline size_t length() const { return str().length(); } 00096 inline size_t find(const std::string& s) const { return str().find(s); } 00097 inline size_t find_last_of(char c) const { return str().find_last_of(c); } 00098 00099 inline operator Raul::Atom() const { return Raul::Atom(_str, 12345); } 00100 00101 private: 00102 const char* _str; 00103 }; 00104 00105 static inline 00106 std::ostream& 00107 operator<<(std::ostream& os, const URI& uri) 00108 { 00109 return (os << uri.c_str()); 00110 } 00111 00112 } // namespace Raul 00113 00114 #endif // RAUL_URI_HPP 00115