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>
40class AipsrcValue;
41template <class T>
42class AipsrcVector;
43class Aipsrc;
44
45// # Typedefs
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
218class Aipsrc {
219 public:
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, Int Nname, const String tname[]);
245 static Bool find(uInt &value, const String &keyword, const Vector<String> &tname);
246 // </group>
247 // This find usually saves you some lines of code, since you can supply the
248 // default you want to use when no such keyword is defined.
249 // If the return value is False, the keyword was not found and the default
250 // was used.
251 // <group>
252 static Bool find(String &value, const String &keyword, const String &default_value);
253 static Bool findNoHome(String &value, const String &keyword, const String &default_value);
254 static Bool find(uInt &value, const String &keyword, Int Nname, const String tname[],
255 const String &default_value);
256 static Bool find(uInt &value, const String &keyword, const Vector<String> &tname,
257 const String &default_value);
258 // </group>
259
260 // Sets foundDir to the first /firstPart/lastPart path that it finds
261 // present on the system, where /firstPart comes from, in order,
262 // this list:
263 // contents of prepends
264 // + useStd ? (., aipsHome(), aipsRoot()) : ()
265 // + contents of appends
266 static Bool findDir(String &foundDir, const String &lastPart = "",
267 const Vector<String> &prepends = Vector<String>(),
268 const Vector<String> &appends = Vector<String>(), Bool useStds = True);
269
270 // Functions to register keywords for later use in get() and set(). The
271 // returned value is the index for get() and set().
272 // <group>
273 static uInt registerRC(const String &keyword, const String &default_value);
274 static uInt registerRC(const String &keyword, Int Nname, const String tname[],
275 const String &default_value);
276 static uInt registerRC(const String &keyword, const Vector<String> &tname,
277 const String &default_value);
278 // </group>
279
280 // Gets are like find, but using registered integers rather than names.
281 // <group>
282 static const String &get(uInt keyword);
283 // get for code
284 static const uInt &get(uInt &code, uInt keyword);
285 // </group>
286
287 // Sets allow registered values to be set
288 // <group>
289 static void set(uInt keyword, const String &default_value);
290 static void set(uInt keyword, Int Nname, const String tname[], const String &default_value);
291 static void set(uInt keyword, const Vector<String> &tname, const String &default_value);
292 // </group>
293
294 // Save a registered keyword value to <src>$HOME/.aipsrc</src>
295 // <group>
296 static void save(uInt keyword);
297 static void save(uInt keyword, const String tname[]);
298 static void save(uInt keyword, const Vector<String> &tname);
299 // </group>
300
301 // Set an AIPSPATH that should be used in stead of a global AIPSPATH.
302 // This call should be made before any Aipsrc related call. The AIPSPATH
303 // will have up to 4 fields (which can all be empty) giving the root, host,
304 // site and arch directory that will be searched for possible
305 // <src>[.]aipsrc</src> files.
306 static void setAipsPath(const String &path = String());
307
308 // Returns the appropriate Casacore or system variable values
309 // <group>
310 static const String &aipsRoot();
311 static const String &aipsArch();
312 static const String &aipsSite();
313 static const String &aipsHost();
314 // Returns: <src>~/aips++</src>
315 static const String &aipsHome();
316 // </group>
317
318 // The <src>reRead()</src> function will reinitialise the static maps and read
319 // the aipsrc files again. It could be useful in some interactive circumstances.
320 // Note: Calling <src>reRead()</src> while using the static maps is not (thread-)safe.
321 // (Getting it right is a lot of work, but why apply settings while processing?)
322 // Note: casa_measures MeasTable.cc reads its <src>iau2000_reg</src> and
323 // <src>iau2000a_reg</src> upon first uses. Those cached values are not re-read,
324 // but only influence what <src>useIAU2000()</src> and <src>useIAU2000A()</src> return.
325 //
326 // <src>lastRead()</src> returns the time last reRead.
327 // <group>
328 static void reRead();
329 static Double lastRead();
330 // </group>
331
332 // The following functions return the full lists of available data. They could
333 // be useful for debugging purposes.
334 // <group>
335 static const Block<String> &values();
336 static const Block<String> &patterns();
337 // </group>
338
339 // The following <src>show()</src> function, useful for debugging, outputs
340 // all keyword/value pairs found
341 static void show(ostream &oStream);
342 // Prints all info on cout
343 static void show();
344 // The following set is a general set of functions
345 // <group>
346 // Read aipsrc type files (without wildcards), and return the unique names
347 // and values in the Vector arguments. The return value is number of names.
348 static uInt genRestore(Vector<String> &namlst, Vector<String> &vallst, const String &fileList);
349 // Save the names/values in file
350 static void genSave(Vector<String> &namlst, Vector<String> &vallst, const String &fnam);
351 // Set (new or overwrite) keyword/value pair
352 static void genSet(Vector<String> &namlst, Vector<String> &vallst, const String &nam,
353 const String &val);
354 // Remove a keyword from list (False if not in list)
355 static Bool genUnSet(Vector<String> &namlst, Vector<String> &vallst, const String &nam);
356 // Get the value of a keyword
357 static Bool genGet(String &val, Vector<String> &namlst, Vector<String> &vallst,
358 const String &nam);
359 // </group>
360
361 protected:
362 // Actual find function
363 static Bool find(String &value, const String &keyword, uInt start);
364 // Actual find function to use during parse() without recursing into parse()
365 static Bool findNoParse(String &value, const String &keyword, uInt start);
366 // The registration function
367 static uInt registerRC(const String &keyword, std::vector<String> &nlst);
368 // Actual saving
369 static void save(const String keyword, const String val);
370
371 private:
372 // # Data
373 // Object to ensure safe multi-threaded lazy single initialization
374 static std::once_flag theirCallOnceFlag;
375 // Last time data was (re)read
377 // List of values belonging to keywords found
379 // List of patterns deducted from names
381 // The start of the non-home values
382 static uInt fileEnd;
383 // The possibly set external AIPSPATH
385 // AIPSROOT
386 static String root;
387 // AIPSARCH
388 static String arch;
389 // AIPSSITE
390 static String site;
391 // AIPSHOST
392 static String host;
393 // AIPSHOME
394 static String home;
395 // HOME
396 static String uhome;
397 // Indicate above filled
398 static Bool filled;
399 // String register list
400 // <group>
401 static std::vector<String> string_values_;
402 static std::vector<String> string_names_;
403 static std::vector<uInt> coded_values_;
404 static std::vector<String> coded_names_;
405 // </group>
406
407 Aipsrc() = delete;
408 ~Aipsrc() = delete;
409
410 // # General member functions
411 // Read in the aipsrc files. Always called using theirCallOnce (except for reRead()).
412 // <group>
413 static void parse();
414 static void doParse(String &fileList);
415 // </group>
416
417 // The following parse function can be used for any list of files. It will
418 // return the list of Patterns and values found, and the last keyword number
419 // of first file in list.
421 const String &fileList);
422
423 // Locate the right keyword in the static maps
424 static Bool matchKeyword(uInt &where, const String &keyword, uInt start);
425 // Fill in root, arch, site, host and home
426 static void fillAips();
427};
428
429} // namespace casacore
430
431#endif
static std::vector< String > string_names_
Definition Aipsrc.h:402
static std::once_flag theirCallOnceFlag
Object to ensure safe multi-threaded lazy single initialization.
Definition Aipsrc.h:374
static Bool find(uInt &value, const String &keyword, Int Nname, const String tname[], const String &default_value)
static String arch
AIPSARCH.
Definition Aipsrc.h:388
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:384
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:390
static Block< String > keywordPattern
List of patterns deducted from names.
Definition Aipsrc.h:380
static void set(uInt keyword, const String &default_value)
Sets allow registered values to be set.
static String root
AIPSROOT.
Definition Aipsrc.h:386
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:401
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:404
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:382
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:396
static Block< String > keywordValue
List of values belonging to keywords found.
Definition Aipsrc.h:378
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:398
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:403
static String host
AIPSHOST.
Definition Aipsrc.h:392
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:376
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:394
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.
String: the storage and methods of handling collections of characters.
Definition String.h:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
AipsrcValue< Double > AipsrcDouble
Definition Aipsrc.h:46
AipsrcValue< Bool > AipsrcBool
Definition Aipsrc.h:48
AipsrcVector< Bool > AipsrcVBool
Definition Aipsrc.h:52
AipsrcValue< Int > AipsrcInt
Definition Aipsrc.h:47
unsigned int uInt
Definition aipstype.h:49
AipsrcVector< Int > AipsrcVInt
Definition Aipsrc.h:51
AipsrcVector< Double > AipsrcVDouble
Definition Aipsrc.h:50
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:360
double Double
Definition aipstype.h:53
AipsrcVector< String > AipsrcVString
Definition Aipsrc.h:53
Aipsrc AipsrcString
Definition Aipsrc.h:49