Back to index

texmacs  1.0.7.15
Functions
file.hpp File Reference
#include "url.hpp"
#include "sys_utils.hpp"
#include "analyze.hpp"

Go to the source code of this file.

Functions

bool load_string (url file_name, string &s, bool fatal)
bool save_string (url file_name, string s, bool fatal=false)
bool is_of_type (url name, string filter)
bool is_regular (url name)
bool is_directory (url name)
bool is_symbolic_link (url name)
bool is_newer (url which, url than)
int last_modified (url u, bool cache_flag=true)
url url_temp (string suffix="")
url url_numbered (url dir, string prefix, string postfix, int i=1)
url url_scratch (string prefix="no_name_", string postfix=".tm", int i=1)
bool is_scratch (url u)
array< stringread_directory (url name, bool &error_flag)
string sys_concretize (url u1)
void system (string which, url u1)
void system (string which, url u1, url u2)
void system (string which, url u1, const char *post)
void system (string which, url u1, const char *sep, url u2)
string eval_system (string which, url u1)
string eval_system (string which, url u1, url u2)
void move (url from, url to)
void copy (url from, url to)
void remove (url what)
void mkdir (url dir)
void change_mode (url u, int mode)
void ps2pdf (url u1, url u2)
int search_score (url u, array< string > a)
array< stringfile_completions (url search, url dir)

Function Documentation

void change_mode ( url  u,
int  mode 
)

Definition at line 578 of file file.cpp.

                              {
#if defined (HAVE_SYS_TYPES_H) && defined (HAVE_SYS_STAT_H)
  char *_u= as_charp (concretize (u));
  (void) ::chmod (_u, mode);
  tm_delete_array (_u);
#else
  string m0= as_string ((mode >> 9) & 7);
  string m1= as_string ((mode >> 6) & 7);
  string m2= as_string ((mode >> 3) & 7);
  string m3= as_string (mode & 7);
  system ("chmod -f " * m0 * m1 * m2 * m3, u);
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

void copy ( url  from,
url  to 
)

Definition at line 536 of file file.cpp.

                      {
  string s;
  if (!load_string (u1, s, false))
    (void) save_string (u2, s, false);
}

Here is the call graph for this function:

string eval_system ( string  which,
url  u1 
) [inline]

Definition at line 46 of file file.hpp.

                                                 {
  return eval_system (which * " " * escape_sh (concretize (u1))); }

Here is the call graph for this function:

Here is the caller graph for this function:

string eval_system ( string  which,
url  u1,
url  u2 
) [inline]

Definition at line 48 of file file.hpp.

                                                         {
  return eval_system (which * " " * escape_sh (concretize (u1)) * " " * escape_sh (concretize (u2))); }

Here is the call graph for this function:

array<string> file_completions ( url  search,
url  dir 
)

Definition at line 655 of file file.cpp.

                                       {
  array<string> a;
  file_completions_dir (a, search, dir);
  return a;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool is_directory ( url  name)

Definition at line 324 of file file.cpp.

{ return is_of_type (name, "d"); }

Here is the call graph for this function:

Here is the caller graph for this function:

bool is_newer ( url  which,
url  than 
)

Definition at line 335 of file file.cpp.

                               {
  struct stat which_stat;
  struct stat than_stat;
  // FIXME: why was this? 
  if (is_cached ("stat_cache.scm", concretize (which))) return false;
  if (is_cached ("stat_cache.scm", concretize (than))) return false;
  // end FIXME
  if (get_attributes (which, &which_stat, true)) return false;
  if (get_attributes (than , &than_stat , true)) return false;
  return which_stat.st_mtime > than_stat.st_mtime;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool is_of_type ( url  name,
string  filter 
)

Definition at line 226 of file file.cpp.

                                     {
  if (filter == "") return true;
  int i, n= N(filter);

  // Files from the web
  if (is_rooted_web (name)) {
    // cout << "  try " << name << "\n";
    url from_web= get_from_web (name);
    // cout << "  --> " << from_web << "\n";
    if (is_none (from_web)) return false;
    for (i=0; i<n; i++)
      switch (filter[i]) {
      case 'd': return false;
      case 'l': return false;
      case 'w': return false;
      case 'x': return false;
      }
    return true;
  }

  // Files from a remote server
  if (is_rooted_tmfs (name)) {
    for (i=0; i<n; i++)
      switch (filter[i]) {
      case 'd': return false;
      case 'l': return false;
      case 'r':
       if (!as_bool (call ("tmfs-permission?", name, "read")))
         return false;
       break;
      case 'w':
       if (!as_bool (call ("tmfs-permission?", name, "write")))
         return false;
       break;
      case 'x': return false;
      }
    return true;
  }

  // Files from the ramdisk
  if (is_ramdisc (name))
    return true;

  // Normal files
#if defined (OS_WIN32) || defined (__MINGW__) || defined (__MINGW32__)
  if ((filter == "x") && (suffix(name) != "exe") && (suffix(name) != "bat"))
    name = glue (name, ".exe");
#endif
  bool preserve_links= false;
  for (i=0; i<n; i++)
    preserve_links= preserve_links || (filter[i] == 'l');
  struct stat buf;
  if (get_attributes (name, &buf, preserve_links)) return false;
  for (i=0; i<n; i++)
    switch (filter[i]) {
      // FIXME: should check user id and group id for r, w and x
    case 'f':
      if (!S_ISREG (buf.st_mode)) return false;
      break;
    case 'd':
      if (!S_ISDIR (buf.st_mode)) return false;
      break;
    case 'l':
#ifdef __MINGW32__
      return false;
#else
      if (!S_ISLNK (buf.st_mode)) return false;
#endif
      break;
    case 'r':
#ifndef __MINGW32__
      if ((buf.st_mode & (S_IRUSR | S_IRGRP | S_IROTH)) == 0) return false;
#else
      if ((buf.st_mode & 292) == 0) return false;
#endif
      break;
    case 'w':
#ifndef __MINGW32__
      if ((buf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0) return false;
#else
      if ((buf.st_mode & 146) == 0) return false;
#endif
      break;
    case 'x':
#if defined (OS_WIN32) || defined (__MINGW__) || defined (__MINGW32__)
      if (suffix(name) == "bat") break;
#endif
#ifndef __MINGW32__
      if ((buf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) return false;
#else
      if ((buf.st_mode & 73) == 0) return false;
#endif
      break;
    }
  return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool is_regular ( url  name)

Definition at line 323 of file file.cpp.

{ return is_of_type (name, "f"); }

Here is the call graph for this function:

bool is_scratch ( url  u)

Definition at line 382 of file file.cpp.

                   {
  return head (u) == url ("$TEXMACS_HOME_PATH/texts/scratch");
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool is_symbolic_link ( url  name)

Definition at line 325 of file file.cpp.

{ return is_of_type (name, "l"); }

Here is the call graph for this function:

Here is the caller graph for this function:

int last_modified ( url  u,
bool  cache_flag = true 
)

Definition at line 328 of file file.cpp.

                                       {
  struct stat u_stat;
  if (get_attributes (u, &u_stat, true, cache_flag)) return 0;
  return u_stat.st_mtime;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool load_string ( url  file_name,
string s,
bool  fatal 
)

Definition at line 45 of file file.cpp.

                                           {
  // cout << "Load " << u << LF;
  url r= u;
  if (!is_rooted_name (r)) r= resolve (r);
  // cout << "Resolved " << r << LF;
  bool err= !is_rooted_name (r);
  if (!err) {
    string name= concretize (r);
    // cout << "Concrete :" << name << LF;
    // File contents in cache?
    bool file_flag= do_cache_file (name);
    bool doc_flag= do_cache_doc (name);
    string cache_type= doc_flag? string ("doc_cache"): string ("file_cache");
    if (doc_flag) cache_load ("doc_cache");
    if (is_cached (cache_type, name) && is_up_to_date (url_parent (r))) {
      s= cache_get (cache_type, name) -> label;
      return false;
    }
    // End caching

    bench_start ("load file");
    char* _name= as_charp (name);
    // cout << "OPEN :" << _name << LF;
#if defined (OS_WIN32)
    FILE* fin= _fopen (_name, "rb");
#elif defined (__MINGW__) || defined (__MINGW32__)
    FILE* fin= fopen (_name, "rb");
#else
    FILE* fin= fopen (_name, "r");
#endif
    if (fin == NULL) {
      err= true;
      if (!occurs ("system", name))
        cerr << "TeXmacs] warning, load error for " << name << ", "
             << sys_errlist[errno] << "\n";
    }
    int size= 0;
    if (!err) {
      if (fseek (fin, 0L, SEEK_END) < 0) err= true;
      else {
       size= ftell (fin);
       if (size<0) err= true;
      }
      if (err) {
        cerr << "TeXmacs] warning, seek failed for " << as_string (u) << "\n";
        fclose (fin);
      }
    }
    if (!err) {
      rewind (fin);
      s->resize (size);
      int read= fread (&(s[0]), 1, size, fin);
      if (read < size) s->resize (read);
      fclose (fin);
    }
    tm_delete_array (_name);
    bench_cumul ("load file");

    // Cache file contents
    if (!err && N(s) <= 10000)
      if (file_flag || doc_flag)
       cache_set (cache_type, name, s);
    // End caching
  }
  if (err && fatal) {
    cerr << "File name= " << as_string (u) << "\n";
    FAILED ("file not readable");
  }
  return err;
}

Here is the call graph for this function:

void mkdir ( url  dir)

Definition at line 558 of file file.cpp.

              {
#if defined (HAVE_SYS_TYPES_H) && defined (HAVE_SYS_STAT_H)
  if (exists (u)) return;
  char *_u= as_charp (concretize (u));
#if defined(__MINGW__) || defined(__MINGW32__)
  (void) ::mkdir (_u);
#else
  (void) ::mkdir (_u, S_IRWXU + S_IRGRP + S_IROTH);
#endif
  tm_delete_array (_u);
#else
#if defined(__MINGW__) || defined(__MINGW32__)
  system ("mkdir", u);
#else
  system ("mkdir -p", u);
#endif
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

void move ( url  from,
url  to 
)

Definition at line 526 of file file.cpp.

                      {
  char *_u1, *_u2;
  _u1 = as_charp (concretize (u1));
  _u2 = as_charp (concretize (u2));
  (void) rename (_u1, _u2);
  tm_delete_array (_u1);
  tm_delete_array (_u2);
}

Here is the call graph for this function:

void ps2pdf ( url  u1,
url  u2 
)

Definition at line 593 of file file.cpp.

                        {
#ifdef OS_WIN32
  char *_u1, *_u2;
  _u1 = as_charp (concretize (u1));
  _u2 = as_charp (concretize (u2));
  XPs2Pdf (_u1, _u2);
  tm_delete_array (_u1);
  tm_delete_array (_u2);
#else
#ifdef MACOSX_EXTENSIONS
  mac_ps_to_pdf (u1, u2);
#else
  system ("ps2pdf", u1, u2);
#endif
#endif
}

Here is the call graph for this function:

array<string> read_directory ( url  name,
bool &  error_flag 
)

Definition at line 406 of file file.cpp.

                                         {
 //  cout << "Directory " << u << LF;
  u= resolve (u, "dr");
  if (is_none (u)) return array<string> ();
  string name= concretize (u);

  // Directory contents in cache?
  if (is_cached ("dir_cache.scm", name) && is_up_to_date (u))
    return cache_dir_get (name);
  bench_start ("read directory");
  // End caching

  DIR* dp;
  char* temp= as_charp (name);
  dp= opendir (temp);
  tm_delete_array (temp);
  error_flag= (dp==NULL);
  if (error_flag) return array<string> ();

  array<string> dir;
  struct dirent* ep;
  while (true) {
    ep= readdir (dp);
    if (ep==NULL) break;
    dir << string (ep->d_name);
  }
  (void) closedir (dp);
  merge_sort (dir);

  // Caching of directory contents
  bench_cumul ("read directory");
  if (do_cache_dir (name))
    cache_dir_set (name, dir);
  // End caching

  return dir;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void remove ( url  what)

Definition at line 543 of file file.cpp.

               {
  u= expand (complete (u));
  if (is_none (u));
  else if (is_or (u)) {
    remove (u[1]);
    remove (u[2]);
  }
  else {
    char *_u= as_charp (concretize (u));
    (void) ::remove (_u);
    tm_delete_array (_u);
  }
}

Here is the call graph for this function:

bool save_string ( url  file_name,
string  s,
bool  fatal = false 
)

Definition at line 117 of file file.cpp.

                                          {
  if (is_rooted_tmfs (u)) {
    bool err= save_to_server (u, s);
    if (err && fatal) {
      cerr << "File name= " << as_string (u) << "\n";
      FAILED ("file not writeable");
    }
    return err;
  }

  // cout << "Save " << u << LF;
  url r= u;
  if (!is_rooted_name (r)) r= resolve (r, "");
  bool err= !is_rooted_name (r);
  if (!err) {
    string name= concretize (r);
    char* _name= as_charp (name);
#if defined (OS_WIN32)
    FILE* fout= _fopen (_name, "wb");
#elif defined (__MINGW__) || defined (__MINGW32__)
    FILE* fout= fopen (_name, "wb");
#else
    FILE* fout= fopen (_name, "w");
#endif
    if (fout == NULL) {
      err= true;
      cerr << "TeXmacs] warning, save error for " << name << ", "
           << sys_errlist[errno] << "\n";
    }
    if (!err) {
      int i, n= N(s);
      for (i=0; i<n; i++)
       fputc (s[i], fout);
      fclose (fout);
    }
    tm_delete_array (_name);

    // Cache file contents
    bool file_flag= do_cache_file (name);
    bool doc_flag= do_cache_doc (name);
    string cache_type= doc_flag? string ("doc_cache"): string ("file_cache");
    if (!err && N(s) <= 10000)
      if (file_flag || doc_flag)
       cache_set (cache_type, name, s);
    (bool) is_up_to_date (url_parent (r), true);
    // End caching
  }

  if (err && fatal) {
    cerr << "File name= " << as_string (u) << "\n";
    FAILED ("file not writeable");
  }
  return err;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int search_score ( url  u,
array< string a 
)

Definition at line 506 of file file.cpp.

                                      {
  string in, suf= suffix (u);
  if (load_string (u, in, false)) return 0;
  in= locase_all (in);
  int i, score= 1, n= N(a);
  for (i=0; i<n; i++) {
    string what= locase_all (a[i]);
    array<int> pos= search (what, in);
    score *= compute_score (what, in, pos, suf);
    if (score == 0) return 0;
    if (score > 1000000) score= 1000000;
  }
  return score;
}

Here is the call graph for this function:

Here is the caller graph for this function:

string sys_concretize ( url  u1) [inline]

Definition at line 34 of file file.hpp.

                                      {
  return escape_sh (concretize (u1)); }

Here is the call graph for this function:

Here is the caller graph for this function:

void system ( string  which,
url  u1 
) [inline]

Definition at line 37 of file file.hpp.

                                          {
  system (which * " " * sys_concretize (u1)); }

Here is the call graph for this function:

void system ( string  which,
url  u1,
url  u2 
) [inline]

Definition at line 39 of file file.hpp.

                                                  {
  system (which * " " * sys_concretize (u1) * " " * sys_concretize (u2)); }

Here is the call graph for this function:

void system ( string  which,
url  u1,
const char *  post 
) [inline]

Definition at line 41 of file file.hpp.

                                                            {
  system (which * " " * sys_concretize (u1) * " " * post); }

Here is the call graph for this function:

void system ( string  which,
url  u1,
const char *  sep,
url  u2 
) [inline]

Definition at line 43 of file file.hpp.

                                                                   {
  system (which * " " * sys_concretize (u1) * " " * sep *
                 " " * sys_concretize (u2)); }

Here is the call graph for this function:

url url_numbered ( url  dir,
string  prefix,
string  postfix,
int  i = 1 
)

Definition at line 366 of file file.cpp.

                                                             {
  if (!exists (dir)) mkdir (dir);
  for (; true; i++) {
    url name= dir * (prefix * as_string (i) * postfix);
    if (!exists (name)) return name;
  }
  return dir * (prefix * "x" * postfix);
}

Here is the call graph for this function:

Here is the caller graph for this function:

url url_scratch ( string  prefix = "no_name_",
string  postfix = ".tm",
int  i = 1 
)

Definition at line 376 of file file.cpp.

                                                   {
  url dir ("$TEXMACS_HOME_PATH/texts/scratch");
  return url_numbered (dir, prefix, postfix, i);
}

Here is the call graph for this function:

Here is the caller graph for this function:

url url_temp ( string  suffix = "")

Definition at line 348 of file file.cpp.

                         {
#ifdef __MINGW32__
  int rnd= raw_time ();
#else
  static bool initialized= false;
  if (!initialized) {
    srandom ((int) raw_time ());
    initialized= true;
  }
  int rnd= random ();
#endif
  string name= "tmp_" * as_string (rnd) * suffix;
  url u ("$TEXMACS_HOME_PATH/system/tmp", name);
  if (exists (u)) return url_temp (suffix);
  return u;
}

Here is the call graph for this function:

Here is the caller graph for this function: