Replaced FILE* by istream and ostream in lcf handling classes
MODIFY
src/reader_lcf.cpp
src/reader_lcf.cpp
#include <cstdarg>
#include "reader_lcf.h"
-
+#include <fstream>
#ifdef NDEBUG
# include <stdio.h>
#endif
std::string LcfReader::error_str;
+LcfReader::LcfReader(std::unique_ptr<std::istream> filestream, std::string encoding) :
+ filename(""),
+ encoding(encoding),
+ stream(std::move(filestream))
+{
+}
+
LcfReader::LcfReader(const char* filename, std::string encoding) :
filename(filename),
encoding(encoding),
- stream(fopen(filename, "rb"))
+ stream(new std::ifstream(filename, std::ios::ios_base::binary | std::ios::ios_base::in))
{
}
LcfReader::LcfReader(const std::string& filename, std::string encoding) :
filename(filename),
encoding(encoding),
- stream(fopen(filename.c_str(), "rb"))
+ stream(new std::ifstream(filename, std::ios::ios_base::binary | std::ios::ios_base::in))
{
}
@@ -37,13 +44,15 @@ LcfReader::~LcfReader() {
}
void LcfReader::Close() {
- if (stream != NULL)
- fclose(stream);
- stream = NULL;
+ stream.reset();
}
size_t LcfReader::Read0(void *ptr, size_t size, size_t nmemb) {
- size_t result = fread(ptr, size, nmemb, stream);
+ if (size == 0) { //avoid division by 0
+ return 0;
+ }
+ //Read nmemb elements of size and return the number of read elements
+ size_t result = stream->read(reinterpret_cast<char*>(ptr), size*nmemb).gcount() / size;
#ifdef NDEBUG
if (result != nmemb && !Eof()) {
perror("Reading error: ");
@@ -178,23 +187,23 @@ void LcfReader::ReadString(std::string& ref, size_t size) {
}
bool LcfReader::IsOk() const {
- return (stream != NULL && !ferror(stream));
+ return (stream && stream->good());
}
bool LcfReader::Eof() const {
- return feof(stream) != 0;
+ return stream->eof();
}
void LcfReader::Seek(size_t pos, SeekMode mode) {
switch (mode) {
case LcfReader::FromStart:
- fseek(stream, pos, SEEK_SET);
+ stream->seekg(pos, std::ios::ios_base::beg);
break;
case LcfReader::FromCurrent:
- fseek(stream, pos, SEEK_CUR);
+ stream->seekg(pos, std::ios::ios_base::cur);
break;
case LcfReader::FromEnd:
- fseek(stream, pos, SEEK_END);
+ stream->seekg(pos, std::ios::ios_base::end);
break;
default:
assert(false && "Invalid SeekMode");
@@ -202,11 +211,11 @@ void LcfReader::Seek(size_t pos, SeekMode mode) {
}
uint32_t LcfReader::Tell() {
- return (uint32_t)ftell(stream);
+ return (uint32_t)stream->tellg();
}
bool LcfReader::Ungetch(uint8_t ch) {
- return (ungetc(ch, stream) == ch);
+ return stream->putback(ch).good();
}
#ifdef _DEBUG

MODIFY
src/reader_lcf.h
src/reader_lcf.h
#include <vector>
#include <iosfwd>
#include <cstring>
-#include <cstdio>
#include <cassert>
+#include <memory>
#include "reader_types.h"
#include "lcf_options.h"
#include "reader_util.h"
*/
class LcfReader {
public:
+ /**
+ * Constructs a new File Reader.
+ *
+ * @param filestream already opened filestream.
+ * @param encoding name of the encoding.
+ */
+ LcfReader(std::unique_ptr<std::istream> filestream, std::string encoding = "");
+
/**
* Constructs a new File Reader.
*
@@ -233,7 +241,7 @@ private:
/** Name of the encoding. */
std::string encoding;
/** File-stream managed by this Reader. */
- FILE* stream;
+ std::unique_ptr<std::istream> stream;
/** Contains the last set error. */
static std::string error_str;

MODIFY
src/writer_lcf.cpp
src/writer_lcf.cpp
*/
#include "writer_lcf.h"
+#include <fstream>
+
+LcfWriter::LcfWriter(std::unique_ptr<std::ostream> filestream, std::string encoding) :
+ filename(""),
+ encoding(encoding),
+ stream(std::move(filestream))
+{
+}
LcfWriter::LcfWriter(const char* filename, std::string encoding) :
filename(filename),
encoding(encoding),
- stream(fopen(filename, "wb"))
+ stream(new std::ofstream(filename, std::ios::ios_base::binary | std::ios::ios_base::out))
{
}
LcfWriter::LcfWriter(const std::string& filename, std::string encoding) :
filename(filename),
encoding(encoding),
- stream(fopen(filename.c_str(), "wb"))
+ stream(new std::ofstream(filename, std::ios::ios_base::binary | std::ios::ios_base::out))
{
}
@@ -28,16 +36,14 @@ LcfWriter::~LcfWriter() {
}
void LcfWriter::Close() {
- if (stream != NULL)
- fclose(stream);
- stream = NULL;
+ stream.reset();
}
void LcfWriter::Write(const void *ptr, size_t size, size_t nmemb) {
#ifdef NDEBUG
- fwrite(ptr, size, nmemb, stream);
+ stream->write(reinterpret_cast<const char*>(ptr), size*nmemb);
#else
- assert(fwrite(ptr, size, nmemb, stream) == nmemb);
+ assert(stream->write(reinterpret_cast<const char*>(ptr), size*nmemb).good());
#endif
}
@@ -118,7 +124,7 @@ void LcfWriter::Write(const std::string& _str) {
}
bool LcfWriter::IsOk() const {
- return (stream != NULL && !ferror(stream));
+ return (stream && stream->good());
}
std::string LcfWriter::Decode(const std::string& str_to_encode) {

MODIFY
src/writer_lcf.h
src/writer_lcf.h
#include <vector>
#include <iosfwd>
#include <cstring>
-#include <cstdio>
+#include <memory>
#include <cassert>
#include "reader_types.h"
#include "lcf_options.h"
class LcfWriter {
public:
+ /**
+ * Constructs a new File Writer.
+ *
+ * @param filestream already opened filestream.
+ * @param encoding name of the encoding.
+ */
+ LcfWriter(std::unique_ptr<std::ostream> filestream, std::string encoding = "");
+
/**
* Constructs a new File Writer.
*
@@ -114,7 +122,7 @@ private:
/** Name of the encoding. */
std::string encoding;
/** File-stream managed by this Writer. */
- FILE* stream;
+ std::unique_ptr<std::ostream> stream;
/**
* Converts a 16bit signed integer to/from little-endian.

Replaced FILE* by istream and ostream in lcf handling classes