Add File::path(), which returns the path the File was initialized with; remove core namespace

This commit is contained in:
Eryn Wells 2015-10-18 08:23:59 -07:00
parent 134f2aa3fb
commit 8641748274
3 changed files with 37 additions and 10 deletions

View file

@ -14,7 +14,6 @@
namespace erw {
namespace core {
struct File
{
@ -36,6 +35,8 @@ struct File
/** Destructor. */
virtual ~File();
String path() const noexcept;
/** Seek to an absolute position in the file. */
virtual File& seek(size_t pos) = 0;
@ -46,8 +47,12 @@ struct File
virtual File& seek(ssize_t offset, SeekFrom from) = 0;
protected:
const String mPath;
/** Convert a File::Mode to an iostream openmode bitset. */
virtual std::ios_base::openmode modeToIOSMode(Mode mode);
File(const String& path);
};
@ -57,7 +62,7 @@ struct InFile
{
/** Open a file at `path` for reading. */
InFile(const String& path, File::Mode mode);
/** Deleted copy constructor. File handles cannot be copied. */
InFile(const InFile& other) = delete;
@ -69,6 +74,8 @@ struct InFile
/** Move `other` to this InFile. File handles cannot be copied. */
InFile& operator=(InFile& other);
String path() const;
/** Read up to `count` characters into the provided `buffer`. */
InFile& read(char* buffer, ssize_t count);
@ -79,6 +86,7 @@ struct InFile
InFile& seek(ssize_t pos, File::SeekFrom from) override;
private:
String mPath;
std::ifstream mStream;
std::ios_base::openmode modeToIOSMode(File::Mode mode) override;
@ -101,5 +109,4 @@ private:
std::ofstream stream;
};
} /* namespace core */
} /* namespace erw */

View file

@ -10,9 +10,7 @@
namespace erw {
namespace core {
typedef std::string String;
} /* namespace core */
} /* namespace erw */

View file

@ -10,16 +10,37 @@
namespace erw {
namespace core {
#pragma mark - File
/*
* File::File --
*/
File::File(const String& path)
: mPath(path)
{ }
/*
* File::~File --
*/
File::~File()
{ }
/*
* InFile::modeToIOSMode --
* File::path --
*/
String
File::path()
const noexcept
{
return mPath;
}
/*
* File::modeToIOSMode --
*/
std::ios_base::openmode
File::modeToIOSMode(Mode mode)
@ -44,7 +65,8 @@ File::modeToIOSMode(Mode mode)
*/
InFile::InFile(const String& path,
InFile::Mode mode)
: mStream(path, modeToIOSMode(mode))
: File(path),
mStream(path, modeToIOSMode(mode))
{ }
@ -52,7 +74,8 @@ InFile::InFile(const String& path,
* InFile::InFile --
*/
InFile::InFile(InFile&& other)
: mStream(std::move(other.mStream))
: File(other.mPath),
mStream(std::move(other.mStream))
{ }
@ -136,5 +159,4 @@ InFile::modeToIOSMode(File::Mode mode)
#pragma mark - OutFile
} /* namespace core */
} /* namespace erw */