1 //===-- MICmdArgValNumber.cpp -----------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // In-house headers:
11 #include "MICmdArgValNumber.h"
12 #include "MICmdArgContext.h"
13
14 //++
15 //------------------------------------------------------------------------------------
16 // Details: CMICmdArgValNumber constructor.
17 // Type: Method.
18 // Args: None.
19 // Return: None.
20 // Throws: None.
21 //--
CMICmdArgValNumber()22 CMICmdArgValNumber::CMICmdArgValNumber()
23 : m_nNumberFormatMask(CMICmdArgValNumber::eArgValNumberFormat_Decimal),
24 m_nNumber(0) {}
25
26 //++
27 //------------------------------------------------------------------------------------
28 // Details: CMICmdArgValNumber constructor.
29 // Type: Method.
30 // Args: vrArgName - (R) Argument's name to search by.
31 // vbMandatory - (R) True = Yes must be present, false =
32 // optional argument.
33 // vbHandleByCmd - (R) True = Command processes *this option,
34 // false = not handled.
35 // vnNumberFormatMask - (R) Mask of the number formats. (Dflt =
36 // CMICmdArgValNumber::eArgValNumberFormat_Decimal)
37 // Return: None.
38 // Throws: None.
39 //--
CMICmdArgValNumber(const CMIUtilString & vrArgName,const bool vbMandatory,const bool vbHandleByCmd,const MIuint vnNumberFormatMask)40 CMICmdArgValNumber::CMICmdArgValNumber(
41 const CMIUtilString &vrArgName, const bool vbMandatory,
42 const bool vbHandleByCmd,
43 const MIuint
44 vnNumberFormatMask /* = CMICmdArgValNumber::eArgValNumberFormat_Decimal*/)
45 : CMICmdArgValBaseTemplate(vrArgName, vbMandatory, vbHandleByCmd),
46 m_nNumberFormatMask(vnNumberFormatMask), m_nNumber(0) {}
47
48 //++
49 //------------------------------------------------------------------------------------
50 // Details: CMICmdArgValNumber destructor.
51 // Type: Overridden.
52 // Args: None.
53 // Return: None.
54 // Throws: None.
55 //--
~CMICmdArgValNumber()56 CMICmdArgValNumber::~CMICmdArgValNumber() {}
57
58 //++
59 //------------------------------------------------------------------------------------
60 // Details: Parse the command's argument options string and try to extract the
61 // value *this
62 // argument is looking for.
63 // Type: Overridden.
64 // Args: vwArgContext - (RW) The command's argument options string.
65 // Return: MIstatus::success - Functional succeeded.
66 // MIstatus::failure - Functional failed.
67 // Throws: None.
68 //--
Validate(CMICmdArgContext & vwArgContext)69 bool CMICmdArgValNumber::Validate(CMICmdArgContext &vwArgContext) {
70 if (vwArgContext.IsEmpty())
71 return m_bMandatory ? MIstatus::failure : MIstatus::success;
72
73 if (vwArgContext.GetNumberArgsPresent() == 1) {
74 const CMIUtilString &rArg(vwArgContext.GetArgsLeftToParse());
75 if (IsArgNumber(rArg) && ExtractNumber(rArg)) {
76 m_bFound = true;
77 m_bValid = true;
78 m_argValue = GetNumber();
79 vwArgContext.RemoveArg(rArg);
80 return MIstatus::success;
81 } else
82 return MIstatus::failure;
83 }
84
85 // More than one option...
86 const CMIUtilString::VecString_t vecOptions(vwArgContext.GetArgs());
87 CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
88 while (it != vecOptions.end()) {
89 const CMIUtilString &rArg(*it);
90 if (IsArgNumber(rArg) && ExtractNumber(rArg)) {
91 m_bFound = true;
92
93 if (vwArgContext.RemoveArg(rArg)) {
94 m_bValid = true;
95 m_argValue = GetNumber();
96 return MIstatus::success;
97 } else
98 return MIstatus::failure;
99 }
100
101 // Next
102 ++it;
103 }
104
105 return MIstatus::failure;
106 }
107
108 //++
109 //------------------------------------------------------------------------------------
110 // Details: Examine the string and determine if it is a valid string type
111 // argument.
112 // Type: Method.
113 // Args: vrTxt - (R) Some text.
114 // Return: bool - True = yes valid arg, false = no.
115 // Throws: None.
116 //--
IsArgNumber(const CMIUtilString & vrTxt) const117 bool CMICmdArgValNumber::IsArgNumber(const CMIUtilString &vrTxt) const {
118 const bool bFormatDecimal(m_nNumberFormatMask &
119 CMICmdArgValNumber::eArgValNumberFormat_Decimal);
120 const bool bFormatHexadecimal(
121 m_nNumberFormatMask &
122 CMICmdArgValNumber::eArgValNumberFormat_Hexadecimal);
123
124 // Look for --someLongOption
125 if (std::string::npos != vrTxt.find("--"))
126 return false;
127
128 if (bFormatDecimal && vrTxt.IsNumber())
129 return true;
130
131 if (bFormatHexadecimal && vrTxt.IsHexadecimalNumber())
132 return true;
133
134 return false;
135 }
136
137 //++
138 //------------------------------------------------------------------------------------
139 // Details: Extract the thread group number from the thread group argument.
140 // Type: Method.
141 // Args: vrTxt - (R) Some text.
142 // Return: MIstatus::success - Functional succeeded.
143 // MIstatus::failure - Functional failed.
144 // Throws: None.
145 //--
ExtractNumber(const CMIUtilString & vrTxt)146 bool CMICmdArgValNumber::ExtractNumber(const CMIUtilString &vrTxt) {
147 MIint64 nNumber = 0;
148 bool bOk = vrTxt.ExtractNumber(nNumber);
149 if (bOk) {
150 m_nNumber = static_cast<MIint64>(nNumber);
151 }
152
153 return bOk;
154 }
155
156 //++
157 //------------------------------------------------------------------------------------
158 // Details: Retrieve the thread group ID found in the argument.
159 // Type: Method.
160 // Args: None.
161 // Return: MIuint - Thread group ID.
162 // Throws: None.
163 //--
GetNumber() const164 MIint64 CMICmdArgValNumber::GetNumber() const { return m_nNumber; }
165