blob: d56e131cfa7e484ef86e0d05dbcf9a4b1d933a2f [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Karl Wiberg6a4d4112018-03-23 10:39:34 +010011#ifndef RTC_BASE_SYSTEM_FILE_WRAPPER_H_
12#define RTC_BASE_SYSTEM_FILE_WRAPPER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +000014#include <stddef.h>
henrikg@webrtc.org863b5362013-12-06 16:05:17 +000015#include <stdio.h>
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +000016
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/critical_section.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000018
Fredrik Solenberg98a91ad2018-04-11 19:04:37 +020019// Implementation that can read (exclusive) or write from/to a file.
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21namespace webrtc {
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +000022
Fredrik Solenberg98a91ad2018-04-11 19:04:37 +020023class FileWrapper final {
phoglund@webrtc.org740be442012-12-12 12:52:15 +000024 public:
Niels Möller5a6ae022019-01-21 11:59:10 +010025 // Opens a file, in read or write mode. Use the is_open() method on the
26 // returned object to check if the open operation was successful. The file is
27 // closed by the destructor.
28 static FileWrapper OpenReadOnly(const char* file_name_utf8);
29 static FileWrapper OpenWriteOnly(const char* file_name_utf8);
niklase@google.com470e71d2011-07-07 08:21:25 +000030
Niels Möller5a6ae022019-01-21 11:59:10 +010031 FileWrapper() = default;
tommia6219cc2016-06-15 10:30:14 -070032
Niels Möller5a6ae022019-01-21 11:59:10 +010033 // Takes over ownership of |file|, closing it on destruction.
34 explicit FileWrapper(FILE* file) : file_(file) {}
35 ~FileWrapper() { Close(); }
tommia6219cc2016-06-15 10:30:14 -070036
37 // Copying is not supported.
38 FileWrapper(const FileWrapper&) = delete;
39 FileWrapper& operator=(const FileWrapper&) = delete;
Niels Möller5a6ae022019-01-21 11:59:10 +010040
41 // Support for move semantics.
42 FileWrapper(FileWrapper&&);
43 FileWrapper& operator=(FileWrapper&&);
44
45 // Returns true if a file has been opened. If the file is not open, no methods
46 // but is_open and Close may be called.
47 bool is_open() const { return file_ != nullptr; }
48
49 // Closes the file, and implies Flush. Returns true on success, false if
50 // writing buffered data fails. On failure, the file is nevertheless closed.
51 // Calling Close on an already closed file does nothing and returns success.
52 bool Close();
53
54 // Write any buffered data to the underlying file. Returns true on success,
55 // false on write error. Note: Flushing when closing, is not required.
56 // TODO(nisse): Delete this method.
57 bool Flush();
58
59 // Seeks to the beginning of file. Returns true on success, false on failure,
60 // e.g., if the underlying file isn't seekable.
61 // TODO(nisse): Delete this method.
62 bool Rewind();
63
64 // Returns number of bytes read. Short count indicates EOF or error.
65 size_t Read(void* buf, size_t length);
66
67 // Returns true if all data was successfully written (or buffered), or false
68 // if there was an error. Writing buffered data can fail later, and is
69 // reported with return value from Flush or Close.
70 bool Write(const void* buf, size_t length);
71
72 private:
73 FILE* file_ = nullptr;
niklase@google.com470e71d2011-07-07 08:21:25 +000074};
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +000075
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000076} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000077
Karl Wiberg6a4d4112018-03-23 10:39:34 +010078#endif // RTC_BASE_SYSTEM_FILE_WRAPPER_H_