blob: 7c1284f8067ce6afdd0b30aecae4626d3b55147d [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//-------------------------------------------------------------------------
Greg Clayton17cd9952011-04-22 03:55:06 +000026
27// Get this value as a uint64_t value if it is encoded as a boolean,
28// uint64_t or int64_t. Other types will cause "fail_value" to be
29// returned
30uint64_t
31OptionValue::GetUInt64Value (uint64_t fail_value, bool *success_ptr)
32{
33 if (success_ptr)
34 *success_ptr = true;
35 switch (GetType())
36 {
37 case OptionValue::eTypeBoolean: return static_cast<OptionValueBoolean *>(this)->GetCurrentValue();
38 case OptionValue::eTypeSInt64: return static_cast<OptionValueSInt64 *>(this)->GetCurrentValue();
39 case OptionValue::eTypeUInt64: return static_cast<OptionValueUInt64 *>(this)->GetCurrentValue();
40 default:
41 break;
42 }
43 if (success_ptr)
44 *success_ptr = false;
45 return fail_value;
46}
47
48
Greg Claytone9728452011-04-21 17:46:10 +000049OptionValueBoolean *
50OptionValue::GetAsBooleanValue ()
51{
52 if (GetType () == OptionValue::eTypeBoolean)
53 return static_cast<OptionValueBoolean *>(this);
54 return NULL;
55}
56
57OptionValueSInt64 *
58OptionValue::GetAsSInt64Value ()
59{
60 if (GetType () == OptionValue::eTypeSInt64)
61 return static_cast<OptionValueSInt64 *>(this);
62 return NULL;
63}
64
65OptionValueUInt64 *
66OptionValue::GetAsUInt64Value ()
67{
68 if (GetType () == OptionValue::eTypeUInt64)
69 return static_cast<OptionValueUInt64 *>(this);
70 return NULL;
71}
72
73OptionValueString *
74OptionValue::GetAsStringValue ()
75{
76 if (GetType () == OptionValue::eTypeString)
77 return static_cast<OptionValueString *>(this);
78 return NULL;
79}
80
81OptionValueFileSpec *
82OptionValue::GetAsFileSpecValue ()
83{
84 if (GetType () == OptionValue::eTypeFileSpec)
85 return static_cast<OptionValueFileSpec *>(this);
86 return NULL;
87}
88
89OptionValueArray *
90OptionValue::GetAsArrayValue ()
91{
92 if (GetType () == OptionValue::eTypeArray)
93 return static_cast<OptionValueArray *>(this);
94 return NULL;
95}
96
97OptionValueDictionary *
98OptionValue::GetAsDictionaryValue ()
99{
100 if (GetType () == OptionValue::eTypeDictionary)
101 return static_cast<OptionValueDictionary *>(this);
102 return NULL;
103}
104
105
Greg Clayton7406c392011-04-20 01:33:38 +0000106//-------------------------------------------------------------------------
Greg Claytonc08770b2011-04-21 19:21:29 +0000107// OptionValueCollection
Greg Clayton7406c392011-04-20 01:33:38 +0000108//-------------------------------------------------------------------------
109
110void
Greg Claytonc08770b2011-04-21 19:21:29 +0000111OptionValueCollection::GetQualifiedName (Stream &strm)
Greg Clayton7406c392011-04-20 01:33:38 +0000112{
113 if (m_parent)
114 {
115 m_parent->GetQualifiedName (strm);
116 strm.PutChar('.');
117 }
118 strm << m_name;
119}
120
Greg Clayton7406c392011-04-20 01:33:38 +0000121
Greg Clayton7406c392011-04-20 01:33:38 +0000122//-------------------------------------------------------------------------
123// OptionValueBoolean
124//-------------------------------------------------------------------------
125void
126OptionValueBoolean::DumpValue (Stream &strm)
127{
128 strm.PutCString (m_current_value ? "true" : "false");
129}
130
Greg Clayton17cd9952011-04-22 03:55:06 +0000131Error
Greg Clayton7406c392011-04-20 01:33:38 +0000132OptionValueBoolean::SetValueFromCString (const char *value_cstr)
133{
Greg Clayton17cd9952011-04-22 03:55:06 +0000134 Error error;
Greg Clayton7406c392011-04-20 01:33:38 +0000135 bool success = false;
136 bool value = Args::StringToBoolean(value_cstr, false, &success);
137 if (success)
138 {
Greg Clayton17cd9952011-04-22 03:55:06 +0000139 m_value_was_set = true;
Greg Clayton7406c392011-04-20 01:33:38 +0000140 m_current_value = value;
Greg Clayton7406c392011-04-20 01:33:38 +0000141 }
Greg Clayton17cd9952011-04-22 03:55:06 +0000142 else
143 {
144 error.SetErrorStringWithFormat ("invalid boolean string value: '%s'\n", value_cstr);
145 }
146 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000147}
148
149//-------------------------------------------------------------------------
150// OptionValueSInt64
151//-------------------------------------------------------------------------
152void
153OptionValueSInt64::DumpValue (Stream &strm)
154{
155 strm.Printf ("%lli", m_current_value);
156}
157
Greg Clayton17cd9952011-04-22 03:55:06 +0000158Error
Greg Clayton7406c392011-04-20 01:33:38 +0000159OptionValueSInt64::SetValueFromCString (const char *value_cstr)
160{
Greg Clayton17cd9952011-04-22 03:55:06 +0000161
162 Error error;
Greg Clayton7406c392011-04-20 01:33:38 +0000163 bool success = false;
164 int64_t value = Args::StringToSInt64 (value_cstr, 0, 0, &success);
165 if (success)
166 {
Greg Clayton17cd9952011-04-22 03:55:06 +0000167 m_value_was_set = true;
Greg Clayton7406c392011-04-20 01:33:38 +0000168 m_current_value = value;
Greg Clayton7406c392011-04-20 01:33:38 +0000169 }
Greg Clayton17cd9952011-04-22 03:55:06 +0000170 else
171 {
172 error.SetErrorStringWithFormat ("invalid int64_t string value: '%s'\n", value_cstr);
173 }
174 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000175}
176
177//-------------------------------------------------------------------------
178// OptionValueUInt64
179//-------------------------------------------------------------------------
Greg Clayton17cd9952011-04-22 03:55:06 +0000180
181lldb::OptionValueSP
182OptionValueUInt64::Create (const char *value_cstr, Error &error)
183{
184 lldb::OptionValueSP value_sp (new OptionValueUInt64());
185 error = value_sp->SetValueFromCString (value_cstr);
186 if (error.Fail())
187 value_sp.reset();
188 return value_sp;
189}
190
191
Greg Clayton7406c392011-04-20 01:33:38 +0000192void
193OptionValueUInt64::DumpValue (Stream &strm)
194{
195 strm.Printf ("0x%llx", m_current_value);
196}
197
Greg Clayton17cd9952011-04-22 03:55:06 +0000198Error
Greg Clayton7406c392011-04-20 01:33:38 +0000199OptionValueUInt64::SetValueFromCString (const char *value_cstr)
200{
Greg Clayton17cd9952011-04-22 03:55:06 +0000201 Error error;
Greg Clayton7406c392011-04-20 01:33:38 +0000202 bool success = false;
203 uint64_t value = Args::StringToUInt64 (value_cstr, 0, 0, &success);
204 if (success)
205 {
Greg Clayton17cd9952011-04-22 03:55:06 +0000206 m_value_was_set = true;
Greg Clayton7406c392011-04-20 01:33:38 +0000207 m_current_value = value;
Greg Clayton7406c392011-04-20 01:33:38 +0000208 }
Greg Clayton17cd9952011-04-22 03:55:06 +0000209 else
210 {
211 error.SetErrorStringWithFormat ("invalid uint64_t string value: '%s'\n", value_cstr);
212 }
213 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000214}
215
216//-------------------------------------------------------------------------
Greg Claytond12aeab2011-04-20 16:37:46 +0000217// OptionValueDictionary
218//-------------------------------------------------------------------------
219void
220OptionValueString::DumpValue (Stream &strm)
221{
222 strm.Printf ("\"%s\"", m_current_value.c_str());
223}
224
Greg Clayton17cd9952011-04-22 03:55:06 +0000225Error
Greg Claytond12aeab2011-04-20 16:37:46 +0000226OptionValueString::SetValueFromCString (const char *value_cstr)
227{
Greg Clayton17cd9952011-04-22 03:55:06 +0000228 m_value_was_set = true;
Greg Claytond12aeab2011-04-20 16:37:46 +0000229 SetCurrentValue (value_cstr);
Greg Clayton17cd9952011-04-22 03:55:06 +0000230 return Error ();
Greg Claytond12aeab2011-04-20 16:37:46 +0000231}
232
233
234
235//-------------------------------------------------------------------------
Greg Clayton7406c392011-04-20 01:33:38 +0000236// OptionValueFileSpec
237//-------------------------------------------------------------------------
238void
239OptionValueFileSpec::DumpValue (Stream &strm)
240{
241 if (m_current_value)
242 {
243 if (m_current_value.GetDirectory())
244 {
245 strm << '"' << m_current_value.GetDirectory();
246 if (m_current_value.GetFilename())
247 strm << '/' << m_current_value.GetFilename();
248 strm << '"';
249 }
250 else
251 {
252 strm << '"' << m_current_value.GetFilename() << '"';
253 }
254 }
255}
256
Greg Clayton17cd9952011-04-22 03:55:06 +0000257Error
Greg Clayton7406c392011-04-20 01:33:38 +0000258OptionValueFileSpec::SetValueFromCString (const char *value_cstr)
259{
260 if (value_cstr && value_cstr[0])
261 m_current_value.SetFile(value_cstr, false);
262 else
263 m_current_value.Clear();
Greg Clayton17cd9952011-04-22 03:55:06 +0000264 m_value_was_set = true;
265 return Error();
Greg Clayton7406c392011-04-20 01:33:38 +0000266}
267
268
269//-------------------------------------------------------------------------
270// OptionValueArray
271//-------------------------------------------------------------------------
272void
273OptionValueArray::DumpValue (Stream &strm)
274{
275 const uint32_t size = m_values.size();
276 for (uint32_t i = 0; i<size; ++i)
277 {
278 strm.Printf("[%u] ", i);
279 m_values[i]->DumpValue (strm);
280 }
281}
282
Greg Clayton17cd9952011-04-22 03:55:06 +0000283Error
Greg Clayton7406c392011-04-20 01:33:38 +0000284OptionValueArray::SetValueFromCString (const char *value_cstr)
285{
Greg Clayton17cd9952011-04-22 03:55:06 +0000286 Error error;
287 error.SetErrorStringWithFormat ("array option values don't yet support being set by string: '%s'\n", value_cstr);
288 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000289}
290
Greg Clayton17cd9952011-04-22 03:55:06 +0000291
292uint64_t
293OptionValueArray::GetUInt64ValueAtIndex (uint32_t idx, uint64_t fail_value, bool *success_ptr) const
294{
295 if (idx < m_values.size())
296 return m_values[idx]->GetUInt64Value (fail_value, success_ptr);
297 return fail_value;
298}
299
300
301
Greg Clayton7406c392011-04-20 01:33:38 +0000302//-------------------------------------------------------------------------
303// OptionValueDictionary
304//-------------------------------------------------------------------------
305void
306OptionValueDictionary::DumpValue (Stream &strm)
307{
308 collection::iterator pos, end = m_values.end();
309
310 for (pos = m_values.begin(); pos != end; ++pos)
311 {
312 strm.Printf("%s=", pos->first.GetCString());
313 pos->second->DumpValue (strm);
314 }
315}
316
Greg Clayton17cd9952011-04-22 03:55:06 +0000317Error
Greg Clayton7406c392011-04-20 01:33:38 +0000318OptionValueDictionary::SetValueFromCString (const char *value_cstr)
319{
Greg Clayton17cd9952011-04-22 03:55:06 +0000320 Error error;
321 error.SetErrorStringWithFormat ("dictionary option values don't yet support being set by string: '%s'\n", value_cstr);
322 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000323}
324
Greg Claytone9728452011-04-21 17:46:10 +0000325lldb::OptionValueSP
326OptionValueDictionary::GetValueForKey (const ConstString &key) const
327{
328 lldb::OptionValueSP value_sp;
329 collection::const_iterator pos = m_values.find (key);
330 if (pos != m_values.end())
331 value_sp = pos->second;
332 return value_sp;
333}
334
335const char *
336OptionValueDictionary::GetStringValueForKey (const ConstString &key)
337{
338 collection::const_iterator pos = m_values.find (key);
339 if (pos != m_values.end())
340 {
341 if (pos->second->GetType() == OptionValue::eTypeString)
342 return static_cast<OptionValueString *>(pos->second.get())->GetCurrentValue();
343 }
344 return NULL;
345}
346
347
348bool
349OptionValueDictionary::SetStringValueForKey (const ConstString &key,
350 const char *value,
351 bool can_replace)
352{
353 collection::const_iterator pos = m_values.find (key);
354 if (pos != m_values.end())
355 {
356 if (!can_replace)
357 return false;
358 if (pos->second->GetType() == OptionValue::eTypeString)
359 {
360 pos->second->SetValueFromCString(value);
361 return true;
362 }
363 }
364 m_values[key] = OptionValueSP (new OptionValueString (value));
365 return true;
366
367}
368
369bool
370OptionValueDictionary::SetValueForKey (const ConstString &key,
371 const lldb::OptionValueSP &value_sp,
372 bool can_replace)
373{
374 // Make sure the value_sp object is allowed to contain
375 // values of the type passed in...
376 if (value_sp && (m_type_mask & value_sp->GetTypeAsMask()))
377 {
378 if (!can_replace)
379 {
380 collection::const_iterator pos = m_values.find (key);
381 if (pos != m_values.end())
382 return false;
383 }
384 m_values[key] = value_sp;
385 return true;
386 }
387 return false;
388}
389
390bool
391OptionValueDictionary::DeleteValueForKey (const ConstString &key)
392{
393 collection::iterator pos = m_values.find (key);
394 if (pos != m_values.end())
395 {
396 m_values.erase(pos);
397 return true;
398 }
399 return false;
400}
401
Greg Clayton7406c392011-04-20 01:33:38 +0000402