/* * gfanlib_polymakefile.cpp * * Created on: Nov 17, 2010 * Author: anders */ #include #include "gfanlib_polymakefile.h" #include #include #include #include using namespace std; static void eatComment2(int &c, stringstream &s) { if(c=='#') { do c=s.get(); while(c!='\n' && !s.eof()); } } static void eatComment(stringstream &s) { int c=s.get(); while(c==' '||c=='\t')c=s.get(); eatComment2(c,s); s.unget(); } static string readUntil(istream &f, int c) { stringstream ret; int c2; c2=f.get(); // c2=fgetc(f); // while(c2!=c && c2!=EOF) while(c2!=c && !f.eof()) { ret< readIntList(istream &s) { list ret; int c=s.peek(); while(((c>='0') && (c<='9'))|| (c==' ')) { // fprintf(Stderr,"?\n"); int r; s >> r; ret.push_back(r); c=s.peek(); } return ret; } namespace gfan{ PolymakeProperty::PolymakeProperty(const std::string &name_, const std::string &value_): value(value_), name(name_) { } void PolymakeFile::open(std::istream &f) { isXml=false; // fileName=string(fileName_); // FILE *f=fopen(fileName.c_str(),"r"); // if(!f)//fprintf(Stderr,"Could not open file:\"%s\"\n",fileName_); // assert(f); int c=f.get();//fgetc(f); while(!f.eof()) { if(c=='_') { readUntil(f,'\n'); } else if(c!='\n') { f.unget(); // ungetc(c,f); string name=readUntil(f,'\n'); // fprintf(Stderr,"Reading:\"%s\"\n",name.c_str()); stringstream value; while(1) { string l=readUntil(f,'\n'); if(l.size()==0)break; value << l <\n"); for(list::const_iterator i=properties.begin();i!=properties.end();i++) { fprintf(f,"\n",i->name.c_str()); fprintf(f,"%s",i->value.c_str()); fprintf(f,"\n"); } fprintf(f,"\n"); } else { fprintf(f,"_application %s\n",application.c_str()); fprintf(f,"_version 2.2\n"); fprintf(f,"_type %s\n",type.c_str()); for(list::const_iterator i=properties.begin();i!=properties.end();i++) { fprintf(f,"\n%s\n",i->name.c_str()); fprintf(f,"%s",i->value.c_str()); } } fclose(f); } void PolymakeFile::writeStream(ostream &file) { if(isXml) { file << "\n"; for(list::const_iterator i=properties.begin();i!=properties.end();i++) { file << "name.c_str() << "\">\n"; file << i->value.c_str(); file << "\n"; } file << "\n"; } else { file << "_application " << application << endl; file << "_version 2.2\n"; file << "_type " << type << endl; for(list::const_iterator i=properties.begin();i!=properties.end();i++) { file << endl << i->name.c_str() << endl; file << i->value; } } } list::iterator PolymakeFile::findProperty(const char *p) { string s(p); for(list::iterator i=properties.begin();i!=properties.end();i++) { if(s==i->name)return i; } return properties.end(); } void PolymakeFile::writeProperty(const char *p, const string &data) { if(hasProperty(p)) { assert(0); } properties.push_back(PolymakeProperty(string(p),data)); } bool PolymakeFile::hasProperty(const char *p, bool doAssert) { if(doAssert) if(findProperty(p)==properties.end()) { fprintf(stderr,"Property: \"%s\" not found in file.\n",p); assert(0); } return findProperty(p)!=properties.end(); } Integer PolymakeFile::readCardinalProperty(const char *p) { assert(hasProperty(p,true)); list::iterator prop=findProperty(p); stringstream s(prop->value); int ret; s>>ret; return ret; } void PolymakeFile::writeCardinalProperty(const char *p, Integer n) { stringstream t; t<::iterator prop=findProperty(p); stringstream s(prop->value); // for(int i=0;i>v; if(s.eof())goto done; w[j]=v; } ret.appendRow(w); } done: if(height>=0)assert(ret.getHeight()==height); // cerr<>v; // while(!s.eof())std::cerr< *comments) { stringstream t; if(comments)assert((int)comments->size()>=m.getHeight()); if(isXml) { t << "\n"; for(int i=0;i"; for(int j=0;j\n"; } t << "\n"; } else { for(int i=0;i > PolymakeFile::readMatrixIncidenceProperty(const char *p) { vector > ret; assert(hasProperty(p,true)); list::iterator prop=findProperty(p); stringstream s(prop->value); while((s.peek()!=-1)&&(s.peek()!='\n')&&(s.peek()!=0)) { // fprintf(Stderr,"!\n"); int c=s.get(); //fprintf(Stderr,"%i",c); assert(c=='{'); ret.push_back(readIntList(s)); c=s.get(); assert(c=='}'); c=s.get(); while(c==' ' || c=='\t')c=s.get(); eatComment2(c,s); assert(c=='\n'); } return ret; } void PolymakeFile::writeIncidenceMatrixProperty(const char *p, const vector > &m) { stringstream t; if(isXml) { t<<""; for(unsigned i=0;i"; list temp=m[i]; temp.sort(); for(list::const_iterator j=temp.begin();j!=temp.end();j++) { if(j!=temp.begin())t<<' '; t<< *j; } t<<"\n"<\n"; } else { for(unsigned i=0;i temp=m[i]; temp.sort(); for(list::const_iterator j=temp.begin();j!=temp.end();j++) { if(j!=temp.begin())t<<' '; t<< *j; } t<<'}'<::iterator prop=findProperty(p); stringstream s(prop->value); list temp=readIntList(s); ZVector ret(temp.size()); int I=0; for(list::const_iterator i=temp.begin();i!=temp.end();i++,I++)ret[I]=*i; return ret; } void PolymakeFile::writeCardinalVectorProperty(const char *p, ZVector const &v) { stringstream t; if(isXml) { t<<""; for(unsigned i=0;i\n"; } else { for(unsigned i=0;i