For now, this is only used in FileName, because it does not change the semantics of DocFileName::mangledFileName.
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));
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
*/
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