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"
21 #include <boost/assert.hpp>
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 operator==(FileName const & lhs, FileName const & rhs)
86 return lhs.absFilename() == rhs.absFilename();
90 bool operator!=(FileName const & lhs, FileName const & rhs)
92 return lhs.absFilename() != rhs.absFilename();
96 bool operator<(FileName const & lhs, FileName const & rhs)
98 return lhs.absFilename() < rhs.absFilename();
102 bool operator>(FileName const & lhs, FileName const & rhs)
104 return lhs.absFilename() > rhs.absFilename();
108 std::ostream & operator<<(std::ostream & os, FileName const & filename)
110 return os << filename.absFilename();
114 DocFileName::DocFileName()
115 : save_abs_path_(true)
119 DocFileName::DocFileName(string const & abs_filename, bool save_abs)
120 : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
124 DocFileName::DocFileName(FileName const & abs_filename, bool save_abs)
125 : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
129 void DocFileName::set(string const & name, string const & buffer_path)
131 save_abs_path_ = absolutePath(name);
132 name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path).absFilename();
133 zipped_valid_ = false;
137 void DocFileName::erase()
140 zipped_valid_ = false;
144 string const DocFileName::relFilename(string const & path) const
146 return makeRelPath(name_, path);
150 string const DocFileName::outputFilename(string const & path) const
152 return save_abs_path_ ? name_ : makeRelPath(name_, path);
156 string const DocFileName::mangledFilename(std::string const & dir) const
158 // We need to make sure that every DocFileName instance for a given
159 // filename returns the same mangled name.
160 typedef map<string, string> MangledMap;
161 static MangledMap mangledNames;
162 MangledMap::const_iterator const it = mangledNames.find(name_);
163 if (it != mangledNames.end())
167 string mname = os::internal_path(name_);
168 // Remove the extension.
169 mname = changeExtension(name_, string());
170 // Replace '/' in the file name with '_'
171 mname = subst(mname, "/", "_");
172 // Replace '.' in the file name with '_'
173 mname = subst(mname, ".", "_");
174 // Replace ' ' in the file name with '_'
175 mname = subst(mname, " ", "_");
176 // Replace ':' in the file name with '_'
177 mname = subst(mname, ":", "_");
178 // Add the extension back on
179 mname = changeExtension(mname, getExtension(name_));
181 // Prepend a counter to the filename. This is necessary to make
182 // the mangled name unique.
183 static int counter = 0;
184 std::ostringstream s;
185 s << counter++ << mname;
188 // MiKTeX's YAP (version 2.4.1803) crashes if the file name
189 // is longer than about 160 characters. MiKTeX's pdflatex
190 // is even pickier. A maximum length of 100 has been proven to work.
191 // If dir.size() > max length, all bets are off for YAP. We truncate
192 // the filename nevertheless, keeping a minimum of 10 chars.
194 string::size_type max_length = std::max(100 - ((int)dir.size() + 1), 10);
196 // If the mangled file name is too long, hack it to fit.
197 // We know we're guaranteed to have a unique file name because
199 if (mname.size() > max_length) {
200 int const half = (int(max_length) / 2) - 2;
202 mname = mname.substr(0, half) + "___" +
203 mname.substr(mname.size() - half);
207 mangledNames[name_] = mname;
212 bool DocFileName::isZipped() const
214 if (!zipped_valid_) {
215 zipped_ = zippedFile(*this);
216 zipped_valid_ = true;
222 string const DocFileName::unzippedFilename() const
224 return unzippedFileName(name_);
228 bool operator==(DocFileName const & lhs, DocFileName const & rhs)
230 return lhs.absFilename() == rhs.absFilename() &&
231 lhs.saveAbsPath() == rhs.saveAbsPath();
235 bool operator!=(DocFileName const & lhs, DocFileName const & rhs)
237 return !(lhs == rhs);
240 } // namespace support