blob: 6ccc8d04b5c62bc98abb996629b4696df194a8bd [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
99OptionValueArray *
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000100OptionValue::GetAsArray ()
Greg Claytone9728452011-04-21 17:46:10 +0000101{
102 if (GetType () == OptionValue::eTypeArray)
103 return static_cast<OptionValueArray *>(this);
104 return NULL;
105}
106
107OptionValueDictionary *
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000108OptionValue::GetAsDictionary ()
Greg Claytone9728452011-04-21 17:46:10 +0000109{
110 if (GetType () == OptionValue::eTypeDictionary)
111 return static_cast<OptionValueDictionary *>(this);
112 return NULL;
113}
114
Caroline Ticedfb2e202011-04-22 05:08:45 +0000115const char *
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000116OptionValue::GetStringValue (const char *fail_value)
Caroline Ticedfb2e202011-04-22 05:08:45 +0000117{
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000118 OptionValueString *option_value = GetAsString ();
119 if (option_value)
120 return option_value->GetCurrentValue();
121 return fail_value;
Caroline Ticedfb2e202011-04-22 05:08:45 +0000122}
123
124uint64_t
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000125OptionValue::GetUInt64Value (uint64_t fail_value)
Caroline Ticedfb2e202011-04-22 05:08:45 +0000126{
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000127 OptionValueUInt64 *option_value = GetAsUInt64 ();
128 if (option_value)
129 return option_value->GetCurrentValue();
130 return fail_value;
131}
132
133lldb::Format
134OptionValue::GetFormatValue (lldb::Format fail_value)
135{
136 OptionValueFormat *option_value = GetAsFormat ();
137 if (option_value)
138 return option_value->GetCurrentValue();
139 return fail_value;
Caroline Ticedfb2e202011-04-22 05:08:45 +0000140}
Greg Claytone9728452011-04-21 17:46:10 +0000141
Greg Clayton7406c392011-04-20 01:33:38 +0000142//-------------------------------------------------------------------------
Greg Claytonc08770b2011-04-21 19:21:29 +0000143// OptionValueCollection
Greg Clayton7406c392011-04-20 01:33:38 +0000144//-------------------------------------------------------------------------
145
146void
Greg Claytonc08770b2011-04-21 19:21:29 +0000147OptionValueCollection::GetQualifiedName (Stream &strm)
Greg Clayton7406c392011-04-20 01:33:38 +0000148{
149 if (m_parent)
150 {
151 m_parent->GetQualifiedName (strm);
152 strm.PutChar('.');
153 }
154 strm << m_name;
155}
156
Greg Clayton7406c392011-04-20 01:33:38 +0000157
Greg Clayton7406c392011-04-20 01:33:38 +0000158//-------------------------------------------------------------------------
159// OptionValueBoolean
160//-------------------------------------------------------------------------
161void
162OptionValueBoolean::DumpValue (Stream &strm)
163{
164 strm.PutCString (m_current_value ? "true" : "false");
165}
166
Greg Clayton17cd9952011-04-22 03:55:06 +0000167Error
Greg Clayton7406c392011-04-20 01:33:38 +0000168OptionValueBoolean::SetValueFromCString (const char *value_cstr)
169{
Greg Clayton17cd9952011-04-22 03:55:06 +0000170 Error error;
Greg Clayton7406c392011-04-20 01:33:38 +0000171 bool success = false;
172 bool value = Args::StringToBoolean(value_cstr, false, &success);
173 if (success)
174 {
Greg Clayton17cd9952011-04-22 03:55:06 +0000175 m_value_was_set = true;
Greg Clayton7406c392011-04-20 01:33:38 +0000176 m_current_value = value;
Greg Clayton7406c392011-04-20 01:33:38 +0000177 }
Greg Clayton17cd9952011-04-22 03:55:06 +0000178 else
179 {
Greg Claytonff44ab42011-04-23 02:04:55 +0000180 if (value_cstr == NULL)
181 error.SetErrorString ("invalid boolean string value: NULL\n");
182 else if (value_cstr[0] == '\0')
183 error.SetErrorString ("invalid boolean string value <empty>\n");
184 else
185 error.SetErrorStringWithFormat ("invalid boolean string value: '%s'\n", value_cstr);
Greg Clayton17cd9952011-04-22 03:55:06 +0000186 }
187 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000188}
189
190//-------------------------------------------------------------------------
191// OptionValueSInt64
192//-------------------------------------------------------------------------
193void
194OptionValueSInt64::DumpValue (Stream &strm)
195{
196 strm.Printf ("%lli", m_current_value);
197}
198
Greg Clayton17cd9952011-04-22 03:55:06 +0000199Error
Greg Clayton7406c392011-04-20 01:33:38 +0000200OptionValueSInt64::SetValueFromCString (const char *value_cstr)
201{
Greg Clayton17cd9952011-04-22 03:55:06 +0000202
203 Error error;
Greg Clayton7406c392011-04-20 01:33:38 +0000204 bool success = false;
205 int64_t value = Args::StringToSInt64 (value_cstr, 0, 0, &success);
206 if (success)
207 {
Greg Clayton17cd9952011-04-22 03:55:06 +0000208 m_value_was_set = true;
Greg Clayton7406c392011-04-20 01:33:38 +0000209 m_current_value = value;
Greg Clayton7406c392011-04-20 01:33:38 +0000210 }
Greg Clayton17cd9952011-04-22 03:55:06 +0000211 else
212 {
213 error.SetErrorStringWithFormat ("invalid int64_t string value: '%s'\n", value_cstr);
214 }
215 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000216}
217
218//-------------------------------------------------------------------------
219// OptionValueUInt64
220//-------------------------------------------------------------------------
Greg Clayton17cd9952011-04-22 03:55:06 +0000221
222lldb::OptionValueSP
223OptionValueUInt64::Create (const char *value_cstr, Error &error)
224{
225 lldb::OptionValueSP value_sp (new OptionValueUInt64());
226 error = value_sp->SetValueFromCString (value_cstr);
227 if (error.Fail())
228 value_sp.reset();
229 return value_sp;
230}
231
232
Greg Clayton7406c392011-04-20 01:33:38 +0000233void
234OptionValueUInt64::DumpValue (Stream &strm)
235{
236 strm.Printf ("0x%llx", m_current_value);
237}
238
Greg Clayton17cd9952011-04-22 03:55:06 +0000239Error
Greg Clayton7406c392011-04-20 01:33:38 +0000240OptionValueUInt64::SetValueFromCString (const char *value_cstr)
241{
Greg Clayton17cd9952011-04-22 03:55:06 +0000242 Error error;
Greg Clayton7406c392011-04-20 01:33:38 +0000243 bool success = false;
244 uint64_t value = Args::StringToUInt64 (value_cstr, 0, 0, &success);
245 if (success)
246 {
Greg Clayton17cd9952011-04-22 03:55:06 +0000247 m_value_was_set = true;
Greg Clayton7406c392011-04-20 01:33:38 +0000248 m_current_value = value;
Greg Clayton7406c392011-04-20 01:33:38 +0000249 }
Greg Clayton17cd9952011-04-22 03:55:06 +0000250 else
251 {
252 error.SetErrorStringWithFormat ("invalid uint64_t string value: '%s'\n", value_cstr);
253 }
254 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000255}
256
257//-------------------------------------------------------------------------
Greg Claytond12aeab2011-04-20 16:37:46 +0000258// OptionValueDictionary
259//-------------------------------------------------------------------------
260void
261OptionValueString::DumpValue (Stream &strm)
262{
263 strm.Printf ("\"%s\"", m_current_value.c_str());
264}
265
Greg Clayton17cd9952011-04-22 03:55:06 +0000266Error
Greg Claytond12aeab2011-04-20 16:37:46 +0000267OptionValueString::SetValueFromCString (const char *value_cstr)
268{
Greg Clayton17cd9952011-04-22 03:55:06 +0000269 m_value_was_set = true;
Greg Claytond12aeab2011-04-20 16:37:46 +0000270 SetCurrentValue (value_cstr);
Greg Clayton17cd9952011-04-22 03:55:06 +0000271 return Error ();
Greg Claytond12aeab2011-04-20 16:37:46 +0000272}
273
Greg Claytond12aeab2011-04-20 16:37:46 +0000274//-------------------------------------------------------------------------
Greg Clayton7406c392011-04-20 01:33:38 +0000275// OptionValueFileSpec
276//-------------------------------------------------------------------------
277void
278OptionValueFileSpec::DumpValue (Stream &strm)
279{
280 if (m_current_value)
281 {
282 if (m_current_value.GetDirectory())
283 {
284 strm << '"' << m_current_value.GetDirectory();
285 if (m_current_value.GetFilename())
286 strm << '/' << m_current_value.GetFilename();
287 strm << '"';
288 }
289 else
290 {
291 strm << '"' << m_current_value.GetFilename() << '"';
292 }
293 }
294}
295
Greg Clayton17cd9952011-04-22 03:55:06 +0000296Error
Greg Clayton7406c392011-04-20 01:33:38 +0000297OptionValueFileSpec::SetValueFromCString (const char *value_cstr)
298{
299 if (value_cstr && value_cstr[0])
300 m_current_value.SetFile(value_cstr, false);
301 else
302 m_current_value.Clear();
Greg Clayton17cd9952011-04-22 03:55:06 +0000303 m_value_was_set = true;
304 return Error();
Greg Clayton7406c392011-04-20 01:33:38 +0000305}
306
307
308//-------------------------------------------------------------------------
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000309// OptionValueFormat
310//-------------------------------------------------------------------------
311void
312OptionValueFormat::DumpValue (Stream &strm)
313{
314 strm.PutCString (GetFormatAsCString (m_current_value));
315}
316
317Error
318OptionValueFormat::SetValueFromCString (const char *value_cstr)
319{
320 Format new_format;
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000321 uint32_t new_byte_size = UINT32_MAX;
322 Error error (Args::StringToFormat(value_cstr, new_format, m_byte_size_prefix_ok ? &new_byte_size : NULL));
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000323 if (error.Success())
324 {
325 m_value_was_set = true;
326 m_current_value = new_format;
Greg Clayton56bbdaf2011-04-28 20:55:26 +0000327 if (new_byte_size != UINT32_MAX)
328 m_current_byte_size = new_byte_size;
Greg Clayton57b3c6b2011-04-27 22:04:39 +0000329 }
330 return error;
331}
332
333
334//-------------------------------------------------------------------------
Greg Clayton7406c392011-04-20 01:33:38 +0000335// OptionValueArray
336//-------------------------------------------------------------------------
337void
338OptionValueArray::DumpValue (Stream &strm)
339{
340 const uint32_t size = m_values.size();
341 for (uint32_t i = 0; i<size; ++i)
342 {
343 strm.Printf("[%u] ", i);
344 m_values[i]->DumpValue (strm);
345 }
346}
347
Greg Clayton17cd9952011-04-22 03:55:06 +0000348Error
Greg Clayton7406c392011-04-20 01:33:38 +0000349OptionValueArray::SetValueFromCString (const char *value_cstr)
350{
Greg Clayton17cd9952011-04-22 03:55:06 +0000351 Error error;
352 error.SetErrorStringWithFormat ("array option values don't yet support being set by string: '%s'\n", value_cstr);
353 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000354}
355
356//-------------------------------------------------------------------------
357// OptionValueDictionary
358//-------------------------------------------------------------------------
359void
360OptionValueDictionary::DumpValue (Stream &strm)
361{
362 collection::iterator pos, end = m_values.end();
363
364 for (pos = m_values.begin(); pos != end; ++pos)
365 {
366 strm.Printf("%s=", pos->first.GetCString());
367 pos->second->DumpValue (strm);
368 }
369}
370
Greg Clayton17cd9952011-04-22 03:55:06 +0000371Error
Greg Clayton7406c392011-04-20 01:33:38 +0000372OptionValueDictionary::SetValueFromCString (const char *value_cstr)
373{
Greg Clayton17cd9952011-04-22 03:55:06 +0000374 Error error;
375 error.SetErrorStringWithFormat ("dictionary option values don't yet support being set by string: '%s'\n", value_cstr);
376 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000377}
378
Greg Claytone9728452011-04-21 17:46:10 +0000379lldb::OptionValueSP
380OptionValueDictionary::GetValueForKey (const ConstString &key) const
381{
382 lldb::OptionValueSP value_sp;
383 collection::const_iterator pos = m_values.find (key);
384 if (pos != m_values.end())
385 value_sp = pos->second;
386 return value_sp;
387}
388
389const char *
390OptionValueDictionary::GetStringValueForKey (const ConstString &key)
391{
392 collection::const_iterator pos = m_values.find (key);
393 if (pos != m_values.end())
394 {
395 if (pos->second->GetType() == OptionValue::eTypeString)
396 return static_cast<OptionValueString *>(pos->second.get())->GetCurrentValue();
397 }
398 return NULL;
399}
400
401
402bool
403OptionValueDictionary::SetStringValueForKey (const ConstString &key,
404 const char *value,
405 bool can_replace)
406{
407 collection::const_iterator pos = m_values.find (key);
408 if (pos != m_values.end())
409 {
410 if (!can_replace)
411 return false;
412 if (pos->second->GetType() == OptionValue::eTypeString)
413 {
414 pos->second->SetValueFromCString(value);
415 return true;
416 }
417 }
418 m_values[key] = OptionValueSP (new OptionValueString (value));
419 return true;
420
421}
422
423bool
424OptionValueDictionary::SetValueForKey (const ConstString &key,
425 const lldb::OptionValueSP &value_sp,
426 bool can_replace)
427{
428 // Make sure the value_sp object is allowed to contain
429 // values of the type passed in...
430 if (value_sp && (m_type_mask & value_sp->GetTypeAsMask()))
431 {
432 if (!can_replace)
433 {
434 collection::const_iterator pos = m_values.find (key);
435 if (pos != m_values.end())
436 return false;
437 }
438 m_values[key] = value_sp;
439 return true;
440 }
441 return false;
442}
443
444bool
445OptionValueDictionary::DeleteValueForKey (const ConstString &key)
446{
447 collection::iterator pos = m_values.find (key);
448 if (pos != m_values.end())
449 {
450 m_values.erase(pos);
451 return true;
452 }
453 return false;
454}
455
Greg Clayton7406c392011-04-20 01:33:38 +0000456