OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_FILE_WRAPPER_H_ | 11 #ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_FILE_WRAPPER_H_ |
12 #define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_FILE_WRAPPER_H_ | 12 #define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_FILE_WRAPPER_H_ |
13 | 13 |
14 #include <stddef.h> | 14 #include <stddef.h> |
15 #include <stdio.h> | 15 #include <stdio.h> |
16 | 16 |
17 #include "webrtc/base/criticalsection.h" | |
17 #include "webrtc/common_types.h" | 18 #include "webrtc/common_types.h" |
18 #include "webrtc/typedefs.h" | 19 #include "webrtc/typedefs.h" |
19 | 20 |
20 // Implementation of an InStream and OutStream that can read (exclusive) or | 21 // Implementation of an InStream and OutStream that can read (exclusive) or |
21 // write from/to a file. | 22 // write from/to a file. |
22 | 23 |
23 namespace webrtc { | 24 namespace webrtc { |
24 | 25 |
26 // TODO(tommi): Remove the base classes, rename to rtc::File and move to base. | |
25 class FileWrapper : public InStream, public OutStream { | 27 class FileWrapper : public InStream, public OutStream { |
26 public: | 28 public: |
27 static const size_t kMaxFileNameSize = 1024; | 29 static const size_t kMaxFileNameSize = 1024; |
28 | 30 |
29 // Factory method. Constructor disabled. | 31 // Factory methods. |
32 // TODO(tommi): Remove Create(). | |
30 static FileWrapper* Create(); | 33 static FileWrapper* Create(); |
34 static FileWrapper Open(const char* file_name_utf8, bool read_only); | |
35 | |
36 FileWrapper(FILE* file, size_t max_size); | |
37 ~FileWrapper() override; | |
38 | |
39 // Support for move semantics. | |
40 FileWrapper(FileWrapper&& other); | |
41 FileWrapper& operator=(FileWrapper&& other); | |
31 | 42 |
32 // Returns true if a file has been opened. | 43 // Returns true if a file has been opened. |
33 virtual bool Open() const = 0; | 44 bool is_open() const { return file_ != nullptr; } |
34 | 45 |
35 // Opens a file in read or write mode, decided by the read_only parameter. | 46 // Opens a file in read or write mode, decided by the read_only parameter. |
36 virtual int OpenFile(const char* file_name_utf8, | 47 int OpenFile(const char* file_name_utf8, bool read_only); |
åsapersson
2016/06/14 15:22:12
make method return bool?
tommi
2016/06/14 20:45:34
Done.
| |
37 bool read_only, | |
38 bool loop = false, | |
39 bool text = false) = 0; | |
40 | 48 |
41 // Initializes the wrapper from an existing handle. |read_only| must match in | 49 // Initializes the wrapper from an existing handle. |read_only| must match in |
åsapersson
2016/06/14 15:22:12
update comment..
tommi
2016/06/14 20:45:34
Done.
| |
42 // the mode the file was opened in. If |manage_file| is true, the wrapper | 50 // the mode the file was opened in. If |manage_file| is true, the wrapper |
43 // takes ownership of |handle| and closes it in CloseFile(). | 51 // takes ownership of |handle| and closes it in CloseFile(). |
44 virtual int OpenFromFileHandle(FILE* handle, | 52 int OpenFromFileHandle(FILE* handle); |
åsapersson
2016/06/14 15:22:12
return bool?
tommi
2016/06/14 20:45:34
Done.
| |
45 bool manage_file, | |
46 bool read_only, | |
47 bool loop = false) = 0; | |
48 | 53 |
49 virtual int CloseFile() = 0; | 54 void CloseFile(); |
50 | 55 |
51 // Limits the file size to |bytes|. Writing will fail after the cap | 56 // Limits the file size to |bytes|. Writing will fail after the cap |
52 // is hit. Pass zero to use an unlimited size. | 57 // is hit. Pass zero to use an unlimited size. |
53 virtual int SetMaxFileSize(size_t bytes) = 0; | 58 // TODO(tommi): Could we move this out into a separate class? |
59 int SetMaxFileSize(size_t bytes); | |
åsapersson
2016/06/14 15:22:12
void?
tommi
2016/06/14 20:45:34
Done.
| |
54 | 60 |
55 // Flush any pending writes. | 61 // Flush any pending writes. Note: Flushing when closing, is not required. |
56 virtual int Flush() = 0; | 62 int Flush(); |
57 | 63 |
58 // Returns the opened file's name in |file_name_utf8|. Provide the size of | 64 // Rewinds the file to the start. |
59 // the buffer in bytes in |size|. The name will be truncated if |size| is | 65 int Rewind() override; |
60 // too small. | 66 int Read(void* buf, size_t length) override; |
61 virtual int FileName(char* file_name_utf8, | 67 bool Write(const void* buf, size_t length) override; |
62 size_t size) const = 0; | |
63 | 68 |
64 // Write |format| to the opened file. Arguments are taken in the same manner | 69 private: |
65 // as printf. That is, supply a format string containing text and | 70 FileWrapper(); |
66 // specifiers. Returns the number of characters written or -1 on error. | |
67 virtual int WriteText(const char* format, ...) = 0; | |
68 | 71 |
69 // Inherited from both Instream and OutStream. | 72 void CloseFileImpl(); |
70 // Rewinds the file to the start. Only available when OpenFile() has been | 73 int FlushImpl(); |
71 // called with |loop| == true or |readOnly| == true. | 74 |
72 // virtual int Rewind() = 0; | 75 // TODO(tommi): Remove the lock. |
73 int Rewind() override; | 76 rtc::CriticalSection lock_; |
77 | |
78 FILE* file_ = nullptr; | |
79 size_t position_ = 0; | |
80 size_t max_size_in_bytes_ = 0; | |
81 | |
82 // Copying is not supported. | |
83 FileWrapper(const FileWrapper&) = delete; | |
84 FileWrapper& operator=(const FileWrapper&) = delete; | |
74 }; | 85 }; |
75 | 86 |
76 } // namespace webrtc | 87 } // namespace webrtc |
77 | 88 |
78 #endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_FILE_WRAPPER_H_ | 89 #endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_FILE_WRAPPER_H_ |
OLD | NEW |