curvedraw
view src/curvefile.cc @ 10:95fada20c638
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 19 Dec 2015 17:40:27 +0200 |
parents | 8bf96e11ed1f |
children | 099fd7adb900 |
line source
1 #include <stdlib.h>
2 #include <ctype.h>
3 #include <string>
4 #include "curvefile.h"
6 static bool save_curve(FILE *fp, const Curve *curve);
8 bool save_curves(const char *fname, const Curve * const *curves, int count)
9 {
10 FILE *fp = fopen(fname, "wb");
11 if(!fp) return false;
13 bool res = save_curves(fp, curves, count);
14 fclose(fp);
15 return res;
16 }
18 bool save_curves(FILE *fp, const Curve * const *curves, int count)
19 {
20 fprintf(stderr, "GCURVES\n");
22 for(int i=0; i<count; i++) {
23 if(!save_curve(fp, curves[i])) {
24 return false;
25 }
26 }
27 return true;
28 }
30 static const char *curve_type_str(CurveType type)
31 {
32 switch(type) {
33 case CURVE_LINEAR:
34 return "polyline";
35 case CURVE_HERMITE:
36 return "hermite";
37 case CURVE_BSPLINE:
38 return "bspline";
39 }
40 abort();
41 }
43 static bool save_curve(FILE *fp, const Curve *curve)
44 {
45 fprintf(fp, "curve {\n");
46 fprintf(fp, " type %s\n", curve_type_str(curve->get_type()));
47 fprintf(fp, " cpcount %d\n", curve->size());
48 for(int i=0; i<curve->size(); i++) {
49 Vector3 cp = curve->get_homo_point(i);
50 fprintf(fp, " cp %g %g %g\n", cp.x, cp.y, cp.z);
51 }
52 fprintf(fp, "}\n");
53 return true;
54 }
56 std::list<Curve*> load_curves(const char *fname)
57 {
58 std::list<Curve*> res;
59 FILE *fp = fopen(fname, "r");
60 if(!fp) return res;
62 res = load_curves(fp);
63 fclose(fp);
64 return res;
65 }
67 static std::string next_token(FILE *fp)
68 {
69 std::string s;
70 int c;
71 while((c = fgetc(fp)) != -1 && isspace(c)); // skip whitespace
72 if(feof(fp)) return s;
73 while((c = fgetc(fp)) != -1 && !isspace(c)) {
74 s.push_back(c);
75 }
76 return s;
77 }
79 static bool expect_str(FILE *fp, const char *s)
80 {
81 std::string tok = next_token(fp);
82 if(tok != std::string(s)) {
83 if(!(tok.empty() && feof(fp))) {
84 fprintf(stderr, "expected: %s\n", s);
85 }
86 return false;
87 }
88 return true;
89 }
91 static bool expect_float(FILE *fp, float *ret)
92 {
93 std::string tok = next_token(fp);
94 const char *cs = tok.c_str();
95 char *endp;
96 *ret = strtod(cs, &endp);
97 if(endp != cs + tok.length()) {
98 if(!(tok.empty() && feof(fp))) {
99 fprintf(stderr, "number expected\n");
100 }
101 return false;
102 }
103 return true;
104 }
106 static bool expect_int(FILE *fp, int *ret)
107 {
108 std::string tok = next_token(fp);
109 const char *cs = tok.c_str();
110 char *endp;
111 *ret = strtol(cs, &endp, 0);
112 if(endp != cs + tok.length()) {
113 if(!(tok.empty() && feof(fp))) {
114 fprintf(stderr, "integer expected\n");
115 }
116 return false;
117 }
118 return true;
119 }
121 static Curve *curve_block(FILE *fp)
122 {
123 if(!expect_str(fp, "curve") || !expect_str(fp, "{")) {
124 return 0;
125 }
127 Curve *curve = new Curve;
128 int cpcount = -1;
129 std::string tok;
130 while(!(tok = next_token(fp)).empty() && tok != "}") {
131 if(tok == "cpcount") {
132 if(cpcount != -1 || !expect_int(fp, &cpcount) || cpcount <= 0) {
133 goto err;
134 }
135 } else if(tok == "type") {
136 tok = next_token(fp);
137 if(tok == "polyline") {
138 curve->set_type(CURVE_LINEAR);
139 } else if(tok == "hermite") {
140 curve->set_type(CURVE_HERMITE);
141 } else if(tok == "bspline") {
142 curve->set_type(CURVE_BSPLINE);
143 } else {
144 goto err;
145 }
146 } else {
147 if(!expect_str(fp, "cp")) {
148 goto err;
149 }
150 Vector3 cp;
151 for(int i=0; i<3; i++) {
152 if(!expect_float(fp, &cp[i])) {
153 goto err;
154 }
155 }
156 curve->add_point(Vector2(cp.x, cp.y), cp.z);
157 }
158 }
160 if(curve->size() != cpcount) {
161 fprintf(stderr, "warning: curve cpcount was %d, but read %d control points\n", cpcount, curve->size());
162 }
164 return curve;
165 err:
166 fprintf(stderr, "failed to parse curve block\n");
167 delete curve;
168 return 0;
169 }
171 std::list<Curve*> load_curves(FILE *fp)
172 {
173 std::list<Curve*> curves;
174 if(!expect_str(fp, "GCURVES")) {
175 fprintf(stderr, "load_curves: failed to load, invalid file format\n");
176 return curves;
177 }
179 Curve *curve;
180 while((curve = curve_block(fp))) {
181 curves.push_back(curve);
182 }
184 int ncurves = (int)curves.size();
185 if(!feof(fp)) {
186 std::list<Curve*>::iterator it = curves.begin();
187 while(it != curves.end()) {
188 delete *it++;
189 }
190 return curves;
191 }
193 return curves;
194 }