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"
18 #include <boost/assert.hpp>
41 FileName::FileName(string const & abs_filename)
44 BOOST_ASSERT(empty() || absolutePath(name_));
45 BOOST_ASSERT(!contains(name_, '\\'));
49 void FileName::set(string const & name)
52 BOOST_ASSERT(absolutePath(name_));
53 BOOST_ASSERT(!contains(name_, '\\'));
57 void FileName::erase()
63 string const FileName::toFilesystemEncoding() const
65 // FIXME UNICODE: correct encoding not implemented yet
70 bool operator==(FileName const & lhs, FileName const & rhs)
72 return lhs.absFilename() == rhs.absFilename();
76 bool operator!=(FileName const & lhs, FileName const & rhs)
78 return lhs.absFilename() != rhs.absFilename();
82 bool operator<(FileName const & lhs, FileName const & rhs)
84 return lhs.absFilename() < rhs.absFilename();
88 bool operator>(FileName const & lhs, FileName const & rhs)
90 return lhs.absFilename() > rhs.absFilename();
94 std::ostream & operator<<(std::ostream & os, FileName const & filename)
96 return os << filename.absFilename();
100 DocFileName::DocFileName()
101 : save_abs_path_(true)
105 DocFileName::DocFileName(string const & abs_filename, bool save_abs)
106 : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
110 void DocFileName::set(string const & name, string const & buffer_path)
112 save_abs_path_ = absolutePath(name);
113 name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path);
114 zipped_valid_ = false;
118 void DocFileName::erase()
121 zipped_valid_ = false;
125 string const DocFileName::relFilename(string const & path) const
127 return makeRelPath(name_, path);
131 string const DocFileName::outputFilename(string const & path) const
133 return save_abs_path_ ? name_ : makeRelPath(name_, path);
137 string const DocFileName::mangledFilename(std::string const & dir) const
139 // We need to make sure that every DocFileName instance for a given
140 // filename returns the same mangled name.
141 typedef map<string, string> MangledMap;
142 static MangledMap mangledNames;
143 MangledMap::const_iterator const it = mangledNames.find(name_);
144 if (it != mangledNames.end())
148 string mname = os::internal_path(name_);
149 // Remove the extension.
150 mname = changeExtension(name_, string());
151 // Replace '/' in the file name with '_'
152 mname = subst(mname, "/", "_");
153 // Replace '.' in the file name with '_'
154 mname = subst(mname, ".", "_");
155 // Replace ' ' in the file name with '_'
156 mname = subst(mname, " ", "_");
157 // Replace ':' in the file name with '_'
158 mname = subst(mname, ":", "_");
159 // Add the extension back on
160 mname = changeExtension(mname, getExtension(name_));
162 // Prepend a counter to the filename. This is necessary to make
163 // the mangled name unique.
164 static int counter = 0;
165 std::ostringstream s;
166 s << counter++ << mname;
169 // MiKTeX's YAP (version 2.4.1803) crashes if the file name
170 // is longer than about 160 characters. MiKTeX's pdflatex
171 // is even pickier. A maximum length of 100 has been proven to work.
172 // If dir.size() > max length, all bets are off for YAP. We truncate
173 // the filename nevertheless, keeping a minimum of 10 chars.
175 string::size_type max_length = std::max(100 - ((int)dir.size() + 1), 10);
177 // If the mangled file name is too long, hack it to fit.
178 // We know we're guaranteed to have a unique file name because
180 if (mname.size() > max_length) {
181 int const half = (int(max_length) / 2) - 2;
183 mname = mname.substr(0, half) + "___" +
184 mname.substr(mname.size() - half);
188 mangledNames[name_] = mname;
193 bool DocFileName::isZipped() const
195 if (!zipped_valid_) {
196 zipped_ = zippedFile(*this);
197 zipped_valid_ = true;
203 string const DocFileName::unzippedFilename() const
205 return unzippedFileName(name_);
209 bool operator==(DocFileName const & lhs, DocFileName const & rhs)
211 return lhs.absFilename() == rhs.absFilename() &&
212 lhs.saveAbsPath() == rhs.saveAbsPath();
216 bool operator!=(DocFileName const & lhs, DocFileName const & rhs)
218 return !(lhs == rhs);
221 } // namespace support