Refactor Input/Output file to use const char* instead of standard string, and to use parent_folder and file_name for input and output files. This also incorporates the move from nearby_connections to nearby.

PiperOrigin-RevId: 418002199
This commit is contained in:
jfcarroll
2021-12-23 07:04:38 -08:00
committed by Copybara-Service
parent 8f20fecfab
commit 9ce7be9812
48 changed files with 373 additions and 541 deletions
+4 -23
View File
@@ -25,21 +25,9 @@ namespace nearby {
namespace shared {
// InputFile
InputFile::InputFile(const char* file_path) : file_path_(file_path) {
// Open the file with the current location at eof (std::ios::ate)
// std::ios::binary - specifies binary access mode
// std::ios::in - allows input (read operations) from a stream
// std::ios::ate - sets the stream's position indicator to the
// end of the stream on opening.
file_.open(std::string(file_path),
std::ios::binary | std::ios::in | std::ios::ate);
// Read the current position in the file and use that for total size.
total_size_ = file_.tellg();
// Reset to the beginning of the file.
file_.seekg(0);
}
InputFile::InputFile(const std::string& path, std::int64_t size)
: file_(path, std::ios::binary), path_(path), total_size_(size) {}
ExceptionOr<ByteArray> InputFile::Read(std::int64_t size) {
if (!file_.is_open()) {
@@ -74,15 +62,8 @@ Exception InputFile::Close() {
// OutputFile
OutputFile::OutputFile(const char* file_path) {
// std::ios::binary - specifies binary access mode
// std::ios::out - allows output (read operations) from
// a stream
// std::ios::trunc - when the file is opened, the old
// contents are immediately removed.
file_ = std::ofstream(file_path,
std::ios::binary | std::ios::out | std::ios::trunc);
}
OutputFile::OutputFile(absl::string_view path)
: file_(std::string(path), std::ios::binary) {}
Exception OutputFile::Write(const ByteArray& data) {
if (!file_.is_open()) {
+7 -7
View File
@@ -29,25 +29,25 @@ namespace shared {
class InputFile final : public api::InputFile {
public:
explicit InputFile(const char* file_path);
explicit InputFile(const std::string& path, std::int64_t size);
~InputFile() override = default;
InputFile(InputFile&&) = default;
InputFile& operator=(InputFile&&) = default;
ExceptionOr<ByteArray> Read(int64_t size) override;
std::string GetFilePath() const override { return file_path_; }
int64_t GetTotalSize() const override { return total_size_; }
ExceptionOr<ByteArray> Read(std::int64_t size) override;
std::string GetFilePath() const override { return path_; }
std::int64_t GetTotalSize() const override { return total_size_; }
Exception Close() override;
private:
std::ifstream file_;
std::string file_path_;
int64_t total_size_;
std::string path_;
std::int64_t total_size_;
};
class OutputFile final : public api::OutputFile {
public:
explicit OutputFile(const char* file_path);
explicit OutputFile(absl::string_view path);
~OutputFile() override = default;
OutputFile(OutputFile&&) = default;
OutputFile& operator=(OutputFile&&) = default;
+13 -19
View File
@@ -14,9 +14,7 @@
#include "platform/impl/shared/file.h"
#include <codecvt>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <memory>
#include <ostream>
@@ -26,10 +24,6 @@
#include "absl/strings/string_view.h"
#include "platform/base/byte_array.h"
#define TEST_FILE_NAME std::string("testfilename.txt")
#define TEST_INVALID_PARENT_FOLDER \
std::string("fake/path/that/has/not/been/created/")
namespace location {
namespace nearby {
namespace shared {
@@ -38,7 +32,7 @@ class FileTest : public ::testing::Test {
protected:
void SetUp() override {
temp_path_ = std::make_unique<TempPath>(TempPath::Local);
path_ = temp_path_->path() + "/" + TEST_FILE_NAME;
path_ = temp_path_->path() + "/file.txt";
std::ofstream output_file(path_);
file_ = std::fstream(path_, std::fstream::in | std::fstream::out);
}
@@ -71,39 +65,39 @@ class FileTest : public ::testing::Test {
};
TEST_F(FileTest, InputFile_NonExistentPath) {
InputFile input_file((TEST_INVALID_PARENT_FOLDER + TEST_FILE_NAME).c_str());
InputFile input_file("/not/a/valid/path.txt", GetSize());
ExceptionOr<ByteArray> read_result = input_file.Read(kMaxSize);
EXPECT_FALSE(read_result.ok());
EXPECT_TRUE(read_result.GetException().Raised(Exception::kIo));
}
TEST_F(FileTest, InputFile_GetFilePath) {
InputFile input_file(path_.c_str());
InputFile input_file(path_, GetSize());
EXPECT_EQ(input_file.GetFilePath(), path_);
}
TEST_F(FileTest, InputFile_EmptyFileEOF) {
InputFile input_file(path_.c_str());
InputFile input_file(path_, GetSize());
AssertEmpty(input_file.Read(kMaxSize));
}
TEST_F(FileTest, InputFile_ReadWorks) {
WriteToFile("abc");
InputFile input_file(path_.c_str());
InputFile input_file(path_, GetSize());
input_file.Read(kMaxSize);
SUCCEED();
}
TEST_F(FileTest, InputFile_ReadUntilEOF) {
WriteToFile("abc");
InputFile input_file(path_.c_str());
InputFile input_file(path_, GetSize());
AssertEquals(input_file.Read(kMaxSize), "abc");
AssertEmpty(input_file.Read(kMaxSize));
}
TEST_F(FileTest, InputFile_ReadWithSize) {
WriteToFile("abc");
InputFile input_file(path_.c_str());
InputFile input_file(path_, GetSize());
AssertEquals(input_file.Read(2), "ab");
AssertEquals(input_file.Read(1), "c");
AssertEmpty(input_file.Read(kMaxSize));
@@ -111,7 +105,7 @@ TEST_F(FileTest, InputFile_ReadWithSize) {
TEST_F(FileTest, InputFile_GetTotalSize) {
WriteToFile("abc");
InputFile input_file(path_.c_str());
InputFile input_file(path_, GetSize());
EXPECT_EQ(input_file.GetTotalSize(), 3);
AssertEquals(input_file.Read(1), "a");
EXPECT_EQ(input_file.GetTotalSize(), 3);
@@ -119,7 +113,7 @@ TEST_F(FileTest, InputFile_GetTotalSize) {
TEST_F(FileTest, InputFile_Close) {
WriteToFile("abc");
InputFile input_file(path_.c_str());
InputFile input_file(path_, GetSize());
input_file.Close();
ExceptionOr<ByteArray> read_result = input_file.Read(kMaxSize);
EXPECT_FALSE(read_result.ok());
@@ -127,23 +121,23 @@ TEST_F(FileTest, InputFile_Close) {
}
TEST_F(FileTest, OutputFile_NonExistentPath) {
OutputFile output_file((TEST_INVALID_PARENT_FOLDER + TEST_FILE_NAME).c_str());
OutputFile output_file("/not/a/valid/path.txt");
ByteArray bytes("a", 1);
EXPECT_TRUE(output_file.Write(bytes).Raised(Exception::kIo));
}
TEST_F(FileTest, OutputFile_Write) {
OutputFile output_file(path_.c_str());
OutputFile output_file(path_);
ByteArray bytes1("a");
ByteArray bytes2("bc");
EXPECT_EQ(output_file.Write(bytes1), Exception{Exception::kSuccess});
EXPECT_EQ(output_file.Write(bytes2), Exception{Exception::kSuccess});
InputFile input_file(path_.c_str());
InputFile input_file(path_, GetSize());
AssertEquals(input_file.Read(kMaxSize), "abc");
}
TEST_F(FileTest, OutputFile_Close) {
OutputFile output_file(path_.c_str());
OutputFile output_file(path_);
output_file.Close();
ByteArray bytes("a");
EXPECT_EQ(output_file.Write(bytes), Exception{Exception::kIo});