Skip to content

Commit 238d071

Browse files
amabluea-maurice
authored andcommitted
Added FileIo class for reading and writing persisted values to disk, as well as
a mock class to faciliate testing. PiperOrigin-RevId: 261015096
1 parent 70b85a9 commit 238d071

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
#ifndef FIREBASE_DATABASE_CLIENT_CPP_SRC_DESKTOP_PERSISTENCE_FILE_IO_H_
16+
#define FIREBASE_DATABASE_CLIENT_CPP_SRC_DESKTOP_PERSISTENCE_FILE_IO_H_
17+
18+
#include <string>
19+
20+
namespace firebase {
21+
namespace database {
22+
namespace internal {
23+
24+
class FileIoInterface {
25+
public:
26+
virtual ~FileIoInterface() = 0;
27+
virtual bool ClearFile(const char* name) = 0;
28+
virtual bool AppendToFile(const char* name, const char* buffer,
29+
size_t size) = 0;
30+
virtual bool ReadFromFile(const char* name, std::string* buffer) = 0;
31+
virtual bool SetByte(const char* name, size_t offset, uint8_t byte) = 0;
32+
};
33+
34+
class FileIo : public FileIoInterface {
35+
public:
36+
~FileIo() override;
37+
bool ClearFile(const char* name) override;
38+
bool AppendToFile(const char* name, const char* buffer, size_t size) override;
39+
bool ReadFromFile(const char* name, std::string* buffer) override;
40+
bool SetByte(const char* name, size_t offset, uint8_t byte) override;
41+
};
42+
43+
} // namespace internal
44+
} // namespace database
45+
} // namespace firebase
46+
47+
#endif // FIREBASE_DATABASE_CLIENT_CPP_SRC_DESKTOP_PERSISTENCE_FILE_IO_H_

0 commit comments

Comments
 (0)