Back to index

texmacs  1.0.7.15
url.hpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : url.hpp
00004 * DESCRIPTION: unified resource location handling
00005 * COPYRIGHT  : (C) 1999  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #ifndef URL_H
00013 #define URL_H
00014 #include "tree.hpp"
00015 
00016 #define URL_SYSTEM 0
00017 #define URL_UNIX 1
00018 #define URL_STANDARD 2
00019 
00020 /******************************************************************************
00021 * The url data type
00022 ******************************************************************************/
00023 
00024 struct url_rep: concrete_struct {
00025   tree t;
00026   inline url_rep (tree t2): t (t2) {}
00027 };
00028 
00029 class url {
00030   CONCRETE(url);
00031 private:
00032   url (tree t): rep (tm_new<url_rep> (t)) {}
00033 public:
00034   url (const char* name);
00035   url (string name);
00036   url (string dir, string name);
00037   inline bool operator == (url u) { return rep->t == u->t; }
00038   inline bool operator != (url u) { return rep->t != u->t; }
00039   inline url operator [] (int i) { return url (rep->t[i]); }
00040   friend url as_url (tree t);
00041 };
00042 CONCRETE_CODE(url);
00043 
00044 tm_ostream& operator << (tm_ostream& out, url u);
00045 string as_string (url u, int type= URL_SYSTEM);
00046 inline url as_url(tree t) { return url(t); }
00047 
00048 /******************************************************************************
00049 * url constructors
00050 ******************************************************************************/
00051 
00052 url url_general (string name, int type);
00053 url url_unix (string name);
00054 url url_unix (string dir, string name);
00055 url url_system (string name);
00056 url url_system (string dir, string name);
00057 url url_standard (string name);
00058 url url_standard (string dir, string name);
00059 
00060 inline url url_none () { return as_url (tuple ("none")); }
00061 inline url url_here () { return as_url (tree (".")); }
00062 inline url url_parent () { return as_url (tree ("..")); }
00063 inline url url_ancestor () { return as_url (tree ("...")); }
00064 inline url url_pwd () { return url_system ("$PWD"); }
00065 
00066 url url_root (string protocol);       // root url
00067 url url_ramdisc (string contents);    // ramdisc with contents contents
00068 url url_wildcard ();                  // any url
00069 url url_wildcard (string name);       // string with * wildcards
00070 
00071 url operator * (url u1, url u2);      // concatenation of url with rootless url
00072 url operator * (url u1, const char* name);
00073 url operator * (url u1, string name);
00074 url operator | (url u1, url u2);      // disjunction of urls like in file paths
00075 
00076 inline url url_parent (url u) { return u * url_parent (); }
00077 
00078 /******************************************************************************
00079 * predicates
00080 ******************************************************************************/
00081 
00082 inline bool is_none (url u) { return is_tuple (u->t, "none", 0); }
00083 inline bool is_here (url u) { return u->t == "."; }
00084 inline bool is_parent (url u) { return u->t == ".."; }
00085 inline bool is_ancestor (url u) { return u->t == "..."; }
00086 inline bool is_atomic (url u) { return is_atomic (u->t); }
00087 inline bool is_concat (url u) { return is_tuple (u->t, "concat", 2); }
00088 inline bool is_or (url u) { return is_tuple (u->t, "or", 2); }
00089 inline bool is_root (url u) {
00090   return is_tuple (u->t, "root") && (N(u->t) >= 2); }
00091 inline bool is_root (url u, string s) {
00092   return is_root (u) && (u[1]->t->label == s); }
00093 inline bool is_root_web (url u) {
00094   return is_root (u, "http") || is_root (u, "ftp") || is_root (u, "blank"); }
00095 inline bool is_root_tmfs (url u) { return is_root (u, "tmfs"); }
00096 inline bool is_root_blank (url u) { return is_root (u, "blank"); }
00097 inline bool is_wildcard (url u) { return is_tuple (u->t, "wildcard"); }
00098 inline bool is_wildcard (url u, int n) {
00099   return is_tuple (u->t, "wildcard", n); }
00100 
00101 bool is_rooted (url u);
00102 bool is_rooted (url u, string protocol);
00103 bool is_rooted_web (url u);
00104 bool is_rooted_tmfs (url u);
00105 bool is_rooted_blank (url u);
00106 bool is_name (url u);
00107 bool is_rooted_name (url u);
00108 bool is_path (url u);
00109 bool is_rooted_path (url u);
00110 bool is_ramdisc (url u);
00111 
00112 /******************************************************************************
00113 * operations on urls
00114 ******************************************************************************/
00115 
00116 url    head (url u);               // keep only the directory of the file
00117 url    tail (url u);               // keep only the file name without path
00118 string suffix (url u);             // get suffix of file
00119 url    glue (url u, string s);     // glue suffix to url tail
00120 url    unglue (url u, int nr);     // remove nr chars from suffix
00121 url    unblank (url u);            // a/b/ -> a/b
00122 url    relative (url base, url u); // a/b, c -> a/c
00123 url    delta (url base, url u);    // relative (a, delta (a, b)) == b
00124 url    reroot (url u, string s);   // reroot using new protocol
00125 url    expand (url u);             // rewrite a/{b:c} -> a/b:a/c
00126 url    sort (url u);               // order items in ors
00127 url    factor (url u);             // inverse of expand; also sorts
00128 bool   descends (url u, url base); // does u descend from base?
00129 bool   is_secure (url u);          // is u secure?
00130 
00131 /******************************************************************************
00132 * url resolution
00133 ******************************************************************************/
00134 
00135 url  complete (url u, string filter= "fr"); // wildcard completion
00136 url  resolve (url u, string filter= "fr");  // find first match only
00137 url  resolve_in_path (url u);               // find file in path
00138 bool exists (url u);                        // file exists
00139 bool exists_in_path (url u);                // file exists in path
00140 bool has_permission (url u, string filter); // check file permissions
00141 url  descendance (url u);                   // utility for style&package menus
00142 string concretize (url u);                  // system name for resolved url
00143 string materialize (url u, string f= "fr"); // resolve + concretize
00144 
00145 #endif // defined URL_H