]> git.lyx.org Git - lyx.git/blob - src/support/filetools.h
7242c108f80fee98b2fe8e138a7f6a059dee8d0c
[lyx.git] / src / support / filetools.h
1 // -*- C++-*-
2 /* lyx-filetool.h : tools functions for file/path handling
3    this file is part of LyX, the High Level Word Processor
4    Copyright 1995-2001, Matthias Ettrich and the LyX Team
5 */
6
7 #ifndef LYX_FILETOOL_H
8 #define LYX_FILETOOL_H
9
10 #ifdef __GNUG__
11 #pragma interface
12 #endif
13
14 #include <vector>
15 #include "LString.h"
16
17
18 ///
19 string const CreateBufferTmpDir (string const & pathfor = string());
20
21 /// Creates directory. Returns true on succes.
22 bool createDirectory(string const & name, int permissions);
23
24 ///
25 string const CreateLyXTmpDir (string const & deflt);
26
27 ///
28 int DestroyBufferTmpDir (string const & tmpdir);
29
30 ///
31 int DestroyLyXTmpDir (string const & tmpdir);
32
33 /** Find file by searching several directories.
34   Uses a string of paths separated by ";"s to find a file to open.
35     Can't cope with pathnames with a ';' in them. Returns full path to file.
36     If path entry begins with $$LyX/, use system_lyxdir.
37     If path entry begins with $$User/, use user_lyxdir.
38     Example: "$$User/doc;$$LyX/doc".
39 */
40 string const FileOpenSearch (string const & path, string const & name, 
41                        string const & ext = string());
42
43 /** Returns the real name of file name in directory path, with optional
44   extension ext.
45   The file is searched in the given path (unless it is an absolute
46   file name), first directly, and then with extension .ext (if given).
47   */
48 string const FileSearch(string const & path, string const & name, 
49                   string const & ext = string());
50
51 /// Returns a vector of all files in directory dir having extension ext.
52 std::vector<string> const DirList(string const & dir,
53                                   string const & ext = string());
54
55 /** Is directory read only?
56   returns 
57     1: dir writeable
58     0: not writeable
59    -1: error- couldn't find out, or unsure
60 */
61 int IsDirWriteable (string const & path);
62
63 /** Is a file readable ?
64   Returns true if the file `path' is readable.
65  */
66 bool IsFileReadable (string const & path);
67
68 /** Is file read only?
69   returns
70     1: read-write
71     0: read_only
72    -1: error (doesn't exist, no access, anything else)
73   */
74 int IsFileWriteable (string const & path);
75
76 ///
77 bool IsLyXFilename(string const & filename);
78
79 ///
80 bool IsSGMLFilename(string const & filename);
81
82 /** Returns the path of a library data file.
83   Search the file name.ext in the subdirectory dir of
84   \begin{enumerate}
85     \item user_lyxdir
86     \item build_lyxdir (if not empty)
87     \item system_lyxdir
88   \end{enumerate}
89     The third parameter `ext' is optional.
90 */
91 string const LibFileSearch(string const & dir, string const & name, 
92                      string const & ext = string());
93
94 /** Same as LibFileSearch(), but tries first to find an
95   internationalized version of the file by prepending $LANG_ to the
96   name 
97   */
98 string const
99 i18nLibFileSearch(string const & dir, string const & name, 
100                   string const & ext = string());
101
102 ///
103 string const GetEnv(string const & envname);
104
105 /// A helper function.
106 string const GetEnvPath(string const & name);
107
108 ///
109 bool PutEnv(string const & envstr);
110
111 ///
112 bool PutEnvPath(string const & envstr);
113
114 /// Substitutes active latex characters with underscores in filename
115 string const MakeLatexName(string const & file);
116
117 /// Put the name in quotes suitable for the current shell
118 string const QuoteName(string const & file);
119
120 /// Is a filename/path absolute?
121 bool AbsolutePath(string const & path);
122
123 /// Add a filename to a path. Any path from filename is stripped first.
124 string const AddName(string const & path, string const & fname);
125
126 /// Append sub-directory(ies) to path in an intelligent way
127 string const AddPath(string const & path, string const & path2);
128
129 /** Change extension of oldname to extension.
130  If oldname does not have an extension, it is appended.
131  If the extension is empty, any extension is removed from the name.
132  */
133 string const
134 ChangeExtension(string const & oldname, string const & extension);
135
136 /// Return the extension of the file (not including the .)
137 string const GetExtension(string const & name);
138
139 /// Create absolute path. If impossible, don't do anything
140 string const ExpandPath(string const & path);
141
142 /** Convert relative path into absolute path based on a basepath.
143   If relpath is absolute, just use that.
144   If basepath doesn't exist use CWD.
145   */
146 string const MakeAbsPath(string const & RelPath = string(), 
147                          string const & BasePath = string());
148
149 /** Creates a nice compact path for displaying. The parameter
150   threshold, if given, specifies the maximal length of the path.
151   */
152 string const
153 MakeDisplayPath(string const & path,
154                 unsigned int threshold = 1000);
155
156 /** Makes relative path out of absolute path.
157   If it is deeper than basepath,
158   it's easy. If basepath and abspath share something (they are all deeper
159   than some directory), it'll be rendered using ..'s. If they are completely
160   different, then the absolute path will be used as relative path
161   WARNING: the absolute path and base path must really be absolute paths!!!
162   */
163 string const
164 MakeRelPath(string const & abspath, string const & basepath);
165
166 /// Strip filename from path name
167 string const OnlyPath(string const & fname);
168
169 /// Normalize a path. Constracts path/../path
170 string const NormalizePath(string const & path);
171
172 /// Strips path from filename
173 string const OnlyFilename(string const & fname);
174
175 /// Get the contents of a file as a huge string
176 string const GetFileContents(string const & fname);
177
178 /** Check and Replace Environmentvariables ${NAME} in Path.
179     Replaces all occurences of these, if they are found in the
180     environment.
181     Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
182 */
183 string const ReplaceEnvironmentPath(string const & path);
184
185 /* Set Link to the path file points to as a symbolic link.
186    Returns true if successful */
187 bool LyXReadLink(string const & file, string & Link);
188
189 /// Uses kpsewhich to find tex files
190 string const findtexfile(string const & fil, string const & format);
191
192 /// remove the autosave-file and give a Message if it can't be done
193 void removeAutosaveFile(string const & filename);
194
195 #endif