casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Misc.h
Go to the documentation of this file.
1 #ifndef MISC_H
2 #define MISC_H
3 /*
4  * ALMA - Atacama Large Millimeter Array
5  * (c) European Southern Observatory, 2002
6  * (c) Associated Universities Inc., 2002
7  * Copyright by ESO (in the framework of the ALMA collaboration),
8  * Copyright by AUI (in the framework of the ALMA collaboration),
9  * All rights reserved.
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free software Foundation; either
14  * version 2.1 of the License, or (at your option) any later verson.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY, without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24  * MA 02111-1307 USA
25  *
26  *
27  * File Misc.h
28  */
29 
30 #include <iostream>
31 #include <fstream>
32 #include <string>
33 #include <vector>
34 #include <set>
35 #include <map>
36 
37 #ifndef WITHOUT_BOOST
38 #include <boost/filesystem/path.hpp>
39 #include <boost/filesystem/convenience.hpp>
40 #include <boost/algorithm/string/trim.hpp>
41 #include <boost/algorithm/string/predicate.hpp>
42 #include <boost/algorithm/string/split.hpp>
43 #else
44 #include <sys/stat.h>
45 #include <sstream>
46 #endif
47 
48 #include <algorithm>
49 #include <cctype>
50 #include <locale>
51 
52 typedef unsigned char xmlChar;
53 
54 struct _xmlNode;
55 typedef struct _xmlNode xmlNode;
57 
58 struct _xmlDoc;
59 typedef struct _xmlDoc xmlDoc;
60 typedef xmlDoc *xmlDocPtr;
61 
62 struct _xsltStylesheet;
63 typedef struct _xsltStylesheet xsltStylesheet;
65 
67 
68 namespace asdm {
83  bool directoryExists(const char* dir);
84 
93  bool createDirectory(const char* dir);
94 
102  bool createPath(const char* path);
103 
104 
108 #define ByteSwap5(x) ByteSwap((unsigned char *) &x,sizeof(x))
109  void ByteSwap(unsigned char * b, int n);
110 
115  class ByteOrder {
116  public:
117  static const ByteOrder* Little_Endian; /*< A unique object to represent a little endian byte order. */
118  static const ByteOrder* Big_Endian; /*< A unique object to represent a big endian byte order. */
119  static const ByteOrder* Machine_Endianity; /*< A unique object storing the endianity of the machine. */
120 
129  std::string toString() const ;
130 
138  static const ByteOrder* fromString(const std::string & s);
139 
140  private:
141  std::string name_;
142 
143  ByteOrder(const std::string & name);
144  virtual ~ByteOrder();
145  static const ByteOrder* machineEndianity();
146  };
147 
155  std::string uniqSlashes(const std::string& s);
156 
157 
158 #ifdef WITHOUT_BOOST
159  // string trimming functions to be used in place of boost functions
160  // uses lambdas (c++11)
161 
162  // trim from start (in place)
163  inline void ltrim(std::string &s) {
164  s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
165  return !std::isspace(ch);
166  }));
167  }
168 
169  // trim from end (in place)
170  inline void rtrim(std::string &s) {
171  s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) {
172  return !std::isspace(ch);
173  }).base(), s.end());
174  }
175 
176  // trim from both ends (in place)
177  inline void trim(std::string &s) {
178  ltrim(s);
179  rtrim(s);
180  }
181 
182  // trim from start (copying)
183  inline std::string ltrim_copy(std::string s) {
184  ltrim(s);
185  return s;
186  }
187 
188  // trim from end (copying)
189  inline std::string rtrim_copy(std::string s) {
190  rtrim(s);
191  return s;
192  }
193 
194  // trim from both ends (copying)
195  inline std::string trim_copy(std::string s) {
196  trim(s);
197  return s;
198  }
199 
200  // return a copy of str with everything transformed to upper case
201  inline std::string str_toupper(std::string s) {
202  std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::toupper(c); });
203  return s;
204  }
205 
206  // return a copy of str with everything transformed to lower case
207  inline std::string str_tolower(std::string s) {
208  std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); });
209  return s;
210  }
211 
212  // split a string into vector using provided character as the delimiter
213  // resuls are added to the pre-existing results vector
214  inline void strsplit(const std::string &str, char delim, std::vector<std::string> &result) {
215  std::stringstream ss(str);
216  std::string token;
217  while(std::getline(ss,token,delim)) {
218  result.push_back(token);
219  }
220  }
221 
222  // check to see if a file exists using posix 'stat(...)'
223  inline bool file_exists(const std::string &filename) {
224  struct stat statbuf;
225  return (stat(filename.c_str(),&statbuf)==0);
226  }
227 
228 #endif
229 
231  public:
233  ASDMUtilsException(const std::string& message);
234 
235  const std::string& getMessage();
236 
237  private:
238  std::string message;
239  };
240 
241  class ASDMUtils {
242  public :
243  enum Origin { UNKNOWN, ALMA, EVLA };
244  static std::string version (const std::string& asdmPath);
245  static std::vector<std::string> telescopeNames(const std::string& asdmPath);
246  static Origin origin(const std::vector<std::string>& telescopeNames);
247  static std::vector<std::string> xmlFilenames(const std::string& asdmPath);
248 
249  static std::string pathToV2V3ALMAxslTransform() ;
250  static std::string pathToV2V3EVLAxslTransform() ;
251  static std::string nameOfV2V3xslTransform(ASDMUtils::Origin origin);
252 
253 #ifndef WITHOUT_BOOST
254  struct DotXMLFilter {
255  public:
256  DotXMLFilter(std::vector<std::string>& filenames);
257  //void operator() (path& p);
258  void operator() ( boost::filesystem::directory_entry& p);
259  private:
260  std::vector<std::string>* filenames;
261  };
262 #endif
263 
264  private :
265  static bool initialize();
266  static bool initialized;
267  static bool hasChild(xmlDocPtr, xmlNodePtr node, const xmlChar* childName);
268  static std::string parseRow(xmlDocPtr, xmlNodePtr node, const xmlChar* childName);
269  static std::set<std::string> evlaValidNames;
270  static std::set<std::string> almaValidNames;
271  static std::map<ASDMUtils::Origin, std::string> filenameOfV2V3xslTransform;
272  static std::map<std::string, std::string> rootSubdir ;
273 
274  static std::string pathToxslTransform( const std::string& xsltFilename);
275  }; // end class ASDMUtil.
276 
277 
304  friend class ASDM;
305 
306  public:
319 
324 
329  virtual ~ASDMParseOptions();
330 
335 
336 
341 
346 
351 
356 
361 
369 
377 
381  friend std::ostream& operator<<(std::ostream& output, const ASDMParseOptions& p);
382 
383  std::string toString() const;
384 
385  private:
388  std::string version_;
392  }; // end class ASDMParseOptions.
393 
399 
400  public:
405 
410  XSLTransformerException(const std::string & m);
411 
415  virtual ~XSLTransformerException();
416 
421  std::string getMessage() const;
422 
423  protected:
424  std::string message;
425 
426  };
427 
428  inline XSLTransformerException::XSLTransformerException() : message ("XSLTransformerException") {}
429  inline XSLTransformerException::XSLTransformerException(const std::string& m) : message(m) {}
431  inline std::string XSLTransformerException::getMessage() const {
432  return "XSLTransformerException : " + message;
433  }
434 
445  public:
451  XSLTransformer();
452 
456  virtual ~XSLTransformer();
457 
467  XSLTransformer(const std::string& xsltPath);
468 
479  void setTransformation(const std::string& xsltPath);
480 
494  std::string operator()(const std::string& xmlPath);
495 
496  private:
497 
499 
501  XSLTransformer(const XSLTransformer & rsh);
502  }; // end class XSLTransformer.
503 
504 
515  public:
516  CharComparator(std::ifstream * is_p = NULL, off_t limit = 0);
517  bool operator() (char cl, char cr);
518 
519  private:
520  std::ifstream* is_p;
521  off_t limit;
522  char* asdmDebug_p;
523  };
524 
538  private:
539  std::string* accumulator_p;
540  std::ifstream* is_p;
541  off_t limit;
543  char* asdmDebug_p;
544  public:
552  CharCompAccumulator(std::string* accumulator_p = NULL, std::ifstream * is_p = NULL , off_t limit = 0);
553 
560  bool operator()(char cl, char cr);
561  };
562 } // end namespace asdm
563 #endif // MISC_H
564 
ASDMParseOptions & asALMA()
This is an ALMA dataset.
std::string toString() const
static const ByteOrder * Machine_Endianity
Definition: Misc.h:119
static std::string version(const std::string &asdmPath)
A class to define a collection of options regarding the way to consider an ASDM dataset especially wh...
Definition: Misc.h:303
std::string message
Definition: Misc.h:238
virtual ~XSLTransformerException()
The destructor.
Definition: Misc.h:430
static std::set< std::string > almaValidNames
Definition: Misc.h:270
static std::string pathToV2V3ALMAxslTransform()
Functor class of for a comparison between a &quot;given&quot; character and a characted assumed to be read in a...
Definition: Misc.h:537
struct _xmlDoc xmlDoc
Definition: Misc.h:59
static std::string nameOfV2V3xslTransform(ASDMUtils::Origin origin)
std::string toString() const
Returns a string representation of this.
Functor class of for a comparison between a &quot;given&quot; character and a characted assumed to be read from...
Definition: Misc.h:514
A class to represent byte order information.
Definition: Misc.h:115
static const ByteOrder * Little_Endian
Definition: Misc.h:117
A class to represent an exception thrown while an XSL transformation is applied by an instance of XSL...
Definition: Misc.h:398
xmlDoc * xmlDocPtr
Definition: Misc.h:60
void setTransformation(const std::string &xsltPath)
A setter to (re) define the XSL transformation associated with the instance of XSLTransformer.
XSLTransformerException()
An empty constructor.
Definition: Misc.h:428
std::string name_
Definition: Misc.h:141
TableExprNode ltrim(const TableExprNode &node)
Definition: ExprNode.h:1545
std::string getMessage() const
Return the message associated to this exception.
Definition: Misc.h:431
XSLTransformer & operator=(const XSLTransformer &rhs)
The ASDM class is the container for all tables.
Definition: ASDM.h:273
unsigned char xmlChar
Definition: Misc.h:52
DotXMLFilter(std::vector< std::string > &filenames)
std::ifstream * is_p
Definition: Misc.h:540
static Origin origin(const std::vector< std::string > &telescopeNames)
A class to apply XSL transformations on an XML document.
Definition: Misc.h:444
std::string operator()(const std::string &xmlPath)
Overloads operator() so that the instance can be used as a functor to apply the transformation on a g...
ABSTRACT CLASSES Abstract class for colors Any implementation of color should be able to provide a hexadecimal form of the if a human readable name(i.e."black").In many places throughout the plotter
ASDMParseOptions & checkRowUniqueness(bool b)
Determines if a control of the uniqueness is performed (b==true) or not (b==false) for each row of ea...
ASDMParseOptions()
A null constructor.
CharComparator(std::ifstream *is_p=NULL, off_t limit=0)
ASDMParseOptions & asEVLA()
This is an EVLA dataset.
ASDMParseOptions & loadTablesOnDemand(bool b)
Load all the tables in memory or only those which are required by the code.
std::string * accumulator_p
Definition: Misc.h:539
TableExprNode rtrim(const TableExprNode &node)
Definition: ExprNode.h:1549
virtual ~ByteOrder()
std::string version_
Definition: Misc.h:388
virtual ~XSLTransformer()
The destructor.
static const ByteOrder * fromString(const std::string &s)
Convert a string to a const ByteOrder*.
static const ByteOrder * machineEndianity()
ASDMParseOptions & operator=(const ASDMParseOptions &rhs)
= operator.
static std::map< ASDMUtils::Origin, std::string > filenameOfV2V3xslTransform
Definition: Misc.h:271
std::string path(const std::string &name)
TableExprNode trim(const TableExprNode &node)
Definition: ExprNode.h:1541
static bool initialized
Definition: Misc.h:266
friend std::ostream & operator<<(std::ostream &output, const ASDMParseOptions &p)
Defines how an instance of ASDMParseOptions is output on an ostream.
static std::vector< std::string > telescopeNames(const std::string &asdmPath)
bool loadTablesOnDemand_
Definition: Misc.h:390
bool operator()(char cl, char cr)
bool createDirectory(const char *dir)
Creates the specified directory.
bool checkRowUniqueness_
Definition: Misc.h:391
xsltStylesheetPtr cur
Definition: Misc.h:498
ByteOrder(const std::string &name)
static std::string pathToV2V3EVLAxslTransform()
void ByteSwap(unsigned char *b, int n)
static bool hasChild(xmlDocPtr, xmlNodePtr node, const xmlChar *childName)
ASDMParseOptions & asIRAM_PDB()
This is an IRAM / Plateau de Bure dataset.
ASDMUtils::Origin origin_
Definition: Misc.h:386
static std::vector< std::string > xmlFilenames(const std::string &asdmPath)
bool operator()(char cl, char cr)
Returns true when tpupper(cl) is equal to cr or when the read head position in is_p is &gt;= limit...
struct _xmlNode xmlNode
Definition: Misc.h:55
bool createPath(const char *path)
Creates a complete path.
static std::map< std::string, std::string > rootSubdir
Definition: Misc.h:272
bool directoryExists(const char *dir)
Some utility methods to manipulate directories.
XSLTransformer()
The empty constructor.
const std::string & getMessage()
static std::string parseRow(xmlDocPtr, xmlNodePtr node, const xmlChar *childName)
std::vector< std::string > * filenames
Definition: Misc.h:260
const Double c
Fundamental physical constants (SI units):
CharCompAccumulator(std::string *accumulator_p=NULL, std::ifstream *is_p=NULL, off_t limit=0)
The constructor.
xsltStylesheet * xsltStylesheetPtr
Definition: Misc.h:64
ASDMParseOptions & asV3()
This is a V3 dataset.
void operator()(boost::filesystem::directory_entry &p)
void operator() (path&amp; p);
static const ByteOrder * Big_Endian
Definition: Misc.h:118
int xmlLoadExtDtdDefaultValue
Definition: Misc.h:66
char * asdmDebug_p
Definition: Misc.h:522
static std::string pathToxslTransform(const std::string &xsltFilename)
virtual ~ASDMParseOptions()
The destructor.
static std::set< std::string > evlaValidNames
Definition: Misc.h:269
static bool initialize()
std::ifstream * is_p
Definition: Misc.h:520
ASDMParseOptions & asV2()
This is a V2 dataset.
std::string uniqSlashes(const std::string &s)
Return a string whose content is equal to the content of s but with all the repetitions of &#39;/&#39; charac...
xmlNode * xmlNodePtr
Definition: Misc.h:56
struct _xsltStylesheet xsltStylesheet
Definition: Misc.h:63
transform(a.begin(), a.end(), std::ostream_iterator< int >(cout,"\n"), compose(unary(h), compose(unary(f), unary(f))))
Global Functions.