casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Misc.h
Go to the documentation of this file.
00001 #ifndef MISC_H
00002 #define MISC_H
00003 /*
00004  * ALMA - Atacama Large Millimeter Array
00005  * (c) European Southern Observatory, 2002
00006  * (c) Associated Universities Inc., 2002
00007  * Copyright by ESO (in the framework of the ALMA collaboration),
00008  * Copyright by AUI (in the framework of the ALMA collaboration),
00009  * All rights reserved.
00010  * 
00011  * This library is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU Lesser General Public
00013  * License as published by the Free software Foundation; either
00014  * version 2.1 of the License, or (at your option) any later verson.
00015  * 
00016  * This library is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY, without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * Lesser General Public License for more details.
00020  * 
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with this library; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00024  * MA 02111-1307  USA
00025  *
00026  *
00027  * File Misc.h
00028  */
00029 
00030 #include <iostream>
00031 #include <fstream>
00032 #include <string> 
00033 #include <vector>
00034 #include <set>
00035 #include <map>
00036 
00037 #include <boost/filesystem/path.hpp>
00038 #include <boost/filesystem/convenience.hpp>
00039 #include <boost/algorithm/string/trim.hpp>
00040 #include <boost/algorithm/string/predicate.hpp>
00041 #include <boost/algorithm/string/split.hpp>
00042 
00043 typedef unsigned char xmlChar;
00044 
00045 struct _xmlNode;
00046 typedef struct _xmlNode xmlNode;
00047 typedef xmlNode *xmlNodePtr;
00048 
00049 struct _xmlDoc;
00050 typedef struct _xmlDoc xmlDoc;
00051 typedef xmlDoc *xmlDocPtr;
00052 
00053 struct _xsltStylesheet;
00054 typedef struct _xsltStylesheet xsltStylesheet;
00055 typedef xsltStylesheet *xsltStylesheetPtr;
00056 
00057 extern "C" int xmlLoadExtDtdDefaultValue;
00058 
00059 namespace asdm {
00074   bool directoryExists(const char* dir);
00075         
00084   bool createDirectory(const char* dir);
00085         
00093   bool createPath(const char* path);
00094         
00095         
00099 #define ByteSwap5(x) ByteSwap((unsigned char *) &x,sizeof(x))    
00100   void ByteSwap(unsigned char * b, int n);
00101 
00106   class ByteOrder {
00107   public:
00108     static const ByteOrder* Little_Endian; /*< A unique object to represent a little endian byte order. */
00109     static const ByteOrder* Big_Endian;    /*< A unique object to represent a big endian byte order. */
00110     static const ByteOrder* Machine_Endianity; /*< A unique object storing the endianity of the machine. */
00111 
00120     std::string toString() const ;
00121 
00129     static const ByteOrder* fromString(const std::string & s);
00130 
00131   private:
00132     std::string name_;
00133     int endianity_;
00134 
00135     ByteOrder(const std::string & name, int endianity);
00136     virtual ~ByteOrder();
00137     static const ByteOrder* machineEndianity(); 
00138   };
00139 
00147   std::string uniqSlashes(const std::string& s);
00148 
00149   class ASDMUtilsException {
00150   public:
00151     ASDMUtilsException();
00152     ASDMUtilsException(const std::string& message);
00153     
00154     const std::string& getMessage();
00155     
00156   private:
00157     std::string message;
00158   };
00159   
00160   class ASDMUtils {
00161   public :
00162     enum Origin { UNKNOWN, ALMA, EVLA };
00163     static std::string          version (const std::string& asdmPath);
00164     static std::vector<std::string> telescopeNames(const std::string& asdmPath);
00165     static Origin               origin(const std::vector<std::string>& telescopeNames);
00166     static std::vector<std::string> xmlFilenames(const std::string& asdmPath);
00167     
00168     static std::string pathToV2V3ALMAxslTransform() ;
00169     static std::string pathToV2V3EVLAxslTransform() ;
00170     static std::string nameOfV2V3xslTransform(ASDMUtils::Origin origin);
00171 
00172     struct DotXMLFilter {
00173     public:
00174       DotXMLFilter(std::vector<std::string>& filenames);
00175       //void operator() (path& p);
00176       void operator() ( boost::filesystem::directory_entry& p);
00177     private:
00178       std::vector<std::string>* filenames;    
00179     };
00180     
00181   private :
00182     static bool initialize();
00183     static bool initialized;
00184     static bool hasChild(xmlDocPtr, xmlNodePtr node, const xmlChar* childName);
00185     static std::string parseRow(xmlDocPtr, xmlNodePtr node, const xmlChar* childName);
00186     static std::set<std::string> evlaValidNames;
00187     static std::set<std::string> almaValidNames;
00188     static std::map<ASDMUtils::Origin, std::string> filenameOfV2V3xslTransform;
00189     static std::map<std::string, std::string> rootSubdir ;
00190 
00191     static std::string pathToxslTransform( const std::string& xsltFilename);
00192   }; // end class ASDMUtil.
00193 
00194 
00220   class ASDMParseOptions {
00221     friend class ASDM;
00222 
00223   public:
00235     ASDMParseOptions();
00236 
00240     ASDMParseOptions(const ASDMParseOptions& x);
00241 
00246     virtual ~ASDMParseOptions();
00247 
00251     ASDMParseOptions & operator=(const ASDMParseOptions & rhs);
00252 
00253     
00257     ASDMParseOptions& asALMA();
00258 
00262     ASDMParseOptions& asIRAM_PDB();
00263 
00267     ASDMParseOptions& asEVLA();
00268 
00272     ASDMParseOptions& asV2();
00273 
00277     ASDMParseOptions& asV3();
00278 
00285     ASDMParseOptions& loadTablesOnDemand(bool b);
00286 
00293     ASDMParseOptions& checkRowUniqueness(bool b);
00294 
00298     friend std::ostream& operator<<(std::ostream& output, const ASDMParseOptions& p);
00299 
00300     std::string toString() const;
00301 
00302   private:
00303     ASDMUtils::Origin   origin_;
00304     bool                detectOrigin_;
00305     std::string         version_;
00306     bool                detectVersion_;
00307     bool                loadTablesOnDemand_;
00308     bool                checkRowUniqueness_;
00309   };                            // end class ASDMParseOptions.
00310 
00315   class XSLTransformerException {
00316 
00317   public:
00321     XSLTransformerException();
00322 
00327     XSLTransformerException(const std::string & m);
00328     
00332     virtual ~XSLTransformerException();
00333 
00338     std::string getMessage() const;
00339 
00340   protected:
00341     std::string message;
00342 
00343   };
00344 
00345   inline XSLTransformerException::XSLTransformerException() : message ("XSLTransformerException") {}
00346   inline XSLTransformerException::XSLTransformerException(const std::string& m) : message(m) {}
00347   inline XSLTransformerException::~XSLTransformerException() {}
00348   inline std::string XSLTransformerException::getMessage() const {
00349     return "XSLTransformerException : " + message;
00350   }
00351 
00361   class XSLTransformer {
00362   public:
00368     XSLTransformer();
00369 
00373     virtual ~XSLTransformer();
00374 
00384     XSLTransformer(const std::string& xsltPath);
00385 
00396     void setTransformation(const std::string& xsltPath);
00397 
00411     std::string operator()(const std::string& xmlPath);
00412     
00413   private:
00414     
00415     xsltStylesheetPtr cur;
00416 
00417     XSLTransformer& operator=(const XSLTransformer & rhs);
00418     XSLTransformer(const XSLTransformer & rsh);    
00419   }; // end class XSLTransformer.
00420 
00421 
00431   class CharComparator {
00432   public:
00433     CharComparator(std::ifstream * is_p = NULL, off_t limit = 0);
00434     bool operator() (char cl, char cr);
00435 
00436   private:
00437     std::ifstream* is_p;
00438     off_t limit;
00439     char* asdmDebug_p; 
00440   };
00441 
00454   class CharCompAccumulator {
00455   private:
00456     std::string*    accumulator_p;
00457     std::ifstream*  is_p;
00458     off_t           limit;
00459     int             nEqualChars;
00460     char*           asdmDebug_p; 
00461   public:
00469     CharCompAccumulator(std::string* accumulator_p = NULL, std::ifstream * is_p = NULL , off_t limit = 0);
00470 
00477     bool operator()(char cl, char cr);
00478   };
00479 } // end namespace asdm
00480 #endif  // MISC_H
00481