~ubuntu-branches/ubuntu/saucy/gfan/saucy-proposed

« back to all changes in this revision

Viewing changes to polymakefile.cpp

  • Committer: Package Import Robot
  • Author(s): Cédric Boutillier
  • Date: 2013-07-09 10:44:01 UTC
  • mfrom: (2.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20130709104401-5q66ozz5j5af0dak
Tags: 0.5+dfsg-3
* Upload to unstable.
* modify remove_failing_tests_on_32bits.patch to replace command of
  0009RenderStairCase test with an empty one instead of deleting it.
* remove lintian override about spelling error

Show diffs side-by-side

added added

removed removed

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