]> git.lyx.org Git - lyx.git/blob - src/support/TempFile.h
Improve file saving strategy
[lyx.git] / src / support / TempFile.h
1 // -*- C++ -*-
2 /**
3  * \file TempFile.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Georg Baum
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef TEMPFILE_H
13 #define TEMPFILE_H
14
15 #include <string>
16
17 namespace lyx {
18 namespace support {
19
20 class FileName;
21
22 /**
23  * Class for safely creating temporary files without race conditions.
24  * The file is created in the constructor, and deleted in the destructor.
25  * You may do anything with the file (including deletion), but the instance
26  * of this class must stay alive as long as the file is needed.
27  */
28 class TempFile {
29 public:
30         /**
31          *Create a temporary file with the given mask.
32          * \p mask must be in filesystem encoding, if it contains a
33          * relative path, the template file will be created in the global
34          * temporary directory as given by 'package().temp_dir()'.
35          * If the mask contains "XXXXXX" this portion will be replaced by
36          * a uniquely generated string. If it does not contain this portion,
37          * it will be automatically appended using a dot. Therefore, please
38          * specify the "XXXXXX" portion if the extension of the generated
39          * name is important (e.g. for the converter machinery).
40          */
41         TempFile(std::string const & mask);
42         TempFile(FileName const & temp_dir, std::string const & mask);
43         ~TempFile();
44         /**
45          * Get the name of the temporary file.
46          * This is empty if the file could not be created.
47          */
48         FileName name() const;
49         /**
50          * Set whether the file should be automatically deleted in the
51          * destructor.
52          * Automatic deletion is the default, but it can be switched off if
53          * the file should be kept, because it should be renamed afterwards.
54          */
55         void setAutoRemove(bool autoremove);
56 private:
57         ///
58         struct Private;
59         Private * d;
60 };
61
62 } // namespace support
63 } // namespace lyx
64
65 #endif