]> git.lyx.org Git - lyx.git/blobdiff - src/support/filename.C
make "make distcheck" work
[lyx.git] / src / support / filename.C
index 121925250eda6ca16f4092c745064d27af5d56be..3f5020f46b62519d81291a1ab0d3d2252b069d0d 100644 (file)
@@ -5,16 +5,24 @@
  *
  * \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/filename.h"
 #include "support/filetools.h"
-#include "lstrings.h"
-#include "LAssert.h"
+#include "support/lstrings.h"
+#include "support/os.h"
+
+#include <boost/assert.hpp>
+
+#include <map>
+#include <sstream>
+
+
+using std::map;
+using std::string;
 
 
 namespace lyx {
@@ -29,7 +37,7 @@ FileName::FileName()
 FileName::FileName(string const & abs_filename, bool save_abs)
        : name_(abs_filename), save_abs_path_(save_abs)
 {
-       Assert(AbsolutePath(name_));
+       BOOST_ASSERT(AbsolutePath(name_));
 }
 
 
@@ -60,7 +68,16 @@ string const FileName::outputFilename(string const & path) const
 
 string const FileName::mangledFilename() const
 {
-       string mname = os::slashify_path(name_);
+       // 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::internal_path(name_);
        // Remove the extension.
        mname = ChangeExtension(name_, string());
        // Replace '/' in the file name with '_'
@@ -68,7 +85,22 @@ string const FileName::mangledFilename() const
        // Replace '.' in the file name with '_'
        mname = subst(mname, ".", "_");
        // Add the extension back on
-       return ChangeExtension(mname, GetExtension(name_));
+       mname = ChangeExtension(mname, GetExtension(name_));
+
+#if defined(__CYGWIN__) || defined(__CYGWIN32__) || defined(_WIN32)
+       // Mangle the drive letter in a Windows-style path.
+       if (mname.size() >= 2 && mname[1] == ':')
+               mname[1] = '_';
+#endif
+
+       // 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;
 }