• Main Page
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

URI.hpp

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 
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 } // namespace Raul
00112 
00113 #endif // RAUL_URI_HPP
00114 

Generated on Wed Sep 29 2010 for RAUL by  doxygen 1.7.1