#include <sstream>
#include <vector>
+#include <QCryptographicHash>
+
#if defined (_WIN32)
#include <io.h>
#include <windows.h>
// 2) build_lyxdir (if not empty)
// 3) system_lyxdir
FileName const libFileSearch(string const & dir, string const & name,
- string const & ext, search_mode mode)
+ string const & ext, search_mode mode,
+ bool const only_global)
{
- FileName fullname = fileSearch(addPath(package().user_support().absFileName(), dir),
- name, ext, mode);
- if (!fullname.empty())
- return fullname;
+ FileName fullname;
+ if (!only_global) {
+ fullname = fileSearch(addPath(package().user_support().absFileName(), dir),
+ name, ext, mode);
+ if (!fullname.empty())
+ return fullname;
+ }
if (!package().build_support().empty())
fullname = fileSearch(addPath(package().build_support().absFileName(), dir),
// Search the string for ${VAR} and $VAR and replace VAR using getenv.
+// If VAR does not exist, ${VAR} and $VAR are left as is in the string.
string const replaceEnvironmentPath(string const & path)
{
+ if (!contains(path, '$'))
+ return path;
+
// ${VAR} is defined as
// $\{[A-Za-z_][A-Za-z_0-9]*\}
static string const envvar_br = "[$]\\{([A-Za-z_][A-Za-z_0-9]*)\\}";
string result = path;
while (1) {
smatch what;
+ bool brackets = true;
if (!regex_match(result, what, envvar_br_re)) {
+ brackets = false;
if (!regex_match(result, what, envvar_re))
break;
}
string env_var = getEnv(what.str(2));
+ if (env_var.empty()) {
+ // temporarily use alert/bell (0x07) in place of $
+ if (brackets)
+ env_var = "\a{" + what.str(2) + '}';
+ else
+ env_var = "\a" + what.str(2);
+ }
result = what.str(1) + env_var + what.str(3);
}
- return result;
+ return subst(result, '\a', '$');
} catch (exception const & e) {
LYXERR0("Something is very wrong: " << e.what());
return path;
FileName const findtexfile(string const & fil, string const & /*format*/,
bool const onlykpse)
{
- /* There is no problem to extend this function too use other
+ /* There is no problem to extend this function to use other
methods to look for files. It could be setup to look
in environment paths and also if wanted as a last resort
to a recursive find. One of the easier extensions would
#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