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 | |
| 11 | #ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_ |
| 12 | #define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_ |
| 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 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 17 | #include "webrtc/common_types.h" |
| 18 | #include "webrtc/typedefs.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | |
| 20 | // Implementation of an InStream and OutStream that can read (exclusive) or |
| 21 | // write from/to a file. |
| 22 | |
| 23 | namespace webrtc { |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 24 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 25 | class FileWrapper : public InStream, public OutStream { |
| 26 | public: |
| 27 | static const size_t kMaxFileNameSize = 1024; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 29 | // Factory method. Constructor disabled. |
| 30 | static FileWrapper* Create(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 32 | // Returns true if a file has been opened. |
| 33 | virtual bool Open() const = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 35 | // Opens a file in read or write mode, decided by the read_only parameter. |
| 36 | virtual int OpenFile(const char* file_name_utf8, |
| 37 | bool read_only, |
| 38 | bool loop = false, |
| 39 | bool text = false) = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 41 | // Initializes the wrapper from an existing handle. |read_only| must match in |
| 42 | // the mode the file was opened in. If |manage_file| is true, the wrapper |
| 43 | // takes ownership of |handle| and closes it in CloseFile(). |
| 44 | virtual int OpenFromFileHandle(FILE* handle, |
| 45 | bool manage_file, |
| 46 | bool read_only, |
| 47 | bool loop = false) = 0; |
| 48 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 49 | virtual int CloseFile() = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 51 | // Limits the file size to |bytes|. Writing will fail after the cap |
| 52 | // is hit. Pass zero to use an unlimited size. |
| 53 | virtual int SetMaxFileSize(size_t bytes) = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 55 | // Flush any pending writes. |
| 56 | virtual int Flush() = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 58 | // Returns the opened file's name in |file_name_utf8|. Provide the size of |
| 59 | // the buffer in bytes in |size|. The name will be truncated if |size| is |
| 60 | // too small. |
| 61 | virtual int FileName(char* file_name_utf8, |
| 62 | size_t size) const = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 63 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 64 | // Write |format| to the opened file. Arguments are taken in the same manner |
| 65 | // as printf. That is, supply a format string containing text and |
| 66 | // specifiers. Returns the number of characters written or -1 on error. |
| 67 | virtual int WriteText(const char* format, ...) = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 69 | // Inherited from Instream. |
| 70 | // Reads |length| bytes from file to |buf|. Returns the number of bytes read |
| 71 | // or -1 on error. |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame^] | 72 | virtual int Read(void* buf, size_t length) = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 73 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 74 | // Inherited from OutStream. |
| 75 | // Writes |length| bytes from |buf| to file. The actual writing may happen |
| 76 | // some time later. Call Flush() to force a write. |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame^] | 77 | virtual bool Write(const void* buf, size_t length) = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 78 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 79 | // Inherited from both Instream and OutStream. |
| 80 | // Rewinds the file to the start. Only available when OpenFile() has been |
| 81 | // called with |loop| == true or |readOnly| == true. |
| 82 | virtual int Rewind() = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 83 | }; |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 84 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 85 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 87 | #endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_ |