blob: 4ab98106496d465fdb3484f5d05309f442deecae [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 Clayton57b3c6b2011-04-27 22:04:39 +000016#include "lldb/Core/State.h"
Greg Clayton7406c392011-04-20 01:33:38 +000017#include "lldb/Core/Stream.h"
18#include "lldb/Interpreter/Args.h"
19
20using namespace lldb;
21using namespace lldb_private;
22
Greg Claytone9728452011-04-21 17:46:10 +000023
24//-------------------------------------------------------------------------
25// OptionValue
26//-------------------------------------------------------------------------
Greg Clayton17cd9952011-04-22 03:55:06 +000027
28// Get this value as a uint64_t value if it is encoded as a boolean,
29// uint64_t or int64_t. Other types will cause "fail_value" to be
30// returned
31uint64_t
32OptionValue::GetUInt64Value (uint64_t fail_value, bool *success_ptr)
33{
34 if (success_ptr)
35 *success_ptr = true;
36 switch (GetType())
37 {
38 case OptionValue::eTypeBoolean: return static_cast<OptionValueBoolean *>(this)->GetCurrentValue();
39 case OptionValue::eTypeSInt64: return static_cast<OptionValueSInt64 *>(this)->GetCurrentValue();
40 case OptionValue::eTypeUInt64: return static_cast<OptionValueUInt64 *>(this)->GetCurrentValue();
41 default:
42 break;
43 }
44 if (success_ptr)
45 *success_ptr = false;
46 return fail_value;
47}
48
49
Greg Claytone9728452011-04-21 17:46:10 +000050OptionValueBoolean *
Greg Clayton57b3c6b2011-04-27 22:04:39 +000051OptionValue::GetAsBoolean ()
Greg Claytone9728452011-04-21 17:46:10 +000052{
53 if (GetType () == OptionValue::eTypeBoolean)
54 return static_cast<OptionValueBoolean *>(this);
55 return NULL;
56}
57
58OptionValueSInt64 *
Greg Clayton57b3c6b2011-04-27 22:04:39 +000059OptionValue::GetAsSInt64 ()
Greg Claytone9728452011-04-21 17:46:10 +000060{
61 if (GetType () == OptionValue::eTypeSInt64)
62 return static_cast<OptionValueSInt64 *>(this);
63 return NULL;
64}
65
66OptionValueUInt64 *
Greg Clayton57b3c6b2011-04-27 22:04:39 +000067OptionValue::GetAsUInt64 ()
Greg Claytone9728452011-04-21 17:46:10 +000068{
69 if (GetType () == OptionValue::eTypeUInt64)
70 return static_cast<OptionValueUInt64 *>(this);
71 return NULL;
72}
73
74OptionValueString *
Greg Clayton57b3c6b2011-04-27 22:04:39 +000075OptionValue::GetAsString ()
Greg Claytone9728452011-04-21 17:46:10 +000076{
77 if (GetType () == OptionValue::eTypeString)
78 return static_cast<OptionValueString *>(this);
79 return NULL;
80}
81
82OptionValueFileSpec *
Greg Clayton57b3c6b2011-04-27 22:04:39 +000083OptionValue::GetAsFileSpec ()
Greg Claytone9728452011-04-21 17:46:10 +000084{
85 if (GetType () == OptionValue::eTypeFileSpec)
86 return static_cast<OptionValueFileSpec *>(this);
87 return NULL;
Greg Clayton57b3c6b2011-04-27 22:04:39 +000088
89}
90
91OptionValueFormat *
92OptionValue::GetAsFormat ()
93{
94 if (GetType () == OptionValue::eTypeFormat)
95 return static_cast<OptionValueFormat *>(this);
96 return NULL;
Greg Claytone9728452011-04-21 17:46:10 +000097}
98
Greg Claytone1f50b92011-05-03 22:09:39 +000099OptionValueUUID *
100OptionValue::GetAsUUID ()
101{
102 if (GetType () == OptionValue::eTypeUUID)
103 return static_cast<OptionValueUUID *>(this);
104 return NULL;
105
106}
107
108
Greg Claytone9728452011-04-21 17:46:10 +0000109OptionValueArray *
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000110OptionValue::GetAsArray ()
Greg Claytone9728452011-04-21 17:46:10 +0000111{
112 if (GetType () == OptionValue::eTypeArray)
113 return static_cast<OptionValueArray *>(this);
114 return NULL;
115}
116
117OptionValueDictionary *
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000118OptionValue::GetAsDictionary ()
Greg Claytone9728452011-04-21 17:46:10 +0000119{
120 if (GetType () == OptionValue::eTypeDictionary)
121 return static_cast<OptionValueDictionary *>(this);
122 return NULL;
123}
124
Caroline Ticedfb2e202011-04-22 05:08:45 +0000125const char *
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000126OptionValue::GetStringValue (const char *fail_value)
Caroline Ticedfb2e202011-04-22 05:08:45 +0000127{
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000128 OptionValueString *option_value = GetAsString ();
129 if (option_value)
130 return option_value->GetCurrentValue();
131 return fail_value;
Caroline Ticedfb2e202011-04-22 05:08:45 +0000132}
133
134uint64_t
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000135OptionValue::GetUInt64Value (uint64_t fail_value)
Caroline Ticedfb2e202011-04-22 05:08:45 +0000136{
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000137 OptionValueUInt64 *option_value = GetAsUInt64 ();
138 if (option_value)
139 return option_value->GetCurrentValue();
140 return fail_value;
141}
142
143lldb::Format
144OptionValue::GetFormatValue (lldb::Format fail_value)
145{
146 OptionValueFormat *option_value = GetAsFormat ();
147 if (option_value)
148 return option_value->GetCurrentValue();
149 return fail_value;
Caroline Ticedfb2e202011-04-22 05:08:45 +0000150}
Greg Claytone9728452011-04-21 17:46:10 +0000151
Greg Clayton7406c392011-04-20 01:33:38 +0000152//-------------------------------------------------------------------------
Greg Claytonc08770b2011-04-21 19:21:29 +0000153// OptionValueCollection
Greg Clayton7406c392011-04-20 01:33:38 +0000154//-------------------------------------------------------------------------
155
156void
Greg Claytonc08770b2011-04-21 19:21:29 +0000157OptionValueCollection::GetQualifiedName (Stream &strm)
Greg Clayton7406c392011-04-20 01:33:38 +0000158{
159 if (m_parent)
160 {
161 m_parent->GetQualifiedName (strm);
162 strm.PutChar('.');
163 }
164 strm << m_name;
165}
166
Greg Clayton7406c392011-04-20 01:33:38 +0000167
Greg Clayton7406c392011-04-20 01:33:38 +0000168//-------------------------------------------------------------------------
169// OptionValueBoolean
170//-------------------------------------------------------------------------
171void
172OptionValueBoolean::DumpValue (Stream &strm)
173{
174 strm.PutCString (m_current_value ? "true" : "false");
175}
176
Greg Clayton17cd9952011-04-22 03:55:06 +0000177Error
Greg Clayton7406c392011-04-20 01:33:38 +0000178OptionValueBoolean::SetValueFromCString (const char *value_cstr)
179{
Greg Clayton17cd9952011-04-22 03:55:06 +0000180 Error error;
Greg Clayton7406c392011-04-20 01:33:38 +0000181 bool success = false;
182 bool value = Args::StringToBoolean(value_cstr, false, &success);
183 if (success)
184 {
Greg Clayton17cd9952011-04-22 03:55:06 +0000185 m_value_was_set = true;
Greg Clayton7406c392011-04-20 01:33:38 +0000186 m_current_value = value;
Greg Clayton7406c392011-04-20 01:33:38 +0000187 }
Greg Clayton17cd9952011-04-22 03:55:06 +0000188 else
189 {
Greg Claytonff44ab42011-04-23 02:04:55 +0000190 if (value_cstr == NULL)
191 error.SetErrorString ("invalid boolean string value: NULL\n");
192 else if (value_cstr[0] == '\0')
193 error.SetErrorString ("invalid boolean string value <empty>\n");
194 else
195 error.SetErrorStringWithFormat ("invalid boolean string value: '%s'\n", value_cstr);
Greg Clayton17cd9952011-04-22 03:55:06 +0000196 }
197 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000198}
199
200//-------------------------------------------------------------------------
201// OptionValueSInt64
202//-------------------------------------------------------------------------
203void
204OptionValueSInt64::DumpValue (Stream &strm)
205{
206 strm.Printf ("%lli", m_current_value);
207}
208
Greg Clayton17cd9952011-04-22 03:55:06 +0000209Error
Greg Clayton7406c392011-04-20 01:33:38 +0000210OptionValueSInt64::SetValueFromCString (const char *value_cstr)
211{
Greg Clayton17cd9952011-04-22 03:55:06 +0000212
213 Error error;
Greg Clayton7406c392011-04-20 01:33:38 +0000214 bool success = false;
215 int64_t value = Args::StringToSInt64 (value_cstr, 0, 0, &success);
216 if (success)
217 {
Greg Clayton17cd9952011-04-22 03:55:06 +0000218 m_value_was_set = true;
Greg Clayton7406c392011-04-20 01:33:38 +0000219 m_current_value = value;
Greg Clayton7406c392011-04-20 01:33:38 +0000220 }
Greg Clayton17cd9952011-04-22 03:55:06 +0000221 else
222 {
223 error.SetErrorStringWithFormat ("invalid int64_t string value: '%s'\n", value_cstr);
224 }
225 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000226}
227
228//-------------------------------------------------------------------------
229// OptionValueUInt64
230//-------------------------------------------------------------------------
Greg Clayton17cd9952011-04-22 03:55:06 +0000231
232lldb::OptionValueSP
233OptionValueUInt64::Create (const char *value_cstr, Error &error)
234{
235 lldb::OptionValueSP value_sp (new OptionValueUInt64());
236 error = value_sp->SetValueFromCString (value_cstr);
237 if (error.Fail())
238 value_sp.reset();
239 return value_sp;
240}
241
242
Greg Clayton7406c392011-04-20 01:33:38 +0000243void
244OptionValueUInt64::DumpValue (Stream &strm)
245{
246 strm.Printf ("0x%llx", m_current_value);
247}
248
Greg Clayton17cd9952011-04-22 03:55:06 +0000249Error
Greg Clayton7406c392011-04-20 01:33:38 +0000250OptionValueUInt64::SetValueFromCString (const char *value_cstr)
251{
Greg Clayton17cd9952011-04-22 03:55:06 +0000252 Error error;
Greg Clayton7406c392011-04-20 01:33:38 +0000253 bool success = false;
254 uint64_t value = Args::StringToUInt64 (value_cstr, 0, 0, &success);
255 if (success)
256 {
Greg Clayton17cd9952011-04-22 03:55:06 +0000257 m_value_was_set = true;
Greg Clayton7406c392011-04-20 01:33:38 +0000258 m_current_value = value;
Greg Clayton7406c392011-04-20 01:33:38 +0000259 }
Greg Clayton17cd9952011-04-22 03:55:06 +0000260 else
261 {
262 error.SetErrorStringWithFormat ("invalid uint64_t string value: '%s'\n", value_cstr);
263 }
264 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000265}
266
267//-------------------------------------------------------------------------
Greg Claytond12aeab2011-04-20 16:37:46 +0000268// OptionValueDictionary
269//-------------------------------------------------------------------------
270void
271OptionValueString::DumpValue (Stream &strm)
272{
273 strm.Printf ("\"%s\"", m_current_value.c_str());
274}
275
Greg Clayton17cd9952011-04-22 03:55:06 +0000276Error
Greg Claytond12aeab2011-04-20 16:37:46 +0000277OptionValueString::SetValueFromCString (const char *value_cstr)
278{
Greg Clayton17cd9952011-04-22 03:55:06 +0000279 m_value_was_set = true;
Greg Claytond12aeab2011-04-20 16:37:46 +0000280 SetCurrentValue (value_cstr);
Greg Clayton17cd9952011-04-22 03:55:06 +0000281 return Error ();
Greg Claytond12aeab2011-04-20 16:37:46 +0000282}
283
Greg Claytond12aeab2011-04-20 16:37:46 +0000284//-------------------------------------------------------------------------
Greg Clayton7406c392011-04-20 01:33:38 +0000285// OptionValueFileSpec
286//-------------------------------------------------------------------------
287void
288OptionValueFileSpec::DumpValue (Stream &strm)
289{
290 if (m_current_value)
291 {
292 if (m_current_value.GetDirectory())
293 {
294 strm << '"' << m_current_value.GetDirectory();
295 if (m_current_value.GetFilename())
296 strm << '/' << m_current_value.GetFilename();
297 strm << '"';
298 }
299 else
300 {
301 strm << '"' << m_current_value.GetFilename() << '"';
302 }
303 }
304}
305
Greg Clayton17cd9952011-04-22 03:55:06 +0000306Error
Greg Clayton7406c392011-04-20 01:33:38 +0000307OptionValueFileSpec::SetValueFromCString (const char *value_cstr)
308{
309 if (value_cstr && value_cstr[0])
310 m_current_value.SetFile(value_cstr, false);
311 else
312 m_current_value.Clear();
Greg Clayton17cd9952011-04-22 03:55:06 +0000313 m_value_was_set = true;
314 return Error();
Greg Clayton7406c392011-04-20 01:33:38 +0000315}
316
317
318//-------------------------------------------------------------------------
Greg Claytone1f50b92011-05-03 22:09:39 +0000319// OptionValueUUID
320//-------------------------------------------------------------------------
321void
322OptionValueUUID::DumpValue (Stream &strm)
323{
324 m_uuid.Dump (&strm);
325}
326
327Error
328OptionValueUUID::SetValueFromCString (const char *value_cstr)
329{
330 Error error;
331 if (m_uuid.SetfromCString(value_cstr) == 0)
332 error.SetErrorStringWithFormat ("invalid uuid string value '%s'", value_cstr);
333 return error;
334}
335
336//-------------------------------------------------------------------------
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000337// OptionValueFormat
338//-------------------------------------------------------------------------
339void
340OptionValueFormat::DumpValue (Stream &strm)
341{
342 strm.PutCString (GetFormatAsCString (m_current_value));
343}
344
345Error
346OptionValueFormat::SetValueFromCString (const char *value_cstr)
347{
348 Format new_format;
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000349 uint32_t new_byte_size = UINT32_MAX;
350 Error error (Args::StringToFormat(value_cstr, new_format, m_byte_size_prefix_ok ? &new_byte_size : NULL));
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000351 if (error.Success())
352 {
353 m_value_was_set = true;
354 m_current_value = new_format;
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000355 if (new_byte_size != UINT32_MAX)
356 m_current_byte_size = new_byte_size;
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000357 }
358 return error;
359}
360
361
362//-------------------------------------------------------------------------
Greg Clayton7406c392011-04-20 01:33:38 +0000363// OptionValueArray
364//-------------------------------------------------------------------------
365void
366OptionValueArray::DumpValue (Stream &strm)
367{
368 const uint32_t size = m_values.size();
369 for (uint32_t i = 0; i<size; ++i)
370 {
371 strm.Printf("[%u] ", i);
372 m_values[i]->DumpValue (strm);
373 }
374}
375
Greg Clayton17cd9952011-04-22 03:55:06 +0000376Error
Greg Clayton7406c392011-04-20 01:33:38 +0000377OptionValueArray::SetValueFromCString (const char *value_cstr)
378{
Greg Clayton17cd9952011-04-22 03:55:06 +0000379 Error error;
380 error.SetErrorStringWithFormat ("array option values don't yet support being set by string: '%s'\n", value_cstr);
381 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000382}
383
384//-------------------------------------------------------------------------
385// OptionValueDictionary
386//-------------------------------------------------------------------------
387void
388OptionValueDictionary::DumpValue (Stream &strm)
389{
390 collection::iterator pos, end = m_values.end();
391
392 for (pos = m_values.begin(); pos != end; ++pos)
393 {
394 strm.Printf("%s=", pos->first.GetCString());
395 pos->second->DumpValue (strm);
396 }
397}
398
Greg Clayton17cd9952011-04-22 03:55:06 +0000399Error
Greg Clayton7406c392011-04-20 01:33:38 +0000400OptionValueDictionary::SetValueFromCString (const char *value_cstr)
401{
Greg Clayton17cd9952011-04-22 03:55:06 +0000402 Error error;
403 error.SetErrorStringWithFormat ("dictionary option values don't yet support being set by string: '%s'\n", value_cstr);
404 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000405}
406
Greg Claytone9728452011-04-21 17:46:10 +0000407lldb::OptionValueSP
408OptionValueDictionary::GetValueForKey (const ConstString &key) const
409{
410 lldb::OptionValueSP value_sp;
411 collection::const_iterator pos = m_values.find (key);
412 if (pos != m_values.end())
413 value_sp = pos->second;
414 return value_sp;
415}
416
417const char *
418OptionValueDictionary::GetStringValueForKey (const ConstString &key)
419{
420 collection::const_iterator pos = m_values.find (key);
421 if (pos != m_values.end())
422 {
423 if (pos->second->GetType() == OptionValue::eTypeString)
424 return static_cast<OptionValueString *>(pos->second.get())->GetCurrentValue();
425 }
426 return NULL;
427}
428
429
430bool
431OptionValueDictionary::SetStringValueForKey (const ConstString &key,
432 const char *value,
433 bool can_replace)
434{
435 collection::const_iterator pos = m_values.find (key);
436 if (pos != m_values.end())
437 {
438 if (!can_replace)
439 return false;
440 if (pos->second->GetType() == OptionValue::eTypeString)
441 {
442 pos->second->SetValueFromCString(value);
443 return true;
444 }
445 }
446 m_values[key] = OptionValueSP (new OptionValueString (value));
447 return true;
448
449}
450
451bool
452OptionValueDictionary::SetValueForKey (const ConstString &key,
453 const lldb::OptionValueSP &value_sp,
454 bool can_replace)
455{
456 // Make sure the value_sp object is allowed to contain
457 // values of the type passed in...
458 if (value_sp && (m_type_mask & value_sp->GetTypeAsMask()))
459 {
460 if (!can_replace)
461 {
462 collection::const_iterator pos = m_values.find (key);
463 if (pos != m_values.end())
464 return false;
465 }
466 m_values[key] = value_sp;
467 return true;
468 }
469 return false;
470}
471
472bool
473OptionValueDictionary::DeleteValueForKey (const ConstString &key)
474{
475 collection::iterator pos = m_values.find (key);
476 if (pos != m_values.end())
477 {
478 m_values.erase(pos);
479 return true;
480 }
481 return false;
482}
483
Greg Clayton7406c392011-04-20 01:33:38 +0000484