niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 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 Wiberg | 6a4d411 | 2018-03-23 10:39:34 +0100 | [diff] [blame] | 11 | #ifndef RTC_BASE_SYSTEM_FILE_WRAPPER_H_ |
| 12 | #define RTC_BASE_SYSTEM_FILE_WRAPPER_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 14 | #include <stddef.h> |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 16 | |
Markus Handell | 3d22108 | 2020-07-16 14:15:23 +0200 | [diff] [blame] | 17 | #include <string> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | |
Fredrik Solenberg | 98a91ad | 2018-04-11 19:04:37 +0200 | [diff] [blame] | 19 | // Implementation that can read (exclusive) or write from/to a file. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 22 | |
Niels Möller | 23213d9 | 2019-01-22 11:01:24 +0100 | [diff] [blame] | 23 | // This class is a thin wrapper around FILE*. It's main features are that it |
| 24 | // owns the FILE*, calling fclose on destruction, and that on windows, file |
| 25 | // names passed to the open methods are always treated as utf-8, regardless of |
| 26 | // system code page. |
| 27 | |
| 28 | // Most of the methods return only a success/fail indication. When needed, an |
| 29 | // optional argument |int* error| should be added to all methods, in the same |
| 30 | // way as for the OpenWriteOnly methods. |
Fredrik Solenberg | 98a91ad | 2018-04-11 19:04:37 +0200 | [diff] [blame] | 31 | class FileWrapper final { |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 32 | public: |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 33 | // Opens a file, in read or write mode. Use the is_open() method on the |
Niels Möller | 23213d9 | 2019-01-22 11:01:24 +0100 | [diff] [blame] | 34 | // returned object to check if the open operation was successful. On failure, |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 35 | // and if `error` is non-null, the system errno value is stored at |*error|. |
Niels Möller | 23213d9 | 2019-01-22 11:01:24 +0100 | [diff] [blame] | 36 | // The file is closed by the destructor. |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 37 | static FileWrapper OpenReadOnly(const char* file_name_utf8); |
Niels Möller | 23213d9 | 2019-01-22 11:01:24 +0100 | [diff] [blame] | 38 | static FileWrapper OpenReadOnly(const std::string& file_name_utf8); |
| 39 | static FileWrapper OpenWriteOnly(const char* file_name_utf8, |
| 40 | int* error = nullptr); |
Niels Möller | 23213d9 | 2019-01-22 11:01:24 +0100 | [diff] [blame] | 41 | static FileWrapper OpenWriteOnly(const std::string& file_name_utf8, |
| 42 | int* error = nullptr); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 44 | FileWrapper() = default; |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 45 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 46 | // Takes over ownership of `file`, closing it on destruction. Calling with |
| 47 | // null `file` is allowed, and results in a FileWrapper with is_open() false. |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 48 | explicit FileWrapper(FILE* file) : file_(file) {} |
| 49 | ~FileWrapper() { Close(); } |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 50 | |
| 51 | // Copying is not supported. |
| 52 | FileWrapper(const FileWrapper&) = delete; |
| 53 | FileWrapper& operator=(const FileWrapper&) = delete; |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 54 | |
| 55 | // Support for move semantics. |
| 56 | FileWrapper(FileWrapper&&); |
| 57 | FileWrapper& operator=(FileWrapper&&); |
| 58 | |
| 59 | // Returns true if a file has been opened. If the file is not open, no methods |
| 60 | // but is_open and Close may be called. |
| 61 | bool is_open() const { return file_ != nullptr; } |
| 62 | |
| 63 | // Closes the file, and implies Flush. Returns true on success, false if |
| 64 | // writing buffered data fails. On failure, the file is nevertheless closed. |
| 65 | // Calling Close on an already closed file does nothing and returns success. |
| 66 | bool Close(); |
| 67 | |
Per Åhgren | b8a9630 | 2020-05-07 15:58:55 +0200 | [diff] [blame] | 68 | // Releases and returns the wrapped file without closing it. This call passes |
| 69 | // the ownership of the file to the caller, and the wrapper is no longer |
| 70 | // responsible for closing it. Similarly the previously wrapped file is no |
| 71 | // longer available for the wrapper to use in any aspect. |
| 72 | FILE* Release(); |
| 73 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 74 | // Write any buffered data to the underlying file. Returns true on success, |
| 75 | // false on write error. Note: Flushing when closing, is not required. |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 76 | bool Flush(); |
| 77 | |
| 78 | // Seeks to the beginning of file. Returns true on success, false on failure, |
| 79 | // e.g., if the underlying file isn't seekable. |
Niels Möller | 7ba3b81 | 2019-08-06 09:58:56 +0200 | [diff] [blame] | 80 | bool Rewind() { return SeekTo(0); } |
| 81 | // TODO(nisse): The seek functions are used only by the WavReader. If that |
| 82 | // code is demoted to test code, seek functions can be deleted from this |
| 83 | // utility. |
| 84 | // Seek relative to current file position. |
| 85 | bool SeekRelative(int64_t offset); |
| 86 | // Seek to given position. |
| 87 | bool SeekTo(int64_t position); |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 88 | |
Björn Terelius | cbd6156 | 2021-03-25 15:52:16 +0100 | [diff] [blame] | 89 | // Returns the file size or -1 if a size could not be determined. |
| 90 | // (A file size might not exists for non-seekable files or file-like |
| 91 | // objects, for example /dev/tty on unix.) |
| 92 | long FileSize(); |
| 93 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 94 | // Returns number of bytes read. Short count indicates EOF or error. |
| 95 | size_t Read(void* buf, size_t length); |
| 96 | |
Niels Möller | 7ba3b81 | 2019-08-06 09:58:56 +0200 | [diff] [blame] | 97 | // If the most recent Read() returned a short count, this methods returns true |
| 98 | // if the short count was due to EOF, and false it it was due to some i/o |
| 99 | // error. |
| 100 | bool ReadEof() const; |
| 101 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 102 | // Returns true if all data was successfully written (or buffered), or false |
| 103 | // if there was an error. Writing buffered data can fail later, and is |
| 104 | // reported with return value from Flush or Close. |
| 105 | bool Write(const void* buf, size_t length); |
| 106 | |
| 107 | private: |
| 108 | FILE* file_ = nullptr; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 109 | }; |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 110 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 111 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 112 | |
Karl Wiberg | 6a4d411 | 2018-03-23 10:39:34 +0100 | [diff] [blame] | 113 | #endif // RTC_BASE_SYSTEM_FILE_WRAPPER_H_ |