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
bool Close() override
std::function< int64_t(const std::string &name, void *buffer, uint64_t size)> read_func
Define an abstract file interface.
Definition: file.h:26
const std::string & file_name() const
Definition: file.h:94
int64_t Write(const void *buffer, uint64_t length) override
All the methods that are virtual are virtual for mocking.
bool Tell(uint64_t *position) override
bool Flush() override
bool Seek(uint64_t position) override
std::function< int64_t(const std::string &name, const void *buffer, uint64_t size)> write_func
static bool ParseCallbackFileName(const std::string &callback_file_name, const BufferCallbackParams **callback_params, std::string *name)
Definition: file.cc:364
bool Open() override
Internal open. Should not be used directly.
int64_t Read(void *buffer, uint64_t length) override
CallbackFile(const char *file_name, const char *mode)
int64_t Size() override