|
| 1 | +// Copyright 2019 Google LLC |
| 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 "database/src/desktop/persistence/file_io.h" |
| 16 | + |
| 17 | +#include <fstream> |
| 18 | +#include <iostream> |
| 19 | + |
| 20 | +#include "third_party/flatbuffers/include/flatbuffers/util.h" |
| 21 | + |
| 22 | +namespace firebase { |
| 23 | +namespace database { |
| 24 | +namespace internal { |
| 25 | + |
| 26 | +FileIoInterface::~FileIoInterface() {} |
| 27 | + |
| 28 | +FileIo::~FileIo() {} |
| 29 | + |
| 30 | +bool FileIo::ClearFile(const char* name) { |
| 31 | + std::ofstream ofs(name, std::ios::binary); |
| 32 | + if (!ofs.is_open()) return false; |
| 33 | + ofs.write(nullptr, 0); |
| 34 | + return !ofs.bad(); |
| 35 | +} |
| 36 | + |
| 37 | +bool FileIo::AppendToFile(const char* name, const char* buffer, size_t size) { |
| 38 | + std::ofstream ofs(name, std::ios::binary | std::ios::app); |
| 39 | + if (!ofs.is_open()) return false; |
| 40 | + ofs.write(buffer, size); |
| 41 | + return !ofs.bad(); |
| 42 | +} |
| 43 | + |
| 44 | +bool FileIo::ReadFromFile(const char* name, std::string* buffer) { |
| 45 | + bool result = flatbuffers::LoadFile(name, true, buffer); |
| 46 | + return result; |
| 47 | +} |
| 48 | + |
| 49 | +bool FileIo::SetByte(const char* name, size_t offset, uint8_t byte) { |
| 50 | + std::fstream ofs(name, std::ios::binary | std::ios::in | std::ios::out); |
| 51 | + ofs.seekp(offset); |
| 52 | + ofs.put(byte); |
| 53 | + return !ofs.bad(); |
| 54 | +} |
| 55 | + |
| 56 | +} // namespace internal |
| 57 | +} // namespace database |
| 58 | +} // namespace firebase |
0 commit comments