casacore
Loading...
Searching...
No Matches
MUString.h
Go to the documentation of this file.
1//# MUString.h: Pointed String class to aid analysis of quantity strings
2//# Copyright (C) 1996,1997,1999,2000,2001
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_MUSTRING_H
27#define CASA_MUSTRING_H
28
29
30//# Includes
31#include <casacore/casa/aips.h>
32#include <casacore/casa/Arrays/ArrayFwd.h>
33#include <casacore/casa/BasicSL/String.h>
34#include <casacore/casa/Containers/Block.h>
35
36//# Forward Declarations
37#include <casacore/casa/iosfwd.h>
38namespace casacore { //# NAMESPACE CASACORE - BEGIN
39
40class Regex;
41
42// <summary>
43// Pointed String class to aid analysis of quantity strings
44// </summary>
45// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tMeasure" demos="">
46// </reviewed>
47
48// <prerequisite>
49// <li> <linkto classString>String</linkto>
50// </prerequisite>
51//
52// <etymology>
53// From Measure Utility String
54// </etymology>
55//
56// <synopsis>
57// The MUString is a class with a String and an embedded pointer. It can be used
58// to linearly analyse a string for its semantics. Imagine for instance a
59// string that represents an angle. It could be formatted as
60// <src>[+-]hh:mm:ss.ttt</src>
61// or as <src>[+-]hh[hH]mm[mM]</src> or as
62// <src>[+-]dd.mm.ss.ttt</src> or with <src>.'s</src> replaced with
63// <src>dms</src> or as <src>[+-]ddd.fff deg</src> etc.<br>
64// The available methods aid in analysing this string (see example).<br>
65// The following analysis method classes are avaible:
66// <ul>
67// <li> construct -- all constructors create a string with the pointer
68// starting at the beginning of the string
69// <li> testX(arg) -- all test methods test if the next available
70// character(s) fulfill the specified argument test. E.g.
71// <src>Bool testSign()</src> test if current character is + or -.
72// If at end of string; False is returned, except for
73// <src>testBlank()</src>. No pointer update. Any method with
74// <em>NC</em> at the end (for no-case) will test irrespective
75// of the case.
76// <li> skipX(arg) -- all skip methods skip all available character(s)
77// fulfilling the specified argument. E.g.
78// <src>void skipSign()</src> will skip all + and - found at
79// the current and subsequent positions. Pointer updated.
80// <li> tSkipX(arg) -- will skip as skipX, and return Bool as in testX.
81// Pointer updated
82// <li> getX(arg) -- will get the indicated X value from the string.
83// Pointer updated. A get will always return a valid result.
84// However, if the value did not exist (e.g.
85// <src>Double getDouble()</src> form a string like <src>"abc"</src>
86// will return 0.0) a False status will be saved. It can be
87// interrogated by the <src>Bool status()</src> function.
88// The string part used in producing the value is also
89// saved, and can be obtained with
90// <src>const String &lastGet()</src>.
91// No saving in case of a simple getChar() is done.
92// <li> stack -- if it is necessary to save the current position of the
93// pointer (for maybe later restoration) a <src>void push()</src>
94// and <src>void pop()</src> are available
95// <li> pointer -- the pointer can be manipulated with <src>void setPtr()</src>
96// and <src>Int getPtr()</src>. Pointers are always protected in
97// their value.
98// </ul>
99// The following types (<em>X</em> in the above list) are available
100// <ul>
101// <li> Char -- a single character
102// <li> CharNC -- a single character with no case
103// <li> String -- a string
104// <li> StringNC -- a string without case
105// <li> Alpha -- a through z and A through Z and _ (underscore)
106// <li> Num -- digits 0 through 9
107// <li> AlphaNum -- a field staring with Alpha, and remainder (if any)
108// Alpha or Num
109// <li> Sign -- a plus or minus
110// <li> Blank -- a space ar a tab
111// <li> uInt -- unsigned integer
112// <li> Int -- an optionally signed integer
113// <li> Double -- a double value
114// </ul>
115// General string aids are available. The main one a minimax, caseless
116// check of an input String against a vector:
117// <src>static uInt minimaxNC(String in, Int N_name, String name[])</src>
118// and its vector equivalent:
119// <src>static uInt minimaxNC(String in, Vector<String> name)</src>.
120// Success is indicated by a return value less than N_name or the
121// vector length.
122// </synopsis>
123//
124// <example>
125// See <linkto class=MVAngle>MVAngle</linkto> class for example background.
126// The following example is the conversion of different input angle formats
127// to a <linkto class=Quantum>Quantity</linkto>. A full blown example,
128// but gives some idea of intricacies involved.
129// <srcblock>
130// res = Quantity(0.0, "rad"); // result
131// MUString tmp(in); // Pointed non-const String
132// tmp.skipBlank();
133// Double s = tmp.getSign(); // sign
134// tmp.push(); // Save position to rescan
135// Double r = tmp.getuInt(); // first field
136// Int tp = 0; // distributor
137// if (tmp.tSkipChar('.')) { // if more than one ., dms format
138// Double r1 = tmp.getuInt();
139// if (tmp.tSkipChar('.')) {
140// r += r1/60.0 + tmp.getDouble()/3600.0;
141// tp = 4;
142// } else { // else value with units
143// tmp.pop(); // Reset position
144// r = tmp.getDouble();
145// };
146// } else if (tmp.tSkipCharNC('d')) { // dms
147// tp = 1;
148// } else if (tmp.tSkipCharNC('h')) { // hms
149// tp = 2;
150// } else if (tmp.tSkipChar(':')) { // hms
151// tp = 3;
152// };
153// switch (tp) {
154// case 0: {
155// UnitVal u; String us;
156// if (!MVAngle::unitString(u,us,tmp)) return False;
157// r *= s;
158// if (u == UnitVal::NODIM) { // check correct dimension
159// res = Quantity(r,"rad");
160// return True;
161// };
162// if (u == UnitVal::ANGLE) {
163// res = Quantity(r,us);
164// return True;
165// };
166// if (u == UnitVal::TIME) {
167// res = Quantity(Quantity(r/240.,us).getBaseValue(), "deg");
168// return True;
169// };
170// return False;
171// };
172// break;
173//
174// case 1:
175// case 2:
176// case 3: { // get remainder od ms and hms formats
177// Char tc = 'm';
178// if (tp == 3) tc = ':';
179// tmp.push();
180// Double r1 = tmp.getuInt();
181// if (tmp.tSkipChar('.')) {
182// tmp.pop();
183// r += tmp.getDouble()/3600.;
184// } else if (tmp.tSkipCharNC(tc)) {
185// r += r1/60.0 + tmp.getDouble()/3600.;
186// } else {
187// r += r1/3600.0;
188// };
189// r *= s;
190// };
191// break;
192//
193// default:
194// break;
195// };
196//
197// switch (tp) { // make correct units
198//
199// case 1:
200// case 4:
201// res = Quantity(r,"deg");
202// break;
203//
204// case 2:
205// case 3:
206// res = Quantity(Quantity(r/240.,"h").getBaseValue(), "deg");
207// break;
208//
209// default:
210// break;
211//
212// };
213// return True;
214// </srcblock>
215// </example>
216//
217// <motivation>
218// The class was written to be able to analyse an input string for its
219// <linkto class=Quantum>Quantum</linkto> representation as value with
220// units, or os a date/time or as an angle.
221// </motivation>
222//
223// <todo asof="1996/11/14">
224// <li> nothing I know of
225// </todo>
226
228{
229public:
230
231//# Friends
232 // Output String starting at pointer
233 friend ostream &operator<<(ostream &os, const MUString &in);
234//# Enumerations
235
236//# Constructors
237 // Default constructor creates an empty string
239 // Create from String; setting pointer at start
240 // <group>
241 MUString(const String &in);
242 MUString(const Char *in);
243 MUString(char in);
244 // </group>
245 // Copy constructor; new pointer will be same as old
246 MUString(const MUString &other);
247 // Copy assignment; new pointer will be same as old
249
250 // Destructor
252
253//# Operators
254 // Obtain remaining string (same as <src>get()</src>).
256
257//# General Member Functions
258 // Save current pointer on internal stack
259 void push();
260 // Restore pointer from stack (or set to start if stack empty)
261 void pop();
262 // Restore stack for one level
263 void unpush();
264
265 // Act on whitespace; adjusting pointer if skip
266 // <group>
267 void skipBlank();
270 // </group>
271
272 // Act on sign; return +1 or -1 depending on signs found (-- == +)
273 // <group>
274 void skipSign();
275 Bool testSign() const;
278 // </group>
279
280 // Act on integer field. If no integer found in 0 returned; and False
281 // <group>
282 void skipInt();
283 Bool testInt() const;
286 void skipuInt();
288 Bool testuInt() const;
290 // </group>
291
292 // Act on Double field. If no value 0 returned and False.
293 // <group>
298 // </group>
299
300 // Act on character(s)
301 // <group>
302 void skipChar(Int n=1);
303 void skipChar(Char ch);
305 void skipCharNC(Char ch);
309 void skipChar(const Regex &ex);
310 Bool tSkipChar(const Regex &ex);
311 void skipAlpha();
313 void skipNum();
317 Bool testChar(Char ch) const;
319 Bool testChar(const Regex &ex) const;
321 Bool testNum() const;
326 // </group>
327
328 // Act on series of characters
329 // <group>
330 Bool testString(const Regex &ex) const;
331 Bool testString(const String &ex) const;
332 Bool testStringNC(const String &ex) const;
336 void skipString(const Regex &ex);
337 void skipString(const String &ex);
338 void skipStringNC(const String &ex);
342 // </group>
343
344 // Match a pair of opening(at pointer)/closing characters (e.g. ( and )).
345 // Return False if wrong semantics. The string between the pair
346 // (excluding them)
347 // will be put in Last. If false, the ptr will be as originally; if True
348 // it will point beyond the matched closing character
350
351 // Get frequency of occurrence
352 Int freqChar(Char ch) const;
353
354 // Get part of string
355 // <group>
359 // </group>
360
361 // Get pointer
362 Int getPtr() const;
363
364 // (Re-)set pointer
365 void setPtr(Int in=0);
366
367 // test for end of string
368 Bool eos() const;
369
370 // Get status last get
371 Bool status() const;
372
373 // Get String found at last get
374 const String &lastGet() const;
375
376 // Do minimax check on list of Strings
377 // <group>
378 static uInt minimaxNC(const String &in, Int N_name,
379 const String tname[]);
380 static uInt minimaxNC(const String &in, const Vector<String> &tname);
381 // </group>
382
383private:
384 // Data
385 // String value
387 // 0-based pointer into string
389 // Length of string
391 // Pointer stack
393 // Pointer into stack
395 // Status of last get
397 // String found at last get
399
400 // Member functions
401 // Make a new pointer between 0 and len inclusive
402 void adjustPtr(Int in);
403
404 // Initialise last settings; return pointer
406 // Set last settings
407 void setLast(Int st);
408
409};
410
411// Global functions
412// <summary> Output global functions </summary>
413// Output
414// <group name=output>
415ostream &operator<<(ostream &os, const MUString &in);
416// </group>
418
419} //# NAMESPACE CASACORE - END
420
421#endif
simple 1-D array
Definition Block.h:198
Bool testChar(const Regex &ex) const
Int initLast()
Initialise last settings; return pointer.
Int getPtr() const
Get pointer.
friend ostream & operator<<(ostream &os, const MUString &in)
Output String starting at pointer.
void setLast(Int st)
Set last settings.
String getStringNC(const String &ex)
MUString(const MUString &other)
Copy constructor; new pointer will be same as old.
String operator()()
Obtain remaining string (same as get()).
Bool status() const
Get status last get.
static uInt minimaxNC(const String &in, const Vector< String > &tname)
MUString(const Char *in)
void pop()
Restore pointer from stack (or set to start if stack empty)
~MUString()
Destructor.
Bool testSign() const
void skipBlank()
Act on whitespace; adjusting pointer if skip.
String str
Data String value.
Definition MUString.h:386
const String & lastGet() const
Get String found at last get.
String get(uInt st)
Bool tSkipString(const String &ex)
void skipStringNC(const String &ex)
void skipChar(Int n=1)
Act on character(s)
Bool testString(const String &ex) const
Bool testNum() const
MUString()
Default constructor creates an empty string.
void skipDouble()
Act on Double field.
Bool testAlpha() const
void adjustPtr(Int in)
Member functions Make a new pointer between 0 and len inclusive.
void skipString(const Regex &ex)
Block< uInt > stack
Pointer stack.
Definition MUString.h:392
Bool testChar(Char ch) const
Bool tSkipString(const Regex &ex)
String getString(const String &ex)
Bool tSkipChar(Char nc)
void skipInt()
Act on integer field.
void unpush()
Restore stack for one level.
static uInt minimaxNC(const String &in, Int N_name, const String tname[])
Do minimax check on list of Strings.
Bool testCharNC(Char ch) const
Bool tSkipCharNC(Char ch)
Bool testStringNC(const String &ex) const
Bool matchPair(Char nd)
Match a pair of opening(at pointer)/closing characters (e.g.
String getString(const Regex &ex)
void setPtr(Int in=0)
(Re-)set pointer
Int freqChar(Char ch) const
Get frequency of occurrence.
void skipChar(const Regex &ex)
Bool testInt() const
Bool testuInt() const
Bool testString(const Regex &ex) const
Act on series of characters.
MUString & operator=(const MUString &other)
Copy assignment; new pointer will be same as old.
Bool testDouble() const
uInt stpt
Pointer into stack.
Definition MUString.h:394
Bool testAlphaNum() const
String get(uInt st, uInt nd)
void skipCharNC(Char ch)
uInt len
Length of string.
Definition MUString.h:390
Bool stat
Status of last get.
Definition MUString.h:396
Bool eos() const
test for end of string
Bool tSkipChar(const Regex &ex)
Bool tSkipOneCharNC(Char ch)
uInt ptr
0-based pointer into string
Definition MUString.h:388
void skipChar(Char ch)
MUString(const String &in)
Create from String; setting pointer at start.
Bool tSkipStringNC(const String &ex)
Bool tSkipOneChar(Char ch)
String lget
String found at last get.
Definition MUString.h:398
String get()
Get part of string.
Bool testBlank() const
void skipSign()
Act on sign; return +1 or -1 depending on signs found (– == +)
void push()
Save current pointer on internal stack.
void skipString(const String &ex)
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
ostream & operator<<(ostream &os, const IComplex &)
Show on ostream.
unsigned int uInt
Definition aipstype.h:49
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
double Double
Definition aipstype.h:53
char Char
Definition aipstype.h:44
ostream & operator<<(ostream &os, const MUString &in)