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