From 241357f559dbe6664a991afce2d222d7492a4cb0 Mon Sep 17 00:00:00 2001 From: "Joachim Wuttke (office)" <j.wuttke@fz-juelich.de> Date: Mon, 1 Mar 2010 18:15:08 +0100 Subject: [PATCH] Li: eliminated need for C yaml library, using yaml-cpp. JWu: purge expr.h --- pub/config.h.in | 3 - pub/configure | 67 ++------------ pub/configure.ac | 4 +- pub/src/Makefile.am | 6 +- pub/src/Makefile.in | 9 +- pub/src/curve.cpp | 3 +- pub/src/expr.cpp | 30 +++--- pub/src/expr.h | 21 ++--- pub/src/file_in.cpp | 217 +++++++++++++++++++++++--------------------- pub/src/rssm.cpp | 200 ++++++++++++++++++++-------------------- pub/src/yamc.cpp | 143 ----------------------------- pub/src/yamc.h | 27 ------ 12 files changed, 255 insertions(+), 475 deletions(-) delete mode 100644 pub/src/yamc.cpp delete mode 100644 pub/src/yamc.h diff --git a/pub/config.h.in b/pub/config.h.in index 6dd0e003..960868d8 100644 --- a/pub/config.h.in +++ b/pub/config.h.in @@ -66,9 +66,6 @@ /* Define to 1 if you have the `readline' library (-lreadline). */ #undef HAVE_LIBREADLINE -/* Define to 1 if you have the `yaml' library (-lyaml). */ -#undef HAVE_LIBYAML - /* Define to 1 if your system has a GNU libc compatible `malloc' function, and to 0 otherwise. */ #undef HAVE_MALLOC diff --git a/pub/configure b/pub/configure index 403f01d7..00e619b1 100755 --- a/pub/configure +++ b/pub/configure @@ -14986,57 +14986,10 @@ else as_fn_error "libreadline not found or corrupted" "$LINENO" 5 fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for yaml_parser_set_input_file in -lyaml" >&5 -$as_echo_n "checking for yaml_parser_set_input_file in -lyaml... " >&6; } -if test "${ac_cv_lib_yaml_yaml_parser_set_input_file+set}" = set; then : - $as_echo_n "(cached) " >&6 -else - ac_check_lib_save_LIBS=$LIBS -LIBS="-lyaml $LIBS" -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -/* Override any GCC internal prototype to avoid an error. - Use char because int might match the return type of a GCC - builtin and then its argument prototype would still apply. */ -#ifdef __cplusplus -extern "C" -#endif -char yaml_parser_set_input_file (); -int -main () -{ -return yaml_parser_set_input_file (); - ; - return 0; -} -_ACEOF -if ac_fn_c_try_link "$LINENO"; then : - ac_cv_lib_yaml_yaml_parser_set_input_file=yes -else - ac_cv_lib_yaml_yaml_parser_set_input_file=no -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext conftest.$ac_ext -LIBS=$ac_check_lib_save_LIBS -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_yaml_yaml_parser_set_input_file" >&5 -$as_echo "$ac_cv_lib_yaml_yaml_parser_set_input_file" >&6; } -if test "x$ac_cv_lib_yaml_yaml_parser_set_input_file" = x""yes; then : - cat >>confdefs.h <<_ACEOF -#define HAVE_LIBYAML 1 -_ACEOF - - LIBS="-lyaml $LIBS" - -else - as_fn_error "libyaml not found or out of sync" "$LINENO" 5 -fi - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for lm_minimize in -llmmin" >&5 -$as_echo_n "checking for lm_minimize in -llmmin... " >&6; } -if test "${ac_cv_lib_lmmin_lm_minimize+set}" = set; then : +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for lmmin in -llmmin" >&5 +$as_echo_n "checking for lmmin in -llmmin... " >&6; } +if test "${ac_cv_lib_lmmin_lmmin+set}" = set; then : $as_echo_n "(cached) " >&6 else ac_check_lib_save_LIBS=$LIBS @@ -15050,27 +15003,27 @@ cat confdefs.h - <<_ACEOF >conftest.$ac_ext #ifdef __cplusplus extern "C" #endif -char lm_minimize (); +char lmmin (); int main () { -return lm_minimize (); +return lmmin (); ; return 0; } _ACEOF if ac_fn_c_try_link "$LINENO"; then : - ac_cv_lib_lmmin_lm_minimize=yes + ac_cv_lib_lmmin_lmmin=yes else - ac_cv_lib_lmmin_lm_minimize=no + ac_cv_lib_lmmin_lmmin=no fi rm -f core conftest.err conftest.$ac_objext \ conftest$ac_exeext conftest.$ac_ext LIBS=$ac_check_lib_save_LIBS fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_lmmin_lm_minimize" >&5 -$as_echo "$ac_cv_lib_lmmin_lm_minimize" >&6; } -if test "x$ac_cv_lib_lmmin_lm_minimize" = x""yes; then : +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_lmmin_lmmin" >&5 +$as_echo "$ac_cv_lib_lmmin_lmmin" >&6; } +if test "x$ac_cv_lib_lmmin_lmmin" = x""yes; then : cat >>confdefs.h <<_ACEOF #define HAVE_LIBLMMIN 1 _ACEOF diff --git a/pub/configure.ac b/pub/configure.ac index 261dd021..7f2faa2e 100644 --- a/pub/configure.ac +++ b/pub/configure.ac @@ -53,10 +53,8 @@ AC_CHECK_LIB([fftw3], [fftw_plan_r2r_1d], , # [AC_MSG_ERROR(boost_regex not found or out of sync)]) AC_CHECK_LIB([readline], [readline], , [AC_MSG_ERROR(libreadline not found or corrupted)]) -AC_CHECK_LIB([yaml], [yaml_parser_set_input_file], , - [AC_MSG_ERROR(libyaml not found or out of sync)]) -AC_CHECK_LIB([lmmin], [lm_minimize], , +AC_CHECK_LIB([lmmin], [lmmin], , [AC_MSG_ERROR(liblmmin not found or out of sync)]) AC_CHECK_LIB([kww], [kwwcf], , [AC_MSG_ERROR(libkww not found or out of sync)]) diff --git a/pub/src/Makefile.am b/pub/src/Makefile.am index 3bb678d5..aa51d148 100644 --- a/pub/src/Makefile.am +++ b/pub/src/Makefile.am @@ -56,10 +56,8 @@ special2.cpp \ special.h \ xax_lex.h \ xax_lex.lpp \ -xax_yacc.ypp \ -yamc.cpp \ -yamc.h -AM_LDFLAGS = -lboost_regex -lread-plus -llmmin -lkww -lyaml -lyaml-cpp +xax_yacc.ypp +AM_LDFLAGS = -lboost_regex -lread-plus -llmmin -lkww -lyaml-cpp # frida2_LDADD $(BOOST_REGEX_LIB) # -lyaml-cpp diff --git a/pub/src/Makefile.in b/pub/src/Makefile.in index af4772fe..4adb6b85 100644 --- a/pub/src/Makefile.in +++ b/pub/src/Makefile.in @@ -59,7 +59,7 @@ am_frida2_OBJECTS = coord.$(OBJEXT) curve.$(OBJEXT) dualplot.$(OBJEXT) \ index.$(OBJEXT) list.$(OBJEXT) manip.$(OBJEXT) mystd.$(OBJEXT) \ olm.$(OBJEXT) opr.$(OBJEXT) plotaux.$(OBJEXT) rng.$(OBJEXT) \ rssm.$(OBJEXT) scan.$(OBJEXT) special2.$(OBJEXT) \ - xax_lex.$(OBJEXT) xax_yacc.$(OBJEXT) yamc.$(OBJEXT) + xax_lex.$(OBJEXT) xax_yacc.$(OBJEXT) frida2_OBJECTS = $(am_frida2_OBJECTS) frida2_LDADD = $(LDADD) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) @@ -268,11 +268,9 @@ special2.cpp \ special.h \ xax_lex.h \ xax_lex.lpp \ -xax_yacc.ypp \ -yamc.cpp \ -yamc.h +xax_yacc.ypp -AM_LDFLAGS = -lboost_regex -lread-plus -llmmin -lkww -lyaml -lyaml-cpp +AM_LDFLAGS = -lboost_regex -lread-plus -llmmin -lkww -lyaml-cpp all: $(BUILT_SOURCES) $(MAKE) $(AM_MAKEFLAGS) all-am @@ -388,7 +386,6 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/special2.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xax_lex.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xax_yacc.Po@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/yamc.Po@am__quote@ .cpp.o: @am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< diff --git a/pub/src/curve.cpp b/pub/src/curve.cpp index a868c842..227c0a9b 100644 --- a/pub/src/curve.cpp +++ b/pub/src/curve.cpp @@ -351,7 +351,8 @@ void NCurveFile::SetFileReference( const string& which ) cout << "\n"; } else { uint lval; - CContext ctx( k ); + CContext ctx; + ctx.request_scalar( k ); T->tree_uival( &lval, &ctx ); if( lval>=NOlm::MOM.size() ) throw "no data file " + strg(lval); diff --git a/pub/src/expr.cpp b/pub/src/expr.cpp index 5f89a21b..3cac8949 100644 --- a/pub/src/expr.cpp +++ b/pub/src/expr.cpp @@ -69,18 +69,9 @@ string CContext::info() const string ret = "dimension: "; switch( reqDim ) { case _1: ret += "scalar"; break; - case _VI: ret += "vector(i)"; break; case _VT: ret += "vector(t)"; break; default: ret += "undefined (BUG!)"; } -/* - ret += ", type: "; - switch( TReqTyp ) { - case _QI: ret += "int"; break; - case _QD: ret += "dbl"; break; - default: ret += "undefined (BUG!)"; - } -*/ ret += ", k: " + strg(k); ret += ", j: " + strg(j); ret += ", i: " + strg(i); @@ -301,7 +292,8 @@ void CRef::coord( CCoord *ret, uint k_in ) const uint k; if (tk) { // get k from reference : - CContext ctx( k_in ); + CContext ctx; + ctx.request_scalar( k_in ); tk->tree_uival(&k, &ctx); if (k>=NOlm::MOM.size()) throw( "coord: file index " + strg(k) + "invalid, maximum is " + @@ -498,7 +490,6 @@ void CTree::tree_val( CTOut *ret, const CContext *ctx ) const // Resolution is given by context 'ctx': uint kconv = setConvK( ctx->k ); uint jconv = kconv == -1 ? -1 : setConvJ( ctx->k, ctx->j, kconv ); - // cout << "DEBUG k=" << ctx->k << ", j=" << ctx->j << " conv k=" << kconv << ", j=" << jconv << "\n"; double conv_norm; // normalization constant (integral of resolution) double conv_step; // step in x/t, if equidistant COld *fv; @@ -699,8 +690,8 @@ void CTree::tree_val( CTOut *ret, const CContext *ctx ) const vt = rarg.vd; } // evaluate function: - CContext cu_ctx( frf.cr.k, frf.cr.j ); - cu_ctx.set_fargs( &vt ); + CContext cu_ctx; + cu_ctx.request_vector_of_farg( frf.cr.k, frf.cr.j, &vt ); frf.cr.fc->T->tree_val( ret, &cu_ctx ); if ( rarg.is_scalar() ) ret->set_d( ret->vd[0] ); @@ -716,7 +707,8 @@ void CTree::tree_val( CTOut *ret, const CContext *ctx ) const void CTree::tree_point_val( double *ret, uint k, uint j, uint i ) const { //cout << "tree_point_val\n"; - CContext ctx( k, j, i ); + CContext ctx; + ctx.request_scalar( k, j, i ); CTOut val; tree_val( &val, &ctx ); if ( !val.is_scalar() ) @@ -731,8 +723,8 @@ void CTree::tree_vec_val( vector<double> *ret, uint k, uint j ) const { //cout << "tree_vec_val\n"; CTOut val; - CContext ctx( k, j ); - ctx.set_nv( ret->size() ); + CContext ctx; + ctx.request_vector_of_indx( k, j, ret->size() ); tree_val( &val, &ctx ); if ( val.is_vector() ) (*ret) = val.vd; @@ -752,15 +744,15 @@ void CTree::tree_curve_val_vec( uint n = fargs.size(); ret->resize( n ); CTOut val; + CContext ctx; if ( !has_farg ) { // curve does not depend on t - CContext ctx( k, j ); + ctx.request_scalar( k, j ); tree_val( &val, &ctx ); if ( !val.is_scalar() ) throw "tree_curve_val (sca) got " + val.info(); (*ret)[0] = val.d; } else { - CContext ctx( k, j ); - ctx.set_fargs( &fargs ); + ctx.request_vector_of_farg( k, j, &fargs ); tree_val( &val, &ctx ); if ( !val.is_vector() ) throw "tree_curve_val (vec) got " + val.info(); diff --git a/pub/src/expr.h b/pub/src/expr.h index a17c0ff8..bb349a5c 100644 --- a/pub/src/expr.h +++ b/pub/src/expr.h @@ -1,7 +1,7 @@ #define PTree boost::shared_ptr<class CTree> #define PRef boost::shared_ptr<class CRef> -//! Context for generic tree evaluation. +//! A Context specifies how a tree shall be evaluated. class CContext { public: @@ -10,27 +10,26 @@ class CContext { _VI, // vector valued as function of data index _I _VT // vector valued as function of function argument t }; - enum TReqTyp { // Type of requested data - _QI, // index - _QD // double - }; uint k, j, i; const vector<double> *fargs; uint nv; TReqDim reqDim; - inline CContext( const uint k_=-1, const uint j_=-1, const uint i_=-1 ): - k(k_), j(j_), i(i_), fargs(0), reqDim(_1) {}; - void set_fargs( const vector<double> *fargs_ ) { - fargs = fargs_; reqDim = _VT; } - void set_nv( const uint nv_ ) { nv = nv_; reqDim = _VI; } + // Explicit constructors: + void request_scalar( const uint _k, const uint _j=-1, const uint _i=-1 ) { + k = _k; j = _j; i = _i; reqDim = _1; /* fargs=0 */ }; + void request_vector_of_farg( const uint _k, const uint _j, + const vector<double> *_fargs ) { + k = _k; j = _j; fargs = _fargs; reqDim = _VT; } + void request_vector_of_indx( const uint _k, const uint _j, + const uint _nv ) { + k = _k; j = _j; nv = _nv; reqDim = _VI; } bool dim_1() const { return reqDim==_1; }; bool dim_vi() const { return reqDim==_VI; }; string info() const; - }; diff --git a/pub/src/file_in.cpp b/pub/src/file_in.cpp index 87fbfbb7..98ab8d71 100644 --- a/pub/src/file_in.cpp +++ b/pub/src/file_in.cpp @@ -8,17 +8,19 @@ #include <stdlib.h> #include <map> #include <iostream> +#include <fstream> #include "mystd.h" #include "olm.h" #include <ask_simple_value.h> #include "file_in.h" -#include "yamc.h" +#include <yaml-cpp.h> +#include <string.h> using namespace std; namespace NFileIn { - void Load_08( FILE *F_in, string flong ); + void Load_08( ifstream& F_in, string flong ); void Load_01( FILE *F_in, string flong ); void LoadScan_01( FILE *F_in, CScan* sout, int nz ); void Load_96( FILE *F_in, string flong ); @@ -43,7 +45,9 @@ void NFileIn::Load(void) mystd::fname_divide( vflong[i], &fdir, &fshort, &fext); if ( fext=="y08" ) { - Load_08( F_in, vflong[i] ); + ifstream F_inpp (vflong[i].c_str()); + Load_08( F_inpp, vflong[i] ); + F_inpp.close(); } else if ( fext=="a01" ) { Load_01( F_in, vflong[i] ); } else if ( fext=="i96" ) { @@ -68,7 +72,7 @@ void NFileIn::Load(void) } //! Load a YAML file in y08 format. -void NFileIn::Load_08( FILE *F_in, string flong ) +void NFileIn::Load_08( ifstream& F_in, string flong ) { string lin, key, val, fdir, fshort, fext, res; double num, vx, vy, dy; @@ -76,151 +80,159 @@ void NFileIn::Load_08( FILE *F_in, string flong ) bool isdata; COlo *olo; CCoord co; - CYamlRead y( F_in ); mystd::fname_divide( flong, &fdir, &fshort, &fext); - y.checktype( YAML_STREAM_START_EVENT, "no stream\n" ); - y.checktype( YAML_DOCUMENT_START_EVENT, "no document\n" ); - y.checktype( YAML_MAPPING_START_EVENT, "main: no map\n" ); - y.checkvalue( "Meta" ); - y.checktype( YAML_MAPPING_START_EVENT, "Meta: no map\n" ); - y.checkvalue( "format" ); - y.checkvalue( "frida/y08 for yaml1" ); - y.checkvalue( "type" ); - string type = y.getscalar( "type" ); - if ( type=="generic tabular data" ){ + YAML::Parser parser(F_in); + YAML::Node doc; + parser.GetNextDocument(doc); + + //start to read Meta + if(!doc.FindValue("Meta")) + throw string("no Meta" ); + try { + YAML::CONTENT_TYPE Metatype = doc["Meta"].GetType(); + if ( Metatype != YAML::CT_MAP && doc["Meta"].size() > 0) + throw string("Meta is not a MAP type" ); + if(!doc["Meta"].FindValue("format")) + throw string("no format in Meta" ); + string str = doc["Meta"]["format"]; + if ( str != "frida/y08 for yaml1" ) + throw string("format is not frida/y08 for yaml1" ); + } catch(YAML::ParserException& e) { + std::cout << "no Meta" << e.what() << "\n"; + } + if(!doc["Meta"].FindValue("type")) + throw string("no type in Meta" ); + string type = doc["Meta"]["type"]; + if ( type == "generic tabular data" ){ isdata = true; olo = (COlo*) new COld(); - } else if ( type=="frida2 curve" ){ + } else if ( type == "frida2 curve" ){ isdata = false; olo = (COlo*) new COlc(); } else throw "File "+flong+" has invalid type "+type; - y.checktype( YAML_MAPPING_END_EVENT, "Meta: no end of map\n" ); - - y.checkvalue( "History" ); - y.checktype( YAML_SEQUENCE_START_EVENT, "History: no seq\n" ); - while( y.scalar_from_seq( "history", &lin ) ){ - olo->lDoc.push_back(lin); + + //start to read History + if(!doc.FindValue("History")) + throw string("no History" ); + if ( doc["History"].GetType() != YAML::CT_SEQUENCE && + doc["History"].size() > 0 ) + throw string("History is not a SEQUENCE type" ); + for (unsigned int i = 0; i < doc["History"].size(); i++) { + string str; + doc["History"][i] >> str; + olo->lDoc.push_back(str); } - y.checkvalue( "Coord" ); - y.checktype( YAML_MAPPING_START_EVENT, "Coord: no map\n" ); - if( !y.getCoord( "x", &(olo->xco) ) ) + //start to read Coord + if(!doc.FindValue("Coord")) + throw string("no Coord" ); + if ( doc["Coord"].GetType() != YAML::CT_MAP && doc["Coord"].size() > 0 ) + throw string("Coord is not a MAP type" ); + if( const YAML::Node *pName = doc["Coord"].FindValue("x") ){ + (*pName)["name"] >> olo->xco.name; + (*pName)["unit"] >> olo->xco.unit; + }else throw string( "no x coord" ); - if( !y.getCoord( "y", &(olo->yco) ) ) + if( const YAML::Node *pName = doc["Coord"].FindValue("y") ){ + (*pName)["name"] >> olo->xco.name; + (*pName)["unit"] >> olo->xco.unit; + }else throw string( "no y coord" ); + iz=0; - while( y.getCoord( "z"+strg(iz), &(co) ) ){ - olo->ZCo.push_back( co ); - ++iz; + while(const YAML::Node *pName = doc["Coord"].FindValue("z"+strg(iz))){ + (*pName)["name"] >> co.name; + (*pName)["unit"] >> co.unit; + olo->ZCo.push_back( co ); + ++iz; + } + + //start to read Param + if(!doc.FindValue("Param")) + throw string("no Param" ); + if ( doc["Param"].GetType() != YAML::CT_SEQUENCE && + doc["Param"].size() > 0) + throw string("Param is not a SEQUENCE type" ); + for(unsigned int iParam = 0; iParam < (doc["Param"].size()); iParam++ ){ + if ( doc["Param"][iParam].GetType() != YAML::CT_MAP ) + throw "Param " + strg(iParam) + " is not a MAP type"; + doc["Param"][iParam]["name"] >> co.name; + doc["Param"][iParam]["unit"] >> co.unit; + doc["Param"][iParam]["value"] >> val; + if( !mystd::any2dbl( val, &num ) ) + throw string( "param(" ) + co.name + "): invalid value " + val; + olo->RPar.push_back( CParam( co, num ) ); } - if( y.event.type != YAML_MAPPING_END_EVENT ) - throw string( "Coord: no end of map" ); - y.done(); - - y.checkvalue( "Param" ); - // Param section may be empty - y.next(); - if( y.event.type==YAML_SEQUENCE_START_EVENT ){ - y.done(); - while( true ){ - y.next(); - if( y.event.type == YAML_SEQUENCE_END_EVENT ) - break; - if( y.event.type != YAML_MAPPING_START_EVENT ) - throw string ( "Param: no map" ); - y.done(); - y.checkvalue( "name" ); - co.name = y.getscalar( "param.name" ); - y.checkvalue( "unit" ); - co.unit = y.getscalar( "param(" + co.name + ").unit" ); - y.checkvalue( "value" ); - val = y.getscalar( "param(" + co.name + ").value" ); - if( !mystd::any2dbl( val, &num ) ) - throw string( "param(" ) + co.name + "): invalid value " + val; - olo->RPar.push_back( CParam( co, num ) ); - y.checktype( YAML_MAPPING_END_EVENT, - "Param "+co.name+": no end of map\n" ); - } - } else { - if( !y.event.type==YAML_SCALAR_EVENT ) - throw( "Param: no empty scalar in empty param section" ); - } - y.done(); - + + //start to read Formula if not a data type if( !isdata ) { - y.checkvalue( "Formula" ); - string expr = y.getscalar( "formula" ); - NCurveFile::setFunction( (COlc*)olo, expr ); + if( const YAML::Node *pName = doc.FindValue("Formula") ){ + string expr; + *pName >> expr; + NCurveFile::setFunction( (COlc*)olo, expr ); + }else + throw string( "no Formula" ); } - y.checkvalue( "Tables" ); - y.checktype( YAML_SEQUENCE_START_EVENT, "Tables: no seq\n" ); - while( true ){ - y.next(); - if( y.event.type == YAML_SEQUENCE_END_EVENT ) - break; - if( y.event.type != YAML_MAPPING_START_EVENT ) - throw string ( "Tables: no map" ); - y.done(); + //start to read Tables + if(!doc.FindValue("Tables")) + throw string("no Tables" ); + if ( doc["Tables"].GetType() != YAML::CT_SEQUENCE && + doc["Tables"].size() > 0 ) + throw string("Tables is not a SEQUENCE type" ); + for (unsigned int iTable = 0; iTable < (doc["Tables"].size()); iTable++) { + if ( doc["Tables"][iTable].GetType() != YAML::CT_MAP ) + throw string("Tables " + strg(iTable) + " is not a MAP type" ); vector<double> z; - for( iz=0; iz<olo->ZCo.size(); ++iz ){ - y.checkvalue( "z" + strg(iz) ); - val = y.getscalar( "z" + strg(iz) + ".value" ); + for(iz=0; iz<olo->ZCo.size(); ++iz ){ + doc["Tables"][iTable]["z" + strg(iz)] >> val; if( !mystd::any2dbl( val, &num ) ) throw "z" + strg(iz) + ": invalid value " + val; z.push_back( num ); } + if( isdata ) { CScan sout; sout.z = z; - string xytag = y.getscalar( "core data tag" ); - if ( xytag=="xy" ){ - val = y.getscalar( "xy core data" ); + YAML::Iterator itxy=doc["Tables"][iTable].begin(); + string xytag = itxy.first(); + if ( xytag == "xy" ){ + itxy.second() >> val; while( val != "" ) { mystd::string_extract_line( val, &lin, &res ); if( sscanf( lin.c_str(), "%lg %lg\n", &vx, &vy )!=2 ) - throw "xy: bad data line: " + lin; + throw "xy: bad data line: " + lin; sout.push_xy( vx, vy ); val = res; - }; - } else if ( xytag=="xyd" ){ - val = y.getscalar( "xyd core data" ); - while( val != "" ) { + }; + }else if ( xytag == "xyd" ){ + itxy.second() >> val; + while( val != "" ) { mystd::string_extract_line( val, &lin, &res ); - if( sscanf( lin.c_str(), "%lg %lg %lg\n", - &vx, &vy, &dy )!=3 ) - throw "xyd: bad data line: " + lin; + if( sscanf( lin.c_str(), + "%lg %lg %lg\n",&vx, &vy, &dy )!=3 ) + throw "xyd: bad data line: " + lin; sout.push_xyd( vx, vy, dy ); val = res; - }; + }; } else throw "invalid xytag " + xytag; ((COld*)olo)->VS.push_back( sout ); - y.next(); } else { CCurve cout; cout.z = z; for( uint ip=0; ip<((COlc*)olo)->nPar(); ++ip ){ - y.checkvalue( "p" + strg(ip) ); - val = y.getscalar( "p" + strg(ip) + ".value" ); + doc["Tables"][iTable]["p" + strg(ip)] >> val; if( !mystd::any2dbl( val, &num ) ) throw "p" + strg(ip) + ": invalid value " + val; cout.P.push_back( num ); } ((COlc*)olo)->VC.push_back( cout ); - y.next(); } - if( y.event.type != YAML_MAPPING_END_EVENT ) - throw string("Table: no end of map after ") + - (isdata?"scan ":"curve ") + strg(olo->nScan()-1) + - ", type " + strg(y.event.type) + - " instead of " + strg(YAML_MAPPING_END_EVENT) + "\n"; - y.done(); } - // y.done(); olo->name = fshort; olo->lDoc.push_back( "loaded as " + flong ); @@ -344,7 +356,7 @@ void NFileIn::LoadScan_01( FILE *F_in, CScan* sout, int nz ) // get xy values: if ( (err=fscanf(F_in, "%4c%u\n", lab, &n))!=2 ) throw "expecting XY head line, found " + strg(err) + " entries"; - if ( strncmp(lab, "XY ", 4) ) + if (strncmp(lab, "XY ", 4) ) throw "found [" + strg(lab) + "] in XY head line"; for (i=0; i<n; i++) { @@ -506,3 +518,4 @@ void NFileIn::Load_96( FILE *F_in, string flong ) NOlm::OloAdd(&old); } + diff --git a/pub/src/rssm.cpp b/pub/src/rssm.cpp index 81922903..ef7cecaa 100644 --- a/pub/src/rssm.cpp +++ b/pub/src/rssm.cpp @@ -14,13 +14,14 @@ #include "olm.h" #include <ask_simple_value.h> #include "rssm.h" -#include "yamc.h" +#include <yaml-cpp.h> +#include <string.h> using namespace std; class RssmRawFile { public: - void RdRawYam( FILE *F_in ); + void RdRawYam( ifstream& F_in ); double daq_time_step; time_t measured_from; time_t measured_until; @@ -34,7 +35,8 @@ class RssmRawFile { //! Read raw data file, store contents as class variables -void RssmRawFile::RdRawYam( FILE *F_in ) +//void RssmRawFile::RdRawYam( FILE *F_in ) +void RssmRawFile::RdRawYam( ifstream& F_in ) { string lin, key, val, bla, blub; CCoord co; @@ -49,37 +51,43 @@ void RssmRawFile::RdRawYam( FILE *F_in ) daq_time_step=0; angles.clear(); - CYamlRead y( F_in ); - - y.checktype( YAML_STREAM_START_EVENT, "no stream\n" ); - y.checktype( YAML_DOCUMENT_START_EVENT, "no document\n" ); - y.checktype( YAML_MAPPING_START_EVENT, "main: no map\n" ); - - y.checkvalue( "Meta" ); - y.checktype( YAML_MAPPING_START_EVENT, "Meta: no map\n" ); - y.checkvalue( "format" ); - val = y.getscalar( "format" ); - if( strncmp( val.c_str(), "acq5.1 for yaml1", 16 ) ) - throw( string("Unexpected format: ") + val + " instead of acq5.1" ); - y.skipvalues( 2 ); - y.checktype( YAML_MAPPING_END_EVENT, "Meta: no end of map\n" ); - - y.checkvalue( "History" ); - y.checktype( YAML_SEQUENCE_START_EVENT, "History: no seq\n" ); - while( y.scalar_from_seq( "History", &val ) ) - ; // do nothing - - y.checkvalue( "Shortpar" ); - y.checktype( YAML_MAPPING_START_EVENT, "Shortpar: no map\n" ); - while( y.pair_from_map( "Shortpar", &key, &val ) ){ - if ( key=="incremental" ){ + YAML::Parser parser(F_in); + YAML::Node doc; + parser.GetNextDocument(doc); + + //start to read Meta + if(!doc.FindValue("Meta")) + throw string("no Meta" ); + try { + YAML::CONTENT_TYPE Metatype = doc["Meta"].GetType(); + if ( Metatype != YAML::CT_MAP && doc["Meta"].size() > 0) + throw string("Meta is not a MAP type" ); + if(!doc["Meta"].FindValue("format")) + throw string("no format in Meta" ); + doc["Meta"]["format"] >> val; + if( strncmp( val.c_str(), "acq5.1 for yaml1", 16 ) ) + throw( string("Unexpected format: ") + val + " instead of acq5.1" ); + } catch(YAML::ParserException& e) { + std::cout << "no Meta" << e.what() << "\n"; + } + + //do nothing in History + + //start to read Shortpar + if(!doc.FindValue("Shortpar")) + throw string("no Shortpar" ); + for(YAML::Iterator it=doc["Shortpar"].begin(); + it!=doc["Shortpar"].end();++it) { + it.first() >> key; + it.second() >> val; + if ( key=="incremental" ){ if ( val=="true" ) incremental = true; else if ( val=="false" ) incremental = false; else throw string( "Invalid value [" ) + val + - "] of 'incremental'"; + "] of 'incremental'"; } else if ( key=="measured_until" ){ char date_string[24]; sscanf( val.c_str(), "%24c", date_string ); @@ -96,57 +104,52 @@ void RssmRawFile::RdRawYam( FILE *F_in ) } if( !daq_time_step ) throw string( "daq time step not found" ); - - y.checkvalue( "Longpar" ); - y.getscalar( "Longpar emptyness" ); - - y.checkvalue( "Detectors" ); - y.checktype( YAML_SEQUENCE_START_EVENT, "Detectors: no seq\n" ); - while( true ){ - y.next(); - if( y.event.type == YAML_SEQUENCE_END_EVENT ) - break; - if( y.event.type != YAML_MAPPING_START_EVENT ) - throw string ( "Dets: no map" ); - y.done(); - if( !y.pair_from_map( "Dets", &key, &val ) ) - throw string( "invalid Det angle entry" ); - if( key != "angle" ) - throw string ( "missing angle" ); - if( !mystd::any2dbl( val, &num ) ){ - num = 0; - printf( "warning: invalid angle %s\n", val.c_str() ); - } - y.next(); - if( y.event.type != YAML_MAPPING_END_EVENT ){ - y.done(); y.next(); y.done(); y.next(); // path_offset ? - if( y.event.type != YAML_MAPPING_END_EVENT ){ - throw string( "Det map does not end" ); + + //do nothing in Longpar + + //start to read Detectors + if(!doc.FindValue("Detectors")) + throw string("no Detectors" ); + if ( doc["Detectors"].GetType() != YAML::CT_SEQUENCE && + doc["Detectors"].size() > 0 ) + throw string("Detectors is not a SEQUENCE type" ); + for (unsigned int iDet = 0; iDet < (doc["Detectors"].size()); + iDet++) { + if ( doc["Detectors"][iDet].GetType() != YAML::CT_MAP ) + throw string("Detectors " + strg(iDet) + + " is not a MAP type" ); + if( const YAML::Node *pName = + doc["Detectors"][iDet].FindValue("angle") ){ + *pName >> val; + if( !mystd::any2dbl( val, &num ) ){ + num = 0; + printf( "warning: invalid angle %s\n", + val.c_str() ); } - } - y.done(); + }else + throw string( "missing angle" ); + // path_offset ? angles.push_back( num ); } - y.done(); - if( angles.size()!=ndet ) - throw string( "ndet inconsistent" ); - - y.checkvalue( "EnergyHistograms" ); - y.checktype( YAML_SEQUENCE_START_EVENT, "E-Hist: no seq\n" ); - while( true ){ - y.next(); - if( y.event.type == YAML_SEQUENCE_END_EVENT ) - break; - if( y.event.type != YAML_SEQUENCE_START_EVENT ) - throw string ( "E-Hist: seq: no seq" ); - y.done(); - val = y.getscalar( "x-value" ); + + //start to read EnergyHistograms + if(!doc.FindValue("EnergyHistograms")) + throw string("no EnergyHistograms" ); + if ( doc["EnergyHistograms"].GetType() != YAML::CT_SEQUENCE && + doc["EnergyHistograms"].size() > 0 ) + throw string("EnergyHistograms is not a SEQUENCE type" ); + for (unsigned int iEne = 0; + iEne < (doc["EnergyHistograms"].size()); iEne++) { + if ( doc["EnergyHistograms"][iEne].GetType() != YAML::CT_SEQUENCE ) + throw "EnergyHistograms " + strg(iEne) + + " is not a SEQUENCE type"; + doc["EnergyHistograms"][iEne][0] >> val; if( !mystd::any2dbl( val, &num ) ) throw string( "E-Hist: invalid x-value " ) + val; for( int i=0; i<4; ++i ) rawdata[i].push_back( num ); for( int i=0; i<4; ++i ){ - val = y.getscalar( "cnts line" ); + doc["EnergyHistograms"][iEne][i+1] >> val;// cnts line if( !mystd::str2vec( val, &numvec ) ) throw string( "cnts line extraction failed" ); if( numvec.size()!=ndet ) @@ -155,32 +158,32 @@ void RssmRawFile::RdRawYam( FILE *F_in ) rawdata[i].push_back( numvec[ii] ); } for( int i=0; i<4; ++i ){ - val = y.getscalar( "time line" ); + doc["EnergyHistograms"][iEne][i+1+4] >> val;// time line if( !mystd::str2vec( val, &numvec ) ) throw string( "time line extraction failed" ); for( int ii=0; ii<numvec.size(); ++ii ) rawdata[i].push_back( numvec[ii] ); } - y.checktype( YAML_SEQUENCE_END_EVENT, "E-Hist entry" ); } - y.done(); - - y.checkvalue( "ChopperHistograms" ); - y.checktype( YAML_SEQUENCE_START_EVENT, "C-Hist: no seq\n" ); - while( true ){ - y.next(); - if( y.event.type == YAML_SEQUENCE_END_EVENT ) - break; - if( y.event.type != YAML_SEQUENCE_START_EVENT ) - throw string ( "C-Hist: seq: no seq" ); - y.done(); - val = y.getscalar( "x-value" ); + + //start to read ChopperHistograms + if(!doc.FindValue("ChopperHistograms")) + throw string("no ChopperHistograms" ); + if ( doc["ChopperHistograms"].GetType() != YAML::CT_SEQUENCE && + doc["ChopperHistograms"].size() > 0 ) + throw string("ChopperHistograms is not a SEQUENCE type" ); + for (unsigned int iCho = 0; + iCho < (doc["ChopperHistograms"].size()); iCho++) { + if ( doc["ChopperHistograms"][iCho].GetType() != YAML::CT_SEQUENCE ) + throw string("ChopperHistograms " + strg(iCho) + + " is not a SEQUENCE type" ); + doc["ChopperHistograms"][iCho][0] >> val; if( !mystd::any2dbl( val, &num ) ) throw string( "C-Hist: invalid x-value " ) + val; for( int i=4; i<6; ++i ) rawdata[i].push_back( num ); - for( int i=4; i<6; ++i ){ - val = y.getscalar( "cnts line" ); + for( int i=4; i<6; ++i ){ // 2 cnts lines + doc["ChopperHistograms"][iCho][i-4+1] >> val;//cnts line if( !mystd::str2vec( val, &numvec ) ) throw string( "cnts line extraction failed" ); if( numvec.size()!=ndet ) @@ -188,17 +191,14 @@ void RssmRawFile::RdRawYam( FILE *F_in ) for( int ii=0; ii<numvec.size(); ++ii ) rawdata[i].push_back( numvec[ii] ); } - for( int i=4; i<6; ++i ){ - val = y.getscalar( "time line" ); + for( int i=4; i<6; ++i ){ // 2 time lines + doc["ChopperHistograms"][iCho][i-4+1+2] >> val;//time line if( !mystd::str2vec( val, &numvec ) ) throw string( "time line extraction failed" ); for( int ii=0; ii<numvec.size(); ++ii ) rawdata[i].push_back( numvec[ii] ); } - y.checktype( YAML_SEQUENCE_END_EVENT, "C-Hist entry" ); } - y.done(); - } void NRSSM::ReadScan( int format, int flag ) @@ -217,12 +217,13 @@ void NRSSM::ReadScan( int format, int flag ) file_f = wask("Read SPHERES data from file"); RssmRawFile R; int ret; - FILE *F_in; + ifstream F_in; if ( format==2 ){ throw string( "XML support has been withdrawn from frida2 on 19may09" ); } else if ( format==5 ){ - if( !(F_in = fopen(file_f.c_str(), "r")) ) { + F_in.open (file_f.c_str(),fstream::in); + if( !(F_in.is_open()) ) { cerr << "Cannot read " << file_f << "\n"; return; } @@ -232,7 +233,7 @@ void NRSSM::ReadScan( int format, int flag ) catch( string &e) { cerr << "Cannot read " << file_f << ":\n"; cerr << e << endl; - if (F_in) fclose(F_in); + if (F_in.is_open()) F_in.close(); return; } ret = 0; @@ -402,18 +403,19 @@ void NRSSM::ReadSeries( int format, int flag ) "XML support has been withdrawn from frida2 on 19may09" ); } else if( format==5 ) { fnam = fser+"a"+strg(isub); - FILE *F_in; - if( !(F_in = fopen( fnam.c_str(), "r" ) ) ) + ifstream F_in; + F_in.open(fnam.c_str(),fstream::in); + if( !(F_in.is_open()) ) break; // cout << "successfully opened "<<fnam<<"\n"; try{ - RssmRawFile rf; + RssmRawFile rf; rf.RdRawYam( F_in ); RR.push_back( rf ); } catch(exception& e) { cerr << e.what() << endl; - if (F_in) fclose(F_in); + if (F_in.is_open()) F_in.close(); return; } ret = 0; diff --git a/pub/src/yamc.cpp b/pub/src/yamc.cpp deleted file mode 100644 index 5b45d966..00000000 --- a/pub/src/yamc.cpp +++ /dev/null @@ -1,143 +0,0 @@ -//**************************************************************************// -//* FRIDA: fast reliable interactive data analysis *// -//* (C) Joachim Wuttke 1990-, v2(C++) 2001- *// -//* http://frida.sourceforge.net *// -//* yamc.cpp *// -//* read YAML data files, using libyaml *// -//**************************************************************************// - -#include <string> -using namespace std; - -#include "coord.h" -#include "yamc.h" // NOTE: this shall be eliminated -#include "mystd.h" - -#include <yaml-cpp.h> // NOTE: this shall be used in the future - -CYamlRead::CYamlRead( FILE *F_in ) -{ - memset( &parser, 0, sizeof(parser) ); - memset( &event, 0, sizeof(event) ); - if (!yaml_parser_initialize( &parser )) - throw string("Could not initialize the parser object"); - yaml_parser_set_input_file( &parser, F_in ); - last_valid = "START"; -} - -CYamlRead::~CYamlRead() -{ - yaml_parser_delete( &parser ); -} - -void CYamlRead::next() -{ - if( !yaml_parser_parse( &parser, &event) ) - throw string( "Invalid input? Last valid entry was \"" ) + - last_valid + "\""; -} - -void CYamlRead::done() -{ - yaml_event_delete( &event); -} - -void CYamlRead::checktype( int type, string errmsg ) -{ - next(); - if( event.type != type ) - throw errmsg; - if( event.type == YAML_SCALAR_EVENT ) - last_valid = (const char*)event.data.scalar.value; - done(); -} - -void CYamlRead::checkvalue( string value ) -{ - next(); - if( event.type != YAML_SCALAR_EVENT ) - throw string("No scalar while expecting ") + value; - if( strcmp((const char*)event.data.scalar.value, value.c_str()) ) - throw string("Found ") + (const char*)event.data.scalar.value + - " instead of " + value; - last_valid = (const char*)event.data.scalar.value; - done(); -} - -void CYamlRead::skipvalues( int repet ) -{ - for( int i=0; i<repet; ++i ){ - checktype( YAML_SCALAR_EVENT, string( "skip " ) + strg(repet) ); - } -} - -bool CYamlRead::scalar_from_seq( string seqname, string *ret ) -{ - next(); - if( event.type == YAML_SEQUENCE_END_EVENT ){ - done(); - return false; - } - if( event.type != YAML_SCALAR_EVENT ) - throw string( "No scalar in sequence " ) + seqname + - "; last valid entry was \"" + last_valid + "\""; - *ret = (const char*)event.data.scalar.value; - last_valid = (const char*)event.data.scalar.value; - done(); - return true; -} - -bool CYamlRead::pair_from_map( string mapname, string *key, string *val ) -{ - next(); - if( event.type == YAML_MAPPING_END_EVENT ){ - done(); - return false; - } - if( event.type != YAML_SCALAR_EVENT ) - throw string( "No scalar key in mapping " ) + mapname + - "; last valid entry was \"" + last_valid + "\""; - *key = (const char*)event.data.scalar.value; - done(); - next(); - if( event.type != YAML_SCALAR_EVENT ) - throw string( "No scalar value in mapping " ) + mapname + - "; last valid entry was \"" + last_valid + "\""; - *val = (const char*)event.data.scalar.value; - last_valid = (const char*)event.data.scalar.value; - done(); - return true; -} - -string CYamlRead::getscalar( string scalarname ) -{ - string ret; - next(); - if( event.type != YAML_SCALAR_EVENT ) - throw string( "No scalar " ) + scalarname + - "; last valid entry was \"" + last_valid + "\""; - ret = (const char*)event.data.scalar.value; - done(); - return ret; -} - -bool CYamlRead::getCoord( string label, CCoord *co ) -{ - next(); - if( event.type == YAML_MAPPING_END_EVENT ) - return false; - if( event.type != YAML_SCALAR_EVENT ) - throw string( "inconsistency where " + label + " expected" ); - if( strcmp((const char*)event.data.scalar.value, label.c_str()) ) - throw string("Found ") + (const char*)event.data.scalar.value + - " instead of " + label; - last_valid = (const char*)event.data.scalar.value; - done(); - checktype( YAML_MAPPING_START_EVENT, label + ": no map\n" ); - checkvalue( "name" ); - co->name = getscalar( label + ".name" ); - checkvalue( "unit" ); - co->unit = getscalar( label + ".unit" ); - checktype( YAML_MAPPING_END_EVENT, label + ": no end of map\n" ); - return true; -} diff --git a/pub/src/yamc.h b/pub/src/yamc.h deleted file mode 100644 index fbea275a..00000000 --- a/pub/src/yamc.h +++ /dev/null @@ -1,27 +0,0 @@ -//**************************************************************************// -//* FRIDA: fast reliable interactive data analysis *// -//* (C) Joachim Wuttke 1990-, v2(C++) 2001- *// -//* http://frida.sourceforge.net *// -//* yamc.h *// -//* read YAML data files, using libyaml *// -//**************************************************************************// - -#include <yaml.h> - -class CYamlRead { - public: - yaml_parser_t parser; - yaml_event_t event; - string last_valid; - CYamlRead( FILE *F_in ); - ~CYamlRead(); - void next(); - void done(); - void checktype( int type, string errmsg ); - void checkvalue( string value ); - void skipvalues( int repet ); - bool scalar_from_seq( string seqname, string *ret ); - bool pair_from_map( string mapname, string *key, string *val ); - string getscalar( string scalarname ); - bool getCoord( string label, CCoord *co ); -}; -- GitLab