All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
safeOutputFile.h
Go to the documentation of this file.
1//
2// Copyright 2017 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_TF_SAFE_OUTPUT_FILE_H
8#define PXR_BASE_TF_SAFE_OUTPUT_FILE_H
9
12
13#include "pxr/pxr.h"
14#include "pxr/base/tf/api.h"
15
16#include <cstdio>
17#include <memory>
18#include <string>
19
20PXR_NAMESPACE_OPEN_SCOPE
21
33{
34 TfSafeOutputFile(TfSafeOutputFile const &) = delete;
35 TfSafeOutputFile &operator=(TfSafeOutputFile const &) = delete;
36public:
37 TfSafeOutputFile() = default;
38
40 : _file(other._file)
41 , _targetFileName(std::move(other._targetFileName))
42 , _tempFileName(std::move(other._tempFileName))
43 { other._file = nullptr; }
44
45 TfSafeOutputFile &operator=(TfSafeOutputFile &&other) {
46 _file = other._file;
47 _targetFileName = std::move(other._targetFileName);
48 _tempFileName = std::move(other._tempFileName);
49 other._file = nullptr;
50 return *this;
51 }
52
55
57 TF_API static TfSafeOutputFile Update(std::string const &fileName);
58
62 TF_API static TfSafeOutputFile Replace(std::string const &fileName);
63
66 TF_API void Close();
67
71 TF_API void Discard();
72
74 FILE *Get() const { return _file; }
75
79 TF_API FILE *ReleaseUpdatedFile();
80
83 TF_API bool IsOpenForUpdate() const;
84
85private:
86 FILE *_file = nullptr;
87 std::string _targetFileName;
88 std::string _tempFileName;
89};
90
91PXR_NAMESPACE_CLOSE_SCOPE
92
93#endif // PXR_BASE_TF_SAFE_OUTPUT_FILE_H
Opens a file for output, either for update "r+" or to completely replace "w+".
TF_API bool IsOpenForUpdate() const
Return true if this TfSafeOutputFile was created by a call to Update(), false otherwise.
TF_API FILE * ReleaseUpdatedFile()
If the underlying file was opened by Update(), return it.
static TF_API TfSafeOutputFile Replace(std::string const &fileName)
Arrange for fileName to be replaced.
static TF_API TfSafeOutputFile Update(std::string const &fileName)
Open fileName for update ("r+").
FILE * Get() const
Return the opened FILE *.
TF_API void Discard()
Close the file.
TF_API void Close()
Close the file.
TF_API ~TfSafeOutputFile()
Destructor invokes Close().