All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
safeOutputFile.h
Go to the documentation of this file.
1 //
2 // Copyright 2017 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef TF_SAFE_OUTPUT_FILE_H
25 #define TF_SAFE_OUTPUT_FILE_H
26 
29 
30 #include "pxr/pxr.h"
31 #include "pxr/base/tf/api.h"
32 
33 #include <cstdio>
34 #include <memory>
35 #include <string>
36 
37 PXR_NAMESPACE_OPEN_SCOPE
38 
50 {
51  TfSafeOutputFile(TfSafeOutputFile const &) = delete;
52  TfSafeOutputFile &operator=(TfSafeOutputFile const &) = delete;
53 public:
54  TfSafeOutputFile() = default;
55 
57  : _file(other._file)
58  , _targetFileName(std::move(other._targetFileName))
59  , _tempFileName(std::move(other._tempFileName))
60  { other._file = nullptr; }
61 
62  TfSafeOutputFile &operator=(TfSafeOutputFile &&other) {
63  _file = other._file;
64  _targetFileName = std::move(other._targetFileName);
65  _tempFileName = std::move(other._tempFileName);
66  other._file = nullptr;
67  return *this;
68  }
69 
71  TF_API ~TfSafeOutputFile();
72 
74  TF_API static TfSafeOutputFile Update(std::string const &fileName);
75 
79  TF_API static TfSafeOutputFile Replace(std::string const &fileName);
80 
83  TF_API void Close();
84 
88  TF_API void Discard();
89 
91  FILE *Get() const { return _file; }
92 
96  TF_API FILE *ReleaseUpdatedFile();
97 
100  TF_API bool IsOpenForUpdate() const;
101 
102 private:
103  FILE *_file = nullptr;
104  std::string _targetFileName;
105  std::string _tempFileName;
106 };
107 
108 PXR_NAMESPACE_CLOSE_SCOPE
109 
110 #endif // TF_SAFE_OUTPUT_FILE_H
static TF_API TfSafeOutputFile Replace(std::string const &fileName)
Arrange for fileName to be replaced.
TF_API ~TfSafeOutputFile()
Destructor invokes Close().
static TF_API TfSafeOutputFile Update(std::string const &fileName)
Open fileName for update (&quot;r+&quot;).
TF_API void Close()
Close the file.
TF_API FILE * ReleaseUpdatedFile()
If the underlying file was opened by Update(), return it.
TF_API bool IsOpenForUpdate() const
Return true if this TfSafeOutputFile was created by a call to Update(), false otherwise.
Opens a file for output, either for update &quot;r+&quot; or to completely replace &quot;w+&quot;.
FILE * Get() const
Return the opened FILE *.
TF_API void Discard()
Close the file.