]> 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 ca193052e0644f28902359007a766bf74b69f807..a36af32567bc2ab16adbc6fc728a740e68238e59 100644 (file)
 
 #include "filename.h"
 
-#include "LAssert.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 {
 namespace support {
@@ -30,7 +38,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_));
 }
 
 
@@ -61,6 +69,15 @@ 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());
@@ -69,7 +86,15 @@ 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_));
+       // 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;
 }