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