diff options
Diffstat (limited to 'Build/source/utils/asymptote/fileio.h')
-rw-r--r-- | Build/source/utils/asymptote/fileio.h | 132 |
1 files changed, 83 insertions, 49 deletions
diff --git a/Build/source/utils/asymptote/fileio.h b/Build/source/utils/asymptote/fileio.h index e67d7e96b74..f7757b17437 100644 --- a/Build/source/utils/asymptote/fileio.h +++ b/Build/source/utils/asymptote/fileio.h @@ -38,23 +38,36 @@ 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 check; // Check whether input file exists. 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;} @@ -88,11 +101,11 @@ public: void dimension(Int Nx=-1, Int Ny=-1, Int Nz=-1) {nx=Nx; ny=Ny; nz=Nz;} - file(const string& name, bool check=true, bool binary=false, + file(const string& name, bool check=true, Mode type=NOMODE, bool binary=false, bool closed=false) : - name(name), linemode(false), csvmode(false), - singlereal(false), singleint(true), - closed(closed), check(check), standard(name.empty()), + 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() {} @@ -107,8 +120,6 @@ public: virtual ~file() {} - virtual const char* Mode() {return "";} - bool isOpen() { if(closed) { ostringstream buf; @@ -133,9 +144,11 @@ public: 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 " << Mode() + buf << rw << " of type " << type << " not supported in " << FileMode() << " mode."; reportError(buf); } @@ -214,6 +227,9 @@ public: void SingleInt(bool b) {singleint=b;} bool SingleInt() {return singleint;} + + void SignedInt(bool b) {signedint=b;} + bool SignedInt() {return signedint;} }; class ifile : public file { @@ -225,16 +241,15 @@ protected: bool comma; public: - ifile(const string& name, char comment, bool check=true, + ifile(const string& name, char comment, bool check=true, Mode type=INPUT, std::ios::openmode mode=std::ios::in) : - file(name,check), stream(&cin), fstream(NULL), comment(comment), mode(mode), - comma(false) { - } + file(name,check,type), stream(&cin), fstream(NULL), comment(comment), + mode(mode), comma(false) {} // Binary file - ifile(const string& name, bool check=true, + ifile(const string& name, bool check=true, Mode type=BINPUT, std::ios::openmode mode=std::ios::in) : - file(name,check,true), mode(mode) {} + file(name,check,type,true), mode(mode) {} ~ifile() {close();} @@ -259,8 +274,6 @@ public: } } - const char* Mode() {return "input";} - bool eol(); bool nexteol(); @@ -313,7 +326,7 @@ public: class iofile : public ifile { public: iofile(const string& name, char comment=0) : - ifile(name,true,comment,std::ios::in | std::ios::out) {} + ifile(name,comment,true,UPDATE,std::ios::in | std::ios::out) {} Int precision(Int p) { return p == 0 ? stream->precision() : stream->precision(p); @@ -342,9 +355,9 @@ protected: std::ofstream *fstream; std::ios::openmode mode; public: - ofile(const string& name, std::ios::openmode mode=std::ios::trunc) : - file(name), stream(&cout), fstream(NULL), mode(mode) { - } + 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();} @@ -361,8 +374,6 @@ public: } } - const char* Mode() {return "output";} - bool text() {return true;} bool eof() {return stream->eof();} bool error() {return stream->fail();} @@ -411,9 +422,9 @@ public: class ibfile : public ifile { public: - ibfile(const string& name, bool check=true, + ibfile(const string& name, bool check=true, Mode type=BINPUT, std::ios::openmode mode=std::ios::in) : - ifile(name,check,mode | std::ios::binary) {} + ifile(name,check,type,mode | std::ios::binary) {} template<class T> void iread(T& val) { val=T(); @@ -422,8 +433,13 @@ public: void Read(bool& val) {iread(val);} void Read(Int& val) { - if(singleint) {int ival; iread(ival); val=ival;} - else iread(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);} @@ -436,8 +452,8 @@ public: class iobfile : public ibfile { public: - iobfile(const string& name) : ibfile(name,true,std::ios::in | std::ios::out) - {} + iobfile(const string& name) : + ibfile(name,true,BUPDATE,std::ios::in | std::ios::out) {} void flush() {if(fstream) fstream->flush();} @@ -448,8 +464,13 @@ public: void write(bool val) {iwrite(val);} void write(Int val) { - if(singleint) iwrite(intcast(val)); - else iwrite(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);} @@ -473,7 +494,7 @@ public: class obfile : public ofile { public: - obfile(const string& name) : ofile(name,std::ios::binary) {} + obfile(const string& name) : ofile(name,BOUTPUT,std::ios::binary) {} template<class T> void iwrite(T val) { @@ -482,8 +503,13 @@ public: void write(bool val) {iwrite(val);} void write(Int val) { - if(singleint) iwrite(intcast(val)); - else iwrite(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);} @@ -513,9 +539,9 @@ protected: xdr::ioxstream *fstream; xdr::xios::open_mode mode; public: - ixfile(const string& name, bool check=true, + ixfile(const string& name, bool check=true, Mode type=XINPUT, xdr::xios::open_mode mode=xdr::xios::in) : - file(name,check,true), fstream(NULL), mode(mode) {} + file(name,check,type,true), fstream(NULL), mode(mode) {} void open() { fstream=new xdr::ioxstream(name.c_str(),mode); @@ -535,8 +561,6 @@ public: ~ixfile() {close();} - const char* Mode() {return "xinput";} - bool eof() {return fstream ? fstream->eof() : true;} bool error() {return fstream ? fstream->fail() : true;} @@ -557,10 +581,12 @@ public: } void Read(Int& val) { - if(singleint) {int ival=0; *fstream >> ival; val=ival;} - else { - val=0; - *fstream >> 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) { @@ -587,13 +613,18 @@ public: class ioxfile : public ixfile { public: - ioxfile(const string& name) : ixfile(name,true,xdr::xios::out) {} + ioxfile(const string& name) : ixfile(name,true,XUPDATE,xdr::xios::out) {} void flush() {if(fstream) fstream->flush();} void write(Int val) { - if(singleint) *fstream << intcast(val); - else *fstream << 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; @@ -613,7 +644,7 @@ public: class oxfile : public file { xdr::oxstream *fstream; public: - oxfile(const string& name) : file(name), fstream(NULL) {} + oxfile(const string& name) : file(name,true,XOUTPUT), fstream(NULL) {} void open() { fstream=new xdr::oxstream((checkLocal(name),name.c_str()),xdr::xios::trunc); @@ -633,8 +664,6 @@ public: ~oxfile() {close();} - const char* Mode() {return "xoutput";} - bool eof() {return fstream ? fstream->eof() : true;} bool error() {return fstream ? fstream->fail() : true;} void clear() {if(fstream) fstream->clear();} @@ -655,8 +684,13 @@ public: } void write(Int val) { - if(singleint) *fstream << intcast(val); - else *fstream << 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; |