diff --git a/src/edif.cpp b/src/edif.cpp index 51e547327802657e96f8a3cadd2aa025c3b8fff9..c4804954f9057de96c76a9ade2be17de921d1581 100644 --- a/src/edif.cpp +++ b/src/edif.cpp @@ -399,10 +399,8 @@ void NEdif::Save( string fmt ) NOlm::IterateEle iter; while((e=iter())) { - if ( !(f = e->P()) ) { - printf("SEVERE / f ?\n"); - return; - } + if( !(f = e->P()) ) + throw string( "SEVERE / f ?" ); string quest = "Save as (."+fmt+")"; fnam = wask( quest.c_str(), f->name); if ( fnam==string("") ) return; @@ -414,16 +412,12 @@ void NEdif::Save( string fmt ) fclose(fd); if (!bask("Overwrite ?")) return; } - if ( !(fd = fopen(fnam.c_str(), "w")) ) { - printf("! cannot write to file %s\n", fnam.c_str()); - return; - } + if ( !(fd = fopen(fnam.c_str(), "w")) ) + throw "! cannot write to file " + fnam; - err = e->Save( fd, fmt ); + e->Save( fd, fmt ); fclose(fd); - - if (err) return; } } diff --git a/src/olm.cpp b/src/olm.cpp index b490c298992ee16392d27a966323fcce15b269f8..e30228c7039164641a26bf792faa6c636f1c1e6c 100644 --- a/src/olm.cpp +++ b/src/olm.cpp @@ -25,7 +25,7 @@ int err; //***************************************************************************// -int COlo::SaveHeader( FILE *fd, bool isdata ) +void COlo::SaveHeader( FILE *fd, bool isdata ) // written for file format y08 { string out; @@ -56,12 +56,12 @@ int COlo::SaveHeader( FILE *fd, bool isdata ) yaml(RPar[i].Co.name).c_str(), yaml(RPar[i].Co.unit).c_str(), RPar[i].val ); - - return 0; } -int COlo::SaveHeader_a01(FILE *fd) -// written for file format ASCII-01 + +//! Write header in format ASCII-01 + +void COlo::SaveHeader_a01(FILE *fd) { fprintf( fd, "fil %s\n", name.c_str() ); lDoc.push_back("saved as "+name+" on "+ @@ -76,8 +76,6 @@ int COlo::SaveHeader_a01(FILE *fd) for ( uint i=0; i<RPar.size(); i++ ) fprintf( fd, "rpa %20.15g %s\n", RPar[i].val, RPar[i].Co.fstr().c_str() ); - - return 0; } @@ -86,51 +84,31 @@ int COlo::SaveHeader_a01(FILE *fd) //* online data files *// //***************************************************************************// -COld::COld(CCoord CX, CCoord CY, CGrid VX, vector<double> VY) -{ - vector<double> VZ; - xco = CX; - yco = CY; +//! Save bulk of online file in one of several formats. - VS.push_back(CScan(VX, VY, VZ)); -} - -int COld::SaveBody( FILE *fd, string fmt ) -// written for file format ASCII-01 +void COld::SaveBody( FILE *fd, string fmt ) { if( fmt=="y08" ){ fprintf( fd, "# %u tables\n", VS.size() ); fprintf( fd, "Tables:\n", VS.size() ); for( uint j=0; j<VS.size(); j++ ){ fprintf( fd, " - # table %u\n", j ); - if( ( err=VS[j].Save( fd, fmt )) ) return err;; + VS[j].Save( fd, fmt ); } - return 0; } else if( fmt=="a01" ){ fprintf( fd, "#j %u\n", VS.size() ); for( uint j=0; j<VS.size(); j++ ){ fprintf( fd, "scn %u\n", j ); - if( ( err=VS[j].Save( fd, fmt )) ) return err;; + VS[j].Save( fd, fmt ); } - return 0; } else if( fmt=="csv" ){ -/* - for( uint iz=0; iz<ZCo.size(); ++iz ){ - fprintf( fd, "z%d\t", iz ); - for( uint j=0; j<VS.size(); j++ ){ - fprintf( fd, "%g\t\t", VS[j].z[iz] ); - } - fprintf( fd, "\n" ); - } -*/ for( uint j=0; j<VS.size(); j++ ){ for( uint i=0; i<VS[j].size(); ++i ){ fprintf( fd, "%g\t", VS[j].y[i] ); } fprintf( fd, "\n" ); } - return 0; } else if( fmt=="tab" ){ for( uint j=0; j<VS.size(); j++ ){ for( uint iz=0; iz<ZCo.size(); ++iz ){ @@ -143,27 +121,29 @@ int COld::SaveBody( FILE *fd, string fmt ) } fprintf( fd, "\n" ); } - return 0; } else if( fmt=="lis" ){ // sozusagen außer Betrieb for( uint j=0; j<VS.size(); j++ ){ - if( ( err=VS[j].Save( fd, fmt )) ) return err;; + VS[j].Save( fd, fmt ); if( j<VS.size()-1 ) fprintf(fd, "\n", j); // ??? // empty line separates scans - return 0; } - } else { - printf( "SaveBody : data format %s not implemented\n" ); - return -1; - } + } else + throw string( "SaveBody: invalid data format" ); } -uint COld::nPts(uint j) const { - if (!nScan() || j>=nScan()) return 0; + +uint COld::nPts( uint j ) const { + if (!nScan() || j>=nScan()) + throw string( "PROGRAM ERROR: nPts called with invalid j="+strg(j) ); return VS[j].size(); } -uint COld::nPts(void) const { - if (!nScan()) return 0; + +//! Return number of points if it is the same for all scans. Else return 0. + +uint COld::nPts() const { + if (!nScan()) + throw string( "PROGRAM ERROR: nPts(void) called while nScan=0" ); uint np = VS[0].size(); for (uint j=0; j<nScan(); ++j) if (VS[j].size()!=np) return 0; @@ -214,20 +194,19 @@ void CEle::Z2R(void) } } -int CEle::Save( FILE *fd, string fmt ) +void CEle::Save( FILE *fd, string fmt ) { if( fmt=="y08"){ - if ((err=((COlo*) Ptr)->SaveHeader( fd, Typ==DATA ))) return err; + ((COlo*) Ptr)->SaveHeader( fd, Typ==DATA ); if (Typ==DATA) { - if ((err=((COld*) Ptr)->SaveBody( fd, fmt ))) return err; + ((COld*) Ptr)->SaveBody( fd, fmt ); } else if (Typ==CURVE) { - if ((err=((COlc*) Ptr)->SaveBody( fd ))) return err; + ((COlc*) Ptr)->SaveBody( fd ); } else - exit(7); - + throw string( "FATAL: invalid Typ in Save" ); + ((COlo*) Ptr)->as_on_disk = true; - return 0; } else if( fmt=="a01"){ fprintf(fd, "ASCII-01 JWu\n"); @@ -236,40 +215,33 @@ int CEle::Save( FILE *fd, string fmt ) fprintf(fd, "typ C\n"); else if(Typ==DATA) fprintf(fd, "typ D\n"); - else { - printf("PROGRAM ERROR Save Ele_type\n"); - exit(7); - } + else + throw string( "FATAL: invalid Typ in Save" ); - if ((err=((COlo*) Ptr)->SaveHeader_a01(fd))) return err; + ((COlo*) Ptr)->SaveHeader_a01(fd); if (Typ==DATA) { - if ((err=((COld*) Ptr)->SaveBody( fd, fmt ))) return err; + ((COld*) Ptr)->SaveBody( fd, fmt ); } else if (Typ==CURVE) { - if ((err=((COlc*) Ptr)->SaveBody( fd ))) return err; + ((COlc*) Ptr)->SaveBody( fd ); } else - exit(7); + throw string( "FATAL: invalid Typ in Save" ); fprintf(fd, "eof\n"); ((COlo*) Ptr)->as_on_disk = true; - return 0; } else if( ( fmt=="csv" || fmt=="tab" ) && Typ==DATA ) { - if ((err=((COld*) Ptr)->SaveBody( fd, fmt ))) return err; - return 0; + ((COld*) Ptr)->SaveBody( fd, fmt ); - } else { - printf( "CEle::Save> unknown format\n" ); - return -1; - } + } else + throw string( "Unknown format" ); }; -int CEle::Load( FILE *fd, string fshort ) +void CEle::Load( FILE *fd, string fshort ) { // obsoleted by File_io ???? - printf(" sorry, in oct07 JWu thought Ele::Load was obsolete\n"); - return -1; + throw string( " sorry, in oct07 JWu thought Ele::Load was obsolete" ); COlc C; COld D; @@ -280,46 +252,31 @@ int CEle::Load( FILE *fd, string fshort ) string lin, test; - if (mystd::freadln(fd, &lin)<=0) { - printf("! file empty or starting with empty line\n"); - goto errex; - } - if(lin!=string("ASCII-01 JWu")) { - printf("! unknown file format %s\n", lin.c_str()); - goto errex; - } - - if (mystd::freadln(fd, &lin)<=0) { - printf("! unexpected empty line\n"); - goto errex; - } - if (lin.substr(0,4)!=string("typ ")) { - printf("! missing \"typ\" entry\n"); - goto errex; - } - // printf(".. found type line %s\n", lin.c_str()); + if (mystd::freadln(fd, &lin)<=0) + throw string( "! file empty or starting with empty line" ); + if(lin!=string("ASCII-01 JWu")) + throw "! unknown file format: " + lin; + if (mystd::freadln(fd, &lin)<=0) + throw string( "! unexpected empty line\n"); + if (lin.substr(0,4)!=string("typ ")) + throw string( "! missing \"typ\" entry\n"); if (lin.substr(4)=="D") { Typ = DATA; Ptr = &D; } else if (lin.substr(4)=="C") { Typ = CURVE; Ptr = &C; - } else { - printf("! invalid type [%s]\n", lin.substr(4).c_str()); - goto errex; - } + } else + throw "FATAL: invalid type: " + lin.substr(4); P()->name = fshort; while (1) { - if (mystd::freadln(fd, &lin)<0) { - printf("! unexpected end-of-file\n"); - goto errex; - } - // printf(".. found line %s\n", lin.c_str()); + if (mystd::freadln(fd, &lin)<0) + throw string( "! unexpected end-of-file" ); if (lin=="eof") { NOlm::OloAdd(P())->P()->as_on_disk = true; - return 0; // this is the regular exit + return; // regular exit } else if (lin.substr(0,4)=="fil ") { if (lin.substr(4)!=fshort) { printf("! warning: file %s renamed from %s\n", @@ -332,46 +289,34 @@ int CEle::Load( FILE *fd, string fshort ) } else if (lin.substr(0,4)=="y ") { P()->yco.load(lin.substr(4)); } else if (lin[0]=='z') { - if (sscanf(lin.substr(1,3).c_str(), "%u", &iz)!=1) { - printf("z coordinates must be numbered\n"); - goto errex; - } - if (iz!=P()->ZCo.size()) { - printf("z coordinates must be sorted\n"); - goto errex; - } + if (sscanf(lin.substr(1,3).c_str(), "%u", &iz)!=1) + throw string( "z coordinates must be numbered" ); + if (iz!=P()->ZCo.size()) + throw string( "z coordinates must be sorted" ); co.load(lin.substr(4)); P()->ZCo.push_back(co); } else if (lin.substr(0,4)=="#j ") { - if (sscanf(lin.substr(4).c_str(), "%u", &nj)!=1) { - printf("after label #j no no. of scans\n"); - goto errex; - } + if (sscanf(lin.substr(4).c_str(), "%u", &nj)!=1) + throw string( "after label #j no no. of scans" ); } else if (lin.substr(0,4)=="scn ") { if (sscanf(lin.substr(4).c_str(), "%u", &j)!=1) { - printf("! number of scan not given\n"); + throw string( "! number of scan not given\n"); } else if (j!=nScan()) { - printf("! reading scan %u, labelled %u\n", - nScan(), j); + printf("scan %u, label %u\n", nScan(), j); + throw string( "inconsistency in input file" ); } if (Typ == DATA) { - if (DS.Load(fd, P()->ZCo.size())) - goto errex; + DS.Load( fd, P()->ZCo.size() ); D.VS.push_back(DS); } else if (Typ == CURVE) { - if (CS.Load(fd, P()->ZCo.size())) - goto errex; + CS.Load( fd, P()->ZCo.size() ); C.VC.push_back(CS); } else - exit(7); + throw string( "FATAL: invalid Typ" ); } else { - printf("! Unexpected label [%s]\n", - lin.substr(0,4).c_str()); - break; + throw "! Unexpected label " + lin.substr(0,4); } } -errex: - return -1; } @@ -428,7 +373,6 @@ CEle* NOlm::OloPushBack( CEle *e ) MOM.push_back( *e ); FSel += CList(MOM.size()-1); return &( MOM.back() ); - // cout << "DEBUG now we have " << MOM.size() << " on-line objects\n"; } void NOlm::OlfDel(void) @@ -457,7 +401,6 @@ void NOlm::OlfLoop(int(*exec)(int fno, COld *fin, COld *fout)) if (!overwrite) SelNew(); COld *fin, ftmp, *fout; int fno; - // cout << "OlfLoop Begin\n"; while((fin=fiter())) { fno = fiter.SelNo(); cout << "-> operate on file " << fno << "\n"; @@ -472,7 +415,6 @@ void NOlm::OlfLoop(int(*exec)(int fno, COld *fin, COld *fout)) if(!(overwrite)) OloAdd(&ftmp); } - // cout << "OlfLoop End\n"; } int NOlm::FJSelAsk (string quest) @@ -498,7 +440,6 @@ int NOlm::FJSelAsk (string quest) return 0; } - // printf("DEBUG FJSelAsk nJ=%d\n", nJ); FJSel.Ask(quest.c_str(), FJSel, CLimits(0, nJ-1)); return 0; @@ -507,7 +448,6 @@ int NOlm::FJSelAsk (string quest) void NOlm::FJSelGet (uint fno, class CList *JSel) { if (FJaskperF) { - // printf("DEBUG going to ask per file (fno=%u)\n", fno); if (fno>=MOM.size()) { printf("SEVERE PROG ERR in FJSelGet invalid fno=%u," " size=%u\n", fno, MOM.size()); @@ -516,9 +456,7 @@ void NOlm::FJSelGet (uint fno, class CList *JSel) } uint nJ = MOM[fno].nScan(); string quest = "Enter spectra for file " + strg(fno); - // printf("DEBUG going to call JSel->Ask\n"); JSel->Ask(quest.c_str(), FJSel, CLimits(0, nJ-1)); - // printf("DEBUG back from JSel->Ask\n"); } else { *JSel = FJSel; } @@ -527,9 +465,7 @@ void NOlm::FJSelGet (uint fno, class CList *JSel) void NOlm::FJSelGetNoask (uint fno, class CList *JSel) { if (FJaskperF) { - // printf("DEBUG FJS Get Noask\n"); *JSel = CList(0, MOM[fno].nScan()-1); - // printf("DEBUG FJS Get Noask ok\n"); } else { *JSel = FJSel; } diff --git a/src/olm.h b/src/olm.h index 434ec55e1626557f1e69fdcc7014b5c17d21b732..0e2b2c1ec2c0224b0e6ba6c363c3f8a91d635e4a 100644 --- a/src/olm.h +++ b/src/olm.h @@ -22,8 +22,8 @@ class COlo { // on-line object (virtual base class for COld, COlc) inline uint nZ(void) const { return ZCo.size(); }; - int SaveHeader( FILE *fd, bool isdata ); - int SaveHeader_a01( FILE *fd); + void SaveHeader( FILE *fd, bool isdata ); + void SaveHeader_a01( FILE *fd); }; class COld : public COlo { // on-line data file @@ -32,13 +32,13 @@ class COld : public COlo { // on-line data file inline COld(void) { }; COld( class COlc const* c ) {}; - COld(CCoord CX, CCoord CY, CGrid VX, vector<double> VY); + //COld(CCoord CX, CCoord CY, CGrid VX, vector<double> VY); inline uint nScan(void) const { return VS.size(); }; uint nPts(uint j) const; uint nPts(void) const; // 0 unless all spectra j have same nPts(j) - int SaveBody( FILE *fd, string fmt ); + void SaveBody( FILE *fd, string fmt ); }; class COlc : public COlo { // on-line curves @@ -110,8 +110,8 @@ class CEle { else exit(7); }; void Z2R(void); - int Load( FILE *fd, string fshort ); - int Save( FILE *fd, string fmt ); + void Load( FILE *fd, string fshort ); + void Save( FILE *fd, string fmt ); }; diff --git a/src/scan.cpp b/src/scan.cpp index 4f42f0f57a7bf8de491fa60e24b91a17a2e993aa..ad88fbe2bbfddd5ed364dc9af81a7a74d933df03 100644 --- a/src/scan.cpp +++ b/src/scan.cpp @@ -118,7 +118,7 @@ void CScan::Clear(void) z.clear(); } -int CScan::Save( FILE *fd, string fmt ) const +void CScan::Save( FILE *fd, string fmt ) const { if( fmt=="y08" ){ for ( uint i=0; i<z.size(); i++ ) @@ -134,7 +134,6 @@ int CScan::Save( FILE *fd, string fmt ) const for( uint i=0; i<size(); i++ ) fprintf( fd, "%20.15g %20.15g\n", x[i], y[i] ); } - return 0; } int CScan::Load(FILE *fd, uint nz) diff --git a/src/scan.h b/src/scan.h index 07efd4213d47bc68913a753382adf75f7524dd24..b760c26bb28c19b823ebddc6ed991aedef87af12 100644 --- a/src/scan.h +++ b/src/scan.h @@ -20,7 +20,7 @@ class CScan { CRange get_yrange(const bool logflag=0) const; CRange get_yrange(const CRange& xsubrange, const bool logflag=0) const; - int Save( FILE *fd, string fmt ) const; + void Save( FILE *fd, string fmt ) const; int Load( FILE *fd, uint nz ); }; bool CScanSortByZ( const CScan& S1, const CScan& S2 );