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(absolutePath(name_));
48 void FileName::set(string const & name)
51 BOOST_ASSERT(absolutePath(name_));
55 void FileName::erase()
61 string const FileName::toFilesystemEncoding() const
63 // FIXME UNICODE: correct encoding not implemented yet
68 bool operator==(FileName const & lhs, FileName const & rhs)
70 return lhs.absFilename() == rhs.absFilename();
74 bool operator!=(FileName const & lhs, FileName const & rhs)
76 return lhs.absFilename() != rhs.absFilename();
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 std::ostream & operator<<(std::ostream & os, FileName const & filename)
94 return os << filename.absFilename();
98 DocFileName::DocFileName()
99 : save_abs_path_(true)
103 DocFileName::DocFileName(string const & abs_filename, bool save_abs)
104 : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
108 void DocFileName::set(string const & name, string const & buffer_path)
110 save_abs_path_ = absolutePath(name);
111 name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path);
112 zipped_valid_ = false;
116 void DocFileName::erase()
119 zipped_valid_ = false;
123 string const DocFileName::relFilename(string const & path) const
125 return makeRelPath(name_, path);
129 string const DocFileName::outputFilename(string const & path) const
131 return save_abs_path_ ? name_ : makeRelPath(name_, path);
135 string const DocFileName::mangledFilename(std::string const & dir) const
137 // We need to make sure that every DocFileName instance for a given
138 // filename returns the same mangled name.
139 typedef map<string, string> MangledMap;
140 static MangledMap mangledNames;
141 MangledMap::const_iterator const it = mangledNames.find(name_);
142 if (it != mangledNames.end())
146 string mname = os::internal_path(name_);
147 // Remove the extension.
148 mname = changeExtension(name_, string());
149 // Replace '/' in the file name with '_'
150 mname = subst(mname, "/", "_");
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 // Add the extension back on
158 mname = changeExtension(mname, getExtension(name_));
160 // Prepend a counter to the filename. This is necessary to make
161 // the mangled name unique.
162 static int counter = 0;
163 std::ostringstream s;
164 s << counter++ << mname;
167 // MiKTeX's YAP (version 2.4.1803) crashes if the file name
168 // is longer than about 160 characters. MiKTeX's pdflatex
169 // is even pickier. A maximum length of 100 has been proven to work.
170 // If dir.size() > max length, all bets are off for YAP. We truncate
171 // the filename nevertheless, keeping a minimum of 10 chars.
173 string::size_type max_length = std::max(100 - ((int)dir.size() + 1), 10);
175 // If the mangled file name is too long, hack it to fit.
176 // We know we're guaranteed to have a unique file name because
178 if (mname.size() > max_length) {
179 int const half = (int(max_length) / 2) - 2;
181 mname = mname.substr(0, half) + "___" +
182 mname.substr(mname.size() - half);
186 mangledNames[name_] = mname;
191 bool DocFileName::isZipped() const
193 if (!zipped_valid_) {
194 zipped_ = zippedFile(*this);
195 zipped_valid_ = true;
201 string const DocFileName::unzippedFilename() const
203 return unzippedFileName(name_);
207 bool operator==(DocFileName const & lhs, DocFileName const & rhs)
209 return lhs.absFilename() == rhs.absFilename() &&
210 lhs.saveAbsPath() == rhs.saveAbsPath();
214 bool operator!=(DocFileName const & lhs, DocFileName const & rhs)
216 return !(lhs == rhs);
219 } // namespace support