]> git.lyx.org Git - lyx.git/blobdiff - src/support/filename.C
* lyxfunctional.h: delete compare_memfun and helper classes
[lyx.git] / src / support / filename.C
index fa23523d181850311ae23ed70ba9720fc81db11f..a36af32567bc2ab16adbc6fc728a740e68238e59 100644 (file)
@@ -5,14 +5,25 @@
  *
  * \author Angus Leeming
  *
- * This file is part of LyX, the document processor.
- * Licence details can be found in the file COPYING.
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
 #include "filename.h"
-#include "support/filetools.h"
+
+#include "filetools.h"
+#include "lstrings.h"
+#include "os.h"
+
+#include <boost/assert.hpp>
+
+#include <map>
+#include <sstream>
+
+
+using std::map;
+using std::string;
 
 
 namespace lyx {
@@ -24,6 +35,13 @@ FileName::FileName()
 {}
 
 
+FileName::FileName(string const & abs_filename, bool save_abs)
+       : name_(abs_filename), save_abs_path_(save_abs)
+{
+       BOOST_ASSERT(AbsolutePath(name_));
+}
+
+
 void FileName::set(string const & name, string const & buffer_path)
 {
        save_abs_path_ = AbsolutePath(name);
@@ -49,6 +67,49 @@ string const FileName::outputFilename(string const & path) const
 }
 
 
+string const FileName::mangledFilename() const
+{
+       // We need to make sure that every FileName instance for a given
+       // filename returns the same mangled name.
+       typedef map<string, string> MangledMap;
+       static MangledMap mangledNames;
+       MangledMap::const_iterator const it = mangledNames.find(name_);
+       if (it != mangledNames.end())
+               return (*it).second;
+
+       // Now the real work
+       string mname = os::slashify_path(name_);
+       // Remove the extension.
+       mname = ChangeExtension(name_, string());
+       // Replace '/' in the file name with '_'
+       mname = subst(mname, "/", "_");
+       // Replace '.' in the file name with '_'
+       mname = subst(mname, ".", "_");
+       // Add the extension back on
+       mname = ChangeExtension(mname, GetExtension(name_));
+       // Prepend a counter to the filename. This is necessary to make
+       // the mangled name unique.
+       static int counter = 0;
+       std::ostringstream s;
+       s << counter++;
+       mname = s.str() + mname;
+       mangledNames[name_] = mname;
+       return mname;
+}
+
+
+bool FileName::isZipped() const
+{
+       return zippedFile(name_);
+}
+
+
+string const FileName::unzippedFilename() const
+{
+       return unzippedFileName(name_);
+}
+
+
 bool operator==(FileName const & lhs, FileName const & rhs)
 {
        return lhs.absFilename() == rhs.absFilename() &&
@@ -61,5 +122,5 @@ bool operator!=(FileName const & lhs, FileName const & rhs)
        return !(lhs == rhs);
 }
 
-} //namespace support
+} // namespace support
 } // namespace lyx