mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
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: 415587802
This commit is contained in:
committed by
Copybara-Service
parent
5ba6623eac
commit
f12bb6c405
@@ -25,9 +25,21 @@ 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);
|
||||
|
||||
InputFile::InputFile(const std::string& path, std::int64_t size)
|
||||
: file_(path, std::ios::binary), path_(path), total_size_(size) {}
|
||||
// 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);
|
||||
}
|
||||
|
||||
ExceptionOr<ByteArray> InputFile::Read(std::int64_t size) {
|
||||
if (!file_.is_open()) {
|
||||
@@ -62,8 +74,15 @@ Exception InputFile::Close() {
|
||||
|
||||
// OutputFile
|
||||
|
||||
OutputFile::OutputFile(absl::string_view path)
|
||||
: file_(std::string(path), std::ios::binary) {}
|
||||
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);
|
||||
}
|
||||
|
||||
Exception OutputFile::Write(const ByteArray& data) {
|
||||
if (!file_.is_open()) {
|
||||
|
||||
@@ -29,25 +29,25 @@ namespace shared {
|
||||
|
||||
class InputFile final : public api::InputFile {
|
||||
public:
|
||||
explicit InputFile(const std::string& path, std::int64_t size);
|
||||
explicit InputFile(const char* file_path);
|
||||
~InputFile() override = default;
|
||||
InputFile(InputFile&&) = default;
|
||||
InputFile& operator=(InputFile&&) = default;
|
||||
|
||||
ExceptionOr<ByteArray> Read(std::int64_t size) override;
|
||||
std::string GetFilePath() const override { return path_; }
|
||||
std::int64_t GetTotalSize() const override { return total_size_; }
|
||||
ExceptionOr<ByteArray> Read(int64_t size) override;
|
||||
std::string GetFilePath() const override { return file_path_; }
|
||||
int64_t GetTotalSize() const override { return total_size_; }
|
||||
Exception Close() override;
|
||||
|
||||
private:
|
||||
std::ifstream file_;
|
||||
std::string path_;
|
||||
std::int64_t total_size_;
|
||||
std::string file_path_;
|
||||
int64_t total_size_;
|
||||
};
|
||||
|
||||
class OutputFile final : public api::OutputFile {
|
||||
public:
|
||||
explicit OutputFile(absl::string_view path);
|
||||
explicit OutputFile(const char* file_path);
|
||||
~OutputFile() override = default;
|
||||
OutputFile(OutputFile&&) = default;
|
||||
OutputFile& operator=(OutputFile&&) = default;
|
||||
|
||||
@@ -14,7 +14,9 @@
|
||||
|
||||
#include "platform/impl/shared/file.h"
|
||||
|
||||
#include <codecvt>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
@@ -24,6 +26,10 @@
|
||||
#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 {
|
||||
@@ -32,7 +38,7 @@ class FileTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
temp_path_ = std::make_unique<TempPath>(TempPath::Local);
|
||||
path_ = temp_path_->path() + "/file.txt";
|
||||
path_ = temp_path_->path() + "/" + TEST_FILE_NAME;
|
||||
std::ofstream output_file(path_);
|
||||
file_ = std::fstream(path_, std::fstream::in | std::fstream::out);
|
||||
}
|
||||
@@ -65,39 +71,39 @@ class FileTest : public ::testing::Test {
|
||||
};
|
||||
|
||||
TEST_F(FileTest, InputFile_NonExistentPath) {
|
||||
InputFile input_file("/not/a/valid/path.txt", GetSize());
|
||||
InputFile input_file((TEST_INVALID_PARENT_FOLDER + TEST_FILE_NAME).c_str());
|
||||
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_, GetSize());
|
||||
InputFile input_file(path_.c_str());
|
||||
EXPECT_EQ(input_file.GetFilePath(), path_);
|
||||
}
|
||||
|
||||
TEST_F(FileTest, InputFile_EmptyFileEOF) {
|
||||
InputFile input_file(path_, GetSize());
|
||||
InputFile input_file(path_.c_str());
|
||||
AssertEmpty(input_file.Read(kMaxSize));
|
||||
}
|
||||
|
||||
TEST_F(FileTest, InputFile_ReadWorks) {
|
||||
WriteToFile("abc");
|
||||
InputFile input_file(path_, GetSize());
|
||||
InputFile input_file(path_.c_str());
|
||||
input_file.Read(kMaxSize);
|
||||
SUCCEED();
|
||||
}
|
||||
|
||||
TEST_F(FileTest, InputFile_ReadUntilEOF) {
|
||||
WriteToFile("abc");
|
||||
InputFile input_file(path_, GetSize());
|
||||
InputFile input_file(path_.c_str());
|
||||
AssertEquals(input_file.Read(kMaxSize), "abc");
|
||||
AssertEmpty(input_file.Read(kMaxSize));
|
||||
}
|
||||
|
||||
TEST_F(FileTest, InputFile_ReadWithSize) {
|
||||
WriteToFile("abc");
|
||||
InputFile input_file(path_, GetSize());
|
||||
InputFile input_file(path_.c_str());
|
||||
AssertEquals(input_file.Read(2), "ab");
|
||||
AssertEquals(input_file.Read(1), "c");
|
||||
AssertEmpty(input_file.Read(kMaxSize));
|
||||
@@ -105,7 +111,7 @@ TEST_F(FileTest, InputFile_ReadWithSize) {
|
||||
|
||||
TEST_F(FileTest, InputFile_GetTotalSize) {
|
||||
WriteToFile("abc");
|
||||
InputFile input_file(path_, GetSize());
|
||||
InputFile input_file(path_.c_str());
|
||||
EXPECT_EQ(input_file.GetTotalSize(), 3);
|
||||
AssertEquals(input_file.Read(1), "a");
|
||||
EXPECT_EQ(input_file.GetTotalSize(), 3);
|
||||
@@ -113,7 +119,7 @@ TEST_F(FileTest, InputFile_GetTotalSize) {
|
||||
|
||||
TEST_F(FileTest, InputFile_Close) {
|
||||
WriteToFile("abc");
|
||||
InputFile input_file(path_, GetSize());
|
||||
InputFile input_file(path_.c_str());
|
||||
input_file.Close();
|
||||
ExceptionOr<ByteArray> read_result = input_file.Read(kMaxSize);
|
||||
EXPECT_FALSE(read_result.ok());
|
||||
@@ -121,23 +127,23 @@ TEST_F(FileTest, InputFile_Close) {
|
||||
}
|
||||
|
||||
TEST_F(FileTest, OutputFile_NonExistentPath) {
|
||||
OutputFile output_file("/not/a/valid/path.txt");
|
||||
OutputFile output_file((TEST_INVALID_PARENT_FOLDER + TEST_FILE_NAME).c_str());
|
||||
ByteArray bytes("a", 1);
|
||||
EXPECT_TRUE(output_file.Write(bytes).Raised(Exception::kIo));
|
||||
}
|
||||
|
||||
TEST_F(FileTest, OutputFile_Write) {
|
||||
OutputFile output_file(path_);
|
||||
OutputFile output_file(path_.c_str());
|
||||
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_, GetSize());
|
||||
InputFile input_file(path_.c_str());
|
||||
AssertEquals(input_file.Read(kMaxSize), "abc");
|
||||
}
|
||||
|
||||
TEST_F(FileTest, OutputFile_Close) {
|
||||
OutputFile output_file(path_);
|
||||
OutputFile output_file(path_.c_str());
|
||||
output_file.Close();
|
||||
ByteArray bytes("a");
|
||||
EXPECT_EQ(output_file.Write(bytes), Exception{Exception::kIo});
|
||||
|
||||
Reference in New Issue
Block a user