"Fossies" - the Fresh Open Source Software Archive 
Member "TeXmacs-2.1.2-src/src/System/Files/web_files.cpp" (5 May 2022, 4995 Bytes) of package /linux/misc/TeXmacs-2.1.2-src.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "web_files.cpp" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
2.1.1_vs_2.1.2.
1
2 /******************************************************************************
3 * MODULE : web_files.cpp
4 * DESCRIPTION: file handling via the web
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 #include "file.hpp"
13 #include "web_files.hpp"
14 #include "sys_utils.hpp"
15 #include "analyze.hpp"
16 #include "hashmap.hpp"
17 #include "scheme.hpp"
18
19 #define MAX_CACHED 25
20 static int web_nr=0;
21 static array<tree> web_cache (MAX_CACHED);
22 static hashmap<tree,tree> web_cache_resolve ("");
23
24 /******************************************************************************
25 * Caching
26 ******************************************************************************/
27
28 static url
29 get_cache (url name) {
30 if (web_cache_resolve->contains (name->t)) {
31 int i, j;
32 tree tmp= web_cache_resolve [name->t];
33 for (i=0; i<MAX_CACHED; i++)
34 if (web_cache[i] == name->t) {
35 // cout << name << " in cache as " << tmp << " at " << i << "\n";
36 for (j=i; ((j+1) % MAX_CACHED) != web_nr; j= (j+1) % MAX_CACHED)
37 web_cache[j]= web_cache[(j+1) % MAX_CACHED];
38 web_cache[j]= name->t;
39 break;
40 }
41 return as_url (tmp); // url_system (tmp);
42 }
43 return url_none ();
44 }
45
46 static url
47 set_cache (url name, url tmp) {
48 web_cache_resolve->reset (web_cache [web_nr]);
49 web_cache [web_nr]= name->t;
50 web_cache_resolve (name->t)= tmp->t;
51 web_nr= (web_nr+1) % MAX_CACHED;
52 return tmp;
53 }
54
55 void
56 web_cache_invalidate (url name) {
57 for (int i=0; i<MAX_CACHED; i++)
58 if (web_cache[i] == name->t) {
59 web_cache[i]= tree ("");
60 web_cache_resolve->reset (name->t);
61 }
62 }
63
64 /******************************************************************************
65 * Web files
66 ******************************************************************************/
67
68 static string
69 web_encode (string s) {
70 return tm_decode (s);
71 }
72
73 static string
74 fetch_tool () {
75 static bool done= false;
76 static string tool= "";
77 if (done) return tool;
78 if (tool == "" && exists_in_path ("wget")) tool= "wget";
79 if (tool == "" && exists_in_path ("curl")) tool= "curl";
80 done= true;
81 return tool;
82 }
83
84 url
85 get_from_web (url name) {
86 if (!is_rooted_web (name)) return url_none ();
87 url res= get_cache (name);
88 if (!is_none (res)) return res;
89
90 string tool= fetch_tool ();
91 if (tool == "") return url_none ();
92
93 url tmp= url_temp ();
94 string tmp_s= escape_sh (concretize (tmp));
95 string cmd= "";
96
97 if (tool == "wget") {
98 cmd= "wget --header='User-Agent: TeXmacs-" TEXMACS_VERSION "' -q";
99 cmd << " --no-check-certificate --tries=1";
100 cmd << " -O " << tmp_s << " " << escape_sh (web_encode (as_string (name)));
101 }
102
103 if (tool == "curl") {
104 cmd= "curl --user-agent TeXmacs-" TEXMACS_VERSION;
105 cmd << " " << escape_sh (web_encode (as_string (name)));
106 cmd << " --output " << tmp_s;
107 }
108
109 //cout << cmd << LF;
110 system (cmd);
111 //cout << "got " << name << " as " << tmp << LF;
112
113 if (file_size (url_system (tmp_s)) <= 0) {
114 remove (tmp);
115 return url_none ();
116 }
117 else return set_cache (name, tmp);
118 }
119
120 /******************************************************************************
121 * Files from a hyperlink file system
122 ******************************************************************************/
123
124 url
125 get_from_server (url u) {
126 if (!is_rooted_tmfs (u)) return url_none ();
127 url res= get_cache (u);
128 if (!is_none (res)) return res;
129
130 string name= as_string (u);
131 if (ends (name, "~") || ends (name, "#")) {
132 if (!is_rooted_tmfs (name)) return url_none ();
133 if (!as_bool (call ("tmfs-can-autosave?", unglue (u, 1))))
134 return url_none ();
135 }
136 string r= as_string (call ("tmfs-load", object (name)));
137 if (r == "") return url_none ();
138 url tmp= url_temp (string (".") * suffix (name));
139 (void) save_string (tmp, r, true);
140
141 //return set_cache (u, tmp);
142 return tmp;
143 // FIXME: certain files could be cached, but others not
144 // for instance, files which are loaded in a delayed fashion
145 // would always be cached as empty files, which is erroneous.
146 }
147
148 bool
149 save_to_server (url u, string s) {
150 if (!is_rooted_tmfs (u)) return true;
151 string name= as_string (u);
152 (void) call ("tmfs-save", object (name), object (s));
153 return false;
154 }
155
156 /******************************************************************************
157 * Ramdisc
158 ******************************************************************************/
159
160 url
161 get_from_ramdisc (url u) {
162 if (!is_ramdisc (u)) return url_none ();
163 url res= get_cache (u);
164 if (!is_none (res)) return (res);
165 url tmp= url_temp (string (".") * suffix (u));
166 save_string (tmp, u[1][2]->t->label);
167 return set_cache (u, tmp);
168 }