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 DocFileName::DocFileName(FileName const & abs_filename, bool save_abs)
121 : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
125 void DocFileName::set(string const & name, string const & buffer_path)
127 save_abs_path_ = absolutePath(name);
128 name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path).absFilename();
129 zipped_valid_ = false;
133 void DocFileName::erase()
136 zipped_valid_ = false;
140 string const DocFileName::relFilename(string const & path) const
142 return makeRelPath(name_, path);
146 string const DocFileName::outputFilename(string const & path) const
148 return save_abs_path_ ? name_ : makeRelPath(name_, path);
152 string const DocFileName::mangledFilename(std::string const & dir) const
154 // We need to make sure that every DocFileName instance for a given
155 // filename returns the same mangled name.
156 typedef map<string, string> MangledMap;
157 static MangledMap mangledNames;
158 MangledMap::const_iterator const it = mangledNames.find(name_);
159 if (it != mangledNames.end())
163 string mname = os::internal_path(name_);
164 // Remove the extension.
165 mname = changeExtension(name_, string());
166 // Replace '/' in the file name with '_'
167 mname = subst(mname, "/", "_");
168 // Replace '.' in the file name with '_'
169 mname = subst(mname, ".", "_");
170 // Replace ' ' in the file name with '_'
171 mname = subst(mname, " ", "_");
172 // Replace ':' in the file name with '_'
173 mname = subst(mname, ":", "_");
174 // Add the extension back on
175 mname = changeExtension(mname, getExtension(name_));
177 // Prepend a counter to the filename. This is necessary to make
178 // the mangled name unique.
179 static int counter = 0;
180 std::ostringstream s;
181 s << counter++ << mname;
184 // MiKTeX's YAP (version 2.4.1803) crashes if the file name
185 // is longer than about 160 characters. MiKTeX's pdflatex
186 // is even pickier. A maximum length of 100 has been proven to work.
187 // If dir.size() > max length, all bets are off for YAP. We truncate
188 // the filename nevertheless, keeping a minimum of 10 chars.
190 string::size_type max_length = std::max(100 - ((int)dir.size() + 1), 10);
192 // If the mangled file name is too long, hack it to fit.
193 // We know we're guaranteed to have a unique file name because
195 if (mname.size() > max_length) {
196 int const half = (int(max_length) / 2) - 2;
198 mname = mname.substr(0, half) + "___" +
199 mname.substr(mname.size() - half);
203 mangledNames[name_] = mname;
208 bool DocFileName::isZipped() const
210 if (!zipped_valid_) {
211 zipped_ = zippedFile(*this);
212 zipped_valid_ = true;
218 string const DocFileName::unzippedFilename() const
220 return unzippedFileName(name_);
224 bool operator==(DocFileName const & lhs, DocFileName const & rhs)
226 return lhs.absFilename() == rhs.absFilename() &&
227 lhs.saveAbsPath() == rhs.saveAbsPath();
231 bool operator!=(DocFileName const & lhs, DocFileName const & rhs)
233 return !(lhs == rhs);
236 } // namespace support