blob: 987ab472aaa35672e7194a2da8011f635e36e48a [file] [log] [blame]
Greg Clayton7406c392011-04-20 01:33:38 +00001//===-- NamedOptionValue.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#include "lldb/Interpreter/NamedOptionValue.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Core/Stream.h"
17#include "lldb/Interpreter/Args.h"
18
19using namespace lldb;
20using namespace lldb_private;
21
Greg Claytone9728452011-04-21 17:46:10 +000022
23//-------------------------------------------------------------------------
24// OptionValue
25//-------------------------------------------------------------------------
26OptionValueBoolean *
27OptionValue::GetAsBooleanValue ()
28{
29 if (GetType () == OptionValue::eTypeBoolean)
30 return static_cast<OptionValueBoolean *>(this);
31 return NULL;
32}
33
34OptionValueSInt64 *
35OptionValue::GetAsSInt64Value ()
36{
37 if (GetType () == OptionValue::eTypeSInt64)
38 return static_cast<OptionValueSInt64 *>(this);
39 return NULL;
40}
41
42OptionValueUInt64 *
43OptionValue::GetAsUInt64Value ()
44{
45 if (GetType () == OptionValue::eTypeUInt64)
46 return static_cast<OptionValueUInt64 *>(this);
47 return NULL;
48}
49
50OptionValueString *
51OptionValue::GetAsStringValue ()
52{
53 if (GetType () == OptionValue::eTypeString)
54 return static_cast<OptionValueString *>(this);
55 return NULL;
56}
57
58OptionValueFileSpec *
59OptionValue::GetAsFileSpecValue ()
60{
61 if (GetType () == OptionValue::eTypeFileSpec)
62 return static_cast<OptionValueFileSpec *>(this);
63 return NULL;
64}
65
66OptionValueArray *
67OptionValue::GetAsArrayValue ()
68{
69 if (GetType () == OptionValue::eTypeArray)
70 return static_cast<OptionValueArray *>(this);
71 return NULL;
72}
73
74OptionValueDictionary *
75OptionValue::GetAsDictionaryValue ()
76{
77 if (GetType () == OptionValue::eTypeDictionary)
78 return static_cast<OptionValueDictionary *>(this);
79 return NULL;
80}
81
82
Greg Clayton7406c392011-04-20 01:33:38 +000083//-------------------------------------------------------------------------
Greg Claytonc08770b2011-04-21 19:21:29 +000084// OptionValueCollection
Greg Clayton7406c392011-04-20 01:33:38 +000085//-------------------------------------------------------------------------
86
87void
Greg Claytonc08770b2011-04-21 19:21:29 +000088OptionValueCollection::GetQualifiedName (Stream &strm)
Greg Clayton7406c392011-04-20 01:33:38 +000089{
90 if (m_parent)
91 {
92 m_parent->GetQualifiedName (strm);
93 strm.PutChar('.');
94 }
95 strm << m_name;
96}
97
Greg Clayton7406c392011-04-20 01:33:38 +000098
Greg Clayton7406c392011-04-20 01:33:38 +000099//-------------------------------------------------------------------------
100// OptionValueBoolean
101//-------------------------------------------------------------------------
102void
103OptionValueBoolean::DumpValue (Stream &strm)
104{
105 strm.PutCString (m_current_value ? "true" : "false");
106}
107
108bool
109OptionValueBoolean::SetValueFromCString (const char *value_cstr)
110{
111 bool success = false;
112 bool value = Args::StringToBoolean(value_cstr, false, &success);
113 if (success)
114 {
115 m_current_value = value;
116 return true;
117 }
118 return false;
119}
120
121//-------------------------------------------------------------------------
122// OptionValueSInt64
123//-------------------------------------------------------------------------
124void
125OptionValueSInt64::DumpValue (Stream &strm)
126{
127 strm.Printf ("%lli", m_current_value);
128}
129
130bool
131OptionValueSInt64::SetValueFromCString (const char *value_cstr)
132{
133 bool success = false;
134 int64_t value = Args::StringToSInt64 (value_cstr, 0, 0, &success);
135 if (success)
136 {
137 m_current_value = value;
138 return true;
139 }
140 return false;
141}
142
143//-------------------------------------------------------------------------
144// OptionValueUInt64
145//-------------------------------------------------------------------------
146void
147OptionValueUInt64::DumpValue (Stream &strm)
148{
149 strm.Printf ("0x%llx", m_current_value);
150}
151
152bool
153OptionValueUInt64::SetValueFromCString (const char *value_cstr)
154{
155 bool success = false;
156 uint64_t value = Args::StringToUInt64 (value_cstr, 0, 0, &success);
157 if (success)
158 {
159 m_current_value = value;
160 return true;
161 }
162 return false;
163}
164
165//-------------------------------------------------------------------------
Greg Claytond12aeab2011-04-20 16:37:46 +0000166// OptionValueDictionary
167//-------------------------------------------------------------------------
168void
169OptionValueString::DumpValue (Stream &strm)
170{
171 strm.Printf ("\"%s\"", m_current_value.c_str());
172}
173
174bool
175OptionValueString::SetValueFromCString (const char *value_cstr)
176{
177 SetCurrentValue (value_cstr);
178 return true;
179}
180
181
182
183//-------------------------------------------------------------------------
Greg Clayton7406c392011-04-20 01:33:38 +0000184// OptionValueFileSpec
185//-------------------------------------------------------------------------
186void
187OptionValueFileSpec::DumpValue (Stream &strm)
188{
189 if (m_current_value)
190 {
191 if (m_current_value.GetDirectory())
192 {
193 strm << '"' << m_current_value.GetDirectory();
194 if (m_current_value.GetFilename())
195 strm << '/' << m_current_value.GetFilename();
196 strm << '"';
197 }
198 else
199 {
200 strm << '"' << m_current_value.GetFilename() << '"';
201 }
202 }
203}
204
205bool
206OptionValueFileSpec::SetValueFromCString (const char *value_cstr)
207{
208 if (value_cstr && value_cstr[0])
209 m_current_value.SetFile(value_cstr, false);
210 else
211 m_current_value.Clear();
212 return true;
213}
214
215
216//-------------------------------------------------------------------------
217// OptionValueArray
218//-------------------------------------------------------------------------
219void
220OptionValueArray::DumpValue (Stream &strm)
221{
222 const uint32_t size = m_values.size();
223 for (uint32_t i = 0; i<size; ++i)
224 {
225 strm.Printf("[%u] ", i);
226 m_values[i]->DumpValue (strm);
227 }
228}
229
230bool
231OptionValueArray::SetValueFromCString (const char *value_cstr)
232{
233 // We must be able to set this using the array specific functions
234 return false;
235}
236
237//-------------------------------------------------------------------------
238// OptionValueDictionary
239//-------------------------------------------------------------------------
240void
241OptionValueDictionary::DumpValue (Stream &strm)
242{
243 collection::iterator pos, end = m_values.end();
244
245 for (pos = m_values.begin(); pos != end; ++pos)
246 {
247 strm.Printf("%s=", pos->first.GetCString());
248 pos->second->DumpValue (strm);
249 }
250}
251
252bool
253OptionValueDictionary::SetValueFromCString (const char *value_cstr)
254{
255 // We must be able to set this using the array specific functions
256 return false;
257}
258
Greg Claytone9728452011-04-21 17:46:10 +0000259lldb::OptionValueSP
260OptionValueDictionary::GetValueForKey (const ConstString &key) const
261{
262 lldb::OptionValueSP value_sp;
263 collection::const_iterator pos = m_values.find (key);
264 if (pos != m_values.end())
265 value_sp = pos->second;
266 return value_sp;
267}
268
269const char *
270OptionValueDictionary::GetStringValueForKey (const ConstString &key)
271{
272 collection::const_iterator pos = m_values.find (key);
273 if (pos != m_values.end())
274 {
275 if (pos->second->GetType() == OptionValue::eTypeString)
276 return static_cast<OptionValueString *>(pos->second.get())->GetCurrentValue();
277 }
278 return NULL;
279}
280
281
282bool
283OptionValueDictionary::SetStringValueForKey (const ConstString &key,
284 const char *value,
285 bool can_replace)
286{
287 collection::const_iterator pos = m_values.find (key);
288 if (pos != m_values.end())
289 {
290 if (!can_replace)
291 return false;
292 if (pos->second->GetType() == OptionValue::eTypeString)
293 {
294 pos->second->SetValueFromCString(value);
295 return true;
296 }
297 }
298 m_values[key] = OptionValueSP (new OptionValueString (value));
299 return true;
300
301}
302
303bool
304OptionValueDictionary::SetValueForKey (const ConstString &key,
305 const lldb::OptionValueSP &value_sp,
306 bool can_replace)
307{
308 // Make sure the value_sp object is allowed to contain
309 // values of the type passed in...
310 if (value_sp && (m_type_mask & value_sp->GetTypeAsMask()))
311 {
312 if (!can_replace)
313 {
314 collection::const_iterator pos = m_values.find (key);
315 if (pos != m_values.end())
316 return false;
317 }
318 m_values[key] = value_sp;
319 return true;
320 }
321 return false;
322}
323
324bool
325OptionValueDictionary::DeleteValueForKey (const ConstString &key)
326{
327 collection::iterator pos = m_values.find (key);
328 if (pos != m_values.end())
329 {
330 m_values.erase(pos);
331 return true;
332 }
333 return false;
334}
335
Greg Clayton7406c392011-04-20 01:33:38 +0000336