]> git.lyx.org Git - features.git/commitdiff
Refactor file-name sanitisation.
authorThibaut Cuvelier <tcuvelier@lyx.org>
Tue, 19 Oct 2021 00:30:47 +0000 (02:30 +0200)
committerThibaut Cuvelier <tcuvelier@lyx.org>
Tue, 19 Oct 2021 07:15:44 +0000 (09:15 +0200)
For now, this is only used in FileName, because it does not change the semantics of DocFileName::mangledFileName.

src/support/FileName.cpp
src/support/filetools.cpp
src/support/filetools.h

index 5d70dd2b6c73798fc4e3b26f7f28acc00301359e..8ad7e00fa0f3cbc196b53b6c983c17e739d8b7d6 100644 (file)
@@ -977,18 +977,7 @@ string DocFileName::mangledFileName(string const & dir, bool use_counter, bool e
                mname = "export_" + onlyFileName() + "_" + toHexHash(mname);
 
        // The mangled name must be a valid LaTeX name.
-       // The list of characters to keep is probably over-restrictive,
-       // but it is not really a problem.
-       // Apart from non-ASCII characters, at least the following characters
-       // are forbidden: '/', '.', ' ', and ':'.
-       // On windows it is not possible to create files with '<', '>' or '?'
-       // in the name.
-       static string const keep = "abcdefghijklmnopqrstuvwxyz"
-                                  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-                                  "+-0123456789;=";
-       string::size_type pos = 0;
-       while ((pos = mname.find_first_not_of(keep, pos)) != string::npos)
-               mname[pos++] = '_';
+       mname = sanitizeFileName(mname);
        // Add the extension back on
        mname = support::changeExtension(mname, getExtension(name));
 
index d0221151799345e1034c4ac52e54ab7077460cf4..cfb245cd549e301d2de7a3edf6b220a6dad71c97 100644 (file)
@@ -1325,5 +1325,26 @@ std::string toHexHash(const std::string & str)
        return fromqstr(QString(hash.toHex()));
 }
 
+
+std::string sanitizeFileName(const std::string & str)
+{
+       // The list of characters to keep is probably over-restrictive,
+       // but it is not really a problem.
+       // Apart from non-ASCII characters, at least the following characters
+       // are forbidden: '/', '.', ' ', and ':'.
+       // On windows it is not possible to create files with '<', '>' or '?'
+       // in the name.
+       static std::string const keep = "abcdefghijklmnopqrstuvwxyz"
+                                  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+                                  "+-0123456789;=";
+
+       std::string name = str;
+       string::size_type pos = 0;
+       while ((pos = name.find_first_not_of(keep, pos)) != string::npos)
+               name[pos++] = '_';
+
+       return name;
+}
+
 } // namespace support
 } // namespace lyx
index 84b2378572694e92f6781976d9a15d61984e1077..404dec210f591a41a5b039d1d586132d04040c08 100644 (file)
@@ -354,6 +354,10 @@ void fileUnlock(int fd, const char * lock_file);
  */
 std::string toHexHash(const std::string & str);
 
+/// Replace non-ASCII characters to ensure that the string can be used as a
+/// file name on all platforms and as a LaTeX name.
+std::string sanitizeFileName(const std::string & str);
+
 } // namespace support
 } // namespace lyx