1 | // **************************************************************************
|
---|
2 | /** @struct Description
|
---|
3 |
|
---|
4 | @brief A struct which stores a name, a unit and a comment
|
---|
5 |
|
---|
6 | To have proper descriptions of commands and services in the network
|
---|
7 | (and later proper informations in the FITS files) this struct provides
|
---|
8 | a simple storage for a name, a unit and a comment.
|
---|
9 |
|
---|
10 | Assume you want to write a descriptive string for a command with three arguments.
|
---|
11 | It could look like this:
|
---|
12 |
|
---|
13 | COMMAND=Description|int[addr]:Address range (from - to)|val[byte]:Value to be set
|
---|
14 |
|
---|
15 | Description is a general description of the command or service itself,
|
---|
16 | int and val are the names of the arguments (e.g. names of FITS columns),
|
---|
17 | addr and byte have the meaning of a unit (e.g. unit of FITS column)
|
---|
18 | and 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
|
---|
20 | line-break character \n.
|
---|
21 |
|
---|
22 | Such a string can then be converted with SplitDescription into a vector
|
---|
23 | of Description objects, each containing the name, the unit and a
|
---|
24 | comment indivdually. The first object will contain COMMAND as name and
|
---|
25 | Description as comment. The unit will remain empty.
|
---|
26 |
|
---|
27 | You can omit either the name, the unit or the comment or any
|
---|
28 | combination of them. The descriptions of the individual format strings
|
---|
29 | are 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
|
---|
31 | have add empty brackets for the units.
|
---|
32 |
|
---|
33 | For a suggestion for rules for the names please have a look at:
|
---|
34 | http://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/ofwg_recomm/r15.html
|
---|
35 |
|
---|
36 | For units please see:
|
---|
37 | http://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 |
|
---|
45 | #include "tools.h"
|
---|
46 |
|
---|
47 | using namespace std;
|
---|
48 | using namespace Tools;
|
---|
49 |
|
---|
50 | // --------------------------------------------------------------------------
|
---|
51 | //
|
---|
52 | //! Construct a Description object
|
---|
53 | //!
|
---|
54 | //! @param n
|
---|
55 | //! Name of the Description, e.g. "temp"
|
---|
56 | //!
|
---|
57 | //! @param c
|
---|
58 | //! Descriptive text of the Description, e.g. "Temperature of the moon"
|
---|
59 | //!
|
---|
60 | //! @param u
|
---|
61 | //! Unit of the Description, e.g. "K"
|
---|
62 | //
|
---|
63 | Description::Description(const string &n, const string &c, const string &u)
|
---|
64 | : name(Trim(n)), comment(Trim(c)), unit(Trim(u))
|
---|
65 | {
|
---|
66 | }
|
---|
67 |
|
---|
68 | // --------------------------------------------------------------------------
|
---|
69 | //
|
---|
70 | //! This function breaks down a descriptive string into its components.
|
---|
71 | //! For details see class reference.
|
---|
72 | //!
|
---|
73 | //! @param buffer
|
---|
74 | //! string which should be broekn into pieces
|
---|
75 | //!
|
---|
76 | //! @returns
|
---|
77 | //! A vector<Description> containing all the descriptions found.
|
---|
78 | //! The first entry contains the Description members name and comment,
|
---|
79 | //! corresponding to the service or command name the Description
|
---|
80 | //! list is for and its corresponding description.
|
---|
81 | //
|
---|
82 | vector<Description> Description::SplitDescription(const string &buffer)
|
---|
83 | {
|
---|
84 | const size_t p0 = buffer.find_first_of('=');
|
---|
85 |
|
---|
86 | const string svc = buffer.substr(0, p0);
|
---|
87 | const string desc = buffer.substr(p0+1);
|
---|
88 |
|
---|
89 | const size_t p = desc.find_first_of('|');
|
---|
90 |
|
---|
91 | // Extract a general description
|
---|
92 | const string d = Trim(desc.substr(0, p));
|
---|
93 |
|
---|
94 | vector<Description> vec;
|
---|
95 | vec.emplace_back(svc, d);
|
---|
96 |
|
---|
97 | if (p==string::npos)
|
---|
98 | return vec;
|
---|
99 |
|
---|
100 | string buf;
|
---|
101 | stringstream stream(desc.substr(p+1));
|
---|
102 | while (getline(stream, buf, '|'))
|
---|
103 | {
|
---|
104 | if (buf.empty())
|
---|
105 | continue;
|
---|
106 |
|
---|
107 | const size_t p1 = buf.find_first_of(':');
|
---|
108 |
|
---|
109 | const string comment = p1==string::npos ? "" : buf.substr(p1+1);
|
---|
110 | if (p1!=string::npos)
|
---|
111 | buf.erase(p1);
|
---|
112 |
|
---|
113 | const size_t p2 = buf.find_last_of('[');
|
---|
114 | const size_t p3 = buf.find_last_of(']');
|
---|
115 |
|
---|
116 | const bool hasunit = p2<p3 && p2!=string::npos;
|
---|
117 |
|
---|
118 | const string unit = hasunit ? buf.substr(p2+1, p3-p2-1) : "";
|
---|
119 | const string name = hasunit ? buf.substr(0, p2) : buf;
|
---|
120 |
|
---|
121 | vec.emplace_back(name, comment, unit);
|
---|
122 | }
|
---|
123 |
|
---|
124 | return vec;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | // --------------------------------------------------------------------------
|
---|
129 | //
|
---|
130 | //! Returns a string with an html formatted text containing the descriptions
|
---|
131 | //! as returned by SplitDescription
|
---|
132 | //!
|
---|
133 | //! @param vec
|
---|
134 | //! vector of Description for the individual arguments. First
|
---|
135 | //! element is the global description of the command or service.
|
---|
136 | //!
|
---|
137 | //! @returns
|
---|
138 | //! string with html formatted text
|
---|
139 | //
|
---|
140 | string Description::GetHtmlDescription(const vector<Description> &vec)
|
---|
141 | {
|
---|
142 | stringstream str;
|
---|
143 | str << "<H3>" << vec[0].name << "</H3>";
|
---|
144 |
|
---|
145 | str << "Usage:";
|
---|
146 | for (vector<Description>::const_iterator i=vec.begin()+1; i!=vec.end(); i++)
|
---|
147 | str << " <font color='maroon'><" << i->name << "></font>";
|
---|
148 |
|
---|
149 | if (vec.size()==1)
|
---|
150 | str << " <no arguments>";
|
---|
151 |
|
---|
152 | str << "<P>" << vec[0].comment << "<P>";
|
---|
153 |
|
---|
154 | str << "<table>";
|
---|
155 |
|
---|
156 | for (vector<Description>::const_iterator i=vec.begin()+1; i!=vec.end(); i++)
|
---|
157 | {
|
---|
158 | str << "<tr>"
|
---|
159 | "<td><font color='maroon'>" << i->name << "</font>";
|
---|
160 |
|
---|
161 | if (i->unit.empty() && !i->comment.empty() && !i->name.empty())
|
---|
162 | str << ':';
|
---|
163 |
|
---|
164 | str << "</td>";
|
---|
165 |
|
---|
166 | if (!i->unit.empty())
|
---|
167 | str << "<td><font color='green'>[" << i->unit << "]</font>";
|
---|
168 |
|
---|
169 | if (!i->unit.empty() && !i->comment.empty())
|
---|
170 | str << ':';
|
---|
171 |
|
---|
172 | str <<
|
---|
173 | "</td>"
|
---|
174 | "<td><font color='navy'>" << i->comment << "</font></td>"
|
---|
175 | "</tr>";
|
---|
176 | }
|
---|
177 |
|
---|
178 | str << "</table>";
|
---|
179 |
|
---|
180 | return str.str();
|
---|
181 | }
|
---|