From 835d6ff079a61b8efa0b8ca42cd70f0f5d9696f4 Mon Sep 17 00:00:00 2001 From: Alex Zolotarev Date: Sat, 12 Feb 2011 13:45:37 +0100 Subject: [PATCH] Added string FileReader::ReadAsText() --- coding/coding_tests/reader_test.cpp | 15 +++++++++++++++ coding/file_reader.cpp | 8 ++++++++ coding/file_reader.hpp | 1 + 3 files changed, 24 insertions(+) diff --git a/coding/coding_tests/reader_test.cpp b/coding/coding_tests/reader_test.cpp index 22de5d5476..1fb584c410 100644 --- a/coding/coding_tests/reader_test.cpp +++ b/coding/coding_tests/reader_test.cpp @@ -66,3 +66,18 @@ UNIT_TEST(FileReaderNonExistentFileTest) { } } + +UNIT_TEST(FileReaderReadAsText) +{ + char const fName[] = "zzzuuuuuummmba"; + { + FileWriter f(fName); + f.Write(fName, ARRAY_SIZE(fName) - 1); + } + { + FileReader f(fName); + string const text = f.ReadAsText(); + TEST_EQUAL(text, fName, ()); + } + FileWriter::DeleteFile(fName); +} diff --git a/coding/file_reader.cpp b/coding/file_reader.cpp index 748d51a847..15a158231c 100644 --- a/coding/file_reader.cpp +++ b/coding/file_reader.cpp @@ -116,3 +116,11 @@ string FileReader::GetName() const { return m_pFileData->GetName(); } + +string FileReader::ReadAsText() const +{ + vector buffer(Size()); + buffer.resize(Size()); + Read(0, &buffer[0], Size()); + return string(reinterpret_cast(&buffer[0]), buffer.size()); +} diff --git a/coding/file_reader.hpp b/coding/file_reader.hpp index 6c073c855b..421a89c778 100644 --- a/coding/file_reader.hpp +++ b/coding/file_reader.hpp @@ -22,6 +22,7 @@ public: bool IsEqual(string const & fName) const; string GetName() const; + string ReadAsText() const; private: FileReader(shared_ptr const & pFileData, uint64_t offset, uint64_t size);