blob: ab4f4d0da64eb329f6c0ef9cfad49bedfea2d16f [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
Greg Clayton3182eff2011-06-23 21:22:24 +000016#include "lldb/Core/FormatManager.h"
Greg Clayton57b3c6b2011-04-27 22:04:39 +000017#include "lldb/Core/State.h"
Greg Clayton7406c392011-04-20 01:33:38 +000018#include "lldb/Core/Stream.h"
19#include "lldb/Interpreter/Args.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
Greg Claytone9728452011-04-21 17:46:10 +000024
25//-------------------------------------------------------------------------
26// OptionValue
27//-------------------------------------------------------------------------
Greg Clayton17cd9952011-04-22 03:55:06 +000028
29// Get this value as a uint64_t value if it is encoded as a boolean,
30// uint64_t or int64_t. Other types will cause "fail_value" to be
31// returned
32uint64_t
33OptionValue::GetUInt64Value (uint64_t fail_value, bool *success_ptr)
34{
35 if (success_ptr)
36 *success_ptr = true;
37 switch (GetType())
38 {
39 case OptionValue::eTypeBoolean: return static_cast<OptionValueBoolean *>(this)->GetCurrentValue();
40 case OptionValue::eTypeSInt64: return static_cast<OptionValueSInt64 *>(this)->GetCurrentValue();
41 case OptionValue::eTypeUInt64: return static_cast<OptionValueUInt64 *>(this)->GetCurrentValue();
42 default:
43 break;
44 }
45 if (success_ptr)
46 *success_ptr = false;
47 return fail_value;
48}
49
50
Greg Claytone9728452011-04-21 17:46:10 +000051OptionValueBoolean *
Greg Clayton57b3c6b2011-04-27 22:04:39 +000052OptionValue::GetAsBoolean ()
Greg Claytone9728452011-04-21 17:46:10 +000053{
54 if (GetType () == OptionValue::eTypeBoolean)
55 return static_cast<OptionValueBoolean *>(this);
56 return NULL;
57}
58
59OptionValueSInt64 *
Greg Clayton57b3c6b2011-04-27 22:04:39 +000060OptionValue::GetAsSInt64 ()
Greg Claytone9728452011-04-21 17:46:10 +000061{
62 if (GetType () == OptionValue::eTypeSInt64)
63 return static_cast<OptionValueSInt64 *>(this);
64 return NULL;
65}
66
67OptionValueUInt64 *
Greg Clayton57b3c6b2011-04-27 22:04:39 +000068OptionValue::GetAsUInt64 ()
Greg Claytone9728452011-04-21 17:46:10 +000069{
70 if (GetType () == OptionValue::eTypeUInt64)
71 return static_cast<OptionValueUInt64 *>(this);
72 return NULL;
73}
74
75OptionValueString *
Greg Clayton57b3c6b2011-04-27 22:04:39 +000076OptionValue::GetAsString ()
Greg Claytone9728452011-04-21 17:46:10 +000077{
78 if (GetType () == OptionValue::eTypeString)
79 return static_cast<OptionValueString *>(this);
80 return NULL;
81}
82
83OptionValueFileSpec *
Greg Clayton57b3c6b2011-04-27 22:04:39 +000084OptionValue::GetAsFileSpec ()
Greg Claytone9728452011-04-21 17:46:10 +000085{
86 if (GetType () == OptionValue::eTypeFileSpec)
87 return static_cast<OptionValueFileSpec *>(this);
88 return NULL;
Greg Clayton57b3c6b2011-04-27 22:04:39 +000089
90}
91
92OptionValueFormat *
93OptionValue::GetAsFormat ()
94{
95 if (GetType () == OptionValue::eTypeFormat)
96 return static_cast<OptionValueFormat *>(this);
97 return NULL;
Greg Claytone9728452011-04-21 17:46:10 +000098}
99
Greg Claytone1f50b92011-05-03 22:09:39 +0000100OptionValueUUID *
101OptionValue::GetAsUUID ()
102{
103 if (GetType () == OptionValue::eTypeUUID)
104 return static_cast<OptionValueUUID *>(this);
105 return NULL;
106
107}
108
109
Greg Claytone9728452011-04-21 17:46:10 +0000110OptionValueArray *
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000111OptionValue::GetAsArray ()
Greg Claytone9728452011-04-21 17:46:10 +0000112{
113 if (GetType () == OptionValue::eTypeArray)
114 return static_cast<OptionValueArray *>(this);
115 return NULL;
116}
117
118OptionValueDictionary *
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000119OptionValue::GetAsDictionary ()
Greg Claytone9728452011-04-21 17:46:10 +0000120{
121 if (GetType () == OptionValue::eTypeDictionary)
122 return static_cast<OptionValueDictionary *>(this);
123 return NULL;
124}
125
Caroline Ticedfb2e202011-04-22 05:08:45 +0000126const char *
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000127OptionValue::GetStringValue (const char *fail_value)
Caroline Ticedfb2e202011-04-22 05:08:45 +0000128{
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000129 OptionValueString *option_value = GetAsString ();
130 if (option_value)
131 return option_value->GetCurrentValue();
132 return fail_value;
Caroline Ticedfb2e202011-04-22 05:08:45 +0000133}
134
135uint64_t
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000136OptionValue::GetUInt64Value (uint64_t fail_value)
Caroline Ticedfb2e202011-04-22 05:08:45 +0000137{
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000138 OptionValueUInt64 *option_value = GetAsUInt64 ();
139 if (option_value)
140 return option_value->GetCurrentValue();
141 return fail_value;
142}
143
144lldb::Format
145OptionValue::GetFormatValue (lldb::Format fail_value)
146{
147 OptionValueFormat *option_value = GetAsFormat ();
148 if (option_value)
149 return option_value->GetCurrentValue();
150 return fail_value;
Caroline Ticedfb2e202011-04-22 05:08:45 +0000151}
Greg Claytone9728452011-04-21 17:46:10 +0000152
Greg Clayton7406c392011-04-20 01:33:38 +0000153//-------------------------------------------------------------------------
Greg Claytonc08770b2011-04-21 19:21:29 +0000154// OptionValueCollection
Greg Clayton7406c392011-04-20 01:33:38 +0000155//-------------------------------------------------------------------------
156
157void
Greg Claytonc08770b2011-04-21 19:21:29 +0000158OptionValueCollection::GetQualifiedName (Stream &strm)
Greg Clayton7406c392011-04-20 01:33:38 +0000159{
160 if (m_parent)
161 {
162 m_parent->GetQualifiedName (strm);
163 strm.PutChar('.');
164 }
165 strm << m_name;
166}
167
Greg Clayton7406c392011-04-20 01:33:38 +0000168
Greg Clayton7406c392011-04-20 01:33:38 +0000169//-------------------------------------------------------------------------
170// OptionValueBoolean
171//-------------------------------------------------------------------------
172void
173OptionValueBoolean::DumpValue (Stream &strm)
174{
175 strm.PutCString (m_current_value ? "true" : "false");
176}
177
Greg Clayton17cd9952011-04-22 03:55:06 +0000178Error
Greg Clayton7406c392011-04-20 01:33:38 +0000179OptionValueBoolean::SetValueFromCString (const char *value_cstr)
180{
Greg Clayton17cd9952011-04-22 03:55:06 +0000181 Error error;
Greg Clayton7406c392011-04-20 01:33:38 +0000182 bool success = false;
183 bool value = Args::StringToBoolean(value_cstr, false, &success);
184 if (success)
185 {
Greg Clayton17cd9952011-04-22 03:55:06 +0000186 m_value_was_set = true;
Greg Clayton7406c392011-04-20 01:33:38 +0000187 m_current_value = value;
Greg Clayton7406c392011-04-20 01:33:38 +0000188 }
Greg Clayton17cd9952011-04-22 03:55:06 +0000189 else
190 {
Greg Claytonff44ab42011-04-23 02:04:55 +0000191 if (value_cstr == NULL)
192 error.SetErrorString ("invalid boolean string value: NULL\n");
193 else if (value_cstr[0] == '\0')
194 error.SetErrorString ("invalid boolean string value <empty>\n");
195 else
196 error.SetErrorStringWithFormat ("invalid boolean string value: '%s'\n", value_cstr);
Greg Clayton17cd9952011-04-22 03:55:06 +0000197 }
198 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000199}
200
201//-------------------------------------------------------------------------
202// OptionValueSInt64
203//-------------------------------------------------------------------------
204void
205OptionValueSInt64::DumpValue (Stream &strm)
206{
207 strm.Printf ("%lli", m_current_value);
208}
209
Greg Clayton17cd9952011-04-22 03:55:06 +0000210Error
Greg Clayton7406c392011-04-20 01:33:38 +0000211OptionValueSInt64::SetValueFromCString (const char *value_cstr)
212{
Greg Clayton17cd9952011-04-22 03:55:06 +0000213
214 Error error;
Greg Clayton7406c392011-04-20 01:33:38 +0000215 bool success = false;
216 int64_t value = Args::StringToSInt64 (value_cstr, 0, 0, &success);
217 if (success)
218 {
Greg Clayton17cd9952011-04-22 03:55:06 +0000219 m_value_was_set = true;
Greg Clayton7406c392011-04-20 01:33:38 +0000220 m_current_value = value;
Greg Clayton7406c392011-04-20 01:33:38 +0000221 }
Greg Clayton17cd9952011-04-22 03:55:06 +0000222 else
223 {
224 error.SetErrorStringWithFormat ("invalid int64_t string value: '%s'\n", value_cstr);
225 }
226 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000227}
228
229//-------------------------------------------------------------------------
230// OptionValueUInt64
231//-------------------------------------------------------------------------
Greg Clayton17cd9952011-04-22 03:55:06 +0000232
233lldb::OptionValueSP
234OptionValueUInt64::Create (const char *value_cstr, Error &error)
235{
236 lldb::OptionValueSP value_sp (new OptionValueUInt64());
237 error = value_sp->SetValueFromCString (value_cstr);
238 if (error.Fail())
239 value_sp.reset();
240 return value_sp;
241}
242
243
Greg Clayton7406c392011-04-20 01:33:38 +0000244void
245OptionValueUInt64::DumpValue (Stream &strm)
246{
247 strm.Printf ("0x%llx", m_current_value);
248}
249
Greg Clayton17cd9952011-04-22 03:55:06 +0000250Error
Greg Clayton7406c392011-04-20 01:33:38 +0000251OptionValueUInt64::SetValueFromCString (const char *value_cstr)
252{
Greg Clayton17cd9952011-04-22 03:55:06 +0000253 Error error;
Greg Clayton7406c392011-04-20 01:33:38 +0000254 bool success = false;
255 uint64_t value = Args::StringToUInt64 (value_cstr, 0, 0, &success);
256 if (success)
257 {
Greg Clayton17cd9952011-04-22 03:55:06 +0000258 m_value_was_set = true;
Greg Clayton7406c392011-04-20 01:33:38 +0000259 m_current_value = value;
Greg Clayton7406c392011-04-20 01:33:38 +0000260 }
Greg Clayton17cd9952011-04-22 03:55:06 +0000261 else
262 {
263 error.SetErrorStringWithFormat ("invalid uint64_t string value: '%s'\n", value_cstr);
264 }
265 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000266}
267
268//-------------------------------------------------------------------------
Greg Claytond12aeab2011-04-20 16:37:46 +0000269// OptionValueDictionary
270//-------------------------------------------------------------------------
271void
272OptionValueString::DumpValue (Stream &strm)
273{
274 strm.Printf ("\"%s\"", m_current_value.c_str());
275}
276
Greg Clayton17cd9952011-04-22 03:55:06 +0000277Error
Greg Claytond12aeab2011-04-20 16:37:46 +0000278OptionValueString::SetValueFromCString (const char *value_cstr)
279{
Greg Clayton17cd9952011-04-22 03:55:06 +0000280 m_value_was_set = true;
Greg Claytond12aeab2011-04-20 16:37:46 +0000281 SetCurrentValue (value_cstr);
Greg Clayton17cd9952011-04-22 03:55:06 +0000282 return Error ();
Greg Claytond12aeab2011-04-20 16:37:46 +0000283}
284
Greg Claytond12aeab2011-04-20 16:37:46 +0000285//-------------------------------------------------------------------------
Greg Clayton7406c392011-04-20 01:33:38 +0000286// OptionValueFileSpec
287//-------------------------------------------------------------------------
288void
289OptionValueFileSpec::DumpValue (Stream &strm)
290{
291 if (m_current_value)
292 {
293 if (m_current_value.GetDirectory())
294 {
295 strm << '"' << m_current_value.GetDirectory();
296 if (m_current_value.GetFilename())
297 strm << '/' << m_current_value.GetFilename();
298 strm << '"';
299 }
300 else
301 {
302 strm << '"' << m_current_value.GetFilename() << '"';
303 }
304 }
305}
306
Greg Clayton17cd9952011-04-22 03:55:06 +0000307Error
Greg Clayton7406c392011-04-20 01:33:38 +0000308OptionValueFileSpec::SetValueFromCString (const char *value_cstr)
309{
310 if (value_cstr && value_cstr[0])
311 m_current_value.SetFile(value_cstr, false);
312 else
313 m_current_value.Clear();
Greg Clayton17cd9952011-04-22 03:55:06 +0000314 m_value_was_set = true;
315 return Error();
Greg Clayton7406c392011-04-20 01:33:38 +0000316}
317
Greg Clayton801417e2011-07-07 01:59:51 +0000318//-------------------------------------------------------------------------
319// OptionValueFileSpecList
320//-------------------------------------------------------------------------
321void
322OptionValueFileSpecList::DumpValue (Stream &strm)
323{
324 m_current_value.Dump(&strm, "\n");
325}
326
327Error
328OptionValueFileSpecList::SetValueFromCString (const char *value_cstr)
329{
330 if (value_cstr && value_cstr[0])
331 {
332 FileSpec file (value_cstr, false);
333 m_current_value.Append(file);
334 }
335 m_value_was_set = true;
336 return Error();
337}
338
Greg Clayton7406c392011-04-20 01:33:38 +0000339
340//-------------------------------------------------------------------------
Greg Claytone1f50b92011-05-03 22:09:39 +0000341// OptionValueUUID
342//-------------------------------------------------------------------------
343void
344OptionValueUUID::DumpValue (Stream &strm)
345{
346 m_uuid.Dump (&strm);
347}
348
349Error
350OptionValueUUID::SetValueFromCString (const char *value_cstr)
351{
352 Error error;
353 if (m_uuid.SetfromCString(value_cstr) == 0)
354 error.SetErrorStringWithFormat ("invalid uuid string value '%s'", value_cstr);
355 return error;
356}
357
358//-------------------------------------------------------------------------
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000359// OptionValueFormat
360//-------------------------------------------------------------------------
361void
362OptionValueFormat::DumpValue (Stream &strm)
363{
Greg Clayton3182eff2011-06-23 21:22:24 +0000364 strm.PutCString (FormatManager::GetFormatAsCString (m_current_value));
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000365}
366
367Error
368OptionValueFormat::SetValueFromCString (const char *value_cstr)
369{
370 Format new_format;
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000371 uint32_t new_byte_size = UINT32_MAX;
372 Error error (Args::StringToFormat(value_cstr, new_format, m_byte_size_prefix_ok ? &new_byte_size : NULL));
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000373 if (error.Success())
374 {
375 m_value_was_set = true;
376 m_current_value = new_format;
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000377 if (new_byte_size != UINT32_MAX)
378 m_current_byte_size = new_byte_size;
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000379 }
380 return error;
381}
382
383
384//-------------------------------------------------------------------------
Greg Clayton7406c392011-04-20 01:33:38 +0000385// OptionValueArray
386//-------------------------------------------------------------------------
387void
388OptionValueArray::DumpValue (Stream &strm)
389{
390 const uint32_t size = m_values.size();
391 for (uint32_t i = 0; i<size; ++i)
392 {
393 strm.Printf("[%u] ", i);
394 m_values[i]->DumpValue (strm);
395 }
396}
397
Greg Clayton17cd9952011-04-22 03:55:06 +0000398Error
Greg Clayton7406c392011-04-20 01:33:38 +0000399OptionValueArray::SetValueFromCString (const char *value_cstr)
400{
Greg Clayton17cd9952011-04-22 03:55:06 +0000401 Error error;
402 error.SetErrorStringWithFormat ("array option values don't yet support being set by string: '%s'\n", value_cstr);
403 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000404}
405
406//-------------------------------------------------------------------------
407// OptionValueDictionary
408//-------------------------------------------------------------------------
409void
410OptionValueDictionary::DumpValue (Stream &strm)
411{
412 collection::iterator pos, end = m_values.end();
413
414 for (pos = m_values.begin(); pos != end; ++pos)
415 {
416 strm.Printf("%s=", pos->first.GetCString());
417 pos->second->DumpValue (strm);
418 }
419}
420
Greg Clayton17cd9952011-04-22 03:55:06 +0000421Error
Greg Clayton7406c392011-04-20 01:33:38 +0000422OptionValueDictionary::SetValueFromCString (const char *value_cstr)
423{
Greg Clayton17cd9952011-04-22 03:55:06 +0000424 Error error;
425 error.SetErrorStringWithFormat ("dictionary option values don't yet support being set by string: '%s'\n", value_cstr);
426 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000427}
428
Greg Claytone9728452011-04-21 17:46:10 +0000429lldb::OptionValueSP
430OptionValueDictionary::GetValueForKey (const ConstString &key) const
431{
432 lldb::OptionValueSP value_sp;
433 collection::const_iterator pos = m_values.find (key);
434 if (pos != m_values.end())
435 value_sp = pos->second;
436 return value_sp;
437}
438
439const char *
440OptionValueDictionary::GetStringValueForKey (const ConstString &key)
441{
442 collection::const_iterator pos = m_values.find (key);
443 if (pos != m_values.end())
444 {
445 if (pos->second->GetType() == OptionValue::eTypeString)
446 return static_cast<OptionValueString *>(pos->second.get())->GetCurrentValue();
447 }
448 return NULL;
449}
450
451
452bool
453OptionValueDictionary::SetStringValueForKey (const ConstString &key,
454 const char *value,
455 bool can_replace)
456{
457 collection::const_iterator pos = m_values.find (key);
458 if (pos != m_values.end())
459 {
460 if (!can_replace)
461 return false;
462 if (pos->second->GetType() == OptionValue::eTypeString)
463 {
464 pos->second->SetValueFromCString(value);
465 return true;
466 }
467 }
468 m_values[key] = OptionValueSP (new OptionValueString (value));
469 return true;
470
471}
472
473bool
474OptionValueDictionary::SetValueForKey (const ConstString &key,
475 const lldb::OptionValueSP &value_sp,
476 bool can_replace)
477{
478 // Make sure the value_sp object is allowed to contain
479 // values of the type passed in...
480 if (value_sp && (m_type_mask & value_sp->GetTypeAsMask()))
481 {
482 if (!can_replace)
483 {
484 collection::const_iterator pos = m_values.find (key);
485 if (pos != m_values.end())
486 return false;
487 }
488 m_values[key] = value_sp;
489 return true;
490 }
491 return false;
492}
493
494bool
495OptionValueDictionary::DeleteValueForKey (const ConstString &key)
496{
497 collection::iterator pos = m_values.find (key);
498 if (pos != m_values.end())
499 {
500 m_values.erase(pos);
501 return true;
502 }
503 return false;
504}
505
Greg Clayton7406c392011-04-20 01:33:38 +0000506