summaryrefslogtreecommitdiff
path: root/webm_parser/include/webm/file_reader.h
blob: 6ccdc4de7b0e5529e7b71eebcdd42fd1edf46046 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2016 The WebM project authors. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS.  All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
#ifndef INCLUDE_WEBM_FILE_READER_H_
#define INCLUDE_WEBM_FILE_READER_H_

#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <memory>

#include "./reader.h"
#include "./status.h"

/**
 \file
 A `Reader` implementation that reads from a `FILE*`.
 */

namespace webm {

/**
 A `Reader` implementation that can read from `FILE*` resources.
 */
class FileReader : public Reader {
 public:
  /**
   Constructs a new, empty reader.
   */
  FileReader() = default;

  /**
   Constructs a new reader, using the provided file as the data source.

   Ownership of the file is taken, and `std::fclose()` will be called when the
   object is destroyed.

   \param file The file to use as a data source. Must not be null. The file will
   be closed (via `std::fclose()`) when the `FileReader`'s destructor runs.
   */
  explicit FileReader(FILE* file);

  /**
   Constructs a new reader by moving the provided reader into the new reader.

   \param other The source reader to move. After moving, it will be reset to an
   empty stream.
   */
  FileReader(FileReader&& other);

  /**
   Moves the provided reader into this reader.

   \param other The source reader to move. After moving, it will be reset to an
   empty stream. May be equal to `*this`, in which case this is a no-op.
   \return `*this`.
   */
  FileReader& operator=(FileReader&& other);

  Status Read(std::size_t num_to_read, std::uint8_t* buffer,
              std::uint64_t* num_actually_read) override;

  Status Skip(std::uint64_t num_to_skip,
              std::uint64_t* num_actually_skipped) override;

  std::uint64_t Position() const override;

 private:
  struct FileCloseFunctor {
    void operator()(FILE* file) const {
      if (file)
        std::fclose(file);
    }
  };

  std::unique_ptr<FILE, FileCloseFunctor> file_;

  // We can't rely on ftell() for the position (since it only returns long, and
  // doesn't work on things like pipes); we need to manually track the reading
  // position.
  std::uint64_t position_ = 0;
};

}  // namespace webm

#endif  // INCLUDE_WEBM_FILE_READER_H_