blob: 42c463cb1529ce448eabd05ce67b6a98595b434d [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
Markus Handell3d221082020-07-16 14:15:23 +020017#include <string>
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
Niels Möller23213d92019-01-22 11:01:24 +010023// 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 Solenberg98a91ad2018-04-11 19:04:37 +020031class FileWrapper final {
phoglund@webrtc.org740be442012-12-12 12:52:15 +000032 public:
Niels Möller5a6ae022019-01-21 11:59:10 +010033 // Opens a file, in read or write mode. Use the is_open() method on the
Niels Möller23213d92019-01-22 11:01:24 +010034 // returned object to check if the open operation was successful. On failure,
35 // and if |error| is non-null, the system errno value is stored at |*error|.
36 // The file is closed by the destructor.
Niels Möller5a6ae022019-01-21 11:59:10 +010037 static FileWrapper OpenReadOnly(const char* file_name_utf8);
Niels Möller23213d92019-01-22 11:01:24 +010038 static FileWrapper OpenReadOnly(const std::string& file_name_utf8);
39 static FileWrapper OpenWriteOnly(const char* file_name_utf8,
40 int* error = nullptr);
41
42 static FileWrapper OpenWriteOnly(const std::string& file_name_utf8,
43 int* error = nullptr);
niklase@google.com470e71d2011-07-07 08:21:25 +000044
Niels Möller5a6ae022019-01-21 11:59:10 +010045 FileWrapper() = default;
tommia6219cc2016-06-15 10:30:14 -070046
Niels Möller646e0962019-06-10 11:55:59 +020047 // Takes over ownership of |file|, closing it on destruction. Calling with
48 // null |file| is allowed, and results in a FileWrapper with is_open() false.
Niels Möller5a6ae022019-01-21 11:59:10 +010049 explicit FileWrapper(FILE* file) : file_(file) {}
50 ~FileWrapper() { Close(); }
tommia6219cc2016-06-15 10:30:14 -070051
52 // Copying is not supported.
53 FileWrapper(const FileWrapper&) = delete;
54 FileWrapper& operator=(const FileWrapper&) = delete;
Niels Möller5a6ae022019-01-21 11:59:10 +010055
56 // Support for move semantics.
57 FileWrapper(FileWrapper&&);
58 FileWrapper& operator=(FileWrapper&&);
59
60 // Returns true if a file has been opened. If the file is not open, no methods
61 // but is_open and Close may be called.
62 bool is_open() const { return file_ != nullptr; }
63
64 // Closes the file, and implies Flush. Returns true on success, false if
65 // writing buffered data fails. On failure, the file is nevertheless closed.
66 // Calling Close on an already closed file does nothing and returns success.
67 bool Close();
68
Per Åhgrenb8a96302020-05-07 15:58:55 +020069 // Releases and returns the wrapped file without closing it. This call passes
70 // the ownership of the file to the caller, and the wrapper is no longer
71 // responsible for closing it. Similarly the previously wrapped file is no
72 // longer available for the wrapper to use in any aspect.
73 FILE* Release();
74
Niels Möller5a6ae022019-01-21 11:59:10 +010075 // Write any buffered data to the underlying file. Returns true on success,
76 // false on write error. Note: Flushing when closing, is not required.
Niels Möller5a6ae022019-01-21 11:59:10 +010077 bool Flush();
78
79 // Seeks to the beginning of file. Returns true on success, false on failure,
80 // e.g., if the underlying file isn't seekable.
Niels Möller7ba3b812019-08-06 09:58:56 +020081 bool Rewind() { return SeekTo(0); }
82 // TODO(nisse): The seek functions are used only by the WavReader. If that
83 // code is demoted to test code, seek functions can be deleted from this
84 // utility.
85 // Seek relative to current file position.
86 bool SeekRelative(int64_t offset);
87 // Seek to given position.
88 bool SeekTo(int64_t position);
Niels Möller5a6ae022019-01-21 11:59:10 +010089
90 // Returns number of bytes read. Short count indicates EOF or error.
91 size_t Read(void* buf, size_t length);
92
Niels Möller7ba3b812019-08-06 09:58:56 +020093 // If the most recent Read() returned a short count, this methods returns true
94 // if the short count was due to EOF, and false it it was due to some i/o
95 // error.
96 bool ReadEof() const;
97
Niels Möller5a6ae022019-01-21 11:59:10 +010098 // Returns true if all data was successfully written (or buffered), or false
99 // if there was an error. Writing buffered data can fail later, and is
100 // reported with return value from Flush or Close.
101 bool Write(const void* buf, size_t length);
102
103 private:
104 FILE* file_ = nullptr;
niklase@google.com470e71d2011-07-07 08:21:25 +0000105};
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +0000106
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000107} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000108
Karl Wiberg6a4d4112018-03-23 10:39:34 +0100109#endif // RTC_BASE_SYSTEM_FILE_WRAPPER_H_