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

Last change on this file since 10312 was 10311, checked in by tbretz, 14 years ago
Added some links to the class reference.
File size: 4.0 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 combination of them.
28The descriptions of the individual format strings are separated by a vertical line.
29
30For a suggestion for rules for the names please have a look at:
31http://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/ofwg_recomm/r15.html
32
33For units please see:
34http://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/general/ogip_93_001/ogip_93_001.html
35
36*/
37// **************************************************************************
38#include "Description.h"
39
40#include <sstream>
41
42using namespace std;
43
44// --------------------------------------------------------------------------
45//
46//! This function removed whitshapces on both sides of a string.
47//!
48//! @param s
49//! string to be cleaned
50//!
51//! @returns
52//! string cleaned from whitespaces
53//
54string Description::Trim(string s)
55{
56 // Trim Both leading and trailing spaces
57 const size_t start = s.find_first_not_of(' '); // Find the first character position after excluding leading blank spaces
58 const size_t end = s.find_last_not_of(' '); // Find the first character position from reverse af
59
60 // if all spaces or empty return an empty string
61 if (string::npos==start || string::npos==end)
62 return string();
63
64 return s.substr(start, end-start+1);
65}
66
67// --------------------------------------------------------------------------
68//
69//! This functio0n breaks down a descriptive string into its components.
70//! For details see class reference.
71//!
72//! @param buffer
73//! string which should be broekn into pieces
74//!
75//! @returns
76//! A vector<Description> containing all the descriptions found.
77//! The first entry contains the discription name and comment.
78//
79vector<Description> Description::SplitDescription(const string &buffer)
80{
81 const size_t p0 = buffer.find_first_of('=');
82
83 const string svc = buffer.substr(0, p0);
84 const string desc = buffer.substr(p0+1);
85
86 const size_t p = desc.find_first_of('|');
87
88 // Extract a general description
89 const string d = Description::Trim(desc.substr(0, p));
90
91 vector<Description> vec;
92 vec.push_back(Description(svc, "", d));
93
94 if (p==string::npos)
95 return vec;
96
97 string buf;
98 stringstream stream(desc.substr(p+1));
99 while (getline(stream, buf, '|'))
100 {
101 if (buf.empty())
102 continue;
103
104 const size_t p1 = buf.find_first_of(':');
105
106 const string comment = p1==string::npos ? "" : buf.substr(p1+1);
107 if (p1!=string::npos)
108 buf.erase(p1);
109
110 const size_t p2 = buf.find_last_of('[');
111 const size_t p3 = buf.find_last_of(']');
112
113 const bool hasunit = p2<p3 && p2!=string::npos;
114
115 const string unit = hasunit ? buf.substr(p2+1, p3-p2-1) : "";
116 const string name = hasunit ? buf.substr(0, p2) : buf;
117
118 vec.push_back(Description(name, unit, comment));
119 }
120
121 return vec;
122}
Note: See TracBrowser for help on using the repository browser.