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>
34 : save_abs_path_(true)
38 FileName::FileName(string const & abs_filename, bool save_abs)
39 : name_(abs_filename), save_abs_path_(save_abs)
41 BOOST_ASSERT(absolutePath(name_));
45 void FileName::set(string const & name, string const & buffer_path)
47 save_abs_path_ = absolutePath(name);
48 name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path);
52 void FileName::erase()
58 string const FileName::relFilename(string const & path) const
60 return makeRelPath(name_, path);
64 string const FileName::outputFilename(string const & path) const
66 return save_abs_path_ ? name_ : makeRelPath(name_, path);
70 string const FileName::mangledFilename(std::string const & dir) const
72 // We need to make sure that every FileName instance for a given
73 // filename returns the same mangled name.
74 typedef map<string, string> MangledMap;
75 static MangledMap mangledNames;
76 MangledMap::const_iterator const it = mangledNames.find(name_);
77 if (it != mangledNames.end())
81 string mname = os::internal_path(name_);
82 // Remove the extension.
83 mname = changeExtension(name_, string());
84 // Replace '/' in the file name with '_'
85 mname = subst(mname, "/", "_");
86 // Replace '.' in the file name with '_'
87 mname = subst(mname, ".", "_");
88 // Replace ' ' in the file name with '_'
89 mname = subst(mname, " ", "_");
90 // Replace ':' in the file name with '_'
91 mname = subst(mname, ":", "_");
92 // Add the extension back on
93 mname = changeExtension(mname, getExtension(name_));
95 // Prepend a counter to the filename. This is necessary to make
96 // the mangled name unique.
97 static int counter = 0;
99 s << counter++ << mname;
102 // MiKTeX's YAP (version 2.4.1803) crashes if the file name
103 // is longer than about 160 characters. MiKTeX's pdflatex
104 // is even pickier. A maximum length of 100 has been proven to work.
105 // If dir.size() > max length, all bets are off for YAP. We truncate
106 // the filename nevertheless, keeping a minimum of 10 chars.
108 string::size_type max_length = std::max(100 - ((int)dir.size() + 1), 10);
110 // If the mangled file name is too long, hack it to fit.
111 // We know we're guaranteed to have a unique file name because
113 if (mname.size() > max_length) {
114 int const half = (int(max_length) / 2) - 2;
116 mname = mname.substr(0, half) + "___" +
117 mname.substr(mname.size() - half);
121 mangledNames[name_] = mname;
126 bool FileName::isZipped() const
128 return zippedFile(name_);
132 string const FileName::unzippedFilename() const
134 return unzippedFileName(name_);
138 bool operator==(FileName const & lhs, FileName const & rhs)
140 return lhs.absFilename() == rhs.absFilename() &&
141 lhs.saveAbsPath() == rhs.saveAbsPath();
145 bool operator!=(FileName const & lhs, FileName const & rhs)
147 return !(lhs == rhs);
150 } // namespace support