]> git.lyx.org Git - lyx.git/blob - src/support/FileName.cpp
create some helper functions in FileName
[lyx.git] / src / support / FileName.cpp
1 /**
2  * \file FileName.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "support/FileName.h"
14 #include "support/filetools.h"
15 #include "support/lstrings.h"
16 #include "support/os.h"
17 #include "support/qstring_helpers.h"
18
19 #include <QFile>
20 #include <QFileInfo>
21
22 #include <boost/filesystem/exception.hpp>
23 #include <boost/filesystem/operations.hpp>
24
25 #include <map>
26 #include <sstream>
27 #include <algorithm>
28
29
30 using std::map;
31 using std::string;
32
33 namespace lyx {
34 namespace support {
35
36
37 /////////////////////////////////////////////////////////////////////
38 //
39 // FileName
40 //
41 /////////////////////////////////////////////////////////////////////
42
43
44 FileName::FileName(string const & abs_filename)
45         : name_(abs_filename)
46 {
47         BOOST_ASSERT(empty() || absolutePath(name_));
48 #if defined(_WIN32)
49         BOOST_ASSERT(!contains(name_, '\\'));
50 #endif
51 }
52
53
54 void FileName::set(string const & name)
55 {
56         name_ = name;
57         BOOST_ASSERT(absolutePath(name_));
58 #if defined(_WIN32)
59         BOOST_ASSERT(!contains(name_, '\\'));
60 #endif
61 }
62
63
64 void FileName::erase()
65 {
66         name_.erase();
67 }
68
69
70 string const FileName::toFilesystemEncoding() const
71 {
72         QByteArray const encoded = QFile::encodeName(toqstr(name_));
73         return string(encoded.begin(), encoded.end());
74 }
75
76
77 FileName const FileName::fromFilesystemEncoding(string const & name)
78 {
79         QByteArray const encoded(name.c_str(), name.length());
80         return FileName(fromqstr(QFile::decodeName(encoded)));
81 }
82
83
84 bool FileName::exists() const
85 {
86         return QFileInfo(toqstr(name_)).exists();
87 }
88
89
90 bool FileName::isReadOnly() const
91 {
92         QFileInfo const fi(toqstr(name_));
93         return fi.isReadable() && !fi.isWritable();
94 }
95
96
97 std::time_t FileName::lastModified() const
98 {
99         return boost::filesystem::last_write_time(toFilesystemEncoding());
100 }
101
102
103 bool operator==(FileName const & lhs, FileName const & rhs)
104 {
105         return lhs.absFilename() == rhs.absFilename();
106 }
107
108
109 bool operator!=(FileName const & lhs, FileName const & rhs)
110 {
111         return lhs.absFilename() != rhs.absFilename();
112 }
113
114
115 bool operator<(FileName const & lhs, FileName const & rhs)
116 {
117         return lhs.absFilename() < rhs.absFilename();
118 }
119
120
121 bool operator>(FileName const & lhs, FileName const & rhs)
122 {
123         return lhs.absFilename() > rhs.absFilename();
124 }
125
126
127 std::ostream & operator<<(std::ostream & os, FileName const & filename)
128 {
129         return os << filename.absFilename();
130 }
131
132
133 /////////////////////////////////////////////////////////////////////
134 //
135 // DocFileName
136 //
137 /////////////////////////////////////////////////////////////////////
138
139
140 DocFileName::DocFileName()
141         : save_abs_path_(true)
142 {}
143
144
145 DocFileName::DocFileName(string const & abs_filename, bool save_abs)
146         : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
147 {}
148
149
150 DocFileName::DocFileName(FileName const & abs_filename, bool save_abs)
151         : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
152 {}
153
154
155 void DocFileName::set(string const & name, string const & buffer_path)
156 {
157         save_abs_path_ = absolutePath(name);
158         name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path).absFilename();
159         zipped_valid_ = false;
160 }
161
162
163 void DocFileName::erase()
164 {
165         name_.erase();
166         zipped_valid_ = false;
167 }
168
169
170 string const DocFileName::relFilename(string const & path) const
171 {
172         // FIXME UNICODE
173         return to_utf8(makeRelPath(from_utf8(name_), from_utf8(path)));
174 }
175
176
177 string const DocFileName::outputFilename(string const & path) const
178 {
179         // FIXME UNICODE
180         return save_abs_path_ ? name_ : to_utf8(makeRelPath(from_utf8(name_), from_utf8(path)));
181 }
182
183
184 string const DocFileName::mangledFilename(std::string const & dir) const
185 {
186         // We need to make sure that every DocFileName instance for a given
187         // filename returns the same mangled name.
188         typedef map<string, string> MangledMap;
189         static MangledMap mangledNames;
190         MangledMap::const_iterator const it = mangledNames.find(name_);
191         if (it != mangledNames.end())
192                 return (*it).second;
193
194         // Now the real work
195         string mname = os::internal_path(name_);
196         // Remove the extension.
197         mname = changeExtension(name_, string());
198         // The mangled name must be a valid LaTeX name.
199         // The list of characters to keep is probably over-restrictive,
200         // but it is not really a problem.
201         // Apart from non-ASCII characters, at least the following characters
202         // are forbidden: '/', '.', ' ', and ':'.
203         // On windows it is not possible to create files with '<', '>' or '?'
204         // in the name.
205         static string const keep = "abcdefghijklmnopqrstuvwxyz"
206                                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
207                                    "+,-0123456789;=";
208         string::size_type pos = 0;
209         while ((pos = mname.find_first_not_of(keep, pos)) != string::npos)
210                 mname[pos++] = '_';
211         // Add the extension back on
212         mname = changeExtension(mname, getExtension(name_));
213
214         // Prepend a counter to the filename. This is necessary to make
215         // the mangled name unique.
216         static int counter = 0;
217         std::ostringstream s;
218         s << counter++ << mname;
219         mname = s.str();
220
221         // MiKTeX's YAP (version 2.4.1803) crashes if the file name
222         // is longer than about 160 characters. MiKTeX's pdflatex
223         // is even pickier. A maximum length of 100 has been proven to work.
224         // If dir.size() > max length, all bets are off for YAP. We truncate
225         // the filename nevertheless, keeping a minimum of 10 chars.
226
227         string::size_type max_length = std::max(100 - ((int)dir.size() + 1), 10);
228
229         // If the mangled file name is too long, hack it to fit.
230         // We know we're guaranteed to have a unique file name because
231         // of the counter.
232         if (mname.size() > max_length) {
233                 int const half = (int(max_length) / 2) - 2;
234                 if (half > 0) {
235                         mname = mname.substr(0, half) + "___" +
236                                 mname.substr(mname.size() - half);
237                 }
238         }
239
240         mangledNames[name_] = mname;
241         return mname;
242 }
243
244
245 bool DocFileName::isZipped() const
246 {
247         if (!zipped_valid_) {
248                 zipped_ = zippedFile(*this);
249                 zipped_valid_ = true;
250         }
251         return zipped_;
252 }
253
254
255 string const DocFileName::unzippedFilename() const
256 {
257         return unzippedFileName(name_);
258 }
259
260
261 bool operator==(DocFileName const & lhs, DocFileName const & rhs)
262 {
263         return lhs.absFilename() == rhs.absFilename() &&
264                 lhs.saveAbsPath() == rhs.saveAbsPath();
265 }
266
267
268 bool operator!=(DocFileName const & lhs, DocFileName const & rhs)
269 {
270         return !(lhs == rhs);
271 }
272
273 } // namespace support
274 } // namespace lyx