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