1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
|
/******
* fileio.h
* Tom Prince and John Bowman 2004/05/10
*
* Handle input/output
******/
#ifndef FILEIO_H
#define FILEIO_H
#include <fstream>
#include <iostream>
#include <sstream>
#include "common.h"
#ifdef HAVE_RPC_RPC_H
#include "xstream.h"
#endif
#include "pair.h"
#include "triple.h"
#include "guide.h"
#include "pen.h"
#include "camperror.h"
#include "interact.h"
#include "errormsg.h"
#include "util.h"
#include "process.h"
namespace vm {
extern bool indebugger;
}
namespace camp {
extern string tab;
extern string newline;
enum Mode {NOMODE,INPUT,OUTPUT,UPDATE,BINPUT,BOUTPUT,BUPDATE,XINPUT,XOUTPUT,
XUPDATE};
static const string FileModes[]={"none",
"input","output","ouptut(update=false)",
"binput","boutput","boutput(update=false)",
"xinput","xoutput","xoutput(update=false)"};
class file : public gc {
protected:
string name;
bool check; // Check whether input file exists.
Mode type;
Int nx,ny,nz; // Array dimensions
bool linemode; // Array reads will stop at eol instead of eof.
bool csvmode; // Read comma-separated values.
bool wordmode; // Delimit strings by white space instead of eol.
bool singlereal; // Read/write single-precision XDR/binary reals.
bool singleint; // Read/write single-precision XDR/binary ints.
bool signedint; // Read/write signed XDR/binary ints.
bool closed; // File has been closed.
bool standard; // Standard input/output
bool binary; // Read in binary mode.
bool nullfield; // Used to detect a final null field in csv+line mode.
string whitespace;
size_t index; // Terminator index.
public:
bool Standard() {return standard;}
void standardEOF() {
#if defined(HAVE_LIBREADLINE) && defined(HAVE_LIBCURSES)
cout << endl;
#endif
}
template<class T>
void purgeStandard(T&) {
if(standard) {
int c;
if(cin.eof())
standardEOF();
else {
cin.clear();
while((c=cin.peek()) != EOF) {
cin.ignore();
if(c == '\n') break;
}
}
}
}
void purgeStandard(string&) {
if(cin.eof())
standardEOF();
}
void dimension(Int Nx=-1, Int Ny=-1, Int Nz=-1) {nx=Nx; ny=Ny; nz=Nz;}
file(const string& name, bool check=true, Mode type=NOMODE, bool binary=false,
bool closed=false) :
name(name), check(check), type(type), linemode(false), csvmode(false),
singlereal(false), singleint(true), signedint(true),
closed(closed), standard(name.empty()),
binary(binary), nullfield(false), whitespace("") {dimension();}
virtual void open() {}
void Check() {
if(error()) {
ostringstream buf;
buf << "Cannot open file \"" << name << "\".";
reportError(buf);
}
}
virtual ~file() {}
bool isOpen() {
if(closed) {
ostringstream buf;
buf << "I/O operation attempted on ";
if(name != "") buf << "closed file \'" << name << "\'.";
else buf << "null file.";
reportError(buf);
}
return true;
}
string filename() {return name;}
virtual bool eol() {return false;}
virtual bool nexteol() {return false;}
virtual bool text() {return false;}
virtual bool eof() {return true;}
virtual bool error() {return true;}
virtual void close() {}
virtual void clear() {}
virtual Int precision(Int) {return 0;}
virtual void flush() {}
virtual size_t tell() {return 0;}
virtual void seek(Int, bool=true) {}
string FileMode() {return FileModes[type];}
void unsupported(const char *rw, const char *type) {
ostringstream buf;
buf << rw << " of type " << type << " not supported in " << FileMode()
<< " mode.";
reportError(buf);
}
void noread(const char *type) {unsupported("Read",type);}
void nowrite(const char *type) {unsupported("Write",type);}
virtual void Read(bool&) {noread("bool");}
virtual void Read(Int&) {noread("int");}
virtual void Read(double&) {noread("real");}
virtual void Read(float&) {noread("real");}
virtual void Read(pair&) {noread("pair");}
virtual void Read(triple&) {noread("triple");}
virtual void Read(char&) {noread("char");}
virtual void Read(string&) {noread("string");}
virtual void readwhite(string&) {noread("string");}
virtual void write(bool) {nowrite("bool");}
virtual void write(Int) {nowrite("int");}
virtual void write(double) {nowrite("real");}
virtual void write(const pair&) {nowrite("pair");}
virtual void write(const triple&) {nowrite("triple");}
virtual void write(const string&) {nowrite("string");}
virtual void write(const pen&) {nowrite("pen");}
virtual void write(guide *) {nowrite("guide");}
virtual void write(const transform&) {nowrite("transform");}
virtual void writeline() {nowrite("string");}
virtual void ignoreComment() {};
virtual void csv() {};
template<class T>
void ignoreComment(T&) {
ignoreComment();
}
void ignoreComment(string&) {}
void ignoreComment(char&) {}
template<class T>
void read(T& val) {
if(binary) Read(val);
else {
if(standard) clear();
if(errorstream::interrupt) throw interrupted();
else {
ignoreComment(val);
val=T();
if(!nullfield)
Read(val);
csv();
whitespace="";
}
}
}
Int Nx() {return nx;}
Int Ny() {return ny;}
Int Nz() {return nz;}
void Nx(Int n) {nx=n;}
void Ny(Int n) {ny=n;}
void Nz(Int n) {nz=n;}
void LineMode(bool b) {linemode=b;}
bool LineMode() {return linemode;}
void CSVMode(bool b) {csvmode=b; if(b) wordmode=false;}
bool CSVMode() {return csvmode;}
void WordMode(bool b) {wordmode=b; if(b) csvmode=false;}
bool WordMode() {return wordmode;}
void SingleReal(bool b) {singlereal=b;}
bool SingleReal() {return singlereal;}
void SingleInt(bool b) {singleint=b;}
bool SingleInt() {return singleint;}
void SignedInt(bool b) {signedint=b;}
bool SignedInt() {return signedint;}
};
class ifile : public file {
protected:
istream *stream;
std::fstream *fstream;
char comment;
std::ios::openmode mode;
bool comma;
public:
ifile(const string& name, char comment, bool check=true, Mode type=INPUT,
std::ios::openmode mode=std::ios::in) :
file(name,check,type), stream(&cin), fstream(NULL), comment(comment),
mode(mode), comma(false) {}
// Binary file
ifile(const string& name, bool check=true, Mode type=BINPUT,
std::ios::openmode mode=std::ios::in) :
file(name,check,type,true), mode(mode) {}
~ifile() {close();}
void open() {
if(standard) {
if(mode & std::ios::binary)
reportError("Cannot open standard input in binary mode");
stream=&cin;
} else {
stream=fstream=new std::fstream(name.c_str(),mode);
if(mode & std::ios::out) {
checkLocal(name);
if(error()) {
delete fstream;
std::ofstream f(name.c_str());
f.close();
}
stream=fstream=new std::fstream(name.c_str(),mode);
}
index=processData().ifile.add(fstream);
if(check) Check();
}
}
bool eol();
bool nexteol();
bool text() {return true;}
bool eof() {return stream->eof();}
bool error() {return stream->fail();}
void close() {
if(!standard && fstream) {
fstream->close();
closed=true;
delete fstream;
fstream=NULL;
processData().ifile.remove(index);
}
}
void clear() {stream->clear();}
void seek(Int pos, bool begin=true) {
if(!standard && fstream) {
clear();
fstream->seekg(pos,begin ? std::ios::beg : std::ios::end);
}
}
size_t tell() {
if(fstream)
return fstream->tellg();
else
return 0;
}
void csv();
virtual void ignoreComment();
// Skip over white space
void readwhite(string& val) {val=string(); *stream >> val;}
void Read(bool &val) {string t; readwhite(t); val=(t == "true");}
void Read(Int& val) {*stream >> val;}
void Read(double& val) {*stream >> val;}
void Read(pair& val) {*stream >> val;}
void Read(triple& val) {*stream >> val;}
void Read(char& val) {stream->get(val);}
void Read(string& val);
};
class iofile : public ifile {
public:
iofile(const string& name, char comment=0) :
ifile(name,comment,true,UPDATE,std::ios::in | std::ios::out) {}
Int precision(Int p) {
return p == 0 ? stream->precision() : stream->precision(p);
}
void flush() {if(fstream) fstream->flush();}
void write(bool val) {*fstream << (val ? "true " : "false ");}
void write(Int val) {*fstream << val;}
void write(double val) {*fstream << val;}
void write(const pair& val) {*fstream << val;}
void write(const triple& val) {*fstream << val;}
void write(const string& val) {*fstream << val;}
void write(const pen& val) {*fstream << val;}
void write(guide *val) {*fstream << *val;}
void write(const transform& val) {*fstream << val;}
void writeline() {
*fstream << newline;
if(errorstream::interrupt) throw interrupted();
}
};
class ofile : public file {
protected:
ostream *stream;
std::ofstream *fstream;
std::ios::openmode mode;
public:
ofile(const string& name, Mode type=OUTPUT,
std::ios::openmode mode=std::ios::trunc) :
file(name,true,type), stream(&cout), fstream(NULL), mode(mode) {}
~ofile() {close();}
void open() {
if(standard) {
if(mode & std::ios::binary)
reportError("Cannot open standard output in binary mode");
stream=&cout;
} else {
checkLocal(name);
stream=fstream=new std::ofstream(name.c_str(),mode | std::ios::trunc);
index=processData().ofile.add(fstream);
Check();
}
}
bool text() {return true;}
bool eof() {return stream->eof();}
bool error() {return stream->fail();}
void close() {
if(!standard && fstream) {
fstream->close();
closed=true;
delete fstream;
fstream=NULL;
processData().ofile.remove(index);
}
}
void clear() {stream->clear();}
Int precision(Int p) {
return p == 0 ? stream->precision() : stream->precision(p);
}
void flush() {stream->flush();}
void seek(Int pos, bool begin=true) {
if(!standard && fstream) {
clear();
fstream->seekp(pos,begin ? std::ios::beg : std::ios::end);
}
}
size_t tell() {
if(fstream)
return fstream->tellp();
else
return 0;
}
void write(bool val) {*stream << (val ? "true " : "false ");}
void write(Int val) {*stream << val;}
void write(double val) {*stream << val;}
void write(const pair& val) {*stream << val;}
void write(const triple& val) {*stream << val;}
void write(const string& val) {*stream << val;}
void write(const pen& val) {*stream << val;}
void write(guide *val) {*stream << *val;}
void write(const transform& val) {*stream << val;}
void writeline();
};
class ibfile : public ifile {
public:
ibfile(const string& name, bool check=true, Mode type=BINPUT,
std::ios::openmode mode=std::ios::in) :
ifile(name,check,type,mode | std::ios::binary) {}
template<class T>
void iread(T& val) {
val=T();
if(fstream) fstream->read((char *) &val,sizeof(T));
}
void Read(bool& val) {iread(val);}
void Read(Int& val) {
if(signedint) {
if(singleint) {int ival; iread(ival); val=ival;}
else iread(val);
} else {
if(singleint) {unsigned ival; iread(ival); val=Intcast(ival);}
else {unsignedInt ival; iread(ival); val=Intcast(ival);}
}
}
void Read(char& val) {iread(val);}
void Read(string& val) {iread(val);}
void Read(double& val) {
if(singlereal) {float fval; iread(fval); val=fval;}
else iread(val);
}
};
class iobfile : public ibfile {
public:
iobfile(const string& name) :
ibfile(name,true,BUPDATE,std::ios::in | std::ios::out) {}
void flush() {if(fstream) fstream->flush();}
template<class T>
void iwrite(T val) {
if(fstream) fstream->write((char *) &val,sizeof(T));
}
void write(bool val) {iwrite(val);}
void write(Int val) {
if(signedint) {
if(singleint) iwrite(intcast(val));
else iwrite(val);
} else {
if(singleint) iwrite(unsignedcast(val));
else iwrite(unsignedIntcast(val));
}
}
void write(const string& val) {iwrite(val);}
void write(const pen& val) {iwrite(val);}
void write(guide *val) {iwrite(val);}
void write(const transform& val) {iwrite(val);}
void write(double val) {
if(singlereal) iwrite((float) val);
else iwrite(val);
}
void write(const pair& val) {
write(val.getx());
write(val.gety());
}
void write(const triple& val) {
write(val.getx());
write(val.gety());
write(val.getz());
}
void writeline() {}
};
class obfile : public ofile {
public:
obfile(const string& name) : ofile(name,BOUTPUT,std::ios::binary) {}
template<class T>
void iwrite(T val) {
if(fstream) fstream->write((char *) &val,sizeof(T));
}
void write(bool val) {iwrite(val);}
void write(Int val) {
if(signedint) {
if(singleint) iwrite(intcast(val));
else iwrite(val);
} else {
if(singleint) iwrite(unsignedcast(val));
else iwrite(unsignedIntcast(val));
}
}
void write(const string& val) {iwrite(val);}
void write(const pen& val) {iwrite(val);}
void write(guide *val) {iwrite(val);}
void write(const transform& val) {iwrite(val);}
void write(double val) {
if(singlereal) iwrite((float) val);
else iwrite(val);
}
void write(const pair& val) {
write(val.getx());
write(val.gety());
}
void write(const triple& val) {
write(val.getx());
write(val.gety());
write(val.getz());
}
void writeline() {}
};
#ifdef HAVE_RPC_RPC_H
class ixfile : public file {
protected:
xdr::ioxstream *fstream;
xdr::xios::open_mode mode;
public:
ixfile(const string& name, bool check=true, Mode type=XINPUT,
xdr::xios::open_mode mode=xdr::xios::in) :
file(name,check,type,true), fstream(NULL), mode(mode) {}
void open() {
fstream=new xdr::ioxstream(name.c_str(),mode);
index=processData().ixfile.add(fstream);
if(check) Check();
}
void close() {
if(fstream) {
fstream->close();
closed=true;
delete fstream;
fstream=NULL;
processData().ixfile.remove(index);
}
}
~ixfile() {close();}
bool eof() {return fstream ? fstream->eof() : true;}
bool error() {return fstream ? fstream->fail() : true;}
void clear() {if(fstream) fstream->clear();}
void seek(Int pos, bool begin=true) {
if(!standard && fstream) {
clear();
fstream->seek(pos,begin ? xdr::xios::beg : xdr::xios::end);
}
}
size_t tell() {
if(fstream)
return fstream->tell();
else
return 0;
}
void Read(Int& val) {
if(signedint) {
if(singleint) {int ival=0; *fstream >> ival; val=ival;}
else {val=0; *fstream >> val;}
} else {
if(singleint) {unsigned ival=0; *fstream >> ival; val=Intcast(ival);}
else {unsignedInt ival=0; *fstream >> ival; val=Intcast(ival);}
}
}
void Read(double& val) {
if(singlereal) {float fval=0.0; *fstream >> fval; val=fval;}
else {
val=0.0;
*fstream >> val;
}
}
void Read(pair& val) {
double x,y;
Read(x);
Read(y);
val=pair(x,y);
}
void Read(triple& val) {
double x,y,z;
Read(x);
Read(y);
Read(z);
val=triple(x,y,z);
}
};
class ioxfile : public ixfile {
public:
ioxfile(const string& name) : ixfile(name,true,XUPDATE,xdr::xios::out) {}
void flush() {if(fstream) fstream->flush();}
void write(Int val) {
if(signedint) {
if(singleint) *fstream << intcast(val);
else *fstream << val;
} else {
if(singleint) *fstream << unsignedcast(val);
else *fstream << unsignedIntcast(val);
}
}
void write(double val) {
if(singlereal) *fstream << (float) val;
else *fstream << val;
}
void write(const pair& val) {
write(val.getx());
write(val.gety());
}
void write(const triple& val) {
write(val.getx());
write(val.gety());
write(val.getz());
}
};
class oxfile : public file {
xdr::oxstream *fstream;
public:
oxfile(const string& name) : file(name,true,XOUTPUT), fstream(NULL) {}
void open() {
fstream=new xdr::oxstream((checkLocal(name),name.c_str()),xdr::xios::trunc);
index=processData().oxfile.add(fstream);
Check();
}
void close() {
if(fstream) {
fstream->close();
closed=true;
delete fstream;
fstream=NULL;
processData().oxfile.remove(index);
}
}
~oxfile() {close();}
bool eof() {return fstream ? fstream->eof() : true;}
bool error() {return fstream ? fstream->fail() : true;}
void clear() {if(fstream) fstream->clear();}
void flush() {if(fstream) fstream->flush();}
void seek(Int pos, bool begin=true) {
if(!standard && fstream) {
clear();
fstream->seek(pos,begin ? xdr::xios::beg : xdr::xios::end);
}
}
size_t tell() {
if(fstream)
return fstream->tell();
else
return 0;
}
void write(Int val) {
if(signedint) {
if(singleint) *fstream << intcast(val);
else *fstream << val;
} else {
if(singleint) *fstream << unsignedcast(val);
else *fstream << unsignedIntcast(val);
}
}
void write(double val) {
if(singlereal) *fstream << (float) val;
else *fstream << val;
}
void write(const pair& val) {
write(val.getx());
write(val.gety());
}
void write(const triple& val) {
write(val.getx());
write(val.gety());
write(val.getz());
}
};
#endif
extern ofile Stdout;
extern file nullfile;
} // namespace camp
#endif // FILEIO_H
|