// Copyright 2022 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef UI_BASE_RESOURCE_SCOPED_FILE_WRITER_H_ #define UI_BASE_RESOURCE_SCOPED_FILE_WRITER_H_ #include <stdio.h> #include "base/files/file_path.h" #include "base/memory/raw_ptr.h" namespace ui { // Convenience class to write data to a file. Usage is the following: // 1) Create a new instance, passing a base::FilePath. // 2) Call Write() repeatedly to write all desired data to the file. // 3) Call valid() whenever you want to know if something failed. // 4) The file is closed automatically on destruction. Though it is possible // to call the Close() method before that. // // If an I/O error happens, a PLOG(ERROR) message will be generated, and // a flag will be set in the writer, telling it to ignore future Write() // requests. This allows the caller to ignore error handling until the // very end, as in: // // { // base::ScopedFileWriter writer(<some-path>); // writer.Write(&foo, sizeof(foo)); // writer.Write(&bar, sizeof(bar)); // .... // writer.Write(&zoo, sizeof(zoo)); // if (!writer.valid()) { // // An error happened. // } // } // closes the file. // class ScopedFileWriter { … }; } // namespace ui #endif // UI_BASE_RESOURCE_SCOPED_FILE_WRITER_H_