-
Notifications
You must be signed in to change notification settings - Fork 0
/
File.cpp
57 lines (53 loc) · 1.28 KB
/
File.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include "shape/Shape.h"
bool loadShape(const char *name, std::vector<SHAPE::Shape *>& shape)
{
int type;
FILE *fin = NULL;
fopen_s(&fin, name, "rb");
if (!fin) return false;
while (!feof(fin))
{
fread(&type, 4, 1, fin);
switch (type)
{
case LINE:
shape.push_back(new SHAPE::Line());
break;
case RECT:
shape.push_back(new SHAPE::Rect());
break;
case CIRCLE:
shape.push_back(new SHAPE::Circle());
break;
default:
return false;
break;
}
int buf[7] = { 0 };
fread(buf, 4, 7, fin);
shape.back()->start.x = buf[0], shape.back()->start.y = buf[1];
shape.back()->end.x = buf[2], shape.back()->end.y = buf[3];
shape.back()->co.r = buf[4], shape.back()->co.g = buf[5], shape.back()->co.b = buf[6];
}
fclose(fin);
return true;
}
bool saveShape(const char *name, std::vector<SHAPE::Shape *> shape)
{
FILE *fout = NULL;
int buf[8] = { 0 };
fopen_s(&fout, name, "wb");
if (!fout) return false;
for (auto it = shape.begin(); it != shape.end(); it++)
{
buf[0] = (*it)->type, buf[1] = (*it)->start.x, buf[2] = (*it)->start.y;
buf[3] = (*it)->end.x, buf[4] = (*it)->end.y;
buf[5] = (*it)->co.r, buf[6] = (*it)->co.g, buf[7] = (*it)->co.b;
fwrite(buf, 4, 8, fout);
}
fclose(fout);
return true;
}