aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/serializertext.cpp170
-rw-r--r--src/serializertext.h49
-rw-r--r--src/test/serializetext.cpp25
3 files changed, 244 insertions, 0 deletions
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 @@
1#include "serializertext.h"
2
3SerializerText::SerializerText(FILE *fhFile, bool bLoading):
4 Serializer(bLoading),
5 fhFile(fhFile),
6 bCloseFile(false)
7{
8}
9
10SerializerText::SerializerText(char *sFileName, bool bLoading):
11 Serializer(bLoading),
12 bCloseFile(true)
13{
14 if (bLoading)
15 {
16 fhFile = fopen(sFileName, "rt");
17 }
18 else
19 {
20 fhFile = fopen(sFileName, "wt");
21 }
22}
23
24SerializerText::~SerializerText()
25{
26 close();
27}
28
29void SerializerText::close()
30{
31 if (fhFile != NULL)
32 {
33 fclose(fhFile);
34 fhFile = NULL;
35 }
36}
37
38void SerializerText::write(void * pData, int32_t nSize)
39{
40 fwrite(pData, nSize, 1, fhFile);
41 fprintf(fhFile, "\n");
42}
43
44void SerializerText::read(void * pData, int32_t nSize)
45{
46 fread(pData, nSize, 1, fhFile);
47 fgetc(fhFile);
48}
49
50Serializer &SerializerText::operator<<(bool p)
51{
52 fprintf(fhFile, "%hhd\n", p);
53 return *this;
54}
55Serializer &SerializerText::operator<<(int8_t p)
56{
57 fprintf(fhFile, "%hhd\n", p);
58 return *this;
59}
60Serializer &SerializerText::operator<<(int16_t p)
61{
62 fprintf(fhFile, "%hd\n", p);
63 return *this;
64}
65Serializer &SerializerText::operator<<(int32_t p)
66{
67 fprintf(fhFile, "%ld\n", p);
68 return *this;
69}
70Serializer &SerializerText::operator<<(int64_t p)
71{
72 fprintf(fhFile, "%lld\n", p);
73 return *this;
74}
75Serializer &SerializerText::operator<<(uint8_t p)
76{
77 fprintf(fhFile, "%hhu\n", p);
78 return *this;
79}
80Serializer &SerializerText::operator<<(uint16_t p)
81{
82 fprintf(fhFile, "%hu\n", p);
83 return *this;
84}
85Serializer &SerializerText::operator<<(uint32_t p)
86{
87 fprintf(fhFile, "%lu\n", p);
88 return *this;
89}
90Serializer &SerializerText::operator<<(uint64_t p)
91{
92 fprintf(fhFile, "%llu\n", p);
93 return *this;
94}
95Serializer &SerializerText::operator<<(float p)
96{
97 fprintf(fhFile, "%f\n", p);
98 return *this;
99}
100Serializer &SerializerText::operator<<(double p)
101{
102 fprintf(fhFile, "%f\n", p);
103 return *this;
104}
105Serializer &SerializerText::operator<<(long double p)
106{
107 fprintf(fhFile, "%Lf\n", p);
108 return *this;
109}
110
111Serializer &SerializerText::operator>>(bool &p)
112{
113 fscanf(fhFile, "%hhd\n", &p);
114 return *this;
115}
116Serializer &SerializerText::operator>>(int8_t &p)
117{
118 fscanf(fhFile, "%hhd\n", &p);
119 return *this;
120}
121Serializer &SerializerText::operator>>(int16_t &p)
122{
123 fscanf(fhFile, "%hd\n", &p);
124 return *this;
125}
126Serializer &SerializerText::operator>>(int32_t &p)
127{
128 fscanf(fhFile, "%ld\n", &p);
129 return *this;
130}
131Serializer &SerializerText::operator>>(int64_t &p)
132{
133 fscanf(fhFile, "%lld\n", &p);
134 return *this;
135}
136Serializer &SerializerText::operator>>(uint8_t &p)
137{
138 fscanf(fhFile, "%hhu\n", &p);
139 return *this;
140}
141Serializer &SerializerText::operator>>(uint16_t &p)
142{
143 fscanf(fhFile, "%hu\n", &p);
144 return *this;
145}
146Serializer &SerializerText::operator>>(uint32_t &p)
147{
148 fscanf(fhFile, "%lu\n", &p);
149 return *this;
150}
151Serializer &SerializerText::operator>>(uint64_t &p)
152{
153 fscanf(fhFile, "%llu\n", &p);
154 return *this;
155}
156Serializer &SerializerText::operator>>(float &p)
157{
158 fscanf(fhFile, "%f\n", &p);
159 return *this;
160}
161Serializer &SerializerText::operator>>(double &p)
162{
163 fscanf(fhFile, "%f\n", &p);
164 return *this;
165}
166Serializer &SerializerText::operator>>(long double &p)
167{
168 fscanf(fhFile, "%Lf\n", &p);
169 return *this;
170}
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 @@
1#ifndef SERIALIZER_BINARY_H
2#define SERIALIZER_BINARY_H
3
4#include "serializer.h"
5#include <stdio.h>
6
7class SerializerText : public Serializer
8{
9public:
10 SerializerText(FILE *fhFile, bool bLoading);
11 SerializerText(char *sFileName, bool bLoading);
12 ~SerializerText();
13
14 virtual void close();
15
16 virtual void write(void *, int32_t);
17 virtual void read(void *, int32_t);
18
19 virtual Serializer &operator<<(bool);
20 virtual Serializer &operator<<(int8_t);
21 virtual Serializer &operator<<(int16_t);
22 virtual Serializer &operator<<(int32_t);
23 virtual Serializer &operator<<(int64_t);
24 virtual Serializer &operator<<(uint8_t);
25 virtual Serializer &operator<<(uint16_t);
26 virtual Serializer &operator<<(uint32_t);
27 virtual Serializer &operator<<(uint64_t);
28 virtual Serializer &operator<<(float);
29 virtual Serializer &operator<<(double);
30 virtual Serializer &operator<<(long double);
31
32 virtual Serializer &operator>>(bool &);
33 virtual Serializer &operator>>(int8_t &);
34 virtual Serializer &operator>>(int16_t &);
35 virtual Serializer &operator>>(int32_t &);
36 virtual Serializer &operator>>(int64_t &);
37 virtual Serializer &operator>>(uint8_t &);
38 virtual Serializer &operator>>(uint16_t &);
39 virtual Serializer &operator>>(uint32_t &);
40 virtual Serializer &operator>>(uint64_t &);
41 virtual Serializer &operator>>(float &);
42 virtual Serializer &operator>>(double &);
43 virtual Serializer &operator>>(long double &);
44private:
45 FILE *fhFile;
46 bool bCloseFile;
47};
48
49#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 @@
1#include "serializertext.h"
2
3int main()
4{
5 SerializerText ar("hello.dat", false);
6
7 ar << 4;
8 ar << 3.993;
9 ar << true;
10
11 ar.close();
12
13 int one=0;float two=0.0;bool three=false;
14
15 SerializerText ar2("hello.dat", true);
16
17 ar2 >> one;
18 ar2 >> two;
19 ar2 >> three;
20
21 printf("out: %d, %d, %s\n", one, two, three);
22
23 return 0;
24}
25