casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Aipsrc.h
Go to the documentation of this file.
1 //# Aipsrc.h: Class to read the casa general resource files
2 //# Copyright (C) 1995,1996,1997,1998,1999,2002,2004,2016
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef CASA_AIPSRC_H
29 #define CASA_AIPSRC_H
30 
31 #include <casacore/casa/aips.h>
35 #include <casacore/casa/OS/Mutex.h>
36 
37 namespace casacore { //# NAMESPACE CASACORE - BEGIN
38 
39 //# Forward declarations
40 template <class T> class AipsrcValue;
41 template <class T> class AipsrcVector;
42 class Aipsrc;
43 
44 //# Typedefs
53 
54 
55 // <summary> Class to read the casa general resource files </summary>
56 
57 // <use visibility=export>
58 
59 // <reviewed reviewer="wyoung" date="1996/11/25" tests="tAipsrc" demos="">
60 // </reviewed>
61 
62 // <prerequisite>
63 // <li> None
64 // </prerequisite>
65 //
66 // <etymology>
67 // A class for getting values from the casa resource files
68 // </etymology>
69 //
70 // <synopsis>
71 // The static Aipsrc class can get information from the casa resource files.
72 // It has the same functionality as getrc (c program used for Casacore
73 // installation scripts).<br>
74 // In addition it acts as a central clearing house between system and
75 // software by providing functionality to obtain Casacore system parameters
76 // (like AIPSPATH elements), and the possibility of storing system wide
77 // information provided by a class for reference by other classes. <br>
78 // The format of a line in a resource file is:
79 // <srcblock>
80 // # Line starting with an # in column 1 is a comment (as is an empty line)
81 // keyword: value
82 // keyword: value
83 // </srcblock>
84 // The keyword (starting at first non-blank)
85 // consists in general of keyword fields separated by periods:
86 //<srcblock>
87 // printer.ps.page
88 // measures.precession.d_interval
89 // measures.nutation.d_interval
90 // </srcblock>
91 // and, by preference, in lower case (but
92 // search is case sensitive) with an <src>_</src> as word-parts separator. <br>
93 // The keyword and value are separated by a <src>:</src>. The value is the string
94 // from the first non-whitespace character after the separator to the end of
95 // the line. Interpretation of the string is in general the program's
96 // responsibility, but special <src>find()</src> calls (see below) exist to
97 // aid.<br>
98 // Any part of the keyword string can be replaced by a wildcard <src>*</src>
99 // to indicate all values with that structure (e.g.
100 // <src>*.d_interval</src> would indicate in the example above both the
101 // precession and the nutation <src>d_interval</src>.<br>
102 // A match between a keyword to be found and a keyword in the resource files
103 // will be the first match (taking wildcards into account) encountered in the
104 // search through the resource files.
105 // The resource files to be looked at can be defined in the environment
106 // variable CASARCFILES. If undefined, the resource files searched are (in the
107 // given order):
108 // <srcblock>
109 // ~/.casarc
110 // ~/.casa/rc
111 // ~/.aipsrc
112 // $AIPSROOT/.aipsrc
113 // $AIPSHOST/aipsrc
114 // $AIPSSITE/aipsrc
115 // $AIPSARCH/aipsrc
116 // </srcblock>
117 // It is not an error for any of the aipsrc files to be absent or empty.
118 // However, it is an error if <em>HOME</em> has not been set:
119 // an exception will occur. AIPSPATH will in general be
120 // read from the global environment variables, but can, before any other
121 // <src>Aipsrc</src> related call, be set with the
122 // <src>setAipsPath()</src> call.<br>
123 // If AIPSPATH is not set in either way, it is set to the home directory.
124 // <p>
125 // The basic interaction with the class is with the static keyword match function
126 // <srcblock>Bool Aipsrc::find(String &result, const String &keyword)
127 // </srcblock>
128 // A set of
129 // <srcblock>Bool AipsrcValue::find(Type &result, const String &keyword, ...)
130 // </srcblock>
131 // are available to interpret the string value found.
132 // (see <linkto class="AipsrcValue">AipsrcValue</linkto>).<br>
133 // All the <src>find</src>
134 // functions have the ability to set a default if there is no match,
135 // while also unit conversion is possible.<br>
136 // The Bool return indicates if the keyword was found, and, in the case of the
137 // interpretative finds, if an 'important' format error was found (e.g.
138 // '+12a' will be accepted as a Double, with a result of '12', since the
139 // standard double conversion in <src>>></src> will produce this result.)
140 // <note role=caution> The search keyword (unlike the file keyword) has no
141 // wildcards. The real name should, of course, be looked for.</note>
142 // To aid in other places, the following (static) methods are available
143 // to get the requested information (derived from <src>HOME</src> and
144 // <src>AIPSPATH</src>, computer system information and/or aipsrc keywords):
145 // <ul>
146 // <li> const String &Aipsrc::aipsRoot()
147 // <li> const String &Aipsrc::aipsArch()
148 // <li> const String &Aipsrc::aipsSite()
149 // <li> const String &Aipsrc::aipsHost()
150 // <li> const String &Aipsrc::aipsHome()
151 // </ul>
152 // Other, numeric, system information can be found in
153 // <linkto class=AipsrcValue>AipsrcValue</linkto>.<br>
154 //
155 // Given an AIPSPATH of
156 // <srcblock>/epp/aips++ sun4sol_gnu epping norma</srcblock>
157 // aipsSite will return
158 // <srcblock>/epp/aips++/sun4sol_gnu/epping</srcblock>.
159 //
160 // The basic find above reacts with the aipsrc files available. If regular
161 // access is necessary (e.g. a lot of routines have to check independently a
162 // certain integration time limit), keywords can be <em>registered</em> to
163 // enable:
164 // <ul>
165 // <li> fast access with integer code, rather than string
166 // <li> ability to set values from programs if no aipsrc information given
167 // (a dynamic default)
168 // <li> update the <src>$HOME/.aipsrc</src> keyword/value list with save()
169 // </ul>
170 // <note role=tip> The registered value is never equal to zero, hence a zero
171 // value can be used to check if registration is done. Also, registering the
172 // same keyword twice is safe, and will produce the same value.</note>
173 // When saving a keyword/value pair in <src>$HOME/.aipsrc</src>, the old
174 // version is saved in <src>$HOME/.aipsrc.old</src>, before the keyword/value
175 // pair is prepended to the file. A limited number of edits of the same keyword
176 // is preserved only (default 5, changeable with the
177 // <src>user.aipsrc.edit.keep</src> keyword.
178 // </synopsis>
179 //
180 // <example>
181 // <srcblock>
182 // String printerPage; // result of keyword find
183 // if(!Aipsrc::find(printerPage, "printer.ps.page")) { // look for keyword match
184 // printerPage = "notSet";
185 // };
186 // </srcblock>
187 // A more convenient way of accomplishing the same result is:
188 // <srcblock>
189 // Aipsrc::find(printerPage, "printer.ps.page", "notSet");
190 // </srcblock>
191 // Here the final argument is the default to use if the keyword is not found
192 // at all.<br>
193 // If you often want to know, dynamically, the current 'printer.ps.page'
194 // value, you could do something like:
195 // <srcblock>
196 // static uInt pp = Aipsrc::registerRC("printer.ps.page", "noSet");
197 // String printerPage = Aipsrc::get(pp);
198 // // Processing, and maybe somewhere else:
199 // Aipsrc::set(pp, "nowSet");
200 // // ...
201 // printerPage = Aipsrc::get(pp);
202 // // and save it to the <src>$HOME/.aipsrc</src> list
203 // Aipsrc::save(pp);
204 // </srcblock>
205 // </example>
206 //
207 // <motivation>
208 // Programs need a way to interact with the aipsrc files.
209 // </motivation>
210 //
211 // <thrown>
212 // <li>AipsError if the environment variables HOME and/or AIPSPATH not set.
213 // </thrown>
214 //
215 // <todo asof="1997/08/07">
216 // </todo>
217 
218 class Aipsrc {
219 
220 public:
221  //# Constructors
222 
223  //# Destructor
224 
225  //# Copy assignment
226 
227  //# Member functions
228  // <thrown>
229  // <li> AipsError if HOME environment variable not set
230  // </thrown>
231  // The <src>find()</src> functions will, given a keyword, return the value
232  // with a matched keyword found in the files. If no match found the
233  // function will be False. The <src>findNoHome()</src> emulates the <src>-i</src>
234  // switch of getrc by bypassing the <src>~/.aipsrc</src> file.
235  // <group>
236  static Bool find(String &value, const String &keyword);
237  static Bool findNoHome(String &value, const String &keyword);
238  // </group>
239 
240  // These finds check a (possible) value of the keyword against a list
241  // of coded values provided, and return an index into the list (N if not
242  // found). Matching is minimax, case insensitive. Always better to use
243  // the one with default. return is False if no keyword or no match.
244  // <group>
245  static Bool find(uInt &value, const String &keyword,
246  Int Nname, const String tname[]);
247  static Bool find(uInt &value, const String &keyword,
248  const Vector<String> &tname);
249  // </group>
250  // This find usually saves you some lines of code, since you can supply the
251  // default you want to use when no such keyword is defined.
252  // If the return value is False, the keyword was not found and the default
253  // was used.
254  // <group>
255  static Bool find(String &value, const String &keyword,
256  const String &deflt);
257  static Bool findNoHome(String &value, const String &keyword,
258  const String &deflt);
259  static Bool find(uInt &value, const String &keyword,
260  Int Nname, const String tname[], const String &deflt);
261  static Bool find(uInt &value, const String &keyword,
262  const Vector<String> &tname, const String &deflt);
263  // </group>
264 
265  // Sets foundDir to the first /firstPart/lastPart path that it finds
266  // present on the system, where /firstPart comes from, in order,
267  // this list:
268  // contents of prepends
269  // + useStd ? (., aipsHome(), aipsRoot()) : ()
270  // + contents of appends
271  static Bool findDir(String& foundDir, const String& lastPart="",
272  const Vector<String>& prepends=Vector<String>(),
273  const Vector<String>& appends=Vector<String>(),
274  Bool useStds=True);
275 
276  // Functions to register keywords for later use in get() and set(). The
277  // returned value is the index for get() and set().
278  // <group>
279  static uInt registerRC(const String &keyword,
280  const String &deflt);
281  static uInt registerRC(const String &keyword,
282  Int Nname, const String tname[], const String &deflt);
283  static uInt registerRC(const String &keyword,
284  const Vector<String> &tname, const String &deflt);
285  // </group>
286 
287  // Gets are like find, but using registered integers rather than names.
288  // <group>
289  static const String &get(uInt keyword);
290  // get for code
291  static const uInt &get(uInt &code, uInt keyword);
292  // </group>
293 
294  // Sets allow registered values to be set
295  // <group>
296  static void set(uInt keyword, const String &deflt);
297  static void set(uInt keyword,
298  Int Nname, const String tname[], const String &deflt);
299  static void set(uInt keyword,
300  const Vector<String> &tname, const String &deflt);
301  // </group>
302 
303  // Save a registered keyword value to <src>$HOME/.aipsrc</src>
304  // <group>
305  static void save(uInt keyword);
306  static void save(uInt keyword, const String tname[]);
307  static void save(uInt keyword, const Vector<String> &tname);
308  // </group>
309 
310  // Set an AIPSPATH that should be used in stead of a global AIPSPATH.
311  // This call should be made before any Aipsrc related call. The AIPSPATH
312  // will have up to 4 fields (which can all be empty) giving the root, host,
313  // site and arch directory that will be searched for possible
314  // <src>[.]aipsrc</src> files.
315  static void setAipsPath(const String &path = String());
316 
317  // Returns the appropriate Casacore or system variable values
318  // <group>
319  static const String &aipsRoot();
320  static const String &aipsArch();
321  static const String &aipsSite();
322  static const String &aipsHost();
323  // Returns: <src>~/aips++</src>
324  static const String &aipsHome();
325  // </group>
326 
327  // The <src>reRead()</src> function will reinitialise the static maps and read
328  // the aipsrc files again. It could be useful in some interactive circumstances.
329  // Note: Calling <src>reRead()</src> while using the static maps is not (thread-)safe.
330  // (Getting it right is a lot of work, but why apply settings while processing?)
331  // Note: casa_measures MeasTable.cc reads its <src>iau2000_reg</src> and
332  // <src>iau2000a_reg</src> upon first uses. Those cached values are not re-read,
333  // but only influence what <src>useIAU2000()</src> and <src>useIAU2000A()</src> return.
334  //
335  // <src>lastRead()</src> returns the time last reRead.
336  // <group>
337  static void reRead();
338  static Double lastRead();
339  // </group>
340 
341 
342  // The following functions return the full lists of available data. They could
343  // be useful for debugging purposes.
344  // <group>
345  static const Block<String> &values();
346  static const Block<String> &patterns();
347  // </group>
348 
349  // The following <src>show()</src> function, useful for debugging, outputs
350  // all keyword/value pairs found
351  static void show(ostream &oStream);
352  // Prints all info on cout
353  static void show();
354  // The following set is a general set of functions
355  // <group>
356  // Read aipsrc type files (without wildcards), and return the unique names
357  // and values in the Vector arguments. The return value is number of names.
358  static uInt genRestore(Vector<String> &namlst, Vector<String> &vallst,
359  const String &fileList);
360  // Save the names/values in file
361  static void genSave(Vector<String> &namlst, Vector<String> &vallst,
362  const String &fnam);
363  // Set (new or overwrite) keyword/value pair
364  static void genSet(Vector<String> &namlst, Vector<String> &vallst,
365  const String &nam, const String &val);
366  // Remove a keyword from list (False if not in list)
367  static Bool genUnSet(Vector<String> &namlst, Vector<String> &vallst,
368  const String &nam);
369  // Get the value of a keyword
370  static Bool genGet(String &val, Vector<String> &namlst, Vector<String> &vallst,
371  const String &nam);
372  // </group>
373 
374 protected:
375  // Actual find function
376  static Bool find(String &value, const String &keyword, uInt start);
377  // Actual find function to use during parse() without recursing into parse()
378  static Bool findNoParse(String &value, const String &keyword, uInt start);
379  // The registration function
380  static uInt registerRC(const String &keyword, Block<String> &nlst);
381  // Actual saving
382  static void save(const String keyword, const String val);
383 
384 private:
385  //# Data
386  // Object to ensure safe multi-threaded lazy single initialization
388  // Last time data was (re)read
389  static Double lastParse;
390  // List of values belonging to keywords found
392  // List of patterns deducted from names
394  // The start of the non-home values
395  static uInt fileEnd;
396  // The possibly set external AIPSPATH
398  // AIPSROOT
399  static String root;
400  // AIPSARCH
401  static String arch;
402  // AIPSSITE
403  static String site;
404  // AIPSHOST
405  static String host;
406  // AIPSHOME
407  static String home;
408  // HOME
409  static String uhome;
410  // Indicate above filled
411  static Bool filled;
412  // String register list
413  // <group>
418  // </group>
419 
420  //# General member functions
421  // Read in the aipsrc files. Always called using theirCallOnce (except for reRead()).
422  // <group>
423  static void parse();
424  static void doParse(String &fileList);
425  // </group>
426 
427  // The following parse function can be used for any list of files. It will
428  // return the list of Patterns and values found, and the last keyword number
429  // of first file in list.
432  uInt &fileEnd, const String &fileList);
433 
434  // Locate the right keyword in the static maps
435  static Bool matchKeyword(uInt &where, const String &keyword,
436  uInt start);
437  // Fill in root, arch, site, host and home
438  static void fillAips();
439 };
440 
441 
442 } //# NAMESPACE CASACORE - END
443 
444 #endif
445 
446 
Wrapper around std::call_once.
Definition: Mutex.h:214
AipsrcVector< Double > AipsrcVDouble
Definition: Aipsrc.h:49
static void reRead()
The reRead() function will reinitialise the static maps and read the aipsrc files again...
Specialization of AipsrcValue for Bool.
Definition: AipsrcValue.h:251
static const String & aipsHome()
Returns: ~/aips++
AipsrcValue< Double > AipsrcDouble
Definition: Aipsrc.h:42
static Double lastParse
Last time data was (re)read.
Definition: Aipsrc.h:389
A 1-D Specialization of the Array class.
int Int
Definition: aipstype.h:50
static Double lastRead()
static String extAipsPath
The possibly set external AIPSPATH.
Definition: Aipsrc.h:397
static Bool genGet(String &val, Vector< String > &namlst, Vector< String > &vallst, const String &nam)
Get the value of a keyword.
AipsrcValue< Bool > AipsrcBool
Definition: Aipsrc.h:47
static void parse()
Read in the aipsrc files.
Read multiple values from the Aipsrc resource files.
Definition: Aipsrc.h:41
static Bool findNoHome(String &value, const String &keyword)
static const String & aipsHost()
static void genSave(Vector< String > &namlst, Vector< String > &vallst, const String &fnam)
Save the names/values in file.
static Block< String > strlst
String register list.
Definition: Aipsrc.h:414
static uInt genParse(Block< String > &keywordPattern, Block< String > &keywordValue, uInt &fileEnd, const String &fileList)
The following parse function can be used for any list of files.
static const String & aipsArch()
static Bool matchKeyword(uInt &where, const String &keyword, uInt start)
Locate the right keyword in the static maps.
static uInt fileEnd
The start of the non-home values.
Definition: Aipsrc.h:395
static Block< String > keywordValue
List of values belonging to keywords found.
Definition: Aipsrc.h:391
static const Block< String > & patterns()
static Block< String > keywordPattern
List of patterns deducted from names.
Definition: Aipsrc.h:393
AipsrcVector< Int > AipsrcVInt
Definition: Aipsrc.h:50
static Bool findDir(String &foundDir, const String &lastPart="", const Vector< String > &prepends=Vector< String >(), const Vector< String > &appends=Vector< String >(), Bool useStds=True)
Sets foundDir to the first /firstPart/lastPart path that it finds present on the system, where /firstPart comes from, in order, this list: contents of prepends.
static const Block< String > & values()
The following functions return the full lists of available data.
static String home
AIPSHOME.
Definition: Aipsrc.h:407
static void show()
Prints all info on cout.
static Block< uInt > codlst
Definition: Aipsrc.h:416
static const String & aipsRoot()
Returns the appropriate Casacore or system variable values.
static String site
AIPSSITE.
Definition: Aipsrc.h:403
double Double
Definition: aipstype.h:55
std::string path(const std::string &name)
static const String & aipsSite()
static Bool genUnSet(Vector< String > &namlst, Vector< String > &vallst, const String &nam)
Remove a keyword from list (False if not in list)
static CallOnce0 theirCallOnce
Object to ensure safe multi-threaded lazy single initialization.
Definition: Aipsrc.h:387
static void fillAips()
Fill in root, arch, site, host and home.
static Bool find(String &value, const String &keyword)
static Block< String > ncodlst
Definition: Aipsrc.h:417
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
Aipsrc AipsrcString
Definition: Aipsrc.h:48
AipsrcValue< Int > AipsrcInt
Definition: Aipsrc.h:46
static void save(uInt keyword)
Save a registered keyword value to $HOME/.aipsrc
simple 1-D array
static Block< String > nstrlst
Definition: Aipsrc.h:415
Class to read values from the Aipsrc general resource files.
Definition: Aipsrc.h:40
static String arch
AIPSARCH.
Definition: Aipsrc.h:401
Class to read the casa general resource files.
Definition: Aipsrc.h:218
static Bool findNoParse(String &value, const String &keyword, uInt start)
Actual find function to use during parse() without recursing into parse()
static void set(uInt keyword, const String &deflt)
Sets allow registered values to be set.
static void setAipsPath(const String &path=String())
Set an AIPSPATH that should be used in stead of a global AIPSPATH.
String: the storage and methods of handling collections of characters.
Definition: String.h:223
static String host
AIPSHOST.
Definition: Aipsrc.h:405
static void doParse(String &fileList)
AipsrcVector< Bool > AipsrcVBool
Definition: Aipsrc.h:51
static String uhome
HOME.
Definition: Aipsrc.h:409
static uInt registerRC(const String &keyword, const String &deflt)
Functions to register keywords for later use in get() and set().
static void genSet(Vector< String > &namlst, Vector< String > &vallst, const String &nam, const String &val)
Set (new or overwrite) keyword/value pair.
static String root
AIPSROOT.
Definition: Aipsrc.h:399
AipsrcVector< String > AipsrcVString
Definition: Aipsrc.h:52
const Bool True
Definition: aipstype.h:43
static uInt genRestore(Vector< String > &namlst, Vector< String > &vallst, const String &fileList)
The following set is a general set of functions.
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
static Bool filled
Indicate above filled.
Definition: Aipsrc.h:411
unsigned int uInt
Definition: aipstype.h:51
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42