1 // Copyright 2019 The libgav1 Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "examples/file_reader_factory.h" 16 17 #include <new> 18 19 #include "examples/logging.h" 20 21 namespace libgav1 { 22 namespace { 23 GetFileReaderOpenFunctions()24std::vector<FileReaderFactory::OpenFunction>* GetFileReaderOpenFunctions() { 25 static auto* open_functions = 26 new (std::nothrow) std::vector<FileReaderFactory::OpenFunction>(); 27 return open_functions; 28 } 29 30 } // namespace 31 RegisterReader(OpenFunction open_function)32bool FileReaderFactory::RegisterReader(OpenFunction open_function) { 33 if (open_function == nullptr) return false; 34 auto* open_functions = GetFileReaderOpenFunctions(); 35 const size_t num_readers = open_functions->size(); 36 open_functions->push_back(open_function); 37 return open_functions->size() == num_readers + 1; 38 } 39 OpenReader(const std::string & file_name,const bool error_tolerant)40std::unique_ptr<FileReaderInterface> FileReaderFactory::OpenReader( 41 const std::string& file_name, const bool error_tolerant /*= false*/) { 42 for (auto* open_function : *GetFileReaderOpenFunctions()) { 43 auto reader = open_function(file_name, error_tolerant); 44 if (reader == nullptr) continue; 45 return reader; 46 } 47 LIBGAV1_EXAMPLES_LOG_ERROR("No file reader able to open input"); 48 return nullptr; 49 } 50 51 } // namespace libgav1 52