1 
2 /******************************************************************************
3 * MODULE     : url.hpp
4 * DESCRIPTION: unified resource location handling
5 * COPYRIGHT  : (C) 1999  Joris van der Hoeven
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11 
12 #ifndef URL_H
13 #define URL_H
14 #include "tree.hpp"
15 
16 #define URL_SYSTEM 0
17 #define URL_UNIX 1
18 #define URL_STANDARD 2
19 
20 /******************************************************************************
21 * The url data type
22 ******************************************************************************/
23 
24 struct url_rep: concrete_struct {
25   tree t;
url_repurl_rep26   inline url_rep (tree t2): t (t2) {}
27 };
28 
29 class url {
30   CONCRETE(url);
31 private:
url(tree t)32   url (tree t): rep (tm_new<url_rep> (t)) {}
33 public:
34   url ();
35   url (const char* name);
36   url (string name);
37   url (string dir, string name);
operator ==(url u)38   inline bool operator == (url u) { return rep->t == u->t; }
operator !=(url u)39   inline bool operator != (url u) { return rep->t != u->t; }
operator [](int i)40   inline url operator [] (int i) { return url (rep->t[i]); }
41   friend url as_url (tree t);
42 };
43 CONCRETE_CODE(url);
44 
45 tm_ostream& operator << (tm_ostream& out, url u);
as_url(tree t)46 inline url as_url (tree t) { return url(t); }
47 string as_string (url u, int type= URL_SYSTEM);
as_tree(url u)48 inline tree as_tree (url u) { return tree (u->t); }
as_system_string(url u)49 inline string as_system_string (url u) { return as_string (u, URL_SYSTEM); }
as_unix_string(url u)50 inline string as_unix_string (url u) { return as_string (u, URL_UNIX); }
as_standard_string(url u)51 inline string as_standard_string (url u) { return as_string (u,URL_STANDARD); }
52 
53 /******************************************************************************
54 * url constructors
55 ******************************************************************************/
56 
57 url url_general (string name, int type);
58 url url_unix (string name);
59 url url_unix (string dir, string name);
60 url url_system (string name);
61 url url_system (string dir, string name);
62 url url_standard (string name);
63 url url_standard (string dir, string name);
64 
url_none()65 inline url url_none () { return as_url (tuple ("none")); }
url_here()66 inline url url_here () { return as_url (tree (".")); }
url_parent()67 inline url url_parent () { return as_url (tree ("..")); }
url_ancestor()68 inline url url_ancestor () { return as_url (tree ("...")); }
url_pwd()69 inline url url_pwd () { return url_system ("$PWD"); }
70 
71 url url_root (string protocol);       // root url
72 url url_ramdisc (string contents);    // ramdisc with contents contents
73 url url_wildcard ();                  // any url
74 url url_wildcard (string name);       // string with * wildcards
75 
76 url operator * (url u1, url u2);      // concatenation of url with rootless url
77 url operator * (url u1, const char* name);
78 url operator * (url u1, string name);
79 url operator | (url u1, url u2);      // disjunction of urls like in file paths
80 
url_parent(url u)81 inline url url_parent (url u) { return u * url_parent (); }
82 
83 /******************************************************************************
84 * predicates
85 ******************************************************************************/
86 
is_none(url u)87 inline bool is_none (url u) { return is_tuple (u->t, "none", 0); }
is_here(url u)88 inline bool is_here (url u) { return u->t == "."; }
is_parent(url u)89 inline bool is_parent (url u) { return u->t == ".."; }
is_ancestor(url u)90 inline bool is_ancestor (url u) { return u->t == "..."; }
is_atomic(url u)91 inline bool is_atomic (url u) { return is_atomic (u->t); }
is_concat(url u)92 inline bool is_concat (url u) { return is_tuple (u->t, "concat", 2); }
is_or(url u)93 inline bool is_or (url u) { return is_tuple (u->t, "or", 2); }
is_root(url u)94 inline bool is_root (url u) {
95   return is_tuple (u->t, "root") && (N(u->t) >= 2); }
is_root(url u,string s)96 inline bool is_root (url u, string s) {
97   return is_root (u) && (u[1]->t->label == s); }
is_root_web(url u)98 inline bool is_root_web (url u) {
99   return is_root (u, "http") || is_root (u, "https") || is_root (u, "ftp") ||
100          is_root (u, "blank"); }
is_root_tmfs(url u)101 inline bool is_root_tmfs (url u) { return is_root (u, "tmfs"); }
is_root_blank(url u)102 inline bool is_root_blank (url u) { return is_root (u, "blank"); }
is_wildcard(url u)103 inline bool is_wildcard (url u) { return is_tuple (u->t, "wildcard"); }
is_wildcard(url u,int n)104 inline bool is_wildcard (url u, int n) {
105   return is_tuple (u->t, "wildcard", n); }
is_pseudo_atomic(url u)106 inline bool is_pseudo_atomic (url u) {
107   return is_atomic (u->t) || is_tuple (u->t, "wildcard", 1); }
108 
109 bool is_rooted (url u);
110 bool is_rooted (url u, string protocol);
111 bool is_rooted_web (url u);
112 bool is_rooted_tmfs (url u);
113 bool is_rooted_tmfs (url u, string sub_protocol);
114 bool is_rooted_blank (url u);
115 bool is_name (url u);
116 bool is_rooted_name (url u);
117 bool is_path (url u);
118 bool is_rooted_path (url u);
119 bool is_ramdisc (url u);
120 
121 /******************************************************************************
122 * operations on urls
123 ******************************************************************************/
124 
125 url    head (url u);                 // keep only the directory of the file
126 url    tail (url u);                 // keep only the file name without path
127 string suffix (url u);               // get suffix of file
128 string basename (url u, string suf); // get basename of file with given suffix
129 string basename (url u);             // get basename of file
130 url    glue (url u, string s);       // glue suffix to url tail
131 url    unglue (url u, int nr);       // remove nr chars from suffix
132 url    unblank (url u);              // a/b/ -> a/b
133 url    relative (url base, url u);   // a/b, c -> a/c
134 url    delta (url base, url u);      // relative (a, delta (a, b)) == b
135 string get_root (url u);             // get root
136 url    unroot (url u);               // remove root
137 url    reroot (url u, string s);     // reroot using new protocol
138 url    expand (url u);               // rewrite a/{b:c} -> a/b:a/c
139 url    sort (url u);                 // order items in ors
140 url    factor (url u);               // inverse of expand; also sorts
141 bool   descends (url u, url base);   // does u descend from base?
142 bool   is_secure (url u);            // is u secure?
143 
144 /******************************************************************************
145 * url resolution
146 ******************************************************************************/
147 
148 url  complete (url u, string filter= "fr"); // wildcard completion
149 url  resolve (url u, string filter= "fr");  // find first match only
150 url  resolve_in_path (url u);               // find file in path
151 bool exists (url u);                        // file exists
152 bool exists_in_path (url u);                // file exists in path
153 bool has_permission (url u, string filter); // check file permissions
154 url  descendance (url u);                   // utility for style&package menus
155 string concretize (url u);                  // system name for resolved url
156 string materialize (url u, string f= "fr"); // resolve + concretize
157 
158 #endif // defined URL_H
159