source: trunk/FACT++/src/Description.cc@ 10350

Last change on this file since 10350 was 10349, checked in by tbretz, 14 years ago
Updated comment.
File size: 3.5 KB
Line 
1// **************************************************************************
2/** @struct Description
3
4@brief A struct which stores a name, a unit and a comment
5
6To have proper descriptions of commands and services in the network
7(and later proper informations in the FITS files) this struct provides
8a simple storage for a name, a unit and a comment.
9
10Assume you want to write a descriptive string for a command with three arguments.
11It could look like this:
12
13 COMMAND=Description|int[addr]:Address range (from - to)|val[byte]:Value to be set
14
15Description is a general description of the command or service itself,
16int and val are the names of the arguments (e.g. names of FITS columns),
17addr and byte have the meaning of a unit (e.g. unit of FITS column)
18and the text after the colon is a description of the arguments
19(e.g. comment of a FITS column). The description must not contain a
20line-break character \n.
21
22Such a string can then be converted with SplitDescription into a vector
23of Description objects, each containing the name, the unit and a
24comment indivdually. The first object will contain COMMAND as name and
25Description as comment. The unit will remain empty.
26
27You can omit either the name, the unit or the comment or any
28combination of them. The descriptions of the individual format strings
29are separated by a vertical line. If you want to enclose the name into
30[]-braces (e.g. marking an optional argument in a dim command), you
31have add empty brackets for the units.
32
33For a suggestion for rules for the names please have a look at:
34http://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/ofwg_recomm/r15.html
35
36For units please see:
37http://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/general/ogip_93_001/ogip_93_001.html
38
39*/
40// **************************************************************************
41#include "Description.h"
42
43#include <sstream>
44
45using namespace std;
46
47// --------------------------------------------------------------------------
48//
49//! This function breaks down a descriptive string into its components.
50//! For details see class reference.
51//!
52//! @param buffer
53//! string which should be broekn into pieces
54//!
55//! @returns
56//! A vector<Description> containing all the descriptions found.
57//! The first entry contains the Description members name and comment,
58//! corresponding to the service or command name the Description
59//! list is for and its corresponding description.
60//
61vector<Description> Description::SplitDescription(const string &buffer)
62{
63 const size_t p0 = buffer.find_first_of('=');
64
65 const string svc = buffer.substr(0, p0);
66 const string desc = buffer.substr(p0+1);
67
68 const size_t p = desc.find_first_of('|');
69
70 // Extract a general description
71 const string d = Trim(desc.substr(0, p));
72
73 vector<Description> vec;
74 vec.push_back(Description(svc, "", d));
75
76 if (p==string::npos)
77 return vec;
78
79 string buf;
80 stringstream stream(desc.substr(p+1));
81 while (getline(stream, buf, '|'))
82 {
83 if (buf.empty())
84 continue;
85
86 const size_t p1 = buf.find_first_of(':');
87
88 const string comment = p1==string::npos ? "" : buf.substr(p1+1);
89 if (p1!=string::npos)
90 buf.erase(p1);
91
92 const size_t p2 = buf.find_last_of('[');
93 const size_t p3 = buf.find_last_of(']');
94
95 const bool hasunit = p2<p3 && p2!=string::npos;
96
97 const string unit = hasunit ? buf.substr(p2+1, p3-p2-1) : "";
98 const string name = hasunit ? buf.substr(0, p2) : buf;
99
100 vec.push_back(Description(name, unit, comment));
101 }
102
103 return vec;
104}
Note: See TracBrowser for help on using the repository browser.