3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
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"
25 #include <boost/filesystem/exception.hpp>
26 #include <boost/filesystem/operations.hpp>
40 /////////////////////////////////////////////////////////////////////
44 /////////////////////////////////////////////////////////////////////
47 FileName::FileName(string const & abs_filename)
50 BOOST_ASSERT(empty() || absolutePath(name_));
52 BOOST_ASSERT(!contains(name_, '\\'));
57 void FileName::set(string const & name)
60 BOOST_ASSERT(absolutePath(name_));
62 BOOST_ASSERT(!contains(name_, '\\'));
67 void FileName::erase()
73 string FileName::toFilesystemEncoding() const
75 QByteArray const encoded = QFile::encodeName(toqstr(name_));
76 return string(encoded.begin(), encoded.end());
80 FileName FileName::fromFilesystemEncoding(string const & name)
82 QByteArray const encoded(name.c_str(), name.length());
83 return FileName(fromqstr(QFile::decodeName(encoded)));
87 bool FileName::exists() const
89 return QFileInfo(toqstr(name_)).exists();
93 bool FileName::isDirectory() const
95 return QFileInfo(toqstr(name_)).isDir();
99 bool FileName::isReadOnly() const
101 QFileInfo const fi(toqstr(name_));
102 return fi.isReadable() && !fi.isWritable();
106 bool FileName::isReadable() const
108 QFileInfo const fi(toqstr(name_));
109 return fi.isReadable();
113 bool FileName::isFileReadable() const
115 QFileInfo const fi(toqstr(name_));
116 return fi.isFile() && fi.isReadable();
120 bool FileName::isWritable() const
122 QFileInfo const fi(toqstr(name_));
123 return fi.isWritable();
127 bool FileName::isDirWritable() const
129 LYXERR(Debug::FILES) << "isDirWriteable: " << *this << std::endl;
131 FileName const tmpfl(tempName(*this, "lyxwritetest"));
141 FileName FileName::tempName(FileName const & dir, std::string const & mask)
143 return support::tempName(dir, mask);
147 std::time_t FileName::lastModified() const
149 return boost::filesystem::last_write_time(toFilesystemEncoding());
153 bool operator==(FileName const & lhs, FileName const & rhs)
155 return lhs.absFilename() == rhs.absFilename();
159 bool operator!=(FileName const & lhs, FileName const & rhs)
161 return lhs.absFilename() != rhs.absFilename();
165 bool operator<(FileName const & lhs, FileName const & rhs)
167 return lhs.absFilename() < rhs.absFilename();
171 bool operator>(FileName const & lhs, FileName const & rhs)
173 return lhs.absFilename() > rhs.absFilename();
177 std::ostream & operator<<(std::ostream & os, FileName const & filename)
179 return os << filename.absFilename();
183 /////////////////////////////////////////////////////////////////////
187 /////////////////////////////////////////////////////////////////////
190 DocFileName::DocFileName()
191 : save_abs_path_(true)
195 DocFileName::DocFileName(string const & abs_filename, bool save_abs)
196 : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
200 DocFileName::DocFileName(FileName const & abs_filename, bool save_abs)
201 : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
205 void DocFileName::set(string const & name, string const & buffer_path)
207 save_abs_path_ = absolutePath(name);
208 name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path).absFilename();
209 zipped_valid_ = false;
213 void DocFileName::erase()
216 zipped_valid_ = false;
220 string const DocFileName::relFilename(string const & path) const
223 return to_utf8(makeRelPath(from_utf8(name_), from_utf8(path)));
227 string const DocFileName::outputFilename(string const & path) const
230 return save_abs_path_ ? name_ : to_utf8(makeRelPath(from_utf8(name_), from_utf8(path)));
234 string const DocFileName::mangledFilename(std::string const & dir) const
236 // We need to make sure that every DocFileName instance for a given
237 // filename returns the same mangled name.
238 typedef map<string, string> MangledMap;
239 static MangledMap mangledNames;
240 MangledMap::const_iterator const it = mangledNames.find(name_);
241 if (it != mangledNames.end())
245 string mname = os::internal_path(name_);
246 // Remove the extension.
247 mname = changeExtension(name_, string());
248 // The mangled name must be a valid LaTeX name.
249 // The list of characters to keep is probably over-restrictive,
250 // but it is not really a problem.
251 // Apart from non-ASCII characters, at least the following characters
252 // are forbidden: '/', '.', ' ', and ':'.
253 // On windows it is not possible to create files with '<', '>' or '?'
255 static string const keep = "abcdefghijklmnopqrstuvwxyz"
256 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
258 string::size_type pos = 0;
259 while ((pos = mname.find_first_not_of(keep, pos)) != string::npos)
261 // Add the extension back on
262 mname = changeExtension(mname, getExtension(name_));
264 // Prepend a counter to the filename. This is necessary to make
265 // the mangled name unique.
266 static int counter = 0;
267 std::ostringstream s;
268 s << counter++ << mname;
271 // MiKTeX's YAP (version 2.4.1803) crashes if the file name
272 // is longer than about 160 characters. MiKTeX's pdflatex
273 // is even pickier. A maximum length of 100 has been proven to work.
274 // If dir.size() > max length, all bets are off for YAP. We truncate
275 // the filename nevertheless, keeping a minimum of 10 chars.
277 string::size_type max_length = std::max(100 - ((int)dir.size() + 1), 10);
279 // If the mangled file name is too long, hack it to fit.
280 // We know we're guaranteed to have a unique file name because
282 if (mname.size() > max_length) {
283 int const half = (int(max_length) / 2) - 2;
285 mname = mname.substr(0, half) + "___" +
286 mname.substr(mname.size() - half);
290 mangledNames[name_] = mname;
295 bool DocFileName::isZipped() const
297 if (!zipped_valid_) {
298 zipped_ = zippedFile(*this);
299 zipped_valid_ = true;
305 string const DocFileName::unzippedFilename() const
307 return unzippedFileName(name_);
311 bool operator==(DocFileName const & lhs, DocFileName const & rhs)
313 return lhs.absFilename() == rhs.absFilename() &&
314 lhs.saveAbsPath() == rhs.saveAbsPath();
318 bool operator!=(DocFileName const & lhs, DocFileName const & rhs)
320 return !(lhs == rhs);
323 } // namespace support