]> git.lyx.org Git - lyx.git/blobdiff - src/support/filetools.h
Let paragraph::requestSpellcheck() consider contained insets
[lyx.git] / src / support / filetools.h
index 5177cdddee81ade1ced639a892c1225d36c5a6cb..404dec210f591a41a5b039d1d586132d04040c08 100644 (file)
 
 #include <utility>
 #include <string>
+#include <set>
 
 namespace lyx {
 namespace support {
 
 class FileName;
 
+/// Record used temp file names
+static std::set<std::string> tmp_names_;
+
+/// Get a temporary file name.
+/**
+* The actual temp file (QTemporaryFile object) is immediately
+* destroyed after the name has been generated, so a new file
+* has to be created manually from the name.
+* This is needed if the temp file has to be manually removed
+* (e.g., when temp files are used as conversion target, and the initial
+* file only serves as a placeholder), since QTemporaryFile objects
+* cannot be manually removed at least on Windows (they are always
+* kept open internally even after close()).
+* In order to avoid race conditions due to duplicate names, we record
+* all used temp file names.
+* If you don't have to remove the temp file manually, use TempFile instead!
+*/
+FileName const tempFileName(FileName, bool const dir = false);
+/// Get temporary file name with custom path
+FileName const tempFileName(FileName const &, std::string const &, bool const dir = false);
+/// Get temporary file name with default path
+FileName const tempFileName(std::string const &, bool const dir = false);
+
+/// Remove and unregister a temporary file.
+void removeTempFile(FileName const &);
+
 /** Creates the global LyX temp dir.
   \p deflt can be an existing directory name. In this case a new directory
   inside \p deflt is created. If \p deflt does not exist yet, \p deflt is
@@ -91,12 +118,14 @@ bool isBinaryFile(FileName const & filename);
       -# user_lyxdir
       -# build_lyxdir (if not empty)
       -# system_lyxdir
-    The third parameter `ext' is optional.
+    \p onlyglobal determines whether user_lyxdir should be included.
+    ext, search_mode and onlyglobal are optional.
 */
 FileName const libFileSearch(std::string const & dir,
                                std::string const & name,
                                std::string const & ext = std::string(),
-                               search_mode mode = must_exist);
+                               search_mode mode = must_exist,
+                               bool const onlyglobal = false);
 
 /** Same as libFileSearch(), but tries first to find an
   internationalized version of the file by prepending $LANG_ to the
@@ -117,10 +146,10 @@ imageLibFileSearch(std::string & dir, std::string const & name,
 
 /// How to quote a filename
 enum quote_style {
-       /** Quote for the (OS dependant) shell. This is needed for command
+       /** Quote for the (OS dependent) shell. This is needed for command
            line arguments of subprocesses. */
        quote_shell,
-       /** Quote a file name for the (OS dependant) shell. This is needed
+       /** Quote a file name for the (OS dependent) shell. This is needed
            for file names as command line arguments of subprocesses. */
        quote_shell_filename,
        /** Quote for python. Use this if you want to store a filename in a
@@ -158,7 +187,7 @@ enum latex_path_dots {
  *  If @c path contains LaTeX special characters, these are escaped.
  *  Eg, '~' -> '\\string~'
  *  If @c path contains spaces, then the returned path is enclosed in
- *  "-quotes. This last fix will lead to successful compiliation of the
+ *  "-quotes. This last fix will lead to successful compilation of the
  *  LaTeX file only if a sufficiently modern LaTeX compiler is used.
  *  If @c ext == EXCLUDE_EXTENSION the extension is left outside the quotes.
  *  This is needed for pdfeTeX, Version 3.141592-1.21a-2.2 (Web2C 7.5.4)
@@ -183,7 +212,11 @@ std::string const quoteName(std::string const & file, quote_style style = quote_
 /// Add a filename to a path. Any path from filename is stripped first.
 std::string const addName(std::string const & path, std::string const & fname);
 
-/// Append sub-directory(ies) to path in an intelligent way
+/// Add a relative path to a path. Does not strip the pathname
+std::string const addPathName(std::string const & path, std::string const & fname);
+
+/// Append sub-directory(ies) to path in an intelligent way. Will append the
+/// trailing directory separator if that is not provided.
 std::string const addPath(std::string const & path, std::string const & path2);
 
 /** Change extension of oldname to extension.
@@ -250,7 +283,7 @@ std::string const onlyPath(std::string const & fname);
 std::string const onlyFileName(std::string const & fname);
 
 /** Check and Replace Environmentvariables ${NAME} in Path.
-    Replaces all occurences of these, if they are found in the
+    Replaces all occurrences of these, if they are found in the
     environment.
     Variables are defined by Var := '${' [a-zA-Z_][a-zA-Z_0-9]* '}'
 */
@@ -282,7 +315,8 @@ bool readLink(FileName const & file, FileName & link);
  * \param format The file format as used by kpsewhich, e.g. "bib", "bst" etc.
  */
 FileName const findtexfile(std::string const & fil,
-                             std::string const & format);
+                                                  std::string const & format,
+                                                  bool const onlykpse = false);
 
 /** \param file1, file2 the two files to be compared. Must have absolute paths.
  *  \returns 1 if \c file1 has a more recent timestamp than \c file2,
@@ -300,13 +334,30 @@ bool prefs2prefs(FileName const & filename, FileName const & tempfile,
 /// Does file \p file need to be updated by configure.py?
 bool configFileNeedsUpdate(std::string const & file);
 
-typedef std::pair<int, std::string> cmd_ret;
+struct cmd_ret {
+       bool valid;
+       std::string result;
+};
 
 cmd_ret const runCommand(std::string const & cmd);
 
 int fileLock(const char * lock_file);
 void fileUnlock(int fd, const char * lock_file);
 
+/** Return the hex-encoded cryptographic hash of a string.
+ * The hash algorithm is not fixed, but it is determined at compile time.
+ * This function is typically used to create relatively stable file names,
+ * because cryptographic hash functions ensure that very small changes in the
+ * input result in large changes in the output.
+ * There is no limit in the length of the input string: it can be a file name
+ * or the contents of a file, for instance.
+ */
+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