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