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

View file

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

View file

@ -10,16 +10,37 @@
namespace erw { namespace erw {
namespace core {
#pragma mark - File #pragma mark - File
/*
* File::File --
*/
File::File(const String& path)
: mPath(path)
{ }
/*
* File::~File --
*/
File::~File() File::~File()
{ } { }
/* /*
* InFile::modeToIOSMode -- * File::path --
*/
String
File::path()
const noexcept
{
return mPath;
}
/*
* File::modeToIOSMode --
*/ */
std::ios_base::openmode std::ios_base::openmode
File::modeToIOSMode(Mode mode) File::modeToIOSMode(Mode mode)
@ -44,7 +65,8 @@ File::modeToIOSMode(Mode mode)
*/ */
InFile::InFile(const String& path, InFile::InFile(const String& path,
InFile::Mode mode) 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::InFile(InFile&& other) 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 #pragma mark - OutFile
} /* namespace core */
} /* namespace erw */ } /* namespace erw */