RAUL  0.8.0
URI.hpp
1 /* This file is part of Raul.
2  * Copyright (C) 2007-2009 David Robillard <http://drobilla.net>
3  *
4  * Raul is free software; you can redistribute it and/or modify it under the
5  * terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
18 #ifndef RAUL_URI_HPP
19 #define RAUL_URI_HPP
20 
21 #include <string>
22 #include <cstring>
23 #include <exception>
24 #include <ostream>
25 #include <glib.h>
26 #include "raul/Atom.hpp"
27 
28 namespace Raul {
29 
30 
37 class URI {
38 public:
39  class BadURI : public std::exception {
40  public:
41  BadURI(const std::string& uri) : _uri(uri) {}
42  ~BadURI() throw() {}
43  const char* what() const throw() { return _uri.c_str(); }
44  private:
45  std::string _uri;
46  };
47 
53  URI(const std::basic_string<char>& uri="nil:0") : _str(g_intern_string(uri.c_str())) {
54  if (!is_valid(uri))
55  throw BadURI(uri);
56  }
57 
63  URI(const char* uri) : _str(g_intern_string(uri)) {
64  if (!is_valid(uri))
65  throw BadURI(uri);
66  }
67 
68  static bool is_valid(const std::basic_string<char>& uri) {
69  return uri.find(":") != std::string::npos;
70  }
71 
73  inline const std::string chop_start(const std::string& str) const {
74  return substr(find(str) + str.length());
75  }
76 
78  std::string chop_scheme() const { return chop_start(":"); }
79 
81  inline std::string scheme() const { return substr(0, find(":")); }
82 
83  inline const std::string str() const { return _str; }
84  inline const char* c_str() const { return _str; }
85 
86  inline std::string substr(size_t start, size_t end=std::string::npos) const {
87  return str().substr(start, end);
88  }
89 
90  inline bool operator<(const URI& uri) const { return strcmp(_str, uri.c_str()) < 0; }
91  inline bool operator<=(const URI& uri) const { return (*this) == uri || (*this) < uri; }
92  inline bool operator==(const URI& uri) const { return _str == uri._str; }
93  inline bool operator!=(const URI& uri) const { return _str != uri._str; }
94 
95  inline size_t length() const { return str().length(); }
96  inline size_t find(const std::string& s) const { return str().find(s); }
97  inline size_t find_last_of(char c) const { return str().find_last_of(c); }
98 
99  inline operator Raul::Atom() const { return Raul::Atom(_str, 12345); }
100 
101 private:
102  const char* _str;
103 };
104 
105 static inline
106 std::ostream&
107 operator<<(std::ostream& os, const URI& uri)
108 {
109  return (os << uri.c_str());
110 }
111 
112 } // namespace Raul
113 
114 #endif // RAUL_URI_HPP
115 
A piece of data with some type.
Definition: Atom.hpp:43
URI(const std::basic_string< char > &uri="nil:0")
Construct a URI from an std::string.
Definition: URI.hpp:53
Simple wrapper around standard string with useful URI-specific methods.
Definition: URI.hpp:37
URI(const char *uri)
Construct a URI from a C string.
Definition: URI.hpp:63
const std::string chop_start(const std::string &str) const
Return path with everything up to and including the first occurence of str chopped.
Definition: URI.hpp:73
Definition: Array.hpp:26
std::string chop_scheme() const
Return the URI with the scheme removed (as a string)
Definition: URI.hpp:78
std::string scheme() const
Return the URI scheme (everything before the first ':')
Definition: URI.hpp:81