]> git.lyx.org Git - lyx.git/blobdiff - src/support/filetools.cpp
Let paragraph::requestSpellcheck() consider contained insets
[lyx.git] / src / support / filetools.cpp
index 41e508b47f9a54dbde32d604b2b3c4099b5a73bf..cfb245cd549e301d2de7a3edf6b220a6dad71c97 100644 (file)
@@ -62,6 +62,8 @@
 #include <sstream>
 #include <vector>
 
+#include <QCryptographicHash>
+
 #if defined (_WIN32)
 #include <io.h>
 #include <windows.h>
@@ -1309,5 +1311,40 @@ void fileUnlock(int fd, const char * /* lock_file*/)
 #endif
 }
 
-} //namespace support
+
+std::string toHexHash(const std::string & str)
+{
+       // Use the best available hashing algorithm. Qt 5 proposes SHA-2, but Qt 4 is limited to SHA-1.
+#if QT_VERSION >= 0x050000
+       auto hashAlgo = QCryptographicHash::Sha256;
+#else
+       auto hashAlgo = QCryptographicHash::Sha1;
+#endif
+
+       QByteArray hash = QCryptographicHash::hash(toqstr(str).toLocal8Bit(), hashAlgo);
+       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