Sierra Toolkit  Version of the Day
Option.cpp
1 
2 #include <sstream>
3 #include <iomanip>
4 #include <map>
5 
6 #include <stk_util/diag/Option.hpp>
7 #include <stk_util/diag/Trace.hpp>
8 #include <stk_util/diag/Writer_fwd.hpp>
9 #include <iostream>
10 
11 namespace stk_classic {
12 namespace diag {
13 
14 OptionMaskParser::Mask
16  const char * mask) const
17 {
18  if (mask) {
19  const std::string mask_string(mask);
20 
21  m_status = true;
22 
23  std::string::const_iterator it0 = mask_string.begin();
24  std::string::const_iterator it1;
25  std::string::const_iterator it2;
26  std::string::const_iterator it3;
27  do {
28  // Trim preceeding spaces
29  while (it0 != mask_string.end() && *it0 == ' ')
30  it0++;
31 
32  if (it0 == mask_string.end())
33  break;
34 
35  for (it1 = it0; it1 != mask_string.end(); ++it1) {
36  if (*it1 == '(' || *it1 == ':' || *it1 == ',')
37  break;
38  }
39 
40  // Trim trailing spaces
41  it2 = it1;
42  while (it2 != it0 && *(it2 - 1) == ' ')
43  --it2;
44 
45  std::string name(it0, it2);
46 
47  // Get argument list
48  if (*it1 == '(') {
49  it2 = it1 + 1;
50 
51  // Trim preceeding spaces
52  while (it2 != mask_string.end() && *it2 == ' ')
53  ++it2;
54 
55  int paren_count = 0;
56 
57  for (; it1 != mask_string.end(); ++it1) {
58  if (*it1 == '(')
59  ++paren_count;
60  else if (*it1 == ')') {
61  --paren_count;
62  if (paren_count == 0)
63  break;
64  }
65  }
66  it3 = it1;
67 
68  // Trim trailing spaces
69  while (it3 != it2 && *(it3 - 1) == ' ')
70  --it3;
71 
72  // Find next argument start
73  for (; it1 != mask_string.end(); ++it1)
74  if (*it1 == ':' || *it1 == ',')
75  break;
76  }
77  else
78  it2 = it3 = it1;
79 
80  const std::string arg(it2, it3);
81 
82  parseArg(name, arg);
83 
84  it0 = it1 + 1;
85  } while (it1 != mask_string.end());
86  }
87 
88  return m_optionMask;
89 }
90 
91 
92 void
94  const std::string & name,
95  const std::string & arg) const
96 {
97  OptionMaskNameMap::const_iterator mask_entry = m_optionMaskNameMap.find(name);
98 
99  if (mask_entry != m_optionMaskNameMap.end()) m_optionMask |= (*mask_entry).second.m_mask;
100  else {
101  Mask mask_hex = 0;
102  std::istringstream mask_hex_stream(name.c_str());
103  if (mask_hex_stream >> std::resetiosflags(std::ios::basefield) >> mask_hex)
104  m_optionMask |= mask_hex;
105  else
106  m_status = false;
107  }
108 }
109 
110 
111 std::ostream &
112 OptionMaskParser::describe(
113  std::ostream & os) const
114 {
115  os << "Specify a comma separated list of:" << std::endl;
116  for (OptionMaskNameMap::const_iterator it = m_optionMaskNameMap.begin(); it != m_optionMaskNameMap.end(); ++it)
117  (*it).second.describe(os);
118 
119  return os;
120 }
121 
122 
123 std::ostream &
124 OptionMaskName::describe(
125  std::ostream & os) const
126 {
127  return os << " " << std::left << std::setw(20) << m_name << "\t" << m_description << std::endl;
128 }
129 
130 } // namespace diag
131 } // namespace stk_classic
_resetiosflags resetiosflags(std::ios_base::fmtflags flags)
Function resetiosflags clears the ios flags as a manipulator.
OptionMaskNameMap m_optionMaskNameMap
Mask name vector.
Definition: Option.hpp:345
virtual void parseArg(const std::string &name, const std::string &arg) const
Definition: Option.cpp:93
void mask(const std::string &name, const Mask l_mask, const std::string &description)
Definition: Option.hpp:332
OptionMask m_optionMask
Most recently parsed mask.
Definition: Option.hpp:346
virtual Mask parse(const char *mask) const
Definition: Option.cpp:15
bool m_status
Result of most recent parse.
Definition: Option.hpp:347
_setw setw(int width)
Function setw sets the width for the next field as a manipulator.
Definition: WriterManip.hpp:44
Sierra Toolkit.
OptionMask Mask
Mask for this option.
Definition: Option.hpp:261