Replaced FILE* by istream and ostream in xml handling classes
MODIFY
src/reader_xml.cpp
src/reader_xml.cpp
#include <sstream>
#include <cstdarg>
#include <cstdio>
+#include <fstream>
#include "reader_lcf.h"
#include "reader_xml.h"
@@ -30,9 +31,17 @@ static void CharacterDataHandler(void* closure, const XML_Char* s, int len) {
}
#endif
+XmlReader::XmlReader(std::unique_ptr<std::istream> filestream) :
+ filename(""),
+ stream(std::move(filestream)),
+ parser(NULL)
+{
+ Open();
+}
+
XmlReader::XmlReader(const std::string& filename) :
filename(filename),
- stream(NULL),
+ stream(new std::ifstream(filename, std::ios::ios_base::in)),
parser(NULL)
{
Open();
@@ -44,7 +53,6 @@ XmlReader::~XmlReader() {
void XmlReader::Open() {
#if defined(LCF_SUPPORT_XML)
- stream = fopen(filename.c_str(), "r");
parser = XML_ParserCreate("UTF-8");
XML_SetUserData(parser, (void*) this);
@@ -57,9 +65,7 @@ void XmlReader::Open() {
void XmlReader::Close() {
#if defined(LCF_SUPPORT_XML)
- if (stream != NULL)
- fclose(stream);
- stream = NULL;
+ stream.reset();
if (parser != NULL)
XML_ParserFree(parser);
@@ -68,7 +74,7 @@ void XmlReader::Close() {
}
bool XmlReader::IsOk() const {
- return (stream != NULL && !ferror(stream) && parser != NULL);
+ return (stream && stream->good() && parser != NULL);
}
void XmlReader::Error(const char* fmt, ...) {
@@ -82,9 +88,9 @@ void XmlReader::Error(const char* fmt, ...) {
void XmlReader::Parse() {
#if defined(LCF_SUPPORT_XML)
static const int bufsize = 4096;
- while (IsOk() && !feof(stream)) {
+ while (IsOk() && !stream->eof()) {
void* buffer = XML_GetBuffer(parser, bufsize);
- int len = fread(buffer, 1, bufsize, stream);
+ int len = stream->read(reinterpret_cast<char*>(buffer),bufsize).gcount();
int result = XML_ParseBuffer(parser, len, len <= 0);
if (result == 0)
Error("%s", XML_ErrorString(XML_GetErrorCode(parser)));

MODIFY
src/reader_xml.h
src/reader_xml.h
@@ -31,6 +31,13 @@ class XmlHandler;
class XmlReader {
public:
+ /**
+ * Constructs a new File Reader.
+ *
+ * @param filestream already opened filestream.
+ */
+ XmlReader(std::unique_ptr<std::istream> filestream);
+
/**
* Constructs a new File Reader.
*
@@ -43,11 +50,6 @@ public:
*/
~XmlReader();
- /*
- * Opens the file.
- */
- void Open();
-
/**
* Closes the opened file.
*/
@@ -103,10 +105,16 @@ public:
void EndElement(const char* name);
protected:
+
+ /*
+ * Opens the file.
+ */
+ void Open();
+
/** Name of the file that is associated with the stream. */
std::string filename;
/** File-stream managed by this Reader. */
- FILE* stream;
+ std::unique_ptr<std::istream> stream;
/** Expat XML parser object. */
#if defined(LCF_SUPPORT_XML)
XML_Parser parser;

MODIFY
src/writer_xml.cpp
src/writer_xml.cpp
*/
#include <vector>
+#include <fstream>
#include "writer_xml.h"
+XmlWriter::XmlWriter(std::unique_ptr<std::ostream> filestream) :
+ filename(""),
+ indent(0),
+ stream(std::move(filestream)),
+ at_bol(true)
+{
+ Open();
+}
+
XmlWriter::XmlWriter(const char* filename) :
filename(filename),
indent(0),
+ stream(new std::ofstream(filename,std::ios::ios_base::out)),
at_bol(true)
{
Open();
@@ -21,6 +32,7 @@ XmlWriter::XmlWriter(const char* filename) :
XmlWriter::XmlWriter(const std::string& filename) :
filename(filename),
indent(0),
+ stream(new std::ofstream(filename, std::ios::ios_base::out)),
at_bol(true)
{
Open();
@@ -31,26 +43,23 @@ XmlWriter::~XmlWriter() {
}
void XmlWriter::Open() {
- stream = fopen(filename.c_str(), "w");
- fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", stream);
+ (*stream) << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
}
void XmlWriter::Close() {
- if (stream != NULL)
- fclose(stream);
- stream = NULL;
+ stream.reset();
}
template <>
void XmlWriter::Write<bool>(const bool& val) {
Indent();
- fputs(val ? "T" : "F", stream);
+ (*stream)<< (val ? "T" : "F");
}
template <>
void XmlWriter::Write<int>(const int& val) {
Indent();
- fprintf(stream, "%d", val);
+ (*stream) << val;
}
template <>
@@ -66,13 +75,13 @@ void XmlWriter::Write<int16_t>(const int16_t& val) {
template <>
void XmlWriter::Write<uint32_t>(const uint32_t& val) {
Indent();
- fprintf(stream, "%u", val);
+ (*stream) << val;
}
template <>
void XmlWriter::Write<double>(const double& val) {
Indent();
- fprintf(stream, "%g", val);
+ (*stream) << val;
}
template <>
@@ -83,27 +92,30 @@ void XmlWriter::Write<std::string>(const std::string& val) {
int c = (int) *it;
switch (c) {
case '<':
- fputs("&lt;", stream);
+ (*stream) << "&lt;";
break;
case '>':
- fputs("&gt;", stream);
+ (*stream) << "&gt;";
break;
case '&':
- fputs("&amp;", stream);
+ (*stream) << "&amp;";
break;
case '\n':
- fputc(c, stream);
+ stream->put(c);
at_bol = true;
Indent();
case '\r':
case '\t':
- fputc(c, stream);
+ stream->put(c);
break;
default:
- if (c >= 0 && c < 32)
- fprintf(stream, "&#x%04x;", 0xE000 + c);
+ if (c >= 0 && c < 32) {
+ char temp[10];
+ snprintf(temp,10, "&#x%04x;", 0xE000 + c);
+ (*stream) << temp;
+ }
else
- fputc(c, stream);
+ stream->put(c);
break;
}
}
@@ -150,7 +162,7 @@ void XmlWriter::WriteVector(const std::vector<T>& val) {
bool first = true;
for (it = val.begin(); it != val.end(); it++) {
if (!first)
- fputc(' ', stream);
+ stream->put(' ');
first = false;
Write<T>(*it);
}
@@ -166,28 +178,30 @@ void XmlWriter::WriteNode(const std::string& name, const T& val) {
void XmlWriter::BeginElement(const std::string& name) {
NewLine();
Indent();
- fprintf(stream, "<%s>", name.c_str());
+ (*stream) << "<" << name << ">";
indent++;
}
void XmlWriter::BeginElement(const std::string& name, int ID) {
NewLine();
Indent();
- fprintf(stream, "<%s id=\"%04d\">", name.c_str(), ID);
+ char temp[6];
+ snprintf(temp, 6, "%04d", ID);
+ (*stream) << "<" << name << "id=\"" << temp << "\n>";
indent++;
}
void XmlWriter::EndElement(const std::string& name) {
indent--;
Indent();
- fprintf(stream, "</%s>", name.c_str());
+ (*stream) << "</" << name << ">";
NewLine();
}
void XmlWriter::NewLine() {
if (at_bol)
return;
- fputc('\n', stream);
+ stream->put('\n');
at_bol = true;
}
@@ -195,12 +209,12 @@ void XmlWriter::Indent() {
if (!at_bol)
return;
for (int i = 0; i < indent; i++)
- fputc(' ', stream);
+ stream->put(' ');
at_bol = false;
}
bool XmlWriter::IsOk() const {
- return (stream != NULL && !ferror(stream));
+ return (stream && stream->good());
}
template void XmlWriter::WriteNode<int>(const std::string& name, const int& val);

MODIFY
src/writer_xml.h
src/writer_xml.h
#include <string>
#include <vector>
+#include <memory>
#include <cstdio>
#include "reader_types.h"
class XmlWriter {
public:
+ /**
+ * Constructs a new XML File Writer.
+ *
+ * @param filestream already opened filestream.
+ */
+ XmlWriter(std::unique_ptr<std::ostream> filestream);
+
/**
* Constructs a new XML File Writer.
*
@@ -40,11 +48,6 @@ public:
*/
~XmlWriter();
- /**
- * Opens the file.
- */
- void Open();
-
/**
* Closes the opened file.
*/
@@ -111,10 +114,15 @@ public:
bool IsOk() const;
protected:
+ /**
+ * Opens the file.
+ */
+ void Open();
+
/** Name of the file that is associated with the stream. */
std::string filename;
/** File-stream managed by this Writer. */
- FILE *stream;
+ std::unique_ptr<std::ostream> stream;
/** Stores indentation level. */
int indent;
/** Indicates if writer cursor is at the beginning of the line. */

Replaced FILE* by istream and ostream in xml handling classes