Back to index

texmacs  1.0.7.15
tmfs_file.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : tmfs_file.cpp
00004 * DESCRIPTION: files and projects
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 
00014 /******************************************************************************
00015 * Versioning routines
00016 ******************************************************************************/
00017 
00018 string
00019 tmfs_create_file (string name, string contents, string user, properties xps) {
00020   string master= tmfs_create_identifier ();
00021   string file  = tmfs_create_ressource ();
00022   properties ps;
00023   ps << seq ("file", file)
00024      << seq ("name", file, name)
00025      << seq ("master", file, master)
00026      << seq ("owner", file, user)
00027      << seq ("in", file, user)
00028      << seq ("read", file, user)
00029      << seq ("write", file, user)
00030      << substitute (xps, "self", file);
00031   tmfs_save_ressource (file, contents, ps);
00032   return file;
00033 }
00034 
00035 collection
00036 tmfs_search_file (string name) {
00037   properties ps; ps << seq ("name", "?file", name) << seq ("file", "?file");
00038   return tmfs_query (ps, "?file");
00039 }
00040 
00041 void
00042 tmfs_save (string file, string contents, properties ps) {
00043   if (tmfs_allows (file, "write") && tmfs_allows (file, "owner"))
00044     tmfs_save_ressource (file, contents, ps);
00045 }
00046 
00047 void
00048 tmfs_save_file (string file, string cont) {
00049   if (tmfs_allows (file, "write"))
00050     tmfs_save_ressource (file, cont, tmfs_load_ressource_properties (file));
00051 }
00052 
00053 string
00054 tmfs_load_file (string file) {
00055   if (tmfs_allows (file, "read"))
00056     return tmfs_load_ressource_file (file);
00057   return "";
00058 }
00059 
00060 /******************************************************************************
00061 * Projects
00062 ******************************************************************************/
00063 
00064 string
00065 tmfs_create_project (string name, string user) {
00066   string project= tmfs_create_ressource ();
00067   string home= tmfs_create_file (name * " - home", "", project);
00068   properties ps;
00069   ps << seq ("project", project)
00070      << seq ("name", project, name)
00071      << seq ("owner", project, user)
00072      << seq ("in", project, user)
00073      << seq ("read", project, user)
00074      << seq ("write", project, user)
00075      << seq ("home", project, home);
00076   tmfs_save_ressource (project, "", ps);
00077   return project;
00078 }
00079 
00080 collection
00081 tmfs_search_project (string name) {
00082   properties ps; ps << seq ("name", "?p", name) << seq ("project", "?p");
00083   return tmfs_query (ps, "?p");
00084 }
00085 
00086 collection
00087 tmfs_get_file_projects (string file) {
00088   return tmfs_query (seq ("in", file, "?project"), "?project");
00089 }
00090 
00091 collection
00092 tmfs_get_project_files (string project) {
00093   properties ps; ps << seq ("in", "?file", project) << seq ("file", "?file");
00094   collection files= tmfs_query (ps, "?file");
00095   collection homes= tmfs_query (seq ("home", project, "?home"), "?home");
00096   return files * invert (homes);
00097 }
00098 
00099 /******************************************************************************
00100 * Branches
00101 ******************************************************************************/
00102 
00103 void
00104 tmfs_branch_file (string old_file, string branch) {
00105   string contents= tmfs_load_file (old_file);
00106   properties ps  = tmfs_get_attributes (old_file);
00107   string new_file= tmfs_create_ressource ();
00108   ps= substitute (ps, old_file, new_file);
00109   properties mp;
00110   mp << seq ("owner", old_file, "?user")
00111      << seq ("in", old_file, "?user")
00112      << seq ("read", old_file, "?user")
00113      << seq ("write", old_file, "?user");
00114   ps= reset (ps, mp);
00115   ps << seq ("owner", new_file, branch)
00116      << seq ("in", new_file, branch)
00117      << seq ("read", new_file, branch)
00118      << seq ("write", new_file, branch);
00119   tmfs_save_ressource (new_file, contents, ps);
00120 }
00121 
00122 string
00123 tmfs_create_branch (string project, string name, string user) {
00124   collection c= tmfs_query (seq ("name", project, "?name"), "?name");
00125   if (N(c) == 0) return "";
00126   string full_name= first (c) * " - " * name;
00127   string branch= tmfs_create_project (full_name, user);
00128   strings files= as_strings (tmfs_get_project_files (project));
00129   for (int i=0; i<N(files); i++)
00130     tmfs_branch_file (files[i], branch);
00131   return branch;
00132 }