curvedraw

view src/curvefile.cc @ 15:37ab3a4c02f8

merged
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 20 Dec 2015 09:06:04 +0200
parents 84a647283237
children 7f795f7fecd6
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(fp, "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 Vector4 cp = curve->get_point(i);
50 fprintf(fp, " cp %g %g %g %g\n", cp.x, cp.y, cp.z, cp.w);
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) {
72 if(!isspace(c)) {
73 ungetc(c, fp);
74 break;
75 }
76 }
78 if(feof(fp)) return s;
79 while((c = fgetc(fp)) != -1) {
80 if(isspace(c)) {
81 ungetc(c, fp);
82 break;
83 }
84 s.push_back(c);
85 }
86 return s;
87 }
89 static bool expect_str(FILE *fp, const char *s)
90 {
91 std::string tok = next_token(fp);
92 if(tok != std::string(s)) {
93 if(tok.empty() && feof(fp)) {
94 fprintf(stderr, "expected: %s\n", s);
95 }
96 return false;
97 }
98 return true;
99 }
101 static bool expect_float(FILE *fp, float *ret)
102 {
103 std::string tok = next_token(fp);
104 const char *cs = tok.c_str();
105 char *endp;
106 *ret = strtod(cs, &endp);
107 if(endp != cs + tok.length()) {
108 if(!(tok.empty() && feof(fp))) {
109 fprintf(stderr, "number expected\n");
110 }
111 return false;
112 }
113 return true;
114 }
116 static bool expect_int(FILE *fp, int *ret)
117 {
118 std::string tok = next_token(fp);
119 const char *cs = tok.c_str();
120 char *endp;
121 *ret = strtol(cs, &endp, 0);
122 if(endp != cs + tok.length()) {
123 if(!(tok.empty() && feof(fp))) {
124 fprintf(stderr, "integer expected\n");
125 }
126 return false;
127 }
128 return true;
129 }
131 static Curve *curve_block(FILE *fp)
132 {
133 if(!expect_str(fp, "curve") || !expect_str(fp, "{")) {
134 return 0;
135 }
137 Curve *curve = new Curve;
138 int cpcount = -1;
139 std::string tok;
140 while(!(tok = next_token(fp)).empty() && tok != "}") {
141 if(tok == "cpcount") {
142 if(cpcount != -1 || !expect_int(fp, &cpcount) || cpcount <= 0) {
143 goto err;
144 }
145 } else if(tok == "type") {
146 tok = next_token(fp);
147 if(tok == "polyline") {
148 curve->set_type(CURVE_LINEAR);
149 } else if(tok == "hermite") {
150 curve->set_type(CURVE_HERMITE);
151 } else if(tok == "bspline") {
152 curve->set_type(CURVE_BSPLINE);
153 } else {
154 goto err;
155 }
156 } else {
157 if(tok != "cp") {
158 goto err;
159 }
160 Vector4 cp;
161 for(int i=0; i<4; i++) {
162 if(!expect_float(fp, &cp[i])) {
163 goto err;
164 }
165 }
166 curve->add_point(Vector3(cp.x, cp.y, cp.z), cp.w);
167 }
168 }
170 if(curve->size() != cpcount) {
171 fprintf(stderr, "warning: curve cpcount was %d, but read %d control points\n", cpcount, curve->size());
172 }
174 return curve;
175 err:
176 fprintf(stderr, "failed to parse curve block\n");
177 delete curve;
178 return 0;
179 }
181 std::list<Curve*> load_curves(FILE *fp)
182 {
183 std::list<Curve*> curves;
184 if(!expect_str(fp, "GCURVES")) {
185 fprintf(stderr, "load_curves: failed to load, invalid file format\n");
186 return curves;
187 }
189 Curve *curve;
190 while((curve = curve_block(fp))) {
191 curves.push_back(curve);
192 }
194 if(!feof(fp)) {
195 std::list<Curve*>::iterator it = curves.begin();
196 while(it != curves.end()) {
197 delete *it++;
198 }
199 return curves;
200 }
202 return curves;
203 }