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_));
48 BOOST_ASSERT(!contains(name_, '\\'));
52 void FileName::set(string const & name)
55 BOOST_ASSERT(absolutePath(name_));
56 BOOST_ASSERT(!contains(name_, '\\'));
60 void FileName::erase()
66 string const FileName::toFilesystemEncoding() const
68 QByteArray const encoded = QFile::encodeName(toqstr(name_));
69 return string(encoded.begin(), encoded.end());
73 FileName const FileName::fromFilesystemEncoding(string const & name)
75 QByteArray const encoded(name.c_str(), name.length());
76 return FileName(fromqstr(QFile::decodeName(encoded)));
80 bool operator==(FileName const & lhs, FileName const & rhs)
82 return lhs.absFilename() == rhs.absFilename();
86 bool operator!=(FileName const & lhs, FileName const & rhs)
88 return lhs.absFilename() != rhs.absFilename();
92 bool operator<(FileName const & lhs, FileName const & rhs)
94 return lhs.absFilename() < rhs.absFilename();
98 bool operator>(FileName const & lhs, FileName const & rhs)
100 return lhs.absFilename() > rhs.absFilename();
104 std::ostream & operator<<(std::ostream & os, FileName const & filename)
106 return os << filename.absFilename();
110 DocFileName::DocFileName()
111 : save_abs_path_(true)
115 DocFileName::DocFileName(string const & abs_filename, bool save_abs)
116 : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
120 void DocFileName::set(string const & name, string const & buffer_path)
122 save_abs_path_ = absolutePath(name);
123 name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path);
124 zipped_valid_ = false;
128 void DocFileName::erase()
131 zipped_valid_ = false;
135 string const DocFileName::relFilename(string const & path) const
137 return makeRelPath(name_, path);
141 string const DocFileName::outputFilename(string const & path) const
143 return save_abs_path_ ? name_ : makeRelPath(name_, path);
147 string const DocFileName::mangledFilename(std::string const & dir) const
149 // We need to make sure that every DocFileName instance for a given
150 // filename returns the same mangled name.
151 typedef map<string, string> MangledMap;
152 static MangledMap mangledNames;
153 MangledMap::const_iterator const it = mangledNames.find(name_);
154 if (it != mangledNames.end())
158 string mname = os::internal_path(name_);
159 // Remove the extension.
160 mname = changeExtension(name_, string());
161 // Replace '/' in the file name with '_'
162 mname = subst(mname, "/", "_");
163 // Replace '.' in the file name with '_'
164 mname = subst(mname, ".", "_");
165 // Replace ' ' in the file name with '_'
166 mname = subst(mname, " ", "_");
167 // Replace ':' in the file name with '_'
168 mname = subst(mname, ":", "_");
169 // Add the extension back on
170 mname = changeExtension(mname, getExtension(name_));
172 // Prepend a counter to the filename. This is necessary to make
173 // the mangled name unique.
174 static int counter = 0;
175 std::ostringstream s;
176 s << counter++ << mname;
179 // MiKTeX's YAP (version 2.4.1803) crashes if the file name
180 // is longer than about 160 characters. MiKTeX's pdflatex
181 // is even pickier. A maximum length of 100 has been proven to work.
182 // If dir.size() > max length, all bets are off for YAP. We truncate
183 // the filename nevertheless, keeping a minimum of 10 chars.
185 string::size_type max_length = std::max(100 - ((int)dir.size() + 1), 10);
187 // If the mangled file name is too long, hack it to fit.
188 // We know we're guaranteed to have a unique file name because
190 if (mname.size() > max_length) {
191 int const half = (int(max_length) / 2) - 2;
193 mname = mname.substr(0, half) + "___" +
194 mname.substr(mname.size() - half);
198 mangledNames[name_] = mname;
203 bool DocFileName::isZipped() const
205 if (!zipped_valid_) {
206 zipped_ = zippedFile(*this);
207 zipped_valid_ = true;
213 string const DocFileName::unzippedFilename() const
215 return unzippedFileName(name_);
219 bool operator==(DocFileName const & lhs, DocFileName const & rhs)
221 return lhs.absFilename() == rhs.absFilename() &&
222 lhs.saveAbsPath() == rhs.saveAbsPath();
226 bool operator!=(DocFileName const & lhs, DocFileName const & rhs)
228 return !(lhs == rhs);
231 } // namespace support