Shaka Packager SDK
callback_file.cc
1 // Copyright 2017 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/file/callback_file.h"
8 
9 #include "packager/base/logging.h"
10 
11 namespace shaka {
12 
13 CallbackFile::CallbackFile(const char* file_name, const char* mode)
14  : File(file_name), file_mode_(mode) {}
15 
16 CallbackFile::~CallbackFile() {}
17 
19  delete this;
20  return true;
21 }
22 
23 int64_t CallbackFile::Read(void* buffer, uint64_t length) {
24  if (!callback_params_->read_func) {
25  LOG(ERROR) << "Read function not defined.";
26  return -1;
27  }
28  return callback_params_->read_func(name_, buffer, length);
29 }
30 
31 int64_t CallbackFile::Write(const void* buffer, uint64_t length) {
32  if (!callback_params_->write_func) {
33  LOG(ERROR) << "Write function not defined.";
34  return -1;
35  }
36  return callback_params_->write_func(name_, buffer, length);
37 }
38 
39 int64_t CallbackFile::Size() {
40  LOG(INFO) << "CallbackFile does not support Size().";
41  return -1;
42 }
43 
45  // Do nothing on Flush.
46  return true;
47 }
48 
49 bool CallbackFile::Seek(uint64_t position) {
50  VLOG(1) << "CallbackFile does not support Seek().";
51  return false;
52 }
53 
54 bool CallbackFile::Tell(uint64_t* position) {
55  VLOG(1) << "CallbackFile does not support Tell().";
56  return false;
57 }
58 
60  if (file_mode_ != "r" && file_mode_ != "w" && file_mode_ != "rb" &&
61  file_mode_ != "wb") {
62  LOG(ERROR) << "CallbackFile does not support file mode " << file_mode_;
63  return false;
64  }
65  return ParseCallbackFileName(file_name(), &callback_params_, &name_);
66 }
67 
68 } // namespace shaka
shaka::CallbackFile::Seek
bool Seek(uint64_t position) override
Definition: callback_file.cc:49
shaka::CallbackFile::Size
int64_t Size() override
Definition: callback_file.cc:39
shaka::CallbackFile::Write
int64_t Write(const void *buffer, uint64_t length) override
Definition: callback_file.cc:31
shaka::File::file_name
const std::string & file_name() const
Definition: file.h:96
shaka
All the methods that are virtual are virtual for mocking.
Definition: gflags_hex_bytes.cc:11
shaka::CallbackFile::Open
bool Open() override
Internal open. Should not be used directly.
Definition: callback_file.cc:59
shaka::CallbackFile::CallbackFile
CallbackFile(const char *file_name, const char *mode)
Definition: callback_file.cc:13
shaka::BufferCallbackParams::read_func
std::function< int64_t(const std::string &name, void *buffer, uint64_t size)> read_func
Definition: buffer_callback_params.h:20
shaka::BufferCallbackParams::write_func
std::function< int64_t(const std::string &name, const void *buffer, uint64_t size)> write_func
Definition: buffer_callback_params.h:30
shaka::CallbackFile::Close
bool Close() override
Definition: callback_file.cc:18
shaka::File
Define an abstract file interface.
Definition: file.h:28
shaka::CallbackFile::Read
int64_t Read(void *buffer, uint64_t length) override
Definition: callback_file.cc:23
shaka::File::ParseCallbackFileName
static bool ParseCallbackFileName(const std::string &callback_file_name, const BufferCallbackParams **callback_params, std::string *name)
Definition: file.cc:412
shaka::CallbackFile::Tell
bool Tell(uint64_t *position) override
Definition: callback_file.cc:54
shaka::CallbackFile::Flush
bool Flush() override
Definition: callback_file.cc:44