xml/Xsil.hh

00001 #ifndef _LIGO_XSIL_H
00002 #define _LIGO_XSIL_H
00003 /*----------------------------------------------------------------------*/
00004 /*                                                                      */
00005 /* Module Name: Xsil                                                    */
00006 /*                                                                      */
00007 /* Module Description: Utility classes for XML/XSIL input/output.       */
00008 /*                                                                      */
00009 /* Revision History:                                                    */
00010 /* Rel   Date     Programmer    Comments                                */
00011 /* 0.1   10Jun00  D. Sigg       First release                           */
00012 /*                                                                      */
00013 /* Documentation References:                                            */
00014 /*      Man Pages: TLGXsil.html                                         */
00015 /*      References: none                                                */
00016 /*                                                                      */
00017 /* Author Information:                                                  */
00018 /* Name          Telephone       Fax             e-mail                 */
00019 /* Daniel Sigg   (509) 372-8132  (509) 372-8137  sigg_d@ligo.mit.edu    */
00020 /*                                                                      */
00021 /*                                                                      */
00022 /*                      -------------------                             */
00023 /*                                                                      */
00024 /*                             LIGO                                     */
00025 /*                                                                      */
00026 /*        THE LASER INTERFEROMETER GRAVITATIONAL WAVE OBSERVATORY.      */
00027 /*                                                                      */
00028 /*                     (C) The LIGO Project, 1999.                      */
00029 /*                                                                      */
00030 /*                                                                      */
00031 /* Caltech                              MIT                             */
00032 /* LIGO Project MS 51-33                LIGO Project NW-17 161          */
00033 /* Pasadena CA 91125                    Cambridge MA 01239              */
00034 /*                                                                      */
00035 /* LIGO Hanford Observatory             LIGO Livingston Observatory     */
00036 /* P.O. Box 1970 S9-02                  19100 LIGO Lane Rd.             */
00037 /* Richland WA 99352                    Livingston, LA 70754            */
00038 /*                                                                      */
00039 /*----------------------------------------------------------------------*/
00040 
00041 #include <iosfwd>
00042 #include <iomanip>
00043 #include <complex>
00044 #include <string>
00045 #include <map>
00046 #include <vector>
00047 #include "Time.hh"
00048 
00049 #if defined(__SUNPRO_CC) && defined(G__DICTIONARY)
00050    namespace xml {}
00051    using namespace xml;
00052 #endif
00053 
00054 namespace xml {
00055 
00056 
00132    const char* const xmlVersion = "<?xml version=\"1.0\"?>";
00133 
00135    const char* const xmlDocType = 
00136    "<!DOCTYPE LIGO_LW [\n"
00137    "<!ELEMENT LIGO_LW ((LIGO_LW|Comment|Param|Time|Table|Array|Stream)*)>\n"
00138    "<!ATTLIST LIGO_LW Name CDATA #IMPLIED Type CDATA #IMPLIED>\n"
00139    "<!ELEMENT Comment (#PCDATA)>\n"
00140    "<!ELEMENT Param (#PCDATA)>\n"
00141    "<!ATTLIST Param Name CDATA #IMPLIED Type CDATA #IMPLIED Dim CDATA #IMPLIED\n"
00142    "                Unit CDATA #IMPLIED>\n"
00143    "<!ELEMENT Table (Comment?,Column*,Stream?)>\n"
00144    "<!ATTLIST Table Name CDATA #IMPLIED Type CDATA #IMPLIED>\n"
00145    "<!ELEMENT Column EMPTY>\n"
00146    "<!ATTLIST Column Name CDATA #IMPLIED Type CDATA #IMPLIED Unit CDATA #IMPLIED>\n"
00147    "<!ELEMENT Array (Dim*,Stream?)>\n"
00148    "<!ATTLIST Array Name CDATA #IMPLIED Type CDATA #IMPLIED>\n"
00149    "<!ELEMENT Dim (#PCDATA)>\n"
00150    "<!ATTLIST Dim Name CDATA #IMPLIED>\n"
00151    "<!ELEMENT Stream (#PCDATA)>\n"
00152    "<!ATTLIST Stream Name CDATA #IMPLIED Type (Remote|Local) \"Local\"\n"
00153    "          Delimiter CDATA \",\" Encoding CDATA #IMPLIED Content CDATA #IMPLIED>\n"
00154    "<!ELEMENT Time (#PCDATA)>\n"
00155    "<!ATTLIST Time Name CDATA #IMPLIED Type (GPS|Unix|ISO-8601) \"ISO-8601\">\n"
00156    "]>";
00157    // "<!DOCTYPE LIGO_LW SYSTEM "
00158    // "\"http://www.cacr.caltech.edu/projects/ligo_lw.dtd\">";
00159 
00161    const char* const xmlLigoLW = "LIGO_LW";
00162 
00164    const char* const xmlContainer = "LIGO_LW";
00166    const char* const xmlTable = "Table";
00168    const char* const xmlTime = "Time";
00170    const char* const xmlParam = "Param";
00172    const char* const xmlColumn = "Column";
00174    const char* const xmlArray = "Array";
00176    const char* const xmlDim = "Dim";
00178    const char* const xmlStream = "Stream";
00179 
00181    const char* const xmlName = "Name";
00183    const char* const xmlType = "Type";
00185    const char* const xmlFlag = "Flag";
00187    const char* const xmlComment = "Comment";
00189    const char* const xmlUnit = "Unit";
00191    const char* const xmlDelimiter = "Delimiter";
00193    const char* const xmlLink = "Link";
00195    const char* const xmlRef = "Ref";
00197    const char* const xmlEncode = "Encoding";
00199    const char* const xmlEncodeStd = "BigEndian,base64";
00201    const char* const xmlEncodeBE = "BigEndian,base64";
00203    const char* const xmlEncodeLE = "LittleEndian,base64";
00204 
00206    const char* const xmlObjTypeTestParameters = "TestParameters";
00208    const char* const xmlObjTypeTimeSeries = "TimeSeries";
00210    const char* const xmlObjTypeFrequencySeries = "FrequencySeries";
00212    const char* const xmlObjTypeSpectrum = "Spectrum";
00214    const char* const xmlObjTypeTransferFunction = "TransferFunction";
00216    const char* const xmlObjTypeCoefficients = "Coefficients";
00218    const char* const xmlObjTypeHistogram = "Histogram";
00220    const char* const xmlObjTypeSettings = "Settings";
00222    const char* const xmlObjTypeResult = "Result";
00223 
00225    const char* const xmlTagClosing = ">";
00226 
00228    const char* const xmlAttrName = " Name=\"";
00229 
00231    const char* const xmlAttrType = " Type=\"";
00232 
00234    const char* const xmlAttrFlag = " Flag=\"";
00235 
00237    const char* const xmlAttrUnit = " Unit=\"";
00238 
00240    const char* const xmlAttrDim = " Dim=\"";
00241 
00243    const char* const xmlAttrEncoding = " Encoding=\"";
00244 
00246    const char* const xmlAttrComment = " Comment=\"";
00247 
00249    const char* const xmlAttrClosing = "\"";
00250 
00251 
00253    enum gdsDataType {
00255    gds_void = 0,
00257    gds_int8 = 1, 
00259    gds_int16 = 2, 
00261    gds_int32 = 3, 
00263    gds_int64 = 4, 
00265    gds_float32 = 5, 
00267    gds_float64 = 6,
00269    gds_complex32 = 7, 
00271    gds_complex64 = 8, 
00273    gds_string = 9,
00275    gds_channel = 10,
00277    gds_bool = 11,
00279    gds_time = 12,
00281    gds_table = 13
00282    };
00283 
00310    class xsilIndent {
00311    protected:
00313       int               fLevel;
00314    public: 
00316       explicit xsilIndent (int level = 0) : fLevel (level) {
00317       }
00319       std::ostream& write (std::ostream& os) const;
00320    };
00321 
00322 
00336    class xsilTagBegin {
00337    protected:
00339       const char*       fTag;
00340    public: 
00342       explicit xsilTagBegin (const char* tag = 0) : fTag (tag) {
00343       }
00345       std::ostream& write (std::ostream& os) const;
00346    };
00347 
00348 
00360    class xsilTagEnd {
00361    protected:
00363       const char*       fTag;
00364    public: 
00366       explicit xsilTagEnd (const char* tag = 0) : fTag (tag) {
00367       }
00369       std::ostream& write (std::ostream& os) const;
00370    };
00371 
00372 
00387    class xsilDimAttr {
00388    protected:
00390       int               fDim;
00391    public:
00393       explicit xsilDimAttr (int dim = 0) : fDim (dim) {
00394       }
00396       std::ostream& write (std::ostream& os) const;
00397    };
00398 
00399 
00414    class xsilEncodingAttr {
00415    public:
00417       xsilEncodingAttr () {
00418       }
00420       std::ostream& write (std::ostream& os) const;
00421    };
00422 
00423 
00438 template <class T>
00439    class xsilDataTypename {
00440    public:
00442       explicit xsilDataTypename () {
00443       }
00444    };
00445 
00446 
00474 template <class T>
00475    class xsilBase64 {
00476    protected:
00478       const T*          fData;
00480       int               fN;
00481    public:
00483       xsilBase64 (const T* data = 0, int N = 0)
00484       : fData (data), fN (N) {
00485       }
00487       std::ostream& write (std::ostream& os) const;
00488    };
00489 
00490 
00492    std::ostream& base64encode (std::ostream& os, const char* p, 
00493                      int num, int elsize);
00495    bool base64decode (const char* code, int codelen,
00496                      char* p, int len, int ctype = 0);
00497 
00511    class xsil_base {
00512    protected:
00514       const char*       fName;
00516       const char*       fUnit;
00518       const char*       fComment;
00520       int               fLevel;
00521    public:
00523       explicit xsil_base (const char* name = 0, int level = 0)
00524       : fName (name), fUnit (0), fComment (0), fLevel(level) {
00525       }
00527       explicit xsil_base (const char* name, const char* unit, 
00528                         int level = 0)
00529       : fName (name), fUnit (unit), fComment (0), fLevel(level) {
00530       }
00532       void SetUnit (const char* unit) {
00533          fUnit = unit; }
00535       void SetComment (const char* comment) {
00536          fComment = comment; }
00538       std::ostream& write (std::ostream& os) const;
00539    };
00540 
00541 
00543    inline std::ostream& operator<< (std::ostream& os,
00544                      const xsilIndent& i) {
00545       return i.write (os); }
00547    inline std::ostream& operator<< (std::ostream& os,
00548                      const xsilTagBegin& t) {
00549       return t.write (os); }
00551    inline std::ostream& operator<< (std::ostream& os,
00552                      const xsilTagEnd& t) {
00553       return t.write (os); }
00555    inline std::ostream& operator<< (std::ostream& os,
00556                      const xsilDimAttr& d) {
00557       return d.write (os); }
00559    inline std::ostream& operator<< (std::ostream& os,
00560                      const xsilEncodingAttr& e) {
00561       return e.write (os); }
00563    inline std::ostream& operator<< (std::ostream& os,
00564                      const xsil_base& b) {
00565       return b.write (os); }
00567    inline std::ostream& operator<< (std::ostream& os,
00568                      const xsilDataTypename<bool>& t) {
00569       return (os << "boolean"); }
00571    inline std::ostream& operator<< (std::ostream& os,
00572                      const xsilDataTypename<char>& t) {
00573       return (os << "byte"); }
00575    inline std::ostream& operator<< (std::ostream& os,
00576                      const xsilDataTypename<short>& t) {
00577       return (os << "short"); }
00579    inline std::ostream& operator<< (std::ostream& os,
00580                      const xsilDataTypename<int>& t) {
00581       return (os << "int"); }
00582 #ifndef __CINT__
00584    inline  std::ostream& operator<< (std::ostream& os,
00585                      const xsilDataTypename<long long>& t) {
00586       return (os << "long"); }
00587 #endif
00589    inline std::ostream& operator<< (std::ostream& os,
00590                      const xsilDataTypename<float>& t) {
00591       return (os << "float"); }
00593    inline std::ostream& operator<< (std::ostream& os,
00594                      const xsilDataTypename<double>& t) {
00595       return (os << "double"); }
00597    inline std::ostream& operator<< (std::ostream& os,
00598                      const xsilDataTypename<std::complex<float> >& t) {
00599       return (os << "floatComplex"); }
00601    inline std::ostream& operator<< (std::ostream& os,
00602                      const xsilDataTypename<std::complex<double> >& t) {
00603       return (os << "doubleComplex"); }
00605    inline std::ostream& operator<< (std::ostream& os,
00606                      const xsilDataTypename<const char*>& t) {
00607       return (os << "string"); }
00609    inline std::ostream& operator<< (std::ostream& os,
00610                      const xsilDataTypename<std::string>& t) {
00611       return (os << "string"); }
00613    inline std::ostream& operator<< (std::ostream& os,
00614                      const xsilDataTypename<Time>& t) {
00615       return (os << "time"); }
00617    struct xsil_table {
00618    };
00620    inline std::ostream& operator<< (std::ostream& os,
00621                      const xsilDataTypename<xml::xsil_table>& t) {
00622       return (os << "table"); }
00623 
00648    class xsilHeader {
00649    public: 
00651       xsilHeader () {
00652       }
00654       std::ostream& write (std::ostream& os) const;
00655    };
00656 
00657 
00669    class xsilTrailer {
00670    public:
00672       xsilTrailer () {
00673       }
00675       std::ostream& write (std::ostream& os) const;
00676    };
00677 
00678 
00690    class xsilComment : public xsil_base {
00691    public:
00693       xsilComment (const char* comment, int level = 2)
00694       : xsil_base (comment, level) {
00695       }
00697       xsilComment (const std::string& comment, int level = 2)
00698       : xsil_base (comment.c_str(), level) {
00699       }
00701       std::ostream& write (std::ostream& os) const;
00702    };
00703 
00715    class xsilTime : public xsil_base {
00716    public:
00718       typedef unsigned long gdstime_t;
00719    
00720    protected:
00722       gdstime_t         fSec;
00724       gdstime_t         fNSec;
00725    public:
00727       xsilTime (const char* name, const Time& time, 
00728                int level = 2)
00729       : xsil_base (name, level), fSec (time.getS()), fNSec (time.getN()) {
00730       }
00732       xsilTime (const char* name, gdstime_t sec, gdstime_t nsec, 
00733                int level = 2)
00734       : xsil_base (name, level), fSec (sec), fNSec (nsec) {
00735       }
00738       std::ostream& write (std::ostream& os, int format = 1) const;
00739    };
00740 
00752 template <class T>
00753    class xsilParameter : public xsil_base {
00754    protected:
00756       const T*          fData;
00758       int               fN;
00759    public:
00760       xsilParameter() : fData (0), fN (0) {
00761       }
00763       xsilParameter (const char* name, const T& x, int N = 1, int level = 2)
00764       : xsil_base (name, level), fData (&x), fN (N) {
00765       }
00767       xsilParameter (const char* name, const char* unit, const T& x, 
00768                     int N = 1, int level = 2)
00769       : xsil_base (name, unit, level), fData (&x), fN (N) {
00770       }
00772       std::ostream& write (std::ostream& os) const;
00773    };
00774 
00775 
00787 template <class T>
00788    class xsilArray : public xsil_base {
00789    protected:
00791       int               fDim[4];
00793       const T*          fData;
00794    
00795    public:
00796       xsilArray () : fData (0) {
00797          fDim[0] = 0; fDim[1] = 0; fDim[2] = 0; fDim[3] = 0; }
00799       xsilArray (const char* name, int dim1, 
00800                 const T* data, int level = 2)
00801       : xsil_base (name, level), fData (data) {
00802          fDim[0] = dim1; fDim[1] = 0; fDim[2] = 0; fDim[3] = 0; }
00804       xsilArray (const char* name, 
00805                 int dim1, int dim2, const T* data, int level = 2)
00806       : xsil_base (name, level), fData (data) {
00807          fDim[0] = dim1; fDim[1] = dim2; fDim[2] = 0; fDim[3] = 0; }
00809       xsilArray (const char* name, int dim1, int dim2, int dim3, 
00810                 const T* data, int level = 2)
00811       : xsil_base (name, level), fData (data) {
00812          fDim[0] = dim1; fDim[1] = dim2; fDim[2] = dim3; fDim[3] = 0; }
00814       xsilArray (const char* name, 
00815                 int dim1, int dim2, int dim3, int dim4,
00816                 const T* data, int level = 2)
00817       : xsil_base (name, level), fData (data) {
00818          fDim[0] = dim1; fDim[1] = dim2; fDim[2] = dim3; fDim[3] = dim4; }
00820       std::ostream& write (std::ostream& os, bool term = true) const;
00821    };
00822 
00823 
00837    class xsilDataBegin : public xsil_base {
00838    protected:
00840       const char*       fType;
00842       const char*       fFlag;
00843    public:
00845       xsilDataBegin (const char* name, const char* type, int level = 1) 
00846       : xsil_base (name, level), fType (type), fFlag (0) {
00847       }
00849       xsilDataBegin (const char* name, const char* type, 
00850                     const char* flag, int level = 1) 
00851       : xsil_base (name, level), fType (type), fFlag (flag) {
00852       }
00854       std::ostream& write (std::ostream& os) const;
00855    };
00856 
00857 
00871 template <class T>
00872    class xsilDataEnd  {
00873    protected:
00875       int               fLevel;
00877       int               fDim[4];
00879       const T*          fData;
00880    public:
00882       xsilDataEnd (int level = 1) 
00883       : fLevel (level), fData (0) {
00884          fDim[0] = 0; fDim[1] = 0; fDim[2] = 0; fDim[3] = 0; }
00886       xsilDataEnd (int dim1, const T* data, int level = 1)
00887       : fLevel (level), fData (data) {
00888          fDim[0] = dim1; fDim[1] = 0; fDim[2] = 0; fDim[3] = 0; }
00890       xsilDataEnd (int dim1, int dim2, const T* data, int level = 1)
00891       : fLevel (level), fData (data) {
00892          fDim[0] = dim1; fDim[1] = dim2; fDim[2] = 0; fDim[3] = 0; }
00894       xsilDataEnd (int dim1, int dim2, int dim3,
00895                   const T* data, int level = 1)
00896       : fLevel (level), fData (data) {
00897          fDim[0] = dim1; fDim[1] = dim2; fDim[2] = dim3; fDim[3] = 0; }
00899       xsilDataEnd (int dim1, int dim2, int dim3, int dim4,
00900                   const T* data, int level = 1)
00901       : fLevel (level), fData (data) {
00902          fDim[0] = dim1; fDim[1] = dim2; fDim[2] = dim3; fDim[3] = dim4; }
00904       std::ostream& write (std::ostream& os, bool term = true) const;
00905    };
00906 
00907 
00938    class xsilTableBegin : public xsil_base {
00939    protected:
00941       const char*       fType;
00942    public:
00944       xsilTableBegin (const char* name, const char* type = 0, int level = 1) 
00945       : xsil_base (name, level), fType (type) {
00946       }
00948       std::ostream& write (std::ostream& os) const;
00949    };
00950 
00951 
00960    class xsilTableEnd {
00961    protected:
00963       int               fLevel;
00964    public:
00966       xsilTableEnd (int level = 1) : fLevel (level) {
00967       }
00969       std::ostream& write (std::ostream& os) const;
00970    };
00971 
00972 
00980 template <class T>
00981    class xsilTableColumn : public xsil_base {
00982    public:
00984       xsilTableColumn (const char* name = 0, int level = 2)
00985       : xsil_base (name, level) {
00986       }
00988       std::ostream& write (std::ostream& os) const;
00989    };
00990 
00991 
01000    class xsilTableDataBegin : public xsil_base {
01001    public:
01003       xsilTableDataBegin (const char* name = 0, int level = 2) 
01004       : xsil_base (name, level) {
01005       }
01007       std::ostream& write (std::ostream& os) const;
01008    };
01009 
01010 
01019    class xsilTableDataEnd {
01020    protected:
01022       int               fLevel;
01023    public:
01025       xsilTableDataEnd (int level = 2) : fLevel (level) {
01026       }
01028       std::ostream& write (std::ostream& os) const;
01029    };
01030 
01031 
01039   template <class T>
01040   class xsilTableEntry {
01041   protected:
01043     const T*            fValue;
01044   public:
01046     xsilTableEntry() 
01047       : fValue (0) 
01048     {}
01049    
01051     xsilTableEntry (const T& value)
01052       : fValue (&value)
01053     {}
01054 
01060     std::ostream& write (std::ostream& os) const;
01061   };
01062 
01070 template <>
01071    std::ostream& xsilTableEntry<std::complex<double> >::write (std::ostream& os) const;
01072 
01073 
01081 template <>
01082    std::ostream& xsilTableEntry<Time>::write (std::ostream& os) const;
01083 
01091 template <>
01092    class xsilTableEntry<const char*> {
01093    protected:
01095       const char*       fValue;
01097       int               fLength;
01098    public:
01100       xsilTableEntry<const char*> (const char* value = 0)
01101       : fValue (value), fLength (-1) {
01102       }
01104       xsilTableEntry<const char*> (const char* value, int len)
01105       : fValue (value), fLength (len) {
01106       }
01108       std::ostream& write (std::ostream& os) const;
01109    };
01110 
01112    std::string xsilStringEscape (const char* p);
01114    std::string xsilEscape (const char* p, int len);
01116    std::string xsilUnescape (const char* p);
01117 
01118 
01126    class xsilTableEntryDelimiter {
01127    public:
01129       xsilTableEntryDelimiter() {
01130       }
01132       std::ostream& write (std::ostream& os) const;
01133    };
01134 
01135 
01137    inline std::ostream& operator<< (std::ostream& os,
01138                      const xsilHeader& h) {
01139       return h.write(os); }
01141    inline std::ostream& operator<< (std::ostream& os,
01142                      const xsilTrailer& t) {
01143       return t.write(os); }
01144 
01146 template <class T>
01147    inline std::ostream& operator<< (std::ostream& os, 
01148                      const xsilParameter<T>& p) {
01149       return p.write (os); }
01150 
01152    inline std::ostream& operator<< (std::ostream& os, 
01153                      const xsilComment& t) {
01154       return t.write (os); }
01155 
01157    inline std::ostream& operator<< (std::ostream& os, 
01158                      const xsilTime& t) {
01159       return t.write (os); }
01160 
01162 template <class T>
01163    inline std::ostream& operator<< (std::ostream& os, 
01164                      const xsilArray<T>& arr) {
01165       return arr.write (os); }
01166 
01168    inline std::ostream& operator<< (std::ostream& os, 
01169                      const xsilDataBegin& d) {
01170       return d.write (os); }
01171 
01173 template <class T>
01174    inline std::ostream& operator<< (std::ostream& os, 
01175                      const xsilDataEnd<T>& d) {
01176       return d.write (os); }
01177 
01179    inline std::ostream& operator<< (std::ostream& os, 
01180                      const xsilTableBegin& d) {
01181       return d.write (os); }
01182 
01184    inline std::ostream& operator<< (std::ostream& os, 
01185                      const xsilTableEnd& d) {
01186       return d.write (os); }
01187 
01189 template <class T>
01190    inline std::ostream& operator<< (std::ostream& os, 
01191                      const xsilTableColumn<T>& d) {
01192       return d.write (os); }
01193 
01195    inline std::ostream& operator<< (std::ostream& os, 
01196                      const xsilTableDataBegin& d) {
01197       return d.write (os); }
01198 
01200    inline std::ostream& operator<< (std::ostream& os, 
01201                      const xsilTableDataEnd& d) {
01202       return d.write (os); }
01203 
01205 template <class T>
01206    inline std::ostream& operator<< (std::ostream& os, 
01207                      const xsilTableEntry<T>& d) {
01208       return d.write (os); }
01209 
01211    inline std::ostream& operator<< (std::ostream& os, 
01212                      const xsilTableEntryDelimiter& d) {
01213       return d.write (os); }
01214 
01215 
01216 #ifdef __CINT__
01217 //    inline std::ostream& operator<< (std::ostream& os, 
01218 //                       const xsilParameter<bool>& p);
01219    inline std::ostream& operator<< (std::ostream& os, 
01220                      const xsilParameter<char>& p);
01221    inline std::ostream& operator<< (std::ostream& os, 
01222                      const xsilParameter<short>& p);
01223    inline std::ostream& operator<< (std::ostream& os, 
01224                      const xsilParameter<int>& p);
01225    inline std::ostream& operator<< (std::ostream& os, 
01226                      const xsilParameter<float>& p);
01227    inline std::ostream& operator<< (std::ostream& os, 
01228                      const xsilParameter<double>& p);
01229    inline std::ostream& operator<< (std::ostream& os, 
01230                      const xsilParameter<std::complex<float> >& p);
01231    inline std::ostream& operator<< (std::ostream& os, 
01232                      const xsilParameter<std::complex<double> >& p);
01233    inline std::ostream& operator<< (std::ostream& os, 
01234                      const xsilParameter<const char*>& p);
01235    inline std::ostream& operator<< (std::ostream& os, 
01236                      const xsilArray<float>& arr);
01237    inline std::ostream& operator<< (std::ostream& os, 
01238                      const xsilArray<double>& arr);
01239    inline std::ostream& operator<< (std::ostream& os, 
01240                      const xsilArray<std::complex<float> >& arr);
01241    inline std::ostream& operator<< (std::ostream& os, 
01242                      const xsilArray<std::complex<double> >& arr);
01243    inline std::ostream& operator<< (std::ostream& os, 
01244                      const xsilDataEnd<float>& d);
01245    inline std::ostream& operator<< (std::ostream& os, 
01246                      const xsilDataEnd<double>& d);
01247    inline std::ostream& operator<< (std::ostream& os, 
01248                      const xsilDataEnd<std::complex<float> >& d);
01249    inline std::ostream& operator<< (std::ostream& os, 
01250                      const xsilDataEnd<std::complex<double> >& d);
01251 //   inline std::ostream& operator<< (std::ostream& os, 
01252 //                       const xsilTableColumn<bool>& p);
01253    inline std::ostream& operator<< (std::ostream& os, 
01254                      const xsilTableColumn<char>& p);
01255    inline std::ostream& operator<< (std::ostream& os, 
01256                      const xsilTableColumn<short>& p);
01257    inline std::ostream& operator<< (std::ostream& os, 
01258                      const xsilTableColumn<int>& p);
01259    inline std::ostream& operator<< (std::ostream& os, 
01260                      const xsilTableColumn<float>& p);
01261    inline std::ostream& operator<< (std::ostream& os, 
01262                      const xsilTableColumn<double>& p);
01263    inline std::ostream& operator<< (std::ostream& os, 
01264                      const xsilTableColumn<std::complex<float> >& p);
01265    inline std::ostream& operator<< (std::ostream& os, 
01266                      const xsilTableColumn<std::complex<double> >& p);
01267    inline std::ostream& operator<< (std::ostream& os, 
01268                      const xsilTableColumn<Time>& p);
01269    inline std::ostream& operator<< (std::ostream& os, 
01270                      const xsilTableColumn<const char*>& p);
01271 //   inline std::ostream& operator<< (std::ostream& os, 
01272 //                       const xsilTableEntry<bool>& p);
01273    inline std::ostream& operator<< (std::ostream& os, 
01274                      const xsilTableEntry<char>& p);
01275    inline std::ostream& operator<< (std::ostream& os, 
01276                      const xsilTableEntry<short>& p);
01277    inline std::ostream& operator<< (std::ostream& os, 
01278                      const xsilTableEntry<int>& p);
01279    inline std::ostream& operator<< (std::ostream& os, 
01280                      const xsilTableEntry<float>& p);
01281    inline std::ostream& operator<< (std::ostream& os, 
01282                      const xsilTableEntry<double>& p);
01283    inline std::ostream& operator<< (std::ostream& os, 
01284                      const xsilTableEntry<std::complex<float> >& p);
01285    inline std::ostream& operator<< (std::ostream& os, 
01286                      const xsilTableEntry<std::complex<double> >& p);
01287    inline std::ostream& operator<< (std::ostream& os, 
01288                      const xsilTableEntry<Time>& p);
01289    inline std::ostream& operator<< (std::ostream& os, 
01290                      const xsilTableEntry<const char*>& p);
01291 #endif
01292 
01335    class xsilHandler {
01336    protected:
01338       bool              fIgnoreNested;
01340       std::string       fComment;
01342       std::string       fUnit;
01343    public:
01345       typedef std::map<std::string, std::string> attrlist;
01346    
01348       explicit xsilHandler (bool ignore = false) :
01349       fIgnoreNested (ignore) {
01350       }
01352       virtual ~xsilHandler() {
01353       }
01355       virtual bool CommentHandler (const std::string& comment);
01356    
01358       virtual bool ParameterHandler (int type, void* x, int size,
01359                         const attrlist& attr);
01361       virtual bool HandleParameter (const std::string& name,
01362                         const attrlist& attr,
01363                         const bool& p, int N = 1) {
01364          return false; }
01366       virtual bool HandleParameter (const std::string& name,
01367                         const attrlist& attr,
01368                         const char& p, int N = 1) {
01369          return false; }
01371       virtual bool HandleParameter (const std::string& name,
01372                         const attrlist& attr,
01373                         const short& p, int N = 1) {
01374          return false; }
01376       virtual bool HandleParameter (const std::string& name,
01377                         const attrlist& attr,
01378                         const int& p, int N = 1) {
01379          return false; }
01380    #ifndef __CINT__
01382       virtual bool HandleParameter (const std::string& name,
01383                         const attrlist& attr,
01384                         const long long& p, int N = 1) {
01385          return false; }
01386    #endif
01388       virtual bool HandleParameter (const std::string& name,
01389                         const attrlist& attr,
01390                         const float& p, int N = 1) {
01391          return false; }
01393       virtual bool HandleParameter (const std::string& name,
01394                         const attrlist& attr,
01395                         const double& p, int N = 1) {
01396          return false; }
01398       virtual bool HandleParameter (const std::string& name,
01399                         const attrlist& attr,
01400                         const std::complex<float>& p, int N = 1) {
01401          return false; }
01403       virtual bool HandleParameter (const std::string& name,
01404                         const attrlist& attr,
01405                         const std::complex<double>& p, int N = 1) {
01406          return false; }
01408       virtual bool HandleParameter (const std::string& name,
01409                         const attrlist& attr,
01410                         const std::string& p) {
01411          return false; }
01412    
01414       virtual bool HandleTime (const std::string& name,
01415                         const attrlist& attr,
01416                         unsigned long sec, unsigned long nsec) {
01417          return false; }
01418    
01420       virtual bool DataHandler (const std::string& name,
01421                         int type, void* x, int size, 
01422                         int dim1, int dim2 = 0, int dim3 = 0,
01423                         int dim4 = 0);
01425       virtual bool HandleData (const std::string& name,
01426                         float* x, int dim1, int dim2 = 0, 
01427                         int dim3 = 0, int dim4 = 0) {
01428          return false; }
01430       virtual bool HandleData (const std::string& name,
01431                         std::complex<float>* x, 
01432                         int dim1, int dim2 = 0, 
01433                         int dim3 = 0, int dim4 = 0) {
01434          return false; }
01436       virtual bool HandleData (const std::string& name,
01437                         double* x, int dim1, int dim2 = 0, 
01438                         int dim3 = 0, int dim4 = 0) {
01439          return false; }
01441       virtual bool HandleData (const std::string& name,
01442                         std::complex<double>* x, 
01443                         int dim1, int dim2 = 0, 
01444                         int dim3 = 0, int dim4 = 0) {
01445          return false; }
01446    
01448       virtual bool HandleTableColumn (int col, const std::string& name,
01449                         int type, const attrlist& attr) {
01450          return false; }
01451    
01453       virtual bool TableEntryHandler (int row, int col, int type, 
01454                         void* x);
01456       virtual bool HandleTableEntry (int row, int col, bool p) {
01457          return false; }
01459       virtual bool HandleTableEntry (int row, int col, char p) {
01460          return false; }
01462       virtual bool HandleTableEntry (int row, int col, short p) {
01463          return false; }
01465       virtual bool HandleTableEntry (int row, int col, int p) {
01466          return false; }
01467    #ifndef __CINT__
01469       virtual bool HandleTableEntry (int row, int col, long long p) {
01470          return false; }
01471    #endif
01473       virtual bool HandleTableEntry (int row, int col, float p) {
01474          return false; }
01476       virtual bool HandleTableEntry (int row, int col, double p) {
01477          return false; }
01479       virtual bool HandleTableEntry (int row, int col,
01480                         const std::complex<float>& p) {
01481          return false; }
01483       virtual bool HandleTableEntry (int row, int col,
01484                         const std::complex<double>& p) {
01485          return false; }
01487       virtual bool HandleTableEntry (int row, int col, const Time& p) {
01488          return false; }
01490       virtual bool HandleTableEntry (int row, int col,
01491                         const std::string& p) {
01492          return false; }
01493    
01495       virtual xsilHandler* GetHandler (const attrlist& attr) {
01496          return fIgnoreNested ? new xsilHandler (true) : 0; }
01498       virtual xsilHandler* GetTableHandler (const attrlist& attr) {
01499          return fIgnoreNested ? new xsilHandler (true) : 0; }
01500    };
01501 
01502 
01516    class xsilHandlerQuery {
01517    public:
01519       typedef std::map<std::string, std::string> attrlist;
01520       xsilHandlerQuery() {
01521       }
01523       virtual ~xsilHandlerQuery() {
01524       }
01526       virtual xsilHandler* GetHandler (const attrlist& attr) = 0;
01528       virtual xsilHandler* GetTableHandler (const attrlist& attr) {
01529          return 0; }
01530    };
01531 
01532 
01540    class xsilHandlerUnknown : public xsilHandler {
01541       xsilHandlerUnknown (const xsilHandlerUnknown&);
01542       xsilHandlerUnknown& operator= (const xsilHandlerUnknown&);
01543    
01544    public:
01546       explicit xsilHandlerUnknown (std::ostream& os, 
01547                         const attrlist* attr = 0, bool ignore = false);
01549       virtual ~xsilHandlerUnknown();
01550    
01552       virtual bool CommentHandler (const std::string& comment);
01553    
01555       virtual bool HandleParameter (const std::string& name,
01556                         const attrlist& attr,
01557                         const bool& p, int N = 1);
01559       virtual bool HandleParameter (const std::string& name,
01560                         const attrlist& attr,
01561                         const char& p, int N = 1);
01563       virtual bool HandleParameter (const std::string& name,
01564                         const attrlist& attr,
01565                         const short& p, int N = 1);
01567       virtual bool HandleParameter (const std::string& name,
01568                         const attrlist& attr,
01569                         const int& p, int N = 1);
01570    #ifndef __CINT__
01572       virtual bool HandleParameter (const std::string& name,
01573                         const attrlist& attr,
01574                         const long long& p, int N = 1);
01575    #endif
01577       virtual bool HandleParameter (const std::string& name,
01578                         const attrlist& attr,
01579                         const float& p, int N = 1);
01581       virtual bool HandleParameter (const std::string& name,
01582                         const attrlist& attr,
01583                         const double& p, int N = 1);
01585       virtual bool HandleParameter (const std::string& name,
01586                         const attrlist& attr,
01587                         const std::complex<float>& p, int N = 1);
01589       virtual bool HandleParameter (const std::string& name,
01590                         const attrlist& attr,
01591                         const std::complex<double>& p, int N = 1);
01593       virtual bool HandleParameter (const std::string& name,
01594                         const attrlist& attr, const std::string& p);
01596       virtual bool HandleTime (const std::string& name,
01597                         const attrlist& attr,
01598                         unsigned long sec, unsigned long nsec);
01600       virtual bool HandleData (const std::string& name,
01601                         float* x, int dim1, int dim2 = 0, 
01602                         int dim3 = 0, int dim4 = 0);
01604       virtual bool HandleData (const std::string& name,
01605                         std::complex<float>* x, 
01606                         int dim1, int dim2 = 0, 
01607                         int dim3 = 0, int dim4 = 0);
01609       virtual bool HandleData (const std::string& name,
01610                         double* x, int dim1, int dim2 = 0, 
01611                         int dim3 = 0, int dim4 = 0);
01613       virtual bool HandleData (const std::string& name,
01614                         std::complex<double>* x, 
01615                         int dim1, int dim2 = 0, 
01616                         int dim3 = 0, int dim4 = 0);
01617    
01618    protected:
01620       std::ostream*     fOs;
01622       bool              fTrailer;
01624       int               fDim[4];
01626       bool              fComplex;
01628       bool              fDouble;
01630       void*             fData;
01631    
01632    };
01633 
01634 
01642    class xsilHandlerQueryUnknown : public xml::xsilHandlerQuery {
01643    public:
01645       xsilHandlerQueryUnknown (std::ostream* os) : fOs (os) {
01646       }
01648       virtual xsilHandler* GetHandler (const attrlist& attr);
01649    
01650    protected:
01652       std::ostream*             fOs;
01653    };
01654 
01660    class xsilHandlerPtr {
01661       mutable xsilHandler* ptr;
01662    public:
01663 
01666       explicit xsilHandlerPtr (xsilHandler* x = 0) : ptr (x) {
01667       }
01670       xsilHandlerPtr (const xsilHandlerPtr& p) : ptr (0) {
01671          *this = p; }
01674       ~xsilHandlerPtr() {
01675          if (ptr) delete ptr; }
01676 
01679       xsilHandlerPtr& operator= (const xsilHandlerPtr& p) {
01680          if (this != &p) {
01681             ptr = p.ptr; p.ptr = 0;
01682          }
01683          return *this; }
01684 
01687       xsilHandler& operator* () const {
01688          return *ptr; }
01689 
01692       xsilHandler* operator-> () const {
01693          return ptr; }
01694 
01697       bool operator== (const xsilHandler* p) {
01698          return (ptr == p); }
01699 
01702       bool operator!= (const xsilHandler* p) {
01703          return (ptr != p); }
01704    };
01705 
01706 
01708    class xsilHandlerTemp {
01709    
01710    public:
01712       enum status {
01713       // normal
01714       normal = 0,
01715       // parameter
01716       param = 1,
01717       // time
01718       time = 2,
01719       // column
01720       column = 3,
01721       // stream
01722       stream = 4,
01723       // array
01724       array = 5,
01725       // array dim
01726       arraydim = 6,
01727       // array stream 
01728       arraystream = 7,
01729       // comment
01730       comment = 8
01731       };
01733       typedef std::map<std::string, std::string> attrlist;
01735       typedef std::vector <int> columnlist;
01736    
01738       bool              fIsTable;
01740       status            fStatus;
01742       xsilHandlerPtr    fHandler;
01744       std::string       fComment;
01746       std::string       fText;
01748       std::string       fDataName;
01750       int               fDataType;
01752       char*             fData;
01754       int               fSize;
01756       std::vector<int>  fDim;
01758       int               fEncoding;
01760       attrlist          fPrmAttr;
01762       std::string       fPrmName;
01764       int               fPrmType;
01766       int               fPrmSize;
01768       columnlist        fColumns;
01770       int               fColumn;
01772       int               fRow;
01774       bool              fIgnoreNextComma;
01775    
01777       xsilHandlerTemp(xsilHandler* handler, bool istable = false)
01778       : fIsTable (istable), fStatus  (normal), fHandler (handler), 
01779       fData (0), fColumn (0), fRow (0), fIgnoreNextComma (false) {
01780       }
01782       ~xsilHandlerTemp() {
01783          SetData (0); } 
01785       void SetData (char* dat) {
01786          if (fData) delete [] fData; 
01787          fData = dat; }
01789       bool FlushTableEntries (bool comma = false);
01791       bool SetTableEntry();
01792    
01793    };
01794 
01829    class xsilParser {
01830    public:
01832       typedef std::map<std::string, std::string> attrlist;
01834       typedef std::vector<xsilHandlerQuery*> handlerquerylist;
01836       typedef std::vector<xsilHandlerTemp> worklist;
01837    
01839       static xsilHandlerQuery& DefaultHandler();
01841       static xsilHandlerQuery& IgnoreAllHandler();
01842    
01843    protected:
01845       handlerquerylist  fHandler;
01847       int               fIgnore;
01849       bool              fNewLine;
01851       bool              fEndXML;
01853       worklist          fWork;
01855       void*             fXML;
01856    
01857    public:
01859       xsilParser ();
01861       ~xsilParser();
01863       void Init();
01865       void Done();
01866    
01869       void AddHandler (xsilHandlerQuery& handler);
01871       bool Parse (const char* p, int len);
01873       bool Parse (const std::string& s) {
01874          return Parse (s.c_str(), s.size()); }
01876       bool Parse (std::istream& is);
01878       bool ParseFile (const char* filename);
01879    
01880    public:
01882       void Startelement (const char* name, const attrlist& attr);
01884       void Texthandler (const char* text, int len);
01886       void Endelement (const char* name);
01887    };
01891 #ifndef __CINT__
01892 
01894 // Template defintions: Due to a strange SUN/C++ bug (generating 
01895 // undefined symbols for string constants) these definitions
01896 // have to be in the header and have to be declared inline.
01897 
01898 // xsilBase64: XML base64 object 
01899 template <class T>
01900 inline std::ostream& xsilBase64<T>::write (std::ostream& os) const 
01901 {
01902   return base64encode (os, (const char*) fData, fN, sizeof (T));
01903 }
01904 
01905 // xsilParameter: XML parameter object
01906 template <class T>
01907    inline std::ostream& xsilParameter<T>::write (std::ostream& os) const 
01908    {
01909       os << xsilIndent (fLevel) << xsilTagBegin (xmlParam);
01910       this->xsil_base::write (os);
01911       os << xmlAttrType << xsilDataTypename<T>() << xmlAttrClosing <<
01912          xsilDimAttr (fN) << xmlTagClosing;
01913       for (int i = 0; i < fN; i++) {
01914          os << (i > 0 ? " " : "") << fData[i];
01915       }
01916       return (os << xsilTagEnd (xmlParam));
01917    }
01918 
01919 // xsilParameter: XML parameter object (string specialization)
01920 template <>
01921    inline std::ostream& xsilParameter<const char*>::write (std::ostream& os) const 
01922    {
01923       os << xsilIndent (fLevel) << xsilTagBegin (xmlParam);
01924       this->xsil_base::write (os);
01925       os << xmlAttrType << xsilDataTypename<const char*>() << xmlAttrClosing 
01926          << xsilDimAttr (fN) << xmlTagClosing;
01927       for (int i = 0; i < fN; i++) {
01928          os << (i > 0 ? "\n" : "") << xsilStringEscape (fData[i]);
01929       }
01930       return (os << xsilTagEnd (xmlParam));
01931    }
01932 
01933 // xsilArray: XML array object
01934 template <class T>
01935    inline std::ostream& xsilArray<T>::write (std::ostream& os, 
01936                      bool term) const 
01937    {
01938       int fN = 1;
01939       bool nValid = false;
01940       for (int i = 0; i < 4; ++i) {
01941          if (fDim[i] > 0) {
01942             fN *= fDim[i];
01943             nValid = true;
01944          }
01945       }
01946       if (!nValid) fN = 0;
01947       if ((fData != 0) && (fN > 0)) {
01948          os << xsilIndent (fLevel) << xsilTagBegin (xmlArray); 
01949          xsil_base::write (os);
01950          os << xmlAttrType << xsilDataTypename<T>() << xmlAttrClosing <<
01951             xmlTagClosing << std::endl;
01952          for (int i = 0; i < 4; ++i) {
01953             if (fDim[i] > 0) {
01954                os << xsilIndent (fLevel+1) << xsilTagBegin (xmlDim) << 
01955                   xmlTagClosing << fDim[i] << xsilTagEnd (xmlDim) << 
01956                   std::endl;
01957             }
01958          }
01959          os << xsilIndent (fLevel+1) << xsilTagBegin (xmlStream) << 
01960             xsilEncodingAttr() << xmlTagClosing << std::endl;
01961          xsilBase64<T> (fData, fN).write (os);
01962          os << xsilIndent (fLevel+1) << xsilTagEnd (xmlStream) << 
01963             std::endl;
01964          os << xsilIndent (fLevel) << xsilTagEnd (xmlArray);
01965       }
01966       return os; 
01967    }
01968 
01969 // xsilDataEnd: XML data end object
01970 template <class T>
01971    inline std::ostream& xsilDataEnd<T>::write (std::ostream& os, 
01972                      bool term) const 
01973    {
01974       os << xsilArray<T> (0, fDim[0], fDim[1], fDim[2], fDim[3],
01975                          fData, fLevel + 1) << std::endl; 
01976       if (term) 
01977          os << xsilIndent (fLevel) << xsilTagEnd (xmlContainer);
01978       return os; 
01979    }
01980 
01981 // xsilTableColumn: XML table column object
01982 template <class T>
01983    inline std::ostream& xsilTableColumn<T>::write (std::ostream& os) const
01984    {
01985       os << xsilIndent (fLevel) << xsilTagBegin (xmlColumn);
01986       this->xsil_base::write (os);
01987       os << xmlAttrType << xsilDataTypename<T>() << xmlAttrClosing
01988          << "/" << xmlTagClosing;
01989       return os;
01990    }
01991 
01992 // xsilTableEntry: XML table entry object
01993 template <class T>
01994    inline std::ostream& xsilTableEntry<T>::write (std::ostream& os) const
01995    {
01996       if (fValue) os << *fValue;
01997       return os;
01998    }
01999 
02001   template<>
02002   inline std::ostream& 
02003   xsilTableEntry<std::complex<double> >::write (std::ostream& os) const {
02004       if (fValue) os << fValue->real() << " " << fValue->imag();
02005       return os;
02006   }
02007 
02008   template<> 
02010   inline std::ostream& 
02011   xsilTableEntry<Time>::write (std::ostream& os) const
02012    {
02013       char buf[64];
02014       TimeStr (*fValue, buf, "%s.%n");
02015       os << buf;
02016       return os;
02017    }
02018 
02019   // xsilTableEntry: XML table entry object (string specialization)
02020   inline std::ostream& 
02021   xsilTableEntry<const char*>::write (std::ostream& os) const
02022    {
02023       if (fValue) os << "\"" << xsilEscape (fValue, fLength) << "\"";
02024       return os;
02025    }
02026 
02027   /*******************************************************************
02028    *  Finish up....
02029    *  \endcond
02030    */
02031 
02032 #endif // __CINT__
02033 }
02034 
02035 #endif // _LIGO_XSIL_H

Generated on Sun Mar 8 19:20:52 2009 for dmt by  doxygen 1.5.4