home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / lyx-0.13.2.tar.gz / lyx-0.13.2.tar / lyx-0.13.2 / src / filetools.h < prev    next >
C/C++ Source or Header  |  1998-04-23  |  8KB  |  285 lines

  1. // -*- C++-*-
  2. //@Man: filetools
  3. /** lyx-filetool.h : tools functions for file/path handling
  4.   this file is part of LyX, the High Level Word Processor
  5.   copyright (C) 1995-1997, Matthias Ettrich and the LyX Team
  6.   */
  7.  
  8. #ifndef __LYX_FILETOOL_H__
  9. #define __LYX_FILETOOL_H__
  10.  
  11. #ifdef __GNUG__
  12. #pragma interface
  13. #endif
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <fcntl.h>
  18. #include <errno.h>
  19. #include "error.h"
  20. #include "LString.h"
  21.  
  22. /** A file class.
  23.   Use this instead of FILE *, it gives a much better structure.
  24.   It should prehaps do a bit more error checking than it does now.
  25.   Currently it is a verbatim copy from p309 of Bjarne Stroupstrups
  26.   The C++ Programming Language. + some additions.
  27.  */
  28. class FilePtr {
  29. public:
  30.     ///
  31.     enum file_mode {
  32.         read,
  33.         write,
  34.         update,
  35.         truncate
  36.     };
  37.     ///
  38.     FilePtr(LString const &name, file_mode mode)
  39.     {
  40.         init();
  41.         do_open(name, mode);
  42.     }
  43.     ///
  44.     FilePtr(FILE *pp) { init(); p = pp; }
  45.     ///
  46.     ~FilePtr() { close(); }
  47.  
  48.     /** Use this if you want to rebind the FilePtr to another file.
  49.      */
  50.     FilePtr& reopen(LString const &name, file_mode mode) {
  51.         // close the file it it is already open
  52.         close();
  53.         // Now open the file.
  54.         do_open(name, mode);
  55.  
  56.         return *this;
  57.     }
  58.     /** Close the file.
  59.       Use this with some carefullness. After it has been used
  60.       the FilePtr is unusable. Only use it if it is important
  61.       that the file is closed before the FilePtr goes out
  62.       of scope. */
  63.     int close() { 
  64.         if (p) {
  65.             int result = fclose(p); 
  66.             p = 0; 
  67.             return result;
  68.         } else 
  69.             return 0;
  70.     }
  71.     /// automatic converson to FILE* if that is needed.
  72.     operator FILE*() { return p; }
  73.     ///
  74.     FilePtr& operator=(FILE *f) { p=f; return *this;}
  75.     ///
  76.     FILE *operator()() { return p; }
  77. private:
  78.     ///
  79.     void do_open(LString const &name, file_mode mode) {
  80.         char modestr[3];
  81.         
  82.         switch(mode) {
  83.             // do appropiate #ifdef here so support EMX
  84. #ifndef __EMX__
  85.         case read: strcpy(modestr, "r"); break;
  86.         case write: strcpy(modestr, "w"); break;
  87. #else
  88.         case read: strcpy(modestr,"rt"); break; // Can read both DOS & UNIX text files.
  89.         case write: strcpy(modestr,"w"); break; // Write UNIX text files.
  90. #endif
  91.             
  92.         case update: strcpy(modestr, "r+"); break;
  93.         case truncate: strcpy(modestr, "w+"); break;
  94.         }
  95.         // Should probably be rewritten to use open(2)
  96.         if((p = fopen(name.c_str(), modestr))) {
  97.             // file succesfully opened.
  98.             if (fcntl(fileno(p),F_SETFD,FD_CLOEXEC) == -1) {
  99.                 p = 0;
  100.             }
  101.         } else {
  102.             // we have an error let's check what it is.
  103.             switch(errno) {
  104.             case EINVAL:
  105.                 // Internal LyX error.
  106.                 lyxerr.print("FilePtr: Wrong parameter given to fopen.");
  107.                 break;
  108.             default:
  109.                 // unknown error
  110.                 break;
  111.             }
  112.         }
  113.     }
  114.     ///
  115.     void init() { p = 0; }
  116.     ///
  117.     FILE *p;
  118. };
  119.  
  120.  
  121. ///
  122. LString CreateBufferTmpDir (LString const & pathfor = LString());
  123.  
  124. /// Creates directory. Returns true on succes.
  125. bool createDirectory(LString const & name, int permissions);
  126.  
  127. ///
  128. LString CreateLyXTmpDir (LString const & deflt);
  129.  
  130. ///
  131. int DestroyBufferTmpDir (LString const & tmpdir);
  132.  
  133. ///
  134. int DestroyLyXTmpDir (LString const & tmpdir);
  135.  
  136. /** Find file by searching several directories.
  137.   Uses a string of paths separated by ";"s to find a file to open.
  138.     Can't cope with pathnames with a ';' in them. Returns full path to file.
  139.     If path entry begins with $$LyX/, use system_lyxdir.
  140.     If path entry begins with $$User/, use user_lyxdir.
  141.     Example: "$$User/doc;$$LyX/doc".
  142. */
  143. LString FileOpenSearch (LString const & path, LString const & name, 
  144.             LString const & ext = LString());
  145.  
  146. /** Returns the real name of file name in directory path, with optional
  147.   extension ext.
  148.   The file is searched in the given path (unless it is an absolute
  149.   file name), first directly, and then with extension .ext (if given).
  150.   */
  151. LString FileSearch(LString const & path, LString const & name, 
  152.            LString const & ext = LString());
  153.  
  154. /** Is directory read only?
  155.   returns 
  156.     1: dir writeable
  157.     0: not writeable
  158.    -1: error- couldn't find out, or unsure
  159.   */
  160. int IsDirWriteable (LString const & path);
  161.  
  162. /** Is a file readable ?
  163.   Returns true if the file `path' is readable.
  164.  */
  165. bool IsFileReadable (LString const & path);
  166.  
  167. /** Is file read only?
  168.   returns
  169.     1: read-write
  170.     0: read_only
  171.    -1: error (doesn't exist, no access, anything else)
  172.   */
  173. int IsFileWriteable (LString const & path);
  174.  
  175. ///
  176. bool IsLyXFilename(LString const & filename);
  177.  
  178. ///
  179. bool IsSGMLFilename(LString const & filename);
  180.  
  181. /** Returns the path of a library data file.
  182.   Search the file name.ext in the subdirectory dir of
  183.   \begin{enumerate}
  184.     \item user_lyxdir
  185.     \item build_lyxdir (if not empty)
  186.     \item system_lyxdir
  187.   \end{enumerate}
  188.     The third parameter `ext' is optional.
  189. */
  190. LString LibFileSearch(LString const & dir, LString const & name, 
  191.               LString const & ext = LString());
  192.  
  193. /// Substitutes spaces with underscores in filename (and path)
  194. LString SpaceLess(LString const & file);
  195.  
  196. /** Returns an unique name to be used as a temporary file. If given,
  197.   'mask' should the prefix to the temporary file, the rest of the
  198.   temporary filename will be made from the pid and three letters.
  199.   */
  200. LString TmpFileName(LString const & dir = LString(), 
  201.             LString const & mask = "lyx_tmp");
  202.  
  203. /// Is a filename/path absolute?
  204. bool AbsolutePath(LString const &path);
  205.  
  206. /// Add a filename to a path. Any path from filename is stripped first.
  207. LString AddName(LString const &Path, LString const &Filename);
  208.  
  209. /// Append sub-directory(ies) to path in an intelligent way
  210. LString AddPath(LString const & path, LString const & path2);
  211.  
  212. /** Change extension of oldname to extension.
  213.  If no_path is true, the path is stripped from the filename.
  214.  If oldname does not have an extension, it is appended.
  215.  If the extension is empty, any extension is removed from the name.
  216.  */
  217. LString ChangeExtension(LString const & oldname, LString const & extension, 
  218.             bool no_path);
  219.  
  220. /// Create absolute path. If impossible, don't do anything
  221. LString ExpandPath(LString const &path);
  222.  
  223. /** 
  224.   Will perform automountd correction Real Soon Now.
  225.   Right now, just checks for PWD and CWD as environment,
  226.   otherwise returns Get_CWD.
  227.   Hmm, what does it take to do automountd correction? (Asger)
  228.   */
  229. LString safer_getcwd();
  230.  
  231. /// gets current working directory
  232. LString GetCWD();
  233.  
  234. /// A helper function.
  235. inline LString getEnvPath(char const *name)
  236. {
  237.     LString pathlist;
  238.     pathlist = getenv(name);
  239. #ifndef __EMX__
  240.     pathlist.subst(':', ';');
  241. #else
  242.     pathlist.subst('\\', '/');
  243. #endif
  244.     return pathlist.strip(';');
  245. }
  246.  
  247. /** Convert relative path into absolute path based on a basepath.
  248.   If relpath is absolute, just use that.
  249.   If basepath doesn't exist use CWD.
  250.   */
  251. LString MakeAbsPath(LString const &RelPath = LString(), 
  252.             LString const &BasePath = LString());
  253.  
  254. /** Creates a nice compact path for displaying. The parameter
  255.   threshold, if given, specifies the maximal length of the path.
  256.   */
  257. LString MakeDisplayPath(LString const & path, int threshold=1000);
  258.  
  259. /** Makes relative path out of absolute path.
  260.   If it is deeper than basepath,
  261.   it's easy. If basepath and abspath share something (they are all deeper
  262.   than some directory), it'll be rendered using ..'s. If they are completely
  263.   different, then the absolute path will be used as relative path
  264.   WARNING: the absolute path and base path must really be absolute paths!!!
  265.   */
  266. LString MakeRelPath(LString const & abspath, LString const & basepath);
  267.  
  268. /// Strip filename from path name
  269. LString OnlyPath(LString const &Filename);
  270.  
  271. /// Normalize a path. Constracts path/../path
  272. LString NormalizePath(LString const &path);
  273.  
  274. /// Strips path from filename
  275. LString OnlyFilename(LString const &Filename);
  276.  
  277. /** Check and Replace Environmentvariables ${NAME} in Path.
  278.     Replaces all occurences of these, if they are found in the
  279.     environment.
  280.     Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
  281. */
  282. LString ReplaceEnvironmentPath(LString const &path);
  283.  
  284. #endif
  285.