From 3df557a87b39fc4c2ff14006506e6992d1620ab0 Mon Sep 17 00:00:00 2001 From: David Date: Sat, 13 May 2006 01:52:31 +0000 Subject: david - initial NOT WORKING commit of SerializerText stuff... --- src/serializertext.cpp | 170 +++++++++++++++++++++++++++++++++++++++++++++ src/serializertext.h | 49 +++++++++++++ src/test/serializetext.cpp | 25 +++++++ 3 files changed, 244 insertions(+) create mode 100644 src/serializertext.cpp create mode 100644 src/serializertext.h create mode 100644 src/test/serializetext.cpp (limited to 'src') diff --git a/src/serializertext.cpp b/src/serializertext.cpp new file mode 100644 index 0000000..c650eae --- /dev/null +++ b/src/serializertext.cpp @@ -0,0 +1,170 @@ +#include "serializertext.h" + +SerializerText::SerializerText(FILE *fhFile, bool bLoading): + Serializer(bLoading), + fhFile(fhFile), + bCloseFile(false) +{ +} + +SerializerText::SerializerText(char *sFileName, bool bLoading): + Serializer(bLoading), + bCloseFile(true) +{ + if (bLoading) + { + fhFile = fopen(sFileName, "rt"); + } + else + { + fhFile = fopen(sFileName, "wt"); + } +} + +SerializerText::~SerializerText() +{ + close(); +} + +void SerializerText::close() +{ + if (fhFile != NULL) + { + fclose(fhFile); + fhFile = NULL; + } +} + +void SerializerText::write(void * pData, int32_t nSize) +{ + fwrite(pData, nSize, 1, fhFile); + fprintf(fhFile, "\n"); +} + +void SerializerText::read(void * pData, int32_t nSize) +{ + fread(pData, nSize, 1, fhFile); + fgetc(fhFile); +} + +Serializer &SerializerText::operator<<(bool p) +{ + fprintf(fhFile, "%hhd\n", p); + return *this; +} +Serializer &SerializerText::operator<<(int8_t p) +{ + fprintf(fhFile, "%hhd\n", p); + return *this; +} +Serializer &SerializerText::operator<<(int16_t p) +{ + fprintf(fhFile, "%hd\n", p); + return *this; +} +Serializer &SerializerText::operator<<(int32_t p) +{ + fprintf(fhFile, "%ld\n", p); + return *this; +} +Serializer &SerializerText::operator<<(int64_t p) +{ + fprintf(fhFile, "%lld\n", p); + return *this; +} +Serializer &SerializerText::operator<<(uint8_t p) +{ + fprintf(fhFile, "%hhu\n", p); + return *this; +} +Serializer &SerializerText::operator<<(uint16_t p) +{ + fprintf(fhFile, "%hu\n", p); + return *this; +} +Serializer &SerializerText::operator<<(uint32_t p) +{ + fprintf(fhFile, "%lu\n", p); + return *this; +} +Serializer &SerializerText::operator<<(uint64_t p) +{ + fprintf(fhFile, "%llu\n", p); + return *this; +} +Serializer &SerializerText::operator<<(float p) +{ + fprintf(fhFile, "%f\n", p); + return *this; +} +Serializer &SerializerText::operator<<(double p) +{ + fprintf(fhFile, "%f\n", p); + return *this; +} +Serializer &SerializerText::operator<<(long double p) +{ + fprintf(fhFile, "%Lf\n", p); + return *this; +} + +Serializer &SerializerText::operator>>(bool &p) +{ + fscanf(fhFile, "%hhd\n", &p); + return *this; +} +Serializer &SerializerText::operator>>(int8_t &p) +{ + fscanf(fhFile, "%hhd\n", &p); + return *this; +} +Serializer &SerializerText::operator>>(int16_t &p) +{ + fscanf(fhFile, "%hd\n", &p); + return *this; +} +Serializer &SerializerText::operator>>(int32_t &p) +{ + fscanf(fhFile, "%ld\n", &p); + return *this; +} +Serializer &SerializerText::operator>>(int64_t &p) +{ + fscanf(fhFile, "%lld\n", &p); + return *this; +} +Serializer &SerializerText::operator>>(uint8_t &p) +{ + fscanf(fhFile, "%hhu\n", &p); + return *this; +} +Serializer &SerializerText::operator>>(uint16_t &p) +{ + fscanf(fhFile, "%hu\n", &p); + return *this; +} +Serializer &SerializerText::operator>>(uint32_t &p) +{ + fscanf(fhFile, "%lu\n", &p); + return *this; +} +Serializer &SerializerText::operator>>(uint64_t &p) +{ + fscanf(fhFile, "%llu\n", &p); + return *this; +} +Serializer &SerializerText::operator>>(float &p) +{ + fscanf(fhFile, "%f\n", &p); + return *this; +} +Serializer &SerializerText::operator>>(double &p) +{ + fscanf(fhFile, "%f\n", &p); + return *this; +} +Serializer &SerializerText::operator>>(long double &p) +{ + fscanf(fhFile, "%Lf\n", &p); + return *this; +} diff --git a/src/serializertext.h b/src/serializertext.h new file mode 100644 index 0000000..992a838 --- /dev/null +++ b/src/serializertext.h @@ -0,0 +1,49 @@ +#ifndef SERIALIZER_BINARY_H +#define SERIALIZER_BINARY_H + +#include "serializer.h" +#include + +class SerializerText : public Serializer +{ +public: + SerializerText(FILE *fhFile, bool bLoading); + SerializerText(char *sFileName, bool bLoading); + ~SerializerText(); + + virtual void close(); + + virtual void write(void *, int32_t); + virtual void read(void *, int32_t); + + virtual Serializer &operator<<(bool); + virtual Serializer &operator<<(int8_t); + virtual Serializer &operator<<(int16_t); + virtual Serializer &operator<<(int32_t); + virtual Serializer &operator<<(int64_t); + virtual Serializer &operator<<(uint8_t); + virtual Serializer &operator<<(uint16_t); + virtual Serializer &operator<<(uint32_t); + virtual Serializer &operator<<(uint64_t); + virtual Serializer &operator<<(float); + virtual Serializer &operator<<(double); + virtual Serializer &operator<<(long double); + + virtual Serializer &operator>>(bool &); + virtual Serializer &operator>>(int8_t &); + virtual Serializer &operator>>(int16_t &); + virtual Serializer &operator>>(int32_t &); + virtual Serializer &operator>>(int64_t &); + virtual Serializer &operator>>(uint8_t &); + virtual Serializer &operator>>(uint16_t &); + virtual Serializer &operator>>(uint32_t &); + virtual Serializer &operator>>(uint64_t &); + virtual Serializer &operator>>(float &); + virtual Serializer &operator>>(double &); + virtual Serializer &operator>>(long double &); +private: + FILE *fhFile; + bool bCloseFile; +}; + +#endif diff --git a/src/test/serializetext.cpp b/src/test/serializetext.cpp new file mode 100644 index 0000000..e7e92d2 --- /dev/null +++ b/src/test/serializetext.cpp @@ -0,0 +1,25 @@ +#include "serializertext.h" + +int main() +{ + SerializerText ar("hello.dat", false); + + ar << 4; + ar << 3.993; + ar << true; + + ar.close(); + + int one=0;float two=0.0;bool three=false; + + SerializerText ar2("hello.dat", true); + + ar2 >> one; + ar2 >> two; + ar2 >> three; + + printf("out: %d, %d, %s\n", one, two, three); + + return 0; +} + -- cgit v1.2.3