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"
22 #include <boost/filesystem/exception.hpp>
23 #include <boost/filesystem/operations.hpp>
37 /////////////////////////////////////////////////////////////////////
41 /////////////////////////////////////////////////////////////////////
44 FileName::FileName(string const & abs_filename)
47 BOOST_ASSERT(empty() || absolutePath(name_));
49 BOOST_ASSERT(!contains(name_, '\\'));
54 void FileName::set(string const & name)
57 BOOST_ASSERT(absolutePath(name_));
59 BOOST_ASSERT(!contains(name_, '\\'));
64 void FileName::erase()
70 string const FileName::toFilesystemEncoding() const
72 QByteArray const encoded = QFile::encodeName(toqstr(name_));
73 return string(encoded.begin(), encoded.end());
77 FileName const FileName::fromFilesystemEncoding(string const & name)
79 QByteArray const encoded(name.c_str(), name.length());
80 return FileName(fromqstr(QFile::decodeName(encoded)));
84 bool FileName::exists() const
86 return QFileInfo(toqstr(name_)).exists();
90 bool FileName::isDir() const
92 return QFileInfo(toqstr(name_)).isDir();
96 bool FileName::isReadOnly() const
98 QFileInfo const fi(toqstr(name_));
99 return fi.isReadable() && !fi.isWritable();
103 std::time_t FileName::lastModified() const
105 return boost::filesystem::last_write_time(toFilesystemEncoding());
109 bool operator==(FileName const & lhs, FileName const & rhs)
111 return lhs.absFilename() == rhs.absFilename();
115 bool operator!=(FileName const & lhs, FileName const & rhs)
117 return lhs.absFilename() != rhs.absFilename();
121 bool operator<(FileName const & lhs, FileName const & rhs)
123 return lhs.absFilename() < rhs.absFilename();
127 bool operator>(FileName const & lhs, FileName const & rhs)
129 return lhs.absFilename() > rhs.absFilename();
133 std::ostream & operator<<(std::ostream & os, FileName const & filename)
135 return os << filename.absFilename();
139 /////////////////////////////////////////////////////////////////////
143 /////////////////////////////////////////////////////////////////////
146 DocFileName::DocFileName()
147 : save_abs_path_(true)
151 DocFileName::DocFileName(string const & abs_filename, bool save_abs)
152 : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
156 DocFileName::DocFileName(FileName const & abs_filename, bool save_abs)
157 : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
161 void DocFileName::set(string const & name, string const & buffer_path)
163 save_abs_path_ = absolutePath(name);
164 name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path).absFilename();
165 zipped_valid_ = false;
169 void DocFileName::erase()
172 zipped_valid_ = false;
176 string const DocFileName::relFilename(string const & path) const
179 return to_utf8(makeRelPath(from_utf8(name_), from_utf8(path)));
183 string const DocFileName::outputFilename(string const & path) const
186 return save_abs_path_ ? name_ : to_utf8(makeRelPath(from_utf8(name_), from_utf8(path)));
190 string const DocFileName::mangledFilename(std::string const & dir) const
192 // We need to make sure that every DocFileName instance for a given
193 // filename returns the same mangled name.
194 typedef map<string, string> MangledMap;
195 static MangledMap mangledNames;
196 MangledMap::const_iterator const it = mangledNames.find(name_);
197 if (it != mangledNames.end())
201 string mname = os::internal_path(name_);
202 // Remove the extension.
203 mname = changeExtension(name_, string());
204 // The mangled name must be a valid LaTeX name.
205 // The list of characters to keep is probably over-restrictive,
206 // but it is not really a problem.
207 // Apart from non-ASCII characters, at least the following characters
208 // are forbidden: '/', '.', ' ', and ':'.
209 // On windows it is not possible to create files with '<', '>' or '?'
211 static string const keep = "abcdefghijklmnopqrstuvwxyz"
212 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
214 string::size_type pos = 0;
215 while ((pos = mname.find_first_not_of(keep, pos)) != string::npos)
217 // Add the extension back on
218 mname = changeExtension(mname, getExtension(name_));
220 // Prepend a counter to the filename. This is necessary to make
221 // the mangled name unique.
222 static int counter = 0;
223 std::ostringstream s;
224 s << counter++ << mname;
227 // MiKTeX's YAP (version 2.4.1803) crashes if the file name
228 // is longer than about 160 characters. MiKTeX's pdflatex
229 // is even pickier. A maximum length of 100 has been proven to work.
230 // If dir.size() > max length, all bets are off for YAP. We truncate
231 // the filename nevertheless, keeping a minimum of 10 chars.
233 string::size_type max_length = std::max(100 - ((int)dir.size() + 1), 10);
235 // If the mangled file name is too long, hack it to fit.
236 // We know we're guaranteed to have a unique file name because
238 if (mname.size() > max_length) {
239 int const half = (int(max_length) / 2) - 2;
241 mname = mname.substr(0, half) + "___" +
242 mname.substr(mname.size() - half);
246 mangledNames[name_] = mname;
251 bool DocFileName::isZipped() const
253 if (!zipped_valid_) {
254 zipped_ = zippedFile(*this);
255 zipped_valid_ = true;
261 string const DocFileName::unzippedFilename() const
263 return unzippedFileName(name_);
267 bool operator==(DocFileName const & lhs, DocFileName const & rhs)
269 return lhs.absFilename() == rhs.absFilename() &&
270 lhs.saveAbsPath() == rhs.saveAbsPath();
274 bool operator!=(DocFileName const & lhs, DocFileName const & rhs)
276 return !(lhs == rhs);
279 } // namespace support