Lolly 1.4.28
Loading...
Searching...
No Matches
Functions
url.cpp File Reference
#include "url.hpp"
#include "analyze.hpp"
#include "sys_utils.hpp"
#include "tbox/tbox.h"
#include <ctype.h>
Include dependency graph for url.cpp:

Go to the source code of this file.

Functions

static url_tree url_tuple (string label)
 
static url_tree url_tuple (string label, string value)
 
static url_tree url_tuple (string label, string value, string content)
 
static url_tree url_tuple (string label, url_tree t1, url_tree t2)
 
static bool is_tuple (url_tree t, string s)
 
static bool is_tuple (url_tree t, const char *s)
 
static bool is_tuple (url_tree t, string s, int n)
 
static bool is_tuple (url_tree t, const char *s, int n)
 
url url_none ()
 
url url_root (string protocol)
 
url url_wildcard ()
 
url url_wildcard (string name)
 
void skip_ipv6 (string s, int &i)
 
static bool heuristic_is_path (string name, int type)
 
static bool heuristic_is_default (string name, int type)
 
static bool heuristic_is_http (string name)
 
static bool heuristic_is_ftp (string name)
 
static bool heuristic_is_mingw_default (string name, int type)
 
static url url_get_atom (string s, int type)
 
url url_get_name (string s, int type, int i)
 
static url url_get_path (string s, int type=URL_STANDARD, int i=0)
 
url url_path (string s, int type)
 
static url url_mingw_default (string name, int type)
 
static url url_default (string name, int type=URL_SYSTEM)
 
url url_general (string name, int type=URL_SYSTEM)
 
url url_system (string name)
 
url url_system (string dir, string name)
 
url url_standard (string name)
 
url url_standard (string dir, string name)
 
url url_pwd ()
 
url url_ramdisc (string contents)
 
static bool is_special_root (url u)
 
static bool is_semi_root (url u)
 
url head (url u)
 
url tail (url u)
 
string suffix (url u, bool use_locase)
 
string suffix (url u)
 
string basename (url u, string suf)
 
string basename (url u)
 
url glue (url u, string s)
 
url unglue (url u, int nr)
 
url unblank (url u)
 
url relative (url base, url u)
 
url delta_sub (url base, url u)
 
url delta (url base, url u)
 
string get_root (url u)
 
url unroot (url u)
 
url reroot (url u, string protocol)
 
static url expand (url u1, url u2)
 
url expand (url u)
 
static bool operator<= (url u1, url u2)
 
static url sort_sub (url add, url to)
 
url sort (url u)
 
static url factor_sorted (url u)
 
static url factor_sub (url u)
 
url factor (url u)
 
bool descends (url u, url base)
 
url operator* (url u1, url u2)
 
url operator* (url u1, const char *name)
 
url operator* (url u1, string name)
 
url url_concat (url u1, url u2)
 
url operator| (url u1, url u2)
 
url url_or (url u1, url u2)
 
bool is_atomic (url u)
 
bool is_root (url u, string s)
 
bool is_root_web (url u)
 
bool is_wildcard (url u, int n)
 
bool is_pseudo_atomic (url u)
 
bool is_rooted (url u)
 
bool is_rooted (url u, string protocol)
 
bool is_rooted_web (url u)
 
bool is_name (url u)
 
bool is_rooted_name (url u)
 
bool is_name_in_path (url u)
 
bool is_ramdisc (url u)
 
string as_string (url u, int type)
 
tm_ostreamoperator<< (tm_ostream &out, url u)
 

Detailed Description

Unitests for url.

Author
Darcy
Date
2019-2023

Definition in file url.cpp.

Function Documentation

◆ url_tuple() [1/4]

static url_tree url_tuple ( string label)
static

Definition at line 23 of file url.cpp.

23 {
24 return url_tree (URL_TUPLE, url_tree (label));
25}
lolly_tree< int > url_tree
Definition url.hpp:26
#define URL_TUPLE
Definition url.hpp:20

◆ url_tuple() [2/4]

static url_tree url_tuple ( string label,
string value )
static

Definition at line 28 of file url.cpp.

28 {
29 return url_tree (URL_TUPLE, url_tree (label), url_tree (value));
30}

◆ url_tuple() [3/4]

static url_tree url_tuple ( string label,
string value,
string content )
static

Definition at line 33 of file url.cpp.

33 {
34 return url_tree (URL_TUPLE, url_tree (label), url_tree (value),
36}
The list class represents a linked list.
Definition list.hpp:48

◆ url_tuple() [4/4]

static url_tree url_tuple ( string label,
url_tree t1,
url_tree t2 )
static

Definition at line 39 of file url.cpp.

39 {
40 return url_tree (URL_TUPLE, url_tree (label), t1, t2);
41}
blackbox t1
blackbox t2

◆ is_tuple() [1/4]

static bool is_tuple ( url_tree t,
string s )
inlinestatic

Definition at line 44 of file url.cpp.

44 {
45 return (t->op == URL_TUPLE) && (N (t) >= 1) && (t[0] == s);
46}
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
blackbox t[13]

◆ is_tuple() [2/4]

static bool is_tuple ( url_tree t,
const char * s )
inlinestatic

Definition at line 48 of file url.cpp.

48 {
49 return (t->op == URL_TUPLE) && (N (t) >= 1) && (t[0] == s);
50}

◆ is_tuple() [3/4]

static bool is_tuple ( url_tree t,
string s,
int n )
inlinestatic

Definition at line 52 of file url.cpp.

52 {
53 return (t->op == URL_TUPLE) && (N (t) == (n + 1)) && (t[0] == s);
54}

◆ is_tuple() [4/4]

static bool is_tuple ( url_tree t,
const char * s,
int n )
inlinestatic

Definition at line 56 of file url.cpp.

56 {
57 return (t->op == URL_TUPLE) && (N (t) == (n + 1)) && (t[0] == s);
58}

◆ url_none()

url url_none ( )

Definition at line 91 of file url.cpp.

91 {
92 return as_url (url_tuple ("none"));
93}
static url_tree url_tuple(string label)
Definition url.cpp:23
url as_url(url_tree t)
Definition url.hpp:61

◆ url_root()

url url_root ( string protocol)

Definition at line 96 of file url.cpp.

96 {
97 return as_url (url_tuple ("root", protocol));
98}

◆ url_wildcard() [1/2]

url url_wildcard ( )

Definition at line 101 of file url.cpp.

101 { // any url
102 return as_url (url_tuple ("wildcard"));
103}

◆ url_wildcard() [2/2]

url url_wildcard ( string name)

Definition at line 106 of file url.cpp.

106 { // string with * wildcards
107 return as_url (url_tuple ("wildcard", name));
108}

◆ skip_ipv6()

void skip_ipv6 ( string s,
int & i )

Definition at line 114 of file url.cpp.

114 {
115 i++;
116 while (i < N (s) &&
117 (s[i] == ':' || (s[i] >= '0' && s[i] <= '9') ||
118 (s[i] >= 'a' && s[i] <= 'f') || (s[i] >= 'A' && s[i] <= 'F')))
119 i++;
120 if (i < N (s) && s[i] == ']') i++;
121}

◆ heuristic_is_path()

static bool heuristic_is_path ( string name,
int type )
static

Definition at line 124 of file url.cpp.

124 {
125 char sep= (type == 0) ? URL_SEPARATOR : ':';
126 int i= 0, n= N (name);
127 while (i < n)
128 if (name[i] == '[') skip_ipv6 (name, i);
129 else if (name[i] == sep) return true;
130 else i++;
131 return false;
132}
#define URL_SEPARATOR
Definition sys_utils.hpp:23
void skip_ipv6(string s, int &i)
Definition url.cpp:114

◆ heuristic_is_default()

static bool heuristic_is_default ( string name,
int type )
static

Definition at line 135 of file url.cpp.

135 {
136#ifdef WINPATHS
137 // FIXME: we probably should take into account 'type' too
138 if (N (name) < 2) return false;
139 if ((name[0] == '\\') && (name[1] == '\\')) return true;
140 return isalpha (name[0]) && (name[1] == ':') &&
141 ((N (name) == 2) || (name[2] == '\\') || (name[2] == '/'));
142#else
143 char sep= (type == 0) ? URL_CONCATER : '/';
144 return (name != "") && (name[0] == sep);
145#endif
146}
#define URL_CONCATER
Definition sys_utils.hpp:22

◆ heuristic_is_http()

static bool heuristic_is_http ( string name)
static

Definition at line 149 of file url.cpp.

149 {
150 return starts (name, "www.");
151 // FIXME: we might want to recognize some other ones like google.com too
152}
bool starts(string s, const char *what)
Definition analyze.cpp:566

◆ heuristic_is_ftp()

static bool heuristic_is_ftp ( string name)
static

Definition at line 155 of file url.cpp.

155 {
156 return starts (name, "ftp.");
157}

◆ heuristic_is_mingw_default()

static bool heuristic_is_mingw_default ( string name,
int type )
static

Definition at line 160 of file url.cpp.

160 {
161#ifdef WINPATHS
162 return type != URL_SYSTEM && N (name) >= 2 && name[0] == '/' &&
163 is_alpha (name[1]) && (N (name) == 2 || name[2] == '/');
164#else
165 (void) name;
166 (void) type;
167 return false;
168#endif
169}
bool is_alpha(string s)
Checks if a string contains only alphabetic characters.
Definition analyze.cpp:46
#define URL_SYSTEM
Definition url.hpp:15

◆ url_get_atom()

static url url_get_atom ( string s,
int type )
static

Definition at line 172 of file url.cpp.

172 {
173 if (type < URL_STANDARD) {
174 if (s == "~") return url_system (get_env ("HOME"));
175 if (starts (s, "$")) {
176 string val= get_env (s (1, N (s)));
177 if (val == "") return url_none ();
178 return unblank (url_system (val));
179 }
180 }
181 if (contains (s, '*')) return url_wildcard (s);
182#ifdef WINPATHS
183 if (N (s) == 2 && ends (s, ":"))
184 s->resize (1); // remove the ':' after unit letter
185#endif
186 return as_url (url_tree (s));
187}
bool ends(string s, const char *what)
Definition analyze.cpp:576
bool contains(T a, array< T > b)
Check if an array contains a specified element.
string get_env(string var)
Definition sys_utils.cpp:25
url url_none()
Definition url.cpp:91
url url_system(string name)
Definition url.cpp:306
url url_wildcard()
Definition url.cpp:101
url unblank(url u)
Definition url.cpp:442
#define URL_STANDARD
Definition url.hpp:17

◆ url_get_name()

url url_get_name ( string s,
int type,
int i )

Definition at line 190 of file url.cpp.

190 {
191 char sep = (type == URL_SYSTEM) ? URL_CONCATER : '/';
192 int n = N (s);
194 while (i < n) {
195 int start= i;
196 while ((i < n) && (s[i] != sep) && (s[i] != '/')) {
197 if (s[i] == '[') skip_ipv6 (s, i);
198 else i++;
199 }
200 if (i > start) {
201 url u = url_get_atom (s (start, i), type);
202 u_list= list (u, u_list);
203 }
204 else if (i == start) {
205 i++;
206 if (i == n) {
207 u_list= list (as_url (url_tree ("")), u_list);
208 }
209 }
210 }
211
212 if (is_nil (u_list)) return as_url (url_tree (""));
213 url ret= u_list->item;
214 u_list = u_list->next;
215 while (!is_nil (u_list)) {
216 url u= u_list->item;
217 if (is_here (u) || (u->t == "")) {
218 // do nothing
219 }
220 else if (is_atomic (u)) {
221 ret= as_url (url_tuple ("concat", u->t, ret->t));
222 }
223 else {
224 ret= u * ret;
225 }
226 u_list= u_list->next;
227 }
228 return ret;
229}
bool is_nil(blackbox x)
Definition blackbox.hpp:29
Definition url.hpp:37
bool is_atomic(url u)
Definition url.cpp:664
static url url_get_atom(string s, int type)
Definition url.cpp:172
bool is_here(url u)
Definition url.hpp:154

◆ url_get_path()

static url url_get_path ( string s,
int type = URL_STANDARD,
int i = 0 )
static

Definition at line 232 of file url.cpp.

232 {
233 char sep = (type == URL_SYSTEM) ? URL_SEPARATOR : ':';
234 int start= i, n= N (s);
235 if (i == n) return url_none ();
236 while ((i < n) && (s[i] != sep)) {
237 if (s[i] == '[') skip_ipv6 (s, i);
238 else i++;
239 }
240 url u= url_general (s (start, i), type);
241 if (i == n) return u;
242 if (start == i) return url_get_path (s, type, i + 1);
243 return u | url_get_path (s, type, i + 1);
244}
static url url_get_path(string s, int type=URL_STANDARD, int i=0)
Definition url.cpp:232
url url_general(string name, int type=URL_SYSTEM)
Definition url.cpp:273

◆ url_path()

url url_path ( string s,
int type )

Definition at line 247 of file url.cpp.

247 {
248 url u= url_get_path (s, type);
249 return u;
250}

◆ url_mingw_default()

static url url_mingw_default ( string name,
int type )
static

Definition at line 253 of file url.cpp.

253 {
254 string s= name (0, 2) * ":" * name (2, N (name));
255 return url_root ("default") * url_get_name (s, type);
256}
url url_root(string protocol)
Definition url.cpp:96
url url_get_name(string s, int type, int i)
Definition url.cpp:190

◆ url_default()

static url url_default ( string name,
int type = URL_SYSTEM )
static

Definition at line 259 of file url.cpp.

259 {
261#ifdef WINPATHS
262 // FIXME: this hack seems a bit too simple
263 if (is_concat (u) && (u[2]->t == "")) u= u[1];
264 // cout << name << " -> " << url_root ("default") * u << "\n";
265 return url_root ("default") * u;
266#else
267 if (u->t == "") return url_root ("default");
268 return url_root ("default") * u;
269#endif
270}
bool is_concat(url u)
Definition url.hpp:174

◆ url_general()

url url_general ( string name,
int type = URL_SYSTEM )

Definition at line 273 of file url.cpp.

273 {
274 if (contains (name, "://")) {
276 string protocol= tokens[0];
277 string path = tokens[1];
278 if (N (tokens) == 2 && is_alphanum (protocol) && N (protocol) >= 2) {
279 if (protocol == "file") return file_url (path);
280 if (protocol == "http") return http_url (path);
281 if (protocol == "https") return https_url (path);
282 if (protocol == "ftp") return ftp_url (path);
283 return url_root (tokens[0]) * url_get_name (tokens[1], type);
284 }
285 }
286 if (starts (name, "local:")) {
287 return file_url (name (6, N (name)));
288 }
289 if (starts (name, "//")) {
290 return blank_url (name (2, N (name)));
291 }
292 if (heuristic_is_path (name, type)) {
293 return url_path (name, type);
294 }
297 return url_mingw_default (name, type);
298 if (type != URL_CLEAN_UNIX) {
299 if (heuristic_is_http (name)) return http_url (name);
300 if (heuristic_is_ftp (name)) return ftp_url (name);
301 }
302 return url_get_name (name, type);
303}
bool is_alphanum(string s)
Definition analyze.cpp:55
array< string > tokenize(string s, string sep)
Definition analyze.cpp:948
bool os_mingw()
static bool heuristic_is_ftp(string name)
Definition url.cpp:155
static bool heuristic_is_path(string name, int type)
Definition url.cpp:124
static bool heuristic_is_default(string name, int type)
Definition url.cpp:135
static url url_mingw_default(string name, int type)
Definition url.cpp:253
static url url_default(string name, int type=URL_SYSTEM)
Definition url.cpp:259
static bool heuristic_is_http(string name)
Definition url.cpp:149
url url_path(string s, int type)
Definition url.cpp:247
static bool heuristic_is_mingw_default(string name, int type)
Definition url.cpp:160
#define URL_CLEAN_UNIX
Definition url.hpp:18

◆ url_system() [1/2]

url url_system ( string name)

Definition at line 306 of file url.cpp.

306 {
307 return url_general (name, URL_SYSTEM);
308}

◆ url_system() [2/2]

url url_system ( string dir,
string name )

Definition at line 311 of file url.cpp.

311 {
312 return url_system (dir) * url_system (name);
313}

◆ url_standard() [1/2]

url url_standard ( string name)

Definition at line 316 of file url.cpp.

316 {
317 return url_general (name, URL_STANDARD);
318}

◆ url_standard() [2/2]

url url_standard ( string dir,
string name )

Definition at line 321 of file url.cpp.

321 {
322 return url_standard (dir) * url_standard (name);
323}
url url_standard(string name)
Definition url.cpp:316

◆ url_pwd()

url url_pwd ( )

Definition at line 326 of file url.cpp.

326 {
327 char path[TB_PATH_MAXN];
329 return url_system (path);
330 }
331 else {
332 TM_FAILED ("FAILED to get pwd");
333 return url_none ();
334 }
335}
#define TM_FAILED(msg)
Macro used to throw an exception with a specified error message.
Definition basic.hpp:93

◆ url_ramdisc()

url url_ramdisc ( string contents)

Definition at line 341 of file url.cpp.

341 {
342 return as_url (url_tuple ("root", "ramdisc", contents));
343}

◆ is_special_root()

static bool is_special_root ( url u)
static

Definition at line 350 of file url.cpp.

350 {
351#ifdef WINPATHS
352 return is_root (u);
353#else
354 return is_root_web (u);
355#endif
356}
bool is_root_web(url u)
Definition url.cpp:672
bool is_root(url u, string s)
Definition url.cpp:668

◆ is_semi_root()

static bool is_semi_root ( url u)
static

Definition at line 359 of file url.cpp.

359 {
360 // url u such that u/.. == u (website or windows drive name)
361 return is_concat (u) && is_special_root (u[1]) && is_atomic (u[2]);
362}
static bool is_special_root(url u)
Definition url.cpp:350

◆ head()

url head ( url u)

Definition at line 365 of file url.cpp.

365 {
366 return u * url_parent ();
367}
url url_parent()
Definition url.hpp:127

◆ tail()

url tail ( url u)

Definition at line 370 of file url.cpp.

370 {
371 if (is_concat (u)) {
372 if (is_root_web (u[1]) && is_atomic (u[2])) return url_here ();
373 return tail (u[2]);
374 }
375 if (is_or (u)) return tail (u[1]) | tail (u[2]);
376 if (is_root (u)) return url_here ();
377 return u;
378}
url tail(url u)
Definition url.cpp:370
bool is_or(url u)
Definition url.hpp:178
url url_here()
Definition url.hpp:122

◆ suffix() [1/2]

string suffix ( url u,
bool use_locase )

Definition at line 381 of file url.cpp.

381 {
382 u= tail (u);
383 if (!is_atomic (u)) return "";
384 string s= as_string (u);
385 int i, n= N (s);
386 for (i= n - 1; i >= 0; i--)
387 if (s[i] == '.') break;
388 if ((i > 0) && (i < n - 1)) {
389 string r= s (i + 1, n);
390 while ((N (r) > 0) && (r[N (r) - 1] == '~' || r[N (r) - 1] == '#'))
391 r= r (0, N (r) - 1);
392 int found= index_of (r, '?');
393 if (found != -1) r= r (0, found);
394 if (use_locase) return locase_all (r);
395 else return r;
396 }
397 return "";
398}
string locase_all(string s)
Converts all uppercase characters in a string to lowercase.
Definition analyze.cpp:137
int index_of(string s, char c)
Definition analyze.cpp:800
string as_string(url u, int type)
Definition url.cpp:729

◆ suffix() [2/2]

string suffix ( url u)

Definition at line 401 of file url.cpp.

401 {
402 return suffix (u, true);
403}
string suffix(url u, bool use_locase)
Definition url.cpp:381

◆ basename() [1/2]

string basename ( url u,
string suf )

Definition at line 406 of file url.cpp.

406 {
407 string s = as_string (tail (u));
408 int found= index_of (s, '?');
409 if (found != -1) s= s (0, found);
410 return remove_suffix (s, suf);
411}
string remove_suffix(string s, string suffix)
Remove the suffix from s if matches.
Definition analyze.cpp:175

◆ basename() [2/2]

string basename ( url u)

Definition at line 414 of file url.cpp.

414 {
415 string s= suffix (u, false);
416 if (!is_empty (s)) s= "." * s;
417 return basename (u, s);
418}
bool is_empty(string s)
Definition string.cpp:316
string basename(url u, string suf)
Definition url.cpp:406

◆ glue()

url glue ( url u,
string s )

Definition at line 421 of file url.cpp.

421 {
422 if (is_atomic (u)) return as_url (url_tree (u->t->label * s));
423 if (is_concat (u)) return u[1] * glue (u[2], s);
424 if (is_or (u)) return glue (u[1], s) | glue (u[2], s);
425 if (is_none (u)) return u;
426 TM_FAILED ("can't glue string to url");
427 return u;
428}
url glue(url u, string s)
Definition url.cpp:421
bool is_none(url u)
Definition url.hpp:166

◆ unglue()

url unglue ( url u,
int nr )

Definition at line 431 of file url.cpp.

431 {
432 if (is_atomic (u))
433 return as_url (url_tree (u->t->label (0, max (N (u->t->label) - nr, 0))));
434 if (is_concat (u)) return u[1] * unglue (u[2], nr);
435 if (is_or (u)) return unglue (u[1], nr) | unglue (u[2], nr);
436 if (is_none (u)) return u;
437 TM_FAILED ("can't unglue from url");
438 return u;
439}
SI max(SI i, SI j)
Returns the maximum of two signed integers.
Definition minmax.hpp:40
url unglue(url u, int nr)
Definition url.cpp:431

◆ unblank()

url unblank ( url u)

Definition at line 442 of file url.cpp.

442 {
443 if (is_concat (u) && (u[2]->t == "")) return u[1];
444 if (is_concat (u)) return u[1] * unblank (u[2]);
445 if (is_or (u)) return unblank (u[1]) | unblank (u[2]);
446 return u;
447}

◆ relative()

url relative ( url base,
url u )

Definition at line 450 of file url.cpp.

450 {
451 return head (base) * u;
452}
url head(url u)
Definition url.cpp:365

◆ delta_sub()

url delta_sub ( url base,
url u )

Definition at line 455 of file url.cpp.

455 {
456 if (is_atomic (base)) return u;
457 if (is_concat (base) && is_concat (u) && (base[1] == u[1])) {
458 if (is_special_root (base[1]) && is_concat (base[2]) && is_concat (u[2]) &&
459 base[2][1] != u[2][1])
460 return url_none ();
461 return delta_sub (base[2], u[2]);
462 }
463 if (is_concat (base) && !is_semi_root (base))
464 return url_parent () * delta_sub (head (base), u);
465 return url_none ();
466}
url delta_sub(url base, url u)
Definition url.cpp:455
static bool is_semi_root(url u)
Definition url.cpp:359

◆ delta()

url delta ( url base,
url u )

Definition at line 469 of file url.cpp.

469 {
470 if (is_or (u)) return delta (base, u[1]) | delta (base, u[2]);
471 url res= delta_sub (base, u);
472 if (is_none (res)) return u;
473 return res;
474}
url delta(url base, url u)
Definition url.cpp:469

◆ get_root()

string get_root ( url u)

Definition at line 477 of file url.cpp.

477 {
478 return u.protocol ();
479}

◆ unroot()

url unroot ( url u)

Definition at line 482 of file url.cpp.

482 {
483 if (is_concat (u)) return unroot (u[1]) * u[2];
484 if (is_or (u)) return unroot (u[1]) | unroot (u[2]);
485 if (is_root (u)) return url_here ();
486 return u;
487}
url unroot(url u)
Definition url.cpp:482

◆ reroot()

url reroot ( url u,
string protocol )

Definition at line 490 of file url.cpp.

490 {
491 if (is_concat (u)) return reroot (u[1], protocol) * u[2];
492 if (is_or (u)) return reroot (u[1], protocol) | reroot (u[2], protocol);
493 if (is_root (u)) return url_root (protocol);
494 return u;
495}
url reroot(url u, string protocol)
Definition url.cpp:490

◆ expand() [1/2]

static url expand ( url u1,
url u2 )
static

Definition at line 498 of file url.cpp.

498 {
499 if (is_or (u1)) return expand (u1[1], u2) | expand (u1[2], u2);
500 if (is_or (u2)) return expand (u1, u2[1]) | expand (u1, u2[2]);
501 if (is_ancestor (u2)) {
502 if (is_concat (u1)) return u1 | expand (u1[1], u2);
503 if (is_special_root (u1)) return u2;
504 return u1 | u2;
505 }
506 if (is_concat (u2) && is_ancestor (u2[1]))
507 return expand (expand (u1, u2[1]), u2[2]);
508 return u1 * u2;
509}
static url expand(url u1, url u2)
Definition url.cpp:498
bool is_ancestor(url u)
Definition url.hpp:162

◆ expand() [2/2]

url expand ( url u)

Definition at line 512 of file url.cpp.

512 {
513 if (is_or (u)) return expand (u[1]) | expand (u[2]);
514 if (is_concat (u)) return expand (expand (u[1]), expand (u[2]));
515 return u;
516}

◆ operator<=()

static bool operator<= ( url u1,
url u2 )
static

Definition at line 518 of file url.cpp.

519 {
520 if (is_atomic (u1) && is_atomic (u2)) return u1->t->label <= u2->t->label;
521 if (is_atomic (u1)) return true;
522 if (is_atomic (u2)) return false;
523 if (is_concat (u1) && is_concat (u2)) {
524 if (u1[1] == u2[1]) return u1[2] <= u2[2];
525 else return u1[1] <= u2[1];
526 }
527 if (is_concat (u1)) return true;
528 if (is_concat (u2)) return false;
529 return true; // does not matter for sorting
530}

◆ sort_sub()

static url sort_sub ( url add,
url to )
static

Definition at line 533 of file url.cpp.

533 {
534 if (is_or (to)) {
535 if (add <= to[1]) return add | to;
536 return to[1] | sort_sub (add, to[2]);
537 }
538 if (add <= to) return add | to;
539 else return to | add;
540}
static url sort_sub(url add, url to)
Definition url.cpp:533

◆ sort()

url sort ( url u)

Definition at line 543 of file url.cpp.

543 {
544 if (is_or (u)) return sort_sub (u[1], sort (u[2]));
545 else return u;
546}
url sort(url u)
Definition url.cpp:543

◆ factor_sorted()

static url factor_sorted ( url u)
static

Definition at line 549 of file url.cpp.

549 {
550 if (!is_or (u)) return u;
551 url v= factor_sorted (u[2]);
552 if (is_concat (u[1])) {
553 if (is_concat (v) && (u[1][1] == v[1])) return u[1][1] * (u[1][2] | v[2]);
554 if (is_or (v) && is_concat (v[1]) && (u[1][1] == v[1][1]))
555 return (u[1][1] * (u[1][2] | v[1][2])) | v[2];
556 }
557 return u[1] | v;
558}
static url factor_sorted(url u)
Definition url.cpp:549

◆ factor_sub()

static url factor_sub ( url u)
static

Definition at line 561 of file url.cpp.

561 {
562 if (is_concat (u)) return u[1] * factor (u[2]);
563 if (is_or (u)) return factor_sub (u[1]) | factor_sub (u[2]);
564 return u;
565}
url factor(url u)
Definition url.cpp:568
static url factor_sub(url u)
Definition url.cpp:561

◆ factor()

url factor ( url u)

Definition at line 568 of file url.cpp.

568 {
569 return factor_sub (factor_sorted (sort (u)));
570}

◆ descends()

bool descends ( url u,
url base )

Definition at line 573 of file url.cpp.

573 {
574 if (u == base) return true;
575 if (is_concat (u) && is_atomic (base)) return u[1] == base;
576 if (is_concat (u) && is_concat (base)) {
578 while (iter_u[1] == iter_base[1]) {
579 iter_u = iter_u[2];
581 if (is_concat (iter_u) && is_concat (iter_base)) {
582 continue;
583 }
584 else {
585 return descends (iter_u, iter_base);
586 }
587 }
588 return false;
589 }
590 if (is_or (u)) return descends (u[1], base) && descends (u[2], base);
591 if (is_or (base)) return descends (u, base[1]) || descends (u, base[2]);
592 return false;
593}
bool descends(url u, url base)
Definition url.cpp:573

◆ operator*() [1/3]

url operator* ( url u1,
url u2 )

Definition at line 596 of file url.cpp.

596 {
597 if (is_root (u2) || (is_concat (u2) && is_root (u2[1]))) {
598 if (is_concat (u1) && is_root_web (u1[1])) {
599 if (is_root (u2, "default") ||
600 (is_concat (u2) && is_root (u2[1], "default"))) {
601 url v= u1[2];
602 while (is_concat (v))
603 v= v[1];
604 if (is_root (u2)) return u1[1] * v;
605 return u1[1] * v * u2[2];
606 }
607 if (is_root (u2, "blank") || (is_concat (u2) && is_root (u2[1], "blank")))
608 return reroot (u2, u1[1][1]->t->label);
609 }
610 if (is_root (u1) && is_rooted (u1, "file")) {
611 return u1 * u2[2];
612 }
613 return u2;
614 }
615 if (is_here (u1) || (u1->t == "")) return u2;
616 if (is_here (u2)) return u1;
617 if (is_none (u1)) return url_none ();
618 if (is_none (u2)) return url_none ();
619 if (u2 == url_parent ()) {
620 if (is_root (u1)) return u1;
621 if (is_pseudo_atomic (u1) && (!is_parent (u1))) return url_here ();
622 if (is_semi_root (u1)) return u1;
623 }
624 if (is_concat (u2) && (u2[1] == url_parent ())) {
625 if (is_root (u1)) return u1 * u2[2];
626 if (is_pseudo_atomic (u1) && (!is_parent (u1))) return u2[2];
627 if (is_semi_root (u1)) return u1 * u2[2];
628 }
629 if (is_concat (u1)) return u1[1] * (u1[2] * u2);
630 return as_url (url_tuple ("concat", u1->t, u2->t));
631}
bool is_pseudo_atomic(url u)
Definition url.cpp:681
bool is_rooted(url u)
Definition url.cpp:686
bool is_parent(url u)
Definition url.hpp:158

◆ operator*() [2/3]

url operator* ( url u1,
const char * name )

Definition at line 634 of file url.cpp.

634 {
635 return u1 * url (name);
636}

◆ operator*() [3/3]

url operator* ( url u1,
string name )

Definition at line 639 of file url.cpp.

639 {
640 return u1 * url (name);
641}

◆ url_concat()

url url_concat ( url u1,
url u2 )

Definition at line 644 of file url.cpp.

644 {
645 return u1 * u2;
646}

◆ operator|()

url operator| ( url u1,
url u2 )

Definition at line 649 of file url.cpp.

649 {
650 if (is_none (u1)) return u2;
651 if (is_none (u2)) return u1;
652 if (is_or (u1)) return u1[1] | (u1[2] | u2);
653 if (u1 == u2) return u2;
654 if (is_or (u2) && (u1 == u2[1])) return u2;
655 return as_url (url_tuple ("or", u1->t, u2->t));
656}

◆ url_or()

url url_or ( url u1,
url u2 )

Definition at line 659 of file url.cpp.

659 {
660 return u1 | u2;
661}

◆ is_atomic()

bool is_atomic ( url u)

Definition at line 664 of file url.cpp.

664 {
665 return is_atomic (u->t);
666}

◆ is_root()

bool is_root ( url u,
string s )

Definition at line 668 of file url.cpp.

668 {
669 return is_root (u) && (u[1]->t->label == s);
670}

◆ is_root_web()

bool is_root_web ( url u)

Definition at line 672 of file url.cpp.

672 {
673 return is_root (u, "http") || is_root (u, "https") || is_root (u, "ftp") ||
674 is_root (u, "blank");
675}

◆ is_wildcard()

bool is_wildcard ( url u,
int n )

Definition at line 677 of file url.cpp.

677 {
678 return is_tuple (u->t, "wildcard", n);
679}
static bool is_tuple(url_tree t, string s)
Definition url.cpp:44

◆ is_pseudo_atomic()

bool is_pseudo_atomic ( url u)

Definition at line 681 of file url.cpp.

681 {
682 return is_atomic (u->t) || is_tuple (u->t, "wildcard", 1);
683}

◆ is_rooted() [1/2]

bool is_rooted ( url u)

Definition at line 686 of file url.cpp.

686 {
687 return is_root (u) || (is_concat (u) && is_rooted (u[1])) ||
688 (is_or (u) && is_rooted (u[1]) && is_rooted (u[2]));
689}

◆ is_rooted() [2/2]

bool is_rooted ( url u,
string protocol )

Definition at line 692 of file url.cpp.

692 {
693 return is_root (u, protocol) ||
694 (is_concat (u) && is_rooted (u[1], protocol)) ||
695 (is_or (u) && is_rooted (u[1], protocol) &&
696 is_rooted (u[2], protocol));
697}

◆ is_rooted_web()

bool is_rooted_web ( url u)

Definition at line 700 of file url.cpp.

700 {
701 return is_root_web (u) || (is_concat (u) && is_rooted_web (u[1])) ||
702 (is_or (u) && is_rooted_web (u[1]) && is_rooted_web (u[2]));
703}
bool is_rooted_web(url u)
Definition url.cpp:700

◆ is_name()

bool is_name ( url u)

Definition at line 706 of file url.cpp.

706 {
707 if (is_atomic (u)) return true;
708 if (!is_concat (u)) return false;
709 return is_name (u[1]) && is_name (u[2]);
710}
bool is_name(url u)
Definition url.cpp:706

◆ is_rooted_name()

bool is_rooted_name ( url u)

Definition at line 713 of file url.cpp.

713 {
714 return is_concat (u) && is_root (u[1]) && is_name (u[2]);
715}

◆ is_name_in_path()

bool is_name_in_path ( url u)

Definition at line 718 of file url.cpp.

718 {
719 if (is_name (u)) return true;
720 return is_concat (u) && is_root (u[1], "default") && is_name (u[2]);
721}

◆ is_ramdisc()

bool is_ramdisc ( url u)

Definition at line 724 of file url.cpp.

724 {
725 return is_concat (u) && is_root (u[1], "ramdisc");
726}

◆ as_string()

string as_string ( url u,
int type )

Definition at line 729 of file url.cpp.

729 {
730 // This routine pretty prints an url as a string.
731 // FIXME: the current algorithm is quadratic in time.
732 if (is_none (u)) return "{}";
733 if (is_atomic (u)) return u->t->label;
734 if (is_concat (u)) {
735 int stype= type;
736 if (is_root (u[1]) &&
737 !(is_root (u[1], "default") || is_root (u[1], "file"))) {
739 }
740 string sep= (stype == URL_SYSTEM ? string (URL_CONCATER) : string ("/"));
741 string s1 = as_string (u[1], type);
742 string s2 = as_string (u[2], stype);
743 if (is_root (u[1], "default")) s1= "";
744 if ((!is_name (u[1])) && (!is_root (u[1]))) s1= "{" * s1 * "}";
745 if ((!is_concat (u[2])) && (!is_atomic (u[2])) && (!is_wildcard (u[2], 1)))
746 s2= "{" * s2 * "}";
747 if (os_win () && // have to return the windows format
748 ((is_root (u[1], "default") && type == URL_SYSTEM) ||
749 is_root (u[1], "file"))) {
750 string root, remain;
751 if (is_concat (u[2])) {
752 root= as_string (u[2][1], type);
753 // root might be unit letter or hostname. It depends on the length
754 remain= as_string (u[2][2], type);
755 }
756 else {
757 root = s2;
758 remain= "";
759 }
760 if (is_root (u[1], "default")) {
761 if (N (root) == 1) return root * ":" * sep * remain; // drive letter
762 else return "\\\\" * root * "\\" * remain;
763 }
764 else {
765 if (N (root) == 1)
766 return s1 * "/" * root * ":" * sep * remain; // local file
767 else return s1 * root * "/" * remain; // remote
768 }
769 }
770 return s1 * sep * s2;
771 }
772 if (is_or (u)) {
773 string s1= as_string (u[1], type);
774 string s2= as_string (u[2], type);
775 if (!is_name_in_path (u[1])) s1= "{" * s1 * "}";
776 if ((!is_or (u[2])) && (!is_name_in_path (u[2]))) s2= "{" * s2 * "}";
777 if (os_win ()) {
778 if (type == URL_STANDARD) return s1 * ":" * s2;
779 else return s1 * string (URL_SEPARATOR) * s2;
780 }
781 else {
782 return s1 * string (URL_SEPARATOR) * s2;
783 }
784 }
785 if (os_win ()) {
786 if (is_root (u, "default")) {
787 int stype= type;
788 if (is_root (u[1]) && (!is_root (u[1], "default"))) stype= URL_STANDARD;
789 if (stype == URL_SYSTEM) return "";
790 else return "/";
791 }
792 }
793 else {
794 if (is_root (u, "default")) return "/";
795 }
796 if (is_root (u, "blank")) return "/";
797 if (is_root (u, "file")) return u[1]->t->label * "://";
798 if (is_root (u)) return u[1]->t->label * ":/";
799 if (is_wildcard (u, 0)) return "**";
800 if (is_wildcard (u, 1)) return u->t[1]->label;
801 TM_FAILED ("bad url");
802 return "";
803}
bool os_win()
Definition sys_utils.cpp:94
bool is_name_in_path(url u)
Definition url.cpp:718
bool is_wildcard(url u, int n)
Definition url.cpp:677

◆ operator<<()

tm_ostream & operator<< ( tm_ostream & out,
url u )

Definition at line 805 of file url.cpp.

806 {
807 return out << as_string (u, URL_SYSTEM);
808}