cmake/Source/cmGeneratedFileStream.h

156 lines
4.9 KiB
C
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2021-09-14 00:13:48 +02:00
#pragma once
2018-01-26 17:06:56 +01:00
#include "cmConfigure.h" // IWYU pragma: keep
2016-07-09 11:21:54 +02:00
2016-10-30 18:24:19 +01:00
#include <string>
2020-02-01 23:06:01 +01:00
#include "cmsys/FStream.hxx"
#include "cm_codecvt.hxx"
// This is the first base class of cmGeneratedFileStream. It will be
// created before and destroyed after the ofstream portion and can
// therefore be used to manage the temporary file.
class cmGeneratedFileStreamBase
{
protected:
// This constructor does not prepare the temporary file. The open
// method must be used.
cmGeneratedFileStreamBase();
// This constructor prepares the temporary output file.
2018-10-28 12:09:07 +01:00
cmGeneratedFileStreamBase(std::string const& name);
// The destructor renames the temporary output file to the real name.
~cmGeneratedFileStreamBase();
// Internal methods to handle the temporary file. Open is always
// called before the real stream is opened. Close is always called
// after the real stream is closed and Okay is set to whether the
// real stream was still valid for writing when it was closed.
2018-10-28 12:09:07 +01:00
void Open(std::string const& name);
bool Close();
// Internal file replacement implementation.
2018-10-28 12:09:07 +01:00
int RenameFile(std::string const& oldname, std::string const& newname);
// Internal file compression implementation.
2018-10-28 12:09:07 +01:00
int CompressFile(std::string const& oldname, std::string const& newname);
// The name of the final destination file for the output.
std::string Name;
2021-09-14 00:13:48 +02:00
// The extension of the temporary file.
std::string TempExt;
// The name of the temporary file.
std::string TempName;
// Whether to do a copy-if-different.
2019-11-11 23:01:05 +01:00
bool CopyIfDifferent = false;
// Whether the real file stream was valid when it was closed.
2019-11-11 23:01:05 +01:00
bool Okay = false;
2015-11-17 17:22:37 +01:00
// Whether the destination file is compressed
2019-11-11 23:01:05 +01:00
bool Compress = false;
2015-11-17 17:22:37 +01:00
// Whether the destination file is compressed
2019-11-11 23:01:05 +01:00
bool CompressExtraExtension = true;
};
/** \class cmGeneratedFileStream
* \brief Output stream for generated files.
*
* File generation should be atomic so that if CMake is killed then a
* generated file is either the original version or the complete new
* version. This stream is used to make sure file generation is
* atomic. Optionally the output file is only replaced if its
* contents have changed to prevent the file modification time from
* being updated.
*/
2018-08-09 18:06:22 +02:00
class cmGeneratedFileStream
: private cmGeneratedFileStreamBase
, public cmsys::ofstream
{
public:
2020-02-01 23:06:01 +01:00
using Stream = cmsys::ofstream;
using Encoding = codecvt::Encoding;
/**
* This constructor prepares a default stream. The open method must
* be used before writing to the stream.
*/
2017-04-14 19:02:05 +02:00
cmGeneratedFileStream(Encoding encoding = codecvt::None);
/**
* This constructor takes the name of the file to be generated. It
* automatically generates a name for the temporary file. If the
* file cannot be opened an error message is produced unless the
* second argument is set to true.
*/
2018-10-28 12:09:07 +01:00
cmGeneratedFileStream(std::string const& name, bool quiet = false,
2017-04-14 19:02:05 +02:00
Encoding encoding = codecvt::None);
/**
* The destructor checks the stream status to be sure the temporary
* file was successfully written before allowing the original to be
* replaced.
*/
2018-01-26 17:06:56 +01:00
~cmGeneratedFileStream() override;
2019-11-11 23:01:05 +01:00
cmGeneratedFileStream(cmGeneratedFileStream const&) = delete;
/**
* Open an output file by name. This should be used only with a
* non-open stream. It automatically generates a name for the
* temporary file. If the file cannot be opened an error message is
* produced unless the second argument is set to true.
*/
2018-10-28 12:09:07 +01:00
cmGeneratedFileStream& Open(std::string const& name, bool quiet = false,
2016-07-09 11:21:54 +02:00
bool binaryFlag = false);
/**
* Close the output file. This should be used only with an open
* stream. The temporary file is atomically renamed to the
2018-08-09 18:06:22 +02:00
* destination file if the stream is still valid when this method
* is called.
*/
bool Close();
/**
* Set whether copy-if-different is done.
*/
void SetCopyIfDifferent(bool copy_if_different);
/**
* Set whether compression is done.
*/
void SetCompression(bool compression);
/**
* Set whether compression has extra extension
*/
void SetCompressionExtraExtension(bool ext);
/**
* Set name of the file that will hold the actual output. This method allows
* the output file to be changed during the use of cmGeneratedFileStream.
*/
2015-04-27 22:25:09 +02:00
void SetName(const std::string& fname);
2021-09-14 00:13:48 +02:00
/**
* Set set a custom temporary file extension used with 'Open'.
* This does not work if the file was opened by the constructor.
*/
void SetTempExt(std::string const& ext);
/**
2022-11-16 20:14:03 +01:00
* Write a specific string using an alternate encoding.
* Afterward, the original encoding is restored.
2021-09-14 00:13:48 +02:00
*/
2022-11-16 20:14:03 +01:00
void WriteAltEncoding(std::string const& data, Encoding encoding);
2021-09-14 00:13:48 +02:00
};