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"
19 #include <QtCore/QFile>
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 bool operator==(FileName const & lhs, FileName const & rhs)
75 return lhs.absFilename() == rhs.absFilename();
79 bool operator!=(FileName const & lhs, FileName const & rhs)
81 return lhs.absFilename() != rhs.absFilename();
85 bool operator<(FileName const & lhs, FileName const & rhs)
87 return lhs.absFilename() < rhs.absFilename();
91 bool operator>(FileName const & lhs, FileName const & rhs)
93 return lhs.absFilename() > rhs.absFilename();
97 std::ostream & operator<<(std::ostream & os, FileName const & filename)
99 return os << filename.absFilename();
103 DocFileName::DocFileName()
104 : save_abs_path_(true)
108 DocFileName::DocFileName(string const & abs_filename, bool save_abs)
109 : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
113 void DocFileName::set(string const & name, string const & buffer_path)
115 save_abs_path_ = absolutePath(name);
116 name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path);
117 zipped_valid_ = false;
121 void DocFileName::erase()
124 zipped_valid_ = false;
128 string const DocFileName::relFilename(string const & path) const
130 return makeRelPath(name_, path);
134 string const DocFileName::outputFilename(string const & path) const
136 return save_abs_path_ ? name_ : makeRelPath(name_, path);
140 string const DocFileName::mangledFilename(std::string const & dir) const
142 // We need to make sure that every DocFileName instance for a given
143 // filename returns the same mangled name.
144 typedef map<string, string> MangledMap;
145 static MangledMap mangledNames;
146 MangledMap::const_iterator const it = mangledNames.find(name_);
147 if (it != mangledNames.end())
151 string mname = os::internal_path(name_);
152 // Remove the extension.
153 mname = changeExtension(name_, string());
154 // Replace '/' in the file name with '_'
155 mname = subst(mname, "/", "_");
156 // Replace '.' in the file name with '_'
157 mname = subst(mname, ".", "_");
158 // Replace ' ' in the file name with '_'
159 mname = subst(mname, " ", "_");
160 // Replace ':' in the file name with '_'
161 mname = subst(mname, ":", "_");
162 // Add the extension back on
163 mname = changeExtension(mname, getExtension(name_));
165 // Prepend a counter to the filename. This is necessary to make
166 // the mangled name unique.
167 static int counter = 0;
168 std::ostringstream s;
169 s << counter++ << mname;
172 // MiKTeX's YAP (version 2.4.1803) crashes if the file name
173 // is longer than about 160 characters. MiKTeX's pdflatex
174 // is even pickier. A maximum length of 100 has been proven to work.
175 // If dir.size() > max length, all bets are off for YAP. We truncate
176 // the filename nevertheless, keeping a minimum of 10 chars.
178 string::size_type max_length = std::max(100 - ((int)dir.size() + 1), 10);
180 // If the mangled file name is too long, hack it to fit.
181 // We know we're guaranteed to have a unique file name because
183 if (mname.size() > max_length) {
184 int const half = (int(max_length) / 2) - 2;
186 mname = mname.substr(0, half) + "___" +
187 mname.substr(mname.size() - half);
191 mangledNames[name_] = mname;
196 bool DocFileName::isZipped() const
198 if (!zipped_valid_) {
199 zipped_ = zippedFile(*this);
200 zipped_valid_ = true;
206 string const DocFileName::unzippedFilename() const
208 return unzippedFileName(name_);
212 bool operator==(DocFileName const & lhs, DocFileName const & rhs)
214 return lhs.absFilename() == rhs.absFilename() &&
215 lhs.saveAbsPath() == rhs.saveAbsPath();
219 bool operator!=(DocFileName const & lhs, DocFileName const & rhs)
221 return !(lhs == rhs);
224 } // namespace support