1 | /* |
---|
2 | * gfanlib_polymakefile.cpp |
---|
3 | * |
---|
4 | * Created on: Nov 17, 2010 |
---|
5 | * Author: anders |
---|
6 | */ |
---|
7 | |
---|
8 | #include "gfanlib_polymakefile.h" |
---|
9 | |
---|
10 | #include <assert.h> |
---|
11 | #include <sstream> |
---|
12 | #include <istream> |
---|
13 | #include <stdio.h> |
---|
14 | |
---|
15 | using namespace std; |
---|
16 | |
---|
17 | static void eatComment2(int &c, stringstream &s) |
---|
18 | { |
---|
19 | if(c=='#') |
---|
20 | { |
---|
21 | do |
---|
22 | c=s.get(); |
---|
23 | while(c!='\n' && !s.eof()); |
---|
24 | } |
---|
25 | } |
---|
26 | |
---|
27 | static void eatComment(stringstream &s) |
---|
28 | { |
---|
29 | int c=s.get(); |
---|
30 | while(c==' '||c=='\t')c=s.get(); |
---|
31 | eatComment2(c,s); |
---|
32 | s.unget(); |
---|
33 | } |
---|
34 | |
---|
35 | static string readUntil(istream &f, int c) |
---|
36 | { |
---|
37 | stringstream ret; |
---|
38 | int c2; |
---|
39 | c2=f.get(); |
---|
40 | // c2=fgetc(f); |
---|
41 | // while(c2!=c && c2!=EOF) |
---|
42 | while(c2!=c && !f.eof()) |
---|
43 | { |
---|
44 | ret<<char(c2); |
---|
45 | //c2=fgetc(f); |
---|
46 | c2=f.get(); |
---|
47 | } |
---|
48 | return ret.str(); |
---|
49 | } |
---|
50 | |
---|
51 | static list<int> readIntList(istream &s) |
---|
52 | { |
---|
53 | list<int> ret; |
---|
54 | int c=s.peek(); |
---|
55 | while(((c>='0') && (c<='9')) || (c==' ')) |
---|
56 | { |
---|
57 | // fprintf(Stderr,"?\n"); |
---|
58 | int r; |
---|
59 | s >> r; |
---|
60 | ret.push_back(r); |
---|
61 | c=s.peek(); |
---|
62 | } |
---|
63 | return ret; |
---|
64 | } |
---|
65 | |
---|
66 | namespace gfan{ |
---|
67 | PolymakeProperty::PolymakeProperty(const std::string &name_, const std::string &value_): |
---|
68 | value(value_), |
---|
69 | name(name_) |
---|
70 | { |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | void PolymakeFile::open(std::istream &f) |
---|
75 | { |
---|
76 | isXml=false; |
---|
77 | // fileName=string(fileName_); |
---|
78 | |
---|
79 | // FILE *f=fopen(fileName.c_str(),"r"); |
---|
80 | // if(!f)//fprintf(Stderr,"Could not open file:\"%s\"\n",fileName_); |
---|
81 | // assert(f); |
---|
82 | |
---|
83 | int c=f.get();//fgetc(f); |
---|
84 | while(!f.eof()) |
---|
85 | { |
---|
86 | if(c=='_') |
---|
87 | { |
---|
88 | readUntil(f,'\n'); |
---|
89 | } |
---|
90 | else if(c!='\n') |
---|
91 | { |
---|
92 | f.unget(); |
---|
93 | // ungetc(c,f); |
---|
94 | string name=readUntil(f,'\n'); |
---|
95 | |
---|
96 | // fprintf(Stderr,"Reading:\"%s\"\n",name.c_str()); |
---|
97 | stringstream value; |
---|
98 | while(1) |
---|
99 | { |
---|
100 | string l=readUntil(f,'\n'); |
---|
101 | if(l.size()==0)break; |
---|
102 | value << l <<endl; |
---|
103 | } |
---|
104 | properties.push_back(PolymakeProperty(name.c_str(),value.str().c_str())); |
---|
105 | } |
---|
106 | c=f.get();//fgetc(f); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | /*void PolymakeFile::open(const char *fileName_) |
---|
111 | { |
---|
112 | isXml=false; |
---|
113 | fileName=string(fileName_); |
---|
114 | |
---|
115 | FILE *f=fopen(fileName.c_str(),"r"); |
---|
116 | // if(!f)//fprintf(Stderr,"Could not open file:\"%s\"\n",fileName_); |
---|
117 | assert(f); |
---|
118 | |
---|
119 | int c=fgetc(f); |
---|
120 | while(c!=EOF) |
---|
121 | { |
---|
122 | if(c=='_') |
---|
123 | { |
---|
124 | readUntil(f,'\n'); |
---|
125 | } |
---|
126 | else if(c!='\n') |
---|
127 | { |
---|
128 | ungetc(c,f); |
---|
129 | string name=readUntil(f,'\n'); |
---|
130 | |
---|
131 | // fprintf(Stderr,"Reading:\"%s\"\n",name.c_str()); |
---|
132 | stringstream value; |
---|
133 | while(1) |
---|
134 | { |
---|
135 | string l=readUntil(f,'\n'); |
---|
136 | if(l.size()==0)break; |
---|
137 | value << l <<endl; |
---|
138 | } |
---|
139 | properties.push_back(PolymakeProperty(name.c_str(),value.str().c_str())); |
---|
140 | } |
---|
141 | c=fgetc(f); |
---|
142 | } |
---|
143 | } |
---|
144 | */ |
---|
145 | void PolymakeFile::create(const char *fileName_, const char *application_, const char *type_, bool isXml_) |
---|
146 | { |
---|
147 | fileName=string(fileName_); |
---|
148 | application=string(application_); |
---|
149 | type=string(type_); |
---|
150 | isXml=isXml_; |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | void PolymakeFile::close() |
---|
155 | { |
---|
156 | FILE *f=fopen(fileName.c_str(),"w"); |
---|
157 | assert(f); |
---|
158 | |
---|
159 | if(isXml) |
---|
160 | { |
---|
161 | fprintf(f,"<properties>\n"); |
---|
162 | |
---|
163 | for(list<PolymakeProperty>::const_iterator i=properties.begin();i!=properties.end();i++) |
---|
164 | { |
---|
165 | fprintf(f,"<property name=\"%s\">\n",i->name.c_str()); |
---|
166 | fprintf(f,"%s",i->value.c_str()); |
---|
167 | fprintf(f,"</property>\n"); |
---|
168 | } |
---|
169 | fprintf(f,"</properties>\n"); |
---|
170 | } |
---|
171 | else |
---|
172 | { |
---|
173 | // fprintf(f,"_application %s\n",application.c_str()); |
---|
174 | // fprintf(f,"_version 2.2\n"); |
---|
175 | // fprintf(f,"_type %s\n",type.c_str()); |
---|
176 | |
---|
177 | for(list<PolymakeProperty>::const_iterator i=properties.begin();i!=properties.end();i++) |
---|
178 | { |
---|
179 | fprintf(f,"\n%s\n",i->name.c_str()); |
---|
180 | fprintf(f,"%s",i->value.c_str()); |
---|
181 | } |
---|
182 | } |
---|
183 | fclose(f); |
---|
184 | } |
---|
185 | |
---|
186 | |
---|
187 | void PolymakeFile::writeStream(ostream &file) |
---|
188 | { |
---|
189 | if(isXml) |
---|
190 | { |
---|
191 | file << "<properties>\n"; |
---|
192 | |
---|
193 | for(list<PolymakeProperty>::const_iterator i=properties.begin();i!=properties.end();i++) |
---|
194 | { |
---|
195 | file << "<property name=\"" << i->name.c_str() << "\">\n"; |
---|
196 | file << i->value.c_str(); |
---|
197 | file << "</property>\n"; |
---|
198 | } |
---|
199 | file << "</properties>\n"; |
---|
200 | } |
---|
201 | else |
---|
202 | { |
---|
203 | // file << "_application " << application << endl; |
---|
204 | // file << "_version 2.2\n"; |
---|
205 | // file << "_type " << type << endl; |
---|
206 | |
---|
207 | for(list<PolymakeProperty>::const_iterator i=properties.begin();i!=properties.end();i++) |
---|
208 | { |
---|
209 | file << endl << i->name.c_str() << endl; |
---|
210 | file << i->value; |
---|
211 | } |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | list<PolymakeProperty>::iterator PolymakeFile::findProperty(const char *p) |
---|
217 | { |
---|
218 | string s(p); |
---|
219 | for(list<PolymakeProperty>::iterator i=properties.begin();i!=properties.end();i++) |
---|
220 | { |
---|
221 | if(s==i->name)return i; |
---|
222 | } |
---|
223 | |
---|
224 | return properties.end(); |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | void PolymakeFile::writeProperty(const char *p, const string &data) |
---|
229 | { |
---|
230 | if(hasProperty(p)) |
---|
231 | { |
---|
232 | assert(0); |
---|
233 | } |
---|
234 | properties.push_back(PolymakeProperty(string(p),data)); |
---|
235 | } |
---|
236 | |
---|
237 | |
---|
238 | bool PolymakeFile::hasProperty(const char *p, bool doAssert) |
---|
239 | { |
---|
240 | if(doAssert) |
---|
241 | if(findProperty(p)==properties.end()) |
---|
242 | { |
---|
243 | fprintf(stderr,"Property: \"%s\" not found in file.\n",p); |
---|
244 | assert(0); |
---|
245 | } |
---|
246 | |
---|
247 | return findProperty(p)!=properties.end(); |
---|
248 | } |
---|
249 | |
---|
250 | |
---|
251 | Integer PolymakeFile::readCardinalProperty(const char *p) |
---|
252 | { |
---|
253 | assert(hasProperty(p,true)); |
---|
254 | |
---|
255 | list<PolymakeProperty>::iterator prop=findProperty(p); |
---|
256 | stringstream s(prop->value); |
---|
257 | |
---|
258 | int ret; |
---|
259 | s>>ret; |
---|
260 | |
---|
261 | return ret; |
---|
262 | } |
---|
263 | |
---|
264 | |
---|
265 | void PolymakeFile::writeCardinalProperty(const char *p, Integer n) |
---|
266 | { |
---|
267 | stringstream t; |
---|
268 | t<<n<<endl; |
---|
269 | writeProperty(p,t.str()); |
---|
270 | } |
---|
271 | |
---|
272 | |
---|
273 | bool PolymakeFile::readBooleanProperty(const char */*p*/) |
---|
274 | { |
---|
275 | return false; |
---|
276 | } |
---|
277 | |
---|
278 | |
---|
279 | void PolymakeFile::writeBooleanProperty(const char */*p*/, bool /*n*/) |
---|
280 | { |
---|
281 | } |
---|
282 | |
---|
283 | |
---|
284 | ZMatrix PolymakeFile::readMatrixProperty(const char *p, int height, int width) |
---|
285 | { |
---|
286 | ZMatrix ret(0,width); |
---|
287 | |
---|
288 | assert(hasProperty(p,true)); |
---|
289 | list<PolymakeProperty>::iterator prop=findProperty(p); |
---|
290 | stringstream s(prop->value); |
---|
291 | // for(int i=0;i<height;i++) |
---|
292 | for(int i=0;;i++) |
---|
293 | { |
---|
294 | if(i==height)break; |
---|
295 | ZVector w(width); |
---|
296 | for(int j=0;j<width;j++) |
---|
297 | { |
---|
298 | int v; |
---|
299 | eatComment(s); |
---|
300 | s>>v; |
---|
301 | if(s.eof())goto done; |
---|
302 | w[j]=v; |
---|
303 | } |
---|
304 | ret.appendRow(w); |
---|
305 | } |
---|
306 | done: |
---|
307 | |
---|
308 | if(height>=0)assert(ret.getHeight()==height); |
---|
309 | // cerr<<p; |
---|
310 | // eatComment(s); |
---|
311 | // int v; |
---|
312 | // s>>v; |
---|
313 | // while(!s.eof())std::cerr<<char(s.get()); |
---|
314 | // assert(s.eof()); |
---|
315 | |
---|
316 | return ret; |
---|
317 | } |
---|
318 | |
---|
319 | |
---|
320 | void PolymakeFile::writeMatrixProperty(const char *p, const ZMatrix &m, bool indexed, const vector<string> *comments) |
---|
321 | { |
---|
322 | stringstream t; |
---|
323 | |
---|
324 | if(comments)assert((int)comments->size()>=m.getHeight()); |
---|
325 | if(isXml) |
---|
326 | { |
---|
327 | t << "<matrix>\n"; |
---|
328 | for(int i=0;i<m.getHeight();i++) |
---|
329 | { |
---|
330 | t << "<vector>"; |
---|
331 | for(int j=0;j<m.getWidth();j++) |
---|
332 | { |
---|
333 | if(j!=0)t<<" "; |
---|
334 | t<<m[i][j]; |
---|
335 | } |
---|
336 | t<<endl; |
---|
337 | t << "</vector>\n"; |
---|
338 | } |
---|
339 | t << "</matrix>\n"; |
---|
340 | } |
---|
341 | else |
---|
342 | { |
---|
343 | for(int i=0;i<m.getHeight();i++) |
---|
344 | { |
---|
345 | for(int j=0;j<m.getWidth();j++) |
---|
346 | { |
---|
347 | if(j!=0)t<<" "; |
---|
348 | t<<m[i][j]; |
---|
349 | } |
---|
350 | if(indexed)t<<"\t# "<<i; |
---|
351 | if(comments)t<<"\t# "<<(*comments)[i]; |
---|
352 | t<<endl; |
---|
353 | } |
---|
354 | } |
---|
355 | writeProperty(p,t.str()); |
---|
356 | } |
---|
357 | |
---|
358 | |
---|
359 | vector<list<int> > PolymakeFile::readMatrixIncidenceProperty(const char *p) |
---|
360 | { |
---|
361 | vector<list<int> > ret; |
---|
362 | assert(hasProperty(p,true)); |
---|
363 | list<PolymakeProperty>::iterator prop=findProperty(p); |
---|
364 | stringstream s(prop->value); |
---|
365 | |
---|
366 | while((s.peek()!=-1)&&(s.peek()!='\n')&&(s.peek()!=0)) |
---|
367 | { |
---|
368 | // fprintf(Stderr,"!\n"); |
---|
369 | int c=s.get(); |
---|
370 | //fprintf(Stderr,"%i",c); |
---|
371 | assert(c=='{'); |
---|
372 | ret.push_back(readIntList(s)); |
---|
373 | c=s.get(); |
---|
374 | assert(c=='}'); |
---|
375 | c=s.get(); |
---|
376 | while(c==' ' || c=='\t')c=s.get(); |
---|
377 | eatComment2(c,s); |
---|
378 | assert(c=='\n'); |
---|
379 | } |
---|
380 | return ret; |
---|
381 | } |
---|
382 | |
---|
383 | |
---|
384 | void PolymakeFile::writeIncidenceMatrixProperty(const char *p, const vector<list<int> > &m) |
---|
385 | { |
---|
386 | stringstream t; |
---|
387 | |
---|
388 | if(isXml) |
---|
389 | { |
---|
390 | t<<"<incidence_matrix>"; |
---|
391 | for(unsigned i=0;i<m.size();i++) |
---|
392 | { |
---|
393 | t<<"<set>"; |
---|
394 | list<int> temp=m[i]; |
---|
395 | temp.sort(); |
---|
396 | for(list<int>::const_iterator j=temp.begin();j!=temp.end();j++) |
---|
397 | { |
---|
398 | if(j!=temp.begin())t<<' '; |
---|
399 | t<< *j; |
---|
400 | } |
---|
401 | t<<"</set>\n"<<endl; |
---|
402 | } |
---|
403 | t<<"</incidence_matrix>\n"; |
---|
404 | } |
---|
405 | else |
---|
406 | { |
---|
407 | for(unsigned i=0;i<m.size();i++) |
---|
408 | { |
---|
409 | t<<'{'; |
---|
410 | list<int> temp=m[i]; |
---|
411 | temp.sort(); |
---|
412 | for(list<int>::const_iterator j=temp.begin();j!=temp.end();j++) |
---|
413 | { |
---|
414 | if(j!=temp.begin())t<<' '; |
---|
415 | t<< *j; |
---|
416 | } |
---|
417 | t<<'}'<<endl; |
---|
418 | } |
---|
419 | } |
---|
420 | writeProperty(p,t.str()); |
---|
421 | } |
---|
422 | |
---|
423 | |
---|
424 | ZVector PolymakeFile::readCardinalVectorProperty(const char *p) |
---|
425 | { |
---|
426 | assert(hasProperty(p,true)); |
---|
427 | list<PolymakeProperty>::iterator prop=findProperty(p); |
---|
428 | stringstream s(prop->value); |
---|
429 | |
---|
430 | list<int> temp=readIntList(s); |
---|
431 | |
---|
432 | ZVector ret(temp.size()); |
---|
433 | int I=0; |
---|
434 | for(list<int>::const_iterator i=temp.begin();i!=temp.end();i++,I++)ret[I]=*i; |
---|
435 | |
---|
436 | return ret; |
---|
437 | } |
---|
438 | |
---|
439 | |
---|
440 | void PolymakeFile::writeCardinalVectorProperty(const char *p, ZVector const &v) |
---|
441 | { |
---|
442 | stringstream t; |
---|
443 | |
---|
444 | if(isXml) |
---|
445 | { |
---|
446 | t<<"<vector>"; |
---|
447 | for(unsigned i=0;i<v.size();i++) |
---|
448 | { |
---|
449 | if(i!=0)t<<" "; |
---|
450 | t<<v[i]; |
---|
451 | } |
---|
452 | t<<"</vector>\n"; |
---|
453 | } |
---|
454 | else |
---|
455 | { |
---|
456 | for(unsigned i=0;i<v.size();i++) |
---|
457 | { |
---|
458 | if(i!=0)t<<" "; |
---|
459 | t<<v[i]; |
---|
460 | } |
---|
461 | t<<endl; |
---|
462 | } |
---|
463 | writeProperty(p,t.str()); |
---|
464 | } |
---|
465 | |
---|
466 | |
---|
467 | void PolymakeFile::writeStringProperty(const char *p, const string &s) |
---|
468 | { |
---|
469 | if(isXml) |
---|
470 | writeProperty(p,s); |
---|
471 | else |
---|
472 | writeProperty(p,s); |
---|
473 | } |
---|
474 | } |
---|