summaryrefslogtreecommitdiffstats
path: root/src/OSSupport
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2015-06-24 16:38:40 +0200
committerMattes D <github@xoft.cz>2015-07-31 08:48:22 +0200
commit7379848ae57634da8755b13c7bf2d21e6c990cb7 (patch)
tree823ad0d5c6afd87b778dc32ac0b99e607ae837fa /src/OSSupport
parentMerge pull request #2371 from SamJBarney/WarningFix (diff)
downloadcuberite-7379848ae57634da8755b13c7bf2d21e6c990cb7.tar
cuberite-7379848ae57634da8755b13c7bf2d21e6c990cb7.tar.gz
cuberite-7379848ae57634da8755b13c7bf2d21e6c990cb7.tar.bz2
cuberite-7379848ae57634da8755b13c7bf2d21e6c990cb7.tar.lz
cuberite-7379848ae57634da8755b13c7bf2d21e6c990cb7.tar.xz
cuberite-7379848ae57634da8755b13c7bf2d21e6c990cb7.tar.zst
cuberite-7379848ae57634da8755b13c7bf2d21e6c990cb7.zip
Diffstat (limited to '')
-rw-r--r--src/OSSupport/File.cpp29
-rw-r--r--src/OSSupport/File.h11
2 files changed, 32 insertions, 8 deletions
diff --git a/src/OSSupport/File.cpp b/src/OSSupport/File.cpp
index 03cddc408..09f6147b2 100644
--- a/src/OSSupport/File.cpp
+++ b/src/OSSupport/File.cpp
@@ -147,7 +147,7 @@ bool cFile::IsEOF(void) const
-int cFile::Read (void * iBuffer, size_t iNumBytes)
+int cFile::Read (void * a_Buffer, size_t a_NumBytes)
{
ASSERT(IsOpen());
@@ -156,14 +156,35 @@ int cFile::Read (void * iBuffer, size_t iNumBytes)
return -1;
}
- return static_cast<int>(fread(iBuffer, 1, static_cast<size_t>(iNumBytes), m_File)); // fread() returns the portion of Count parameter actually read, so we need to send iNumBytes as Count
+ return static_cast<int>(fread(a_Buffer, 1, a_NumBytes, m_File)); // fread() returns the portion of Count parameter actually read, so we need to send a_a_NumBytes as Count
}
-int cFile::Write(const void * iBuffer, size_t iNumBytes)
+AString cFile::Read(size_t a_NumBytes)
+{
+ ASSERT(IsOpen());
+
+ if (!IsOpen())
+ {
+ return AString();
+ }
+
+ // HACK: This depends on the knowledge that AString::data() returns the internal buffer, rather than a copy of it.
+ AString res;
+ res.resize(a_NumBytes);
+ auto newSize = fread(const_cast<char *>(res.data()), 1, a_NumBytes, m_File);
+ res.resize(newSize);
+ return res;
+}
+
+
+
+
+
+int cFile::Write(const void * a_Buffer, size_t a_NumBytes)
{
ASSERT(IsOpen());
@@ -172,7 +193,7 @@ int cFile::Write(const void * iBuffer, size_t iNumBytes)
return -1;
}
- int res = static_cast<int>(fwrite(iBuffer, 1, static_cast<size_t>(iNumBytes), m_File)); // fwrite() returns the portion of Count parameter actually written, so we need to send iNumBytes as Count
+ int res = static_cast<int>(fwrite(a_Buffer, 1, a_NumBytes, m_File)); // fwrite() returns the portion of Count parameter actually written, so we need to send a_NumBytes as Count
return res;
}
diff --git a/src/OSSupport/File.h b/src/OSSupport/File.h
index 6281d1494..b8381ac0e 100644
--- a/src/OSSupport/File.h
+++ b/src/OSSupport/File.h
@@ -80,11 +80,14 @@ public:
bool IsOpen(void) const;
bool IsEOF(void) const;
- /** Reads up to iNumBytes bytes into iBuffer, returns the number of bytes actually read, or -1 on failure; asserts if not open */
- int Read (void * iBuffer, size_t iNumBytes);
+ /** Reads up to a_NumBytes bytes into a_Buffer, returns the number of bytes actually read, or -1 on failure; asserts if not open */
+ int Read(void * a_Buffer, size_t a_NumBytes);
- /** Writes up to iNumBytes bytes from iBuffer, returns the number of bytes actually written, or -1 on failure; asserts if not open */
- int Write(const void * iBuffer, size_t iNumBytes);
+ /** Reads up to a_NumBytes bytes, returns the bytes actually read, or empty string on failure; asserts if not open */
+ AString Read(size_t a_NumBytes);
+
+ /** Writes up to a_NumBytes bytes from a_Buffer, returns the number of bytes actually written, or -1 on failure; asserts if not open */
+ int Write(const void * a_Buffer, size_t a_NumBytes);
/** Seeks to iPosition bytes from file start, returns old position or -1 for failure; asserts if not open */
long Seek (int iPosition);