Back to index

texmacs  1.0.7.15
tmfs_convert.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : tmfs_convert.cpp
00004 * DESCRIPTION: conversions from/into conventional file systems
00005 * COPYRIGHT  : (C) 2007  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 #include "tmfs.hpp"
00013 #include "analyze.hpp"
00014 #include "file.hpp"
00015 #include "merge_sort.hpp"
00016 
00017 /******************************************************************************
00018 * Useful subroutines
00019 ******************************************************************************/
00020 
00021 bool
00022 is_cruft (string s) {
00023   return
00024     s == "CVS" ||
00025     s == ".svn" ||
00026     ends (s, "~");
00027 }
00028 
00029 string
00030 create_name (string name, string contents) {
00031   (void) contents;
00032   return name;
00033 }
00034 
00035 string
00036 load_string (url u) {
00037   string s= "";
00038   (void) load_string (u, s, false);
00039   return s;
00040 }
00041 
00042 void
00043 create_parents (url u) {
00044   if (!exists (url_parent (u))) {
00045     if (!is_root (u)) create_parents (url_parent (u));
00046     mkdir (url_parent (u));
00047   }
00048 }
00049 
00050 /******************************************************************************
00051 * Importation and exportation
00052 ******************************************************************************/
00053 
00054 bool
00055 operator < (string a, string b) {
00056   return a <= b && a != b;
00057 }
00058 
00059 bool
00060 operator <= (property p1, property p2) {
00061   for (int i=0; i<min(N(p1),N(p2)); i++)
00062     if (p1[i] < p2[i]) return true;
00063     else if (p2[i] < p1[i]) return false;
00064   return N(p1) <= N(p2);
00065 }
00066 
00067 properties
00068 tmfs_list_heads_inside (url u, string prj) {
00069   strings files= as_strings (tmfs_get_project_files (prj));
00070   properties r;
00071   for (int i=0; i<N(files); i++) {
00072     string id= files[i];
00073     collection pl= tmfs_query (seq ("mirror", id, prj, "?delta"), "?delta");
00074     if (N(pl) > 0) {
00075       url v= first (pl);
00076       if (u == url_here () || descends (v, u))
00077        r << seq (as_string (v), id);
00078     }
00079   }
00080   merge_sort (r);
00081   return r;
00082 }
00083 
00084 void
00085 tmfs_export (url prj_dir, url u, string prj) {
00086   properties ps= tmfs_list_heads_inside (u, prj);
00087   for (int i=0; i<N(ps); i++) {
00088     url    v = ps[i][0];
00089     string file= ps[i][1];
00090     cout << "Process " << (prj_dir * v) << "\n";
00091     if (!exists (prj_dir * v)) {
00092       string val= tmfs_load_file (file);
00093       create_parents (prj_dir * v);
00094       save_string (prj_dir * v, val);
00095       cout << "Export " << file << " -> " << (prj_dir * v) << "\n";
00096     }
00097     else {
00098       string val1= load_string (prj_dir * v);
00099       string val2= tmfs_load_file (file);
00100       if (val2 != val1) {
00101        save_string (prj_dir * v, val2);
00102        cout << "Update " << file << " -> " << (prj_dir * v) << "\n";
00103       }
00104     }
00105   }
00106 }
00107 
00108 void
00109 tmfs_import (url prj_dir, url u, string prj) {
00110   cout << "Process " << (prj_dir * u) << "\n";
00111   if (is_or (u)) {
00112     tmfs_import (prj_dir, u[1], prj);
00113     tmfs_import (prj_dir, u[2], prj);
00114   }
00115   else if (is_directory (prj_dir * u)) {
00116     bool flag;
00117     array<string> a= read_directory (prj_dir * u, flag);
00118     if (flag) return;
00119     for (int i=0; i<N(a); i++)
00120       if (!is_cruft (a[i]) && a[i] != "." && a[i] != "..")
00121        tmfs_import (prj_dir, u * a[i], prj);
00122   }
00123   else if (is_regular (prj_dir * u)) {
00124     string loc= as_string (u);
00125     properties ps;
00126     ps << seq ("mirror", "?file", prj, loc) << seq ("in", "?file", prj);
00127     collection files= tmfs_query (ps, "?file");
00128     if (N (files) == 0) {
00129       properties xps; xps << seq ("mirror", "self", prj, loc);
00130       string val = load_string (prj_dir * u);
00131       string name= create_name (as_string (tail (u)), val);
00132       string file= tmfs_create_file (name, val, prj, xps);
00133       cout << "Import " << u << " -> " << file << "\n";
00134     }
00135     else {
00136       string file= first (files);
00137       string val1= tmfs_load_file (file);
00138       string val2= load_string (prj_dir * u);
00139       if (val1 == val2) return;
00140       tmfs_save_file (file, val2);
00141       cout << "Update " << u << " -> " << file << "\n";
00142     }
00143   }
00144 
00145   properties ps= tmfs_list_heads_inside (u, prj);
00146   for (int i=0; i<N(ps); i++) {
00147     url    v = ps[i][0];
00148     string file= ps[i][1];
00149     if (!exists (prj_dir * v)) {
00150       tmfs_reset_head (file);
00151       cout << "Remove " << v << " -> " << file << "\n";
00152     }
00153   }
00154 }
00155 
00156 /******************************************************************************
00157 * Importation based on root directories for users and projects
00158 ******************************************************************************/
00159 
00160 void
00161 tmfs_set_root (string prj, url u) {
00162   properties ps;
00163   if (!is_none (u)) ps << seq ("root", prj, as_string (u));
00164   tmfs_change_attributes (prj, ps);
00165 }
00166 
00167 url
00168 tmfs_get_root (string prj) {
00169   collection c= tmfs_query (seq ("root", prj, "?url"), "?url");
00170   if (N(c) != 1) return url_none ();
00171   return url (first (c));
00172 }
00173 
00174 collection
00175 tmfs_get_projects (url u) {
00176   collection c= tmfs_query (seq ("root", "?prj", as_string (u)), "?prj");
00177   if (N(c) != 0 || is_root (u)) return c;
00178   return tmfs_get_projects (url_parent (u));
00179 }
00180 
00181 url
00182 delta_dir (url base, url sub) {
00183   return url_parent (delta (base * "dummy1", sub * "dummy2"));
00184 }
00185 
00186 void
00187 tmfs_import (url u) {
00188   strings prjs= as_strings (tmfs_get_projects (u));
00189   if (N(prjs) == 0)
00190     cerr << "TeXmacs] error: no project for " << u << "\n";
00191   else if (N(prjs) > 1)
00192     cerr << "TeXmacs] error: too many projects for " << u << "\n";
00193   else {
00194     url prj_dir= tmfs_get_root (prjs[0]);
00195     tmfs_import (prj_dir, delta_dir (prj_dir, u), prjs[0]);
00196   }
00197 }
00198 
00199 void
00200 tmfs_export (url u) {
00201   strings prjs= as_strings (tmfs_get_projects (u));
00202   if (N(prjs) == 0)
00203     cerr << "TeXmacs] error: no project for " << u << "\n";
00204   else if (N(prjs) > 1)
00205     cerr << "TeXmacs] error: too many projects for " << u << "\n";
00206   else {
00207     url prj_dir= tmfs_get_root (prjs[0]);
00208     tmfs_export (prj_dir, delta_dir (prj_dir, u), prjs[0]);
00209   }
00210 }