Branch data Line data Source code
1 : : // ----------------- BEGIN LICENSE BLOCK --------------------------------- 2 : : // 3 : : // Copyright (C) 2018-2021 Intel Corporation 4 : : // 5 : : // SPDX-License-Identifier: MIT 6 : : // 7 : : // ----------------- END LICENSE BLOCK ----------------------------------- 8 : : 9 : : #include "ad/map/serialize/StorageFile.hpp" 10 : : #include "ad/map/access/Logging.hpp" 11 : : 12 : : namespace ad { 13 : : namespace map { 14 : : namespace serialize { 15 : : 16 : 169 : StorageFile::StorageFile() 17 : : { 18 : 169 : file_ = nullptr; 19 : 169 : } 20 : : 21 : 169 : StorageFile::~StorageFile() 22 : : { 23 [ + + ]: 169 : if (file_ != nullptr) 24 : : { 25 : 2 : access::getLogger()->error("StorageFile::dtor: File is not closed!"); 26 : 2 : fclose(file_); 27 : : } 28 : 169 : } 29 : : 30 : : ////////////////////////// 31 : : // IStorage Implementation 32 : : 33 : 171 : bool StorageFile::doOpen(std::string const &config, std::string const &flags) 34 : : { 35 [ + + ]: 171 : if (file_ == nullptr) 36 : : { 37 : 169 : file_ = fopen(config.c_str(), flags.c_str()); 38 : 169 : return file_ != nullptr; 39 : : } 40 : : else 41 : : { 42 [ + - ]: 4 : access::getLogger()->error("StorageFile::DoOpen: File already open! {}", config); 43 : 2 : return false; 44 : : } 45 : : } 46 : : 47 : 169 : bool StorageFile::doClose() 48 : : { 49 [ + + ]: 169 : if (file_ != nullptr) 50 : : { 51 : 167 : fclose(file_); 52 : 167 : file_ = nullptr; 53 : 167 : return true; 54 : : } 55 : : else 56 : : { 57 [ + - ]: 2 : access::getLogger()->error("StorageFile: Attempt to close already closed file!"); 58 : 2 : return false; 59 : : } 60 : : } 61 : : 62 : 175879 : bool StorageFile::doWrite(const void *x, size_t bytes) 63 : : { 64 [ + + ]: 175879 : if (file_ != nullptr) 65 : : { 66 : 175877 : return fwrite(x, bytes, 1, file_) == 1; 67 : : } 68 : : else 69 : : { 70 [ + - ]: 2 : access::getLogger()->error("StorageFile::DoWrite: File not open."); 71 : 2 : return false; 72 : : } 73 : : } 74 : : 75 : 10151400 : bool StorageFile::doRead(void *x, size_t bytes) 76 : : { 77 [ + + ]: 10151400 : if (file_ != nullptr) 78 : : { 79 : 10151400 : return fread(x, bytes, 1, file_) == 1; 80 : : } 81 : : else 82 : : { 83 [ + - ]: 2 : access::getLogger()->error("StorageFile::DoRead: File not open."); 84 : 2 : return false; 85 : : } 86 : : } 87 : : 88 : : } // namespace serialize 89 : : } // namespace map 90 : : } // namespace ad