1 | // **************************************************************************
|
---|
2 | /** @struct State
|
---|
3 |
|
---|
4 | @brief A struct which stores an index, a comment and a name of a State
|
---|
5 |
|
---|
6 | To have proper descriptions of states in the network, this struct provides
|
---|
7 | a simple storage for the properties of a state.
|
---|
8 |
|
---|
9 | Assume you want to write a descriptive string for a state machine
|
---|
10 | with two states, it could look like this:
|
---|
11 |
|
---|
12 | "1:Disconnected=Connection not established\n2:Connected=Connection established."
|
---|
13 |
|
---|
14 | Such a string can then be converted with SplitStates into a vector
|
---|
15 | of State objects.
|
---|
16 |
|
---|
17 | */
|
---|
18 | // **************************************************************************
|
---|
19 | #include "State.h"
|
---|
20 |
|
---|
21 | #include <sstream>
|
---|
22 | #include <algorithm>
|
---|
23 |
|
---|
24 | #include "tools.h"
|
---|
25 |
|
---|
26 | using namespace std;
|
---|
27 |
|
---|
28 | // --------------------------------------------------------------------------
|
---|
29 | //
|
---|
30 | //! Construct a Description object
|
---|
31 | //!
|
---|
32 | //! @param i
|
---|
33 | //! Index of the state, e.g. 1
|
---|
34 | //!
|
---|
35 | //! @param n
|
---|
36 | //! Name of the state, e.g. 'Connected'
|
---|
37 | //!
|
---|
38 | //! @param c
|
---|
39 | //! Descriptive text of the state, e.g. "Connection to hardware established."
|
---|
40 | //
|
---|
41 | State::State(int i, const std::string &n, const std::string &c)
|
---|
42 | : index(i), name(Trim(n)), comment(Trim(c))
|
---|
43 | {
|
---|
44 | }
|
---|
45 |
|
---|
46 | // --------------------------------------------------------------------------
|
---|
47 | //
|
---|
48 | //! This function breaks down a descriptive string into its components.
|
---|
49 | //! For details see class reference.
|
---|
50 | //!
|
---|
51 | //! @param buffer
|
---|
52 | //! string which should be broekn into pieces
|
---|
53 | //!
|
---|
54 | //! @returns
|
---|
55 | //! A vector<State> containing all the states found.
|
---|
56 | //
|
---|
57 | vector<State> State::SplitStates(const string &buffer)
|
---|
58 | {
|
---|
59 | vector<State> vec;
|
---|
60 |
|
---|
61 | string buf;
|
---|
62 | stringstream stream(buffer);
|
---|
63 | while (getline(stream, buf, '\n'))
|
---|
64 | {
|
---|
65 | if (buf.empty())
|
---|
66 | continue;
|
---|
67 |
|
---|
68 | const size_t p1 = buf.find_first_of(':');
|
---|
69 | const size_t p2 = buf.find_first_of('=');
|
---|
70 |
|
---|
71 | stringstream s(buf.substr(0, p1));
|
---|
72 |
|
---|
73 | int index;
|
---|
74 | s >> index;
|
---|
75 |
|
---|
76 | const string name = buf.substr(p1+1, p2-p1-1);
|
---|
77 | const string comment = p2==string::npos ? "" : buf.substr(p2+1);
|
---|
78 |
|
---|
79 | vec.push_back(State(index, name, comment));
|
---|
80 | }
|
---|
81 |
|
---|
82 | sort(vec.begin(), vec.end(), State::Compare);
|
---|
83 |
|
---|
84 | return vec;
|
---|
85 | }
|
---|