xref: /aosp_15_r20/external/libwebm/webm_parser/include/webm/file_reader.h (revision 103e46e4cd4b6efcf6001f23fa8665fb110abf8d)
1 // Copyright (c) 2016 The WebM project authors. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS.  All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 #ifndef INCLUDE_WEBM_FILE_READER_H_
9 #define INCLUDE_WEBM_FILE_READER_H_
10 
11 #include <cstdint>
12 #include <cstdio>
13 #include <cstdlib>
14 #include <memory>
15 
16 #include "./reader.h"
17 #include "./status.h"
18 
19 /**
20  \file
21  A `Reader` implementation that reads from a `FILE*`.
22  */
23 
24 namespace webm {
25 
26 /**
27  A `Reader` implementation that can read from `FILE*` resources.
28  */
29 class FileReader : public Reader {
30  public:
31   /**
32    Constructs a new, empty reader.
33    */
34   FileReader() = default;
35 
36   /**
37    Constructs a new reader, using the provided file as the data source.
38 
39    Ownership of the file is taken, and `std::fclose()` will be called when the
40    object is destroyed.
41 
42    \param file The file to use as a data source. Must not be null. The file will
43    be closed (via `std::fclose()`) when the `FileReader`'s destructor runs.
44    */
45   explicit FileReader(FILE* file);
46 
47   /**
48    Constructs a new reader by moving the provided reader into the new reader.
49 
50    \param other The source reader to move. After moving, it will be reset to an
51    empty stream.
52    */
53   FileReader(FileReader&& other);
54 
55   /**
56    Moves the provided reader into this reader.
57 
58    \param other The source reader to move. After moving, it will be reset to an
59    empty stream. May be equal to `*this`, in which case this is a no-op.
60    \return `*this`.
61    */
62   FileReader& operator=(FileReader&& other);
63 
64   Status Read(std::size_t num_to_read, std::uint8_t* buffer,
65               std::uint64_t* num_actually_read) override;
66 
67   Status Skip(std::uint64_t num_to_skip,
68               std::uint64_t* num_actually_skipped) override;
69 
70   /**
71    Moves the reader to a new absolute byte position in the file.
72 
73    It is required to call DidSeek() on the parser after successfully seeking.
74    Seeking will only work on actual files, not stdin or pipes.
75 
76    \param seek_position The new absolute byte position in the file.
77    \return `Status::kOkCompleted` if reader position is now `seek_position`.
78    `Status::kSeekFailed` if the reader was unable to seek to `seek_position`
79    such as when the file is stdin.
80    */
81   Status Seek(std::uint64_t seek_position);
82 
83   std::uint64_t Position() const override;
84 
85  private:
86   struct FileCloseFunctor {
operatorFileCloseFunctor87     void operator()(FILE* file) const {
88       if (file)
89         std::fclose(file);
90     }
91   };
92 
93   std::unique_ptr<FILE, FileCloseFunctor> file_;
94 
95   // We can't rely on ftell() for the position (since it only returns long, and
96   // doesn't work on things like pipes); we need to manually track the reading
97   // position.
98   std::uint64_t position_ = 0;
99 };
100 
101 }  // namespace webm
102 
103 #endif  // INCLUDE_WEBM_FILE_READER_H_
104