blob: 04306e2b2f4cf496a46e7916e03af8bfbb1c18e9 [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;
321 Error error (Args::StringToFormat(value_cstr, new_format));
322 if (error.Success())
323 {
324 m_value_was_set = true;
325 m_current_value = new_format;
326 }
327 return error;
328}
329
330
331//-------------------------------------------------------------------------
Greg Clayton7406c392011-04-20 01:33:38 +0000332// OptionValueArray
333//-------------------------------------------------------------------------
334void
335OptionValueArray::DumpValue (Stream &strm)
336{
337 const uint32_t size = m_values.size();
338 for (uint32_t i = 0; i<size; ++i)
339 {
340 strm.Printf("[%u] ", i);
341 m_values[i]->DumpValue (strm);
342 }
343}
344
Greg Clayton17cd9952011-04-22 03:55:06 +0000345Error
Greg Clayton7406c392011-04-20 01:33:38 +0000346OptionValueArray::SetValueFromCString (const char *value_cstr)
347{
Greg Clayton17cd9952011-04-22 03:55:06 +0000348 Error error;
349 error.SetErrorStringWithFormat ("array option values don't yet support being set by string: '%s'\n", value_cstr);
350 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000351}
352
353//-------------------------------------------------------------------------
354// OptionValueDictionary
355//-------------------------------------------------------------------------
356void
357OptionValueDictionary::DumpValue (Stream &strm)
358{
359 collection::iterator pos, end = m_values.end();
360
361 for (pos = m_values.begin(); pos != end; ++pos)
362 {
363 strm.Printf("%s=", pos->first.GetCString());
364 pos->second->DumpValue (strm);
365 }
366}
367
Greg Clayton17cd9952011-04-22 03:55:06 +0000368Error
Greg Clayton7406c392011-04-20 01:33:38 +0000369OptionValueDictionary::SetValueFromCString (const char *value_cstr)
370{
Greg Clayton17cd9952011-04-22 03:55:06 +0000371 Error error;
372 error.SetErrorStringWithFormat ("dictionary option values don't yet support being set by string: '%s'\n", value_cstr);
373 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000374}
375
Greg Claytone9728452011-04-21 17:46:10 +0000376lldb::OptionValueSP
377OptionValueDictionary::GetValueForKey (const ConstString &key) const
378{
379 lldb::OptionValueSP value_sp;
380 collection::const_iterator pos = m_values.find (key);
381 if (pos != m_values.end())
382 value_sp = pos->second;
383 return value_sp;
384}
385
386const char *
387OptionValueDictionary::GetStringValueForKey (const ConstString &key)
388{
389 collection::const_iterator pos = m_values.find (key);
390 if (pos != m_values.end())
391 {
392 if (pos->second->GetType() == OptionValue::eTypeString)
393 return static_cast<OptionValueString *>(pos->second.get())->GetCurrentValue();
394 }
395 return NULL;
396}
397
398
399bool
400OptionValueDictionary::SetStringValueForKey (const ConstString &key,
401 const char *value,
402 bool can_replace)
403{
404 collection::const_iterator pos = m_values.find (key);
405 if (pos != m_values.end())
406 {
407 if (!can_replace)
408 return false;
409 if (pos->second->GetType() == OptionValue::eTypeString)
410 {
411 pos->second->SetValueFromCString(value);
412 return true;
413 }
414 }
415 m_values[key] = OptionValueSP (new OptionValueString (value));
416 return true;
417
418}
419
420bool
421OptionValueDictionary::SetValueForKey (const ConstString &key,
422 const lldb::OptionValueSP &value_sp,
423 bool can_replace)
424{
425 // Make sure the value_sp object is allowed to contain
426 // values of the type passed in...
427 if (value_sp && (m_type_mask & value_sp->GetTypeAsMask()))
428 {
429 if (!can_replace)
430 {
431 collection::const_iterator pos = m_values.find (key);
432 if (pos != m_values.end())
433 return false;
434 }
435 m_values[key] = value_sp;
436 return true;
437 }
438 return false;
439}
440
441bool
442OptionValueDictionary::DeleteValueForKey (const ConstString &key)
443{
444 collection::iterator pos = m_values.find (key);
445 if (pos != m_values.end())
446 {
447 m_values.erase(pos);
448 return true;
449 }
450 return false;
451}
452
Greg Clayton7406c392011-04-20 01:33:38 +0000453