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.
15 #include "filetools.h"
19 #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() 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::slashify_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 // Add the extension back on
89 mname = ChangeExtension(mname, GetExtension(name_));
90 // Prepend a counter to the filename. This is necessary to make
91 // the mangled name unique.
92 static int counter = 0;
95 mname = s.str() + mname;
96 mangledNames[name_] = mname;
101 bool FileName::isZipped() const
103 return zippedFile(name_);
107 string const FileName::unzippedFilename() const
109 return unzippedFileName(name_);
113 bool operator==(FileName const & lhs, FileName const & rhs)
115 return lhs.absFilename() == rhs.absFilename() &&
116 lhs.saveAbsPath() == rhs.saveAbsPath();
120 bool operator!=(FileName const & lhs, FileName const & rhs)
122 return !(lhs == rhs);
125 } // namespace support