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