]> git.lyx.org Git - lyx.git/blob - src/support/filetools.h
Use lyxconvert script for reading old files
[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 <utility>
16 #include "LString.h"
17
18
19 ///
20 string const CreateBufferTmpDir(string const & pathfor = string());
21
22 /// Creates directory. Returns true on succes.
23 bool createDirectory(string const & name, int permissions);
24
25 ///
26 string const CreateLyXTmpDir(string const & deflt);
27
28 ///
29 int DestroyBufferTmpDir(string const & tmpdir);
30
31 ///
32 int DestroyLyXTmpDir(string const & tmpdir);
33
34 /** Find file by searching several directories.
35   Uses a string of paths separated by ";"s to find a file to open.
36     Can't cope with pathnames with a ';' in them. Returns full path to file.
37     If path entry begins with $$LyX/, use system_lyxdir.
38     If path entry begins with $$User/, use user_lyxdir.
39     Example: "$$User/doc;$$LyX/doc".
40 */
41 string const FileOpenSearch(string const & path, string const & name,
42                        string const & ext = string());
43
44 /** Returns the real name of file name in directory path, with optional
45   extension ext.
46   The file is searched in the given path (unless it is an absolute
47   file name), first directly, and then with extension .ext (if given).
48   */
49 string const FileSearch(string const & path, string const & name,
50                   string const & ext = string());
51
52 /// Returns a vector of all files in directory dir having extension ext.
53 std::vector<string> const DirList(string const & dir,
54                                   string const & ext = string());
55
56 /** Is directory read only?
57   returns
58     true: dir writeable
59     false: not writeable
60 */
61 bool 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 /** Takes a command with arguments as input and tries to see whether
103   the command itself can be found in one of the scripts/ directories.
104   If it is found, return the command with fully qualified script name,
105   either return it unchanged */
106 string const LibScriptSearch(string const & command);
107
108 ///
109 string const GetEnv(string const & envname);
110
111 /// A helper function.
112 string const GetEnvPath(string const & name);
113
114 ///
115 bool PutEnv(string const & envstr);
116
117 ///
118 bool PutEnvPath(string const & envstr);
119
120 /// Substitutes active latex characters with underscores in filename
121 string const MakeLatexName(string const & file);
122
123 /// Put the name in quotes suitable for the current shell
124 string const QuoteName(string const & file);
125
126 /// Add a filename to a path. Any path from filename is stripped first.
127 string const AddName(string const & path, string const & fname);
128
129 /// Append sub-directory(ies) to path in an intelligent way
130 string const AddPath(string const & path, string const & path2);
131
132 /** Change extension of oldname to extension.
133  If oldname does not have an extension, it is appended.
134  If the extension is empty, any extension is removed from the name.
135  */
136 string const
137 ChangeExtension(string const & oldname, string const & extension);
138
139 /// Return the extension of the file (not including the .)
140 string const GetExtension(string const & name);
141
142 /// Return the type of the file as an extension from contents
143 string const getExtFromContents(string const & name);
144
145 /// check for zipped file
146 bool zippedFile(string const & name);
147
148 /// unzip a file
149 string const unzipFile(string const & zipped_file);
150
151 /// Returns true is path is absolute
152 bool AbsolutePath(string const & path);
153
154 /// Create absolute path. If impossible, don't do anything
155 string const ExpandPath(string const & path);
156
157 /** Convert relative path into absolute path based on a basepath.
158   If relpath is absolute, just use that.
159   If basepath doesn't exist use CWD.
160   */
161 string const MakeAbsPath(string const & RelPath = string(),
162                          string const & BasePath = string());
163
164 /** Creates a nice compact path for displaying. The parameter
165   threshold, if given, specifies the maximal length of the path.
166   */
167 string const
168 MakeDisplayPath(string const & path, unsigned int threshold = 1000);
169
170 /** Makes relative path out of absolute path.
171   If it is deeper than basepath,
172   it's easy. If basepath and abspath share something (they are all deeper
173   than some directory), it'll be rendered using ..'s. If they are completely
174   different, then the absolute path will be used as relative path
175   WARNING: the absolute path and base path must really be absolute paths!!!
176   */
177 string const
178 MakeRelPath(string const & abspath, string const & basepath);
179
180 /// Strip filename from path name
181 string const OnlyPath(string const & fname);
182
183 /// Normalize a path. Constracts path/../path
184 string const NormalizePath(string const & path);
185
186 /// Strips path from filename
187 string const OnlyFilename(string const & fname);
188
189 /// Get the contents of a file as a huge string
190 string const GetFileContents(string const & fname);
191
192 /** Check and Replace Environmentvariables ${NAME} in Path.
193     Replaces all occurences of these, if they are found in the
194     environment.
195     Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
196 */
197 string const ReplaceEnvironmentPath(string const & path);
198
199 /* Set \c link to the path \c file points to as a symbolic link.
200    If \c resolve is true, then \c link is an absolute path
201    Returns true if successful */
202 bool LyXReadLink(string const & file, string & link, bool resolve = false);
203
204 /// Uses kpsewhich to find tex files
205 string const findtexfile(string const & fil, string const & format);
206
207 /// remove the autosave-file and give a Message if it can't be done
208 void removeAutosaveFile(string const & filename);
209
210 /// read the BoundingBox entry from a ps/eps/pdf-file
211 string const readBB_from_PSFile(string const & file);
212
213 typedef std::pair<int, string> cmd_ret;
214
215 cmd_ret const RunCommand(string const & cmd);
216
217 #endif