blob: 6b5dd7e3c0b01f012a63587a3af06b1a6a5803a7 [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
Caroline Ticedfb2e202011-04-22 05:08:45 +0000105const char *
106OptionValue::GetStringValue ()
107{
108 if (GetType () == OptionValue::eTypeString)
109 return static_cast<OptionValueString *>(this)->GetCurrentValue();
110 return NULL;
111}
112
113uint64_t
114OptionValue::GetUInt64Value ()
115{
116 if (GetType () == OptionValue::eTypeUInt64)
117 return static_cast<OptionValueUInt64 *>(this)->GetCurrentValue();
118 return 0;
119}
Greg Claytone9728452011-04-21 17:46:10 +0000120
Greg Clayton7406c392011-04-20 01:33:38 +0000121//-------------------------------------------------------------------------
Greg Claytonc08770b2011-04-21 19:21:29 +0000122// OptionValueCollection
Greg Clayton7406c392011-04-20 01:33:38 +0000123//-------------------------------------------------------------------------
124
125void
Greg Claytonc08770b2011-04-21 19:21:29 +0000126OptionValueCollection::GetQualifiedName (Stream &strm)
Greg Clayton7406c392011-04-20 01:33:38 +0000127{
128 if (m_parent)
129 {
130 m_parent->GetQualifiedName (strm);
131 strm.PutChar('.');
132 }
133 strm << m_name;
134}
135
Greg Clayton7406c392011-04-20 01:33:38 +0000136
Greg Clayton7406c392011-04-20 01:33:38 +0000137//-------------------------------------------------------------------------
138// OptionValueBoolean
139//-------------------------------------------------------------------------
140void
141OptionValueBoolean::DumpValue (Stream &strm)
142{
143 strm.PutCString (m_current_value ? "true" : "false");
144}
145
Greg Clayton17cd9952011-04-22 03:55:06 +0000146Error
Greg Clayton7406c392011-04-20 01:33:38 +0000147OptionValueBoolean::SetValueFromCString (const char *value_cstr)
148{
Greg Clayton17cd9952011-04-22 03:55:06 +0000149 Error error;
Greg Clayton7406c392011-04-20 01:33:38 +0000150 bool success = false;
151 bool value = Args::StringToBoolean(value_cstr, false, &success);
152 if (success)
153 {
Greg Clayton17cd9952011-04-22 03:55:06 +0000154 m_value_was_set = true;
Greg Clayton7406c392011-04-20 01:33:38 +0000155 m_current_value = value;
Greg Clayton7406c392011-04-20 01:33:38 +0000156 }
Greg Clayton17cd9952011-04-22 03:55:06 +0000157 else
158 {
159 error.SetErrorStringWithFormat ("invalid boolean string value: '%s'\n", value_cstr);
160 }
161 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000162}
163
164//-------------------------------------------------------------------------
165// OptionValueSInt64
166//-------------------------------------------------------------------------
167void
168OptionValueSInt64::DumpValue (Stream &strm)
169{
170 strm.Printf ("%lli", m_current_value);
171}
172
Greg Clayton17cd9952011-04-22 03:55:06 +0000173Error
Greg Clayton7406c392011-04-20 01:33:38 +0000174OptionValueSInt64::SetValueFromCString (const char *value_cstr)
175{
Greg Clayton17cd9952011-04-22 03:55:06 +0000176
177 Error error;
Greg Clayton7406c392011-04-20 01:33:38 +0000178 bool success = false;
179 int64_t value = Args::StringToSInt64 (value_cstr, 0, 0, &success);
180 if (success)
181 {
Greg Clayton17cd9952011-04-22 03:55:06 +0000182 m_value_was_set = true;
Greg Clayton7406c392011-04-20 01:33:38 +0000183 m_current_value = value;
Greg Clayton7406c392011-04-20 01:33:38 +0000184 }
Greg Clayton17cd9952011-04-22 03:55:06 +0000185 else
186 {
187 error.SetErrorStringWithFormat ("invalid int64_t string value: '%s'\n", value_cstr);
188 }
189 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000190}
191
192//-------------------------------------------------------------------------
193// OptionValueUInt64
194//-------------------------------------------------------------------------
Greg Clayton17cd9952011-04-22 03:55:06 +0000195
196lldb::OptionValueSP
197OptionValueUInt64::Create (const char *value_cstr, Error &error)
198{
199 lldb::OptionValueSP value_sp (new OptionValueUInt64());
200 error = value_sp->SetValueFromCString (value_cstr);
201 if (error.Fail())
202 value_sp.reset();
203 return value_sp;
204}
205
206
Greg Clayton7406c392011-04-20 01:33:38 +0000207void
208OptionValueUInt64::DumpValue (Stream &strm)
209{
210 strm.Printf ("0x%llx", m_current_value);
211}
212
Greg Clayton17cd9952011-04-22 03:55:06 +0000213Error
Greg Clayton7406c392011-04-20 01:33:38 +0000214OptionValueUInt64::SetValueFromCString (const char *value_cstr)
215{
Greg Clayton17cd9952011-04-22 03:55:06 +0000216 Error error;
Greg Clayton7406c392011-04-20 01:33:38 +0000217 bool success = false;
218 uint64_t value = Args::StringToUInt64 (value_cstr, 0, 0, &success);
219 if (success)
220 {
Greg Clayton17cd9952011-04-22 03:55:06 +0000221 m_value_was_set = true;
Greg Clayton7406c392011-04-20 01:33:38 +0000222 m_current_value = value;
Greg Clayton7406c392011-04-20 01:33:38 +0000223 }
Greg Clayton17cd9952011-04-22 03:55:06 +0000224 else
225 {
226 error.SetErrorStringWithFormat ("invalid uint64_t string value: '%s'\n", value_cstr);
227 }
228 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000229}
230
231//-------------------------------------------------------------------------
Greg Claytond12aeab2011-04-20 16:37:46 +0000232// OptionValueDictionary
233//-------------------------------------------------------------------------
234void
235OptionValueString::DumpValue (Stream &strm)
236{
237 strm.Printf ("\"%s\"", m_current_value.c_str());
238}
239
Greg Clayton17cd9952011-04-22 03:55:06 +0000240Error
Greg Claytond12aeab2011-04-20 16:37:46 +0000241OptionValueString::SetValueFromCString (const char *value_cstr)
242{
Greg Clayton17cd9952011-04-22 03:55:06 +0000243 m_value_was_set = true;
Greg Claytond12aeab2011-04-20 16:37:46 +0000244 SetCurrentValue (value_cstr);
Greg Clayton17cd9952011-04-22 03:55:06 +0000245 return Error ();
Greg Claytond12aeab2011-04-20 16:37:46 +0000246}
247
248
249
250//-------------------------------------------------------------------------
Greg Clayton7406c392011-04-20 01:33:38 +0000251// OptionValueFileSpec
252//-------------------------------------------------------------------------
253void
254OptionValueFileSpec::DumpValue (Stream &strm)
255{
256 if (m_current_value)
257 {
258 if (m_current_value.GetDirectory())
259 {
260 strm << '"' << m_current_value.GetDirectory();
261 if (m_current_value.GetFilename())
262 strm << '/' << m_current_value.GetFilename();
263 strm << '"';
264 }
265 else
266 {
267 strm << '"' << m_current_value.GetFilename() << '"';
268 }
269 }
270}
271
Greg Clayton17cd9952011-04-22 03:55:06 +0000272Error
Greg Clayton7406c392011-04-20 01:33:38 +0000273OptionValueFileSpec::SetValueFromCString (const char *value_cstr)
274{
275 if (value_cstr && value_cstr[0])
276 m_current_value.SetFile(value_cstr, false);
277 else
278 m_current_value.Clear();
Greg Clayton17cd9952011-04-22 03:55:06 +0000279 m_value_was_set = true;
280 return Error();
Greg Clayton7406c392011-04-20 01:33:38 +0000281}
282
283
284//-------------------------------------------------------------------------
285// OptionValueArray
286//-------------------------------------------------------------------------
287void
288OptionValueArray::DumpValue (Stream &strm)
289{
290 const uint32_t size = m_values.size();
291 for (uint32_t i = 0; i<size; ++i)
292 {
293 strm.Printf("[%u] ", i);
294 m_values[i]->DumpValue (strm);
295 }
296}
297
Greg Clayton17cd9952011-04-22 03:55:06 +0000298Error
Greg Clayton7406c392011-04-20 01:33:38 +0000299OptionValueArray::SetValueFromCString (const char *value_cstr)
300{
Greg Clayton17cd9952011-04-22 03:55:06 +0000301 Error error;
302 error.SetErrorStringWithFormat ("array option values don't yet support being set by string: '%s'\n", value_cstr);
303 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000304}
305
Greg Clayton17cd9952011-04-22 03:55:06 +0000306
307uint64_t
308OptionValueArray::GetUInt64ValueAtIndex (uint32_t idx, uint64_t fail_value, bool *success_ptr) const
309{
310 if (idx < m_values.size())
311 return m_values[idx]->GetUInt64Value (fail_value, success_ptr);
312 return fail_value;
313}
314
315
316
Greg Clayton7406c392011-04-20 01:33:38 +0000317//-------------------------------------------------------------------------
318// OptionValueDictionary
319//-------------------------------------------------------------------------
320void
321OptionValueDictionary::DumpValue (Stream &strm)
322{
323 collection::iterator pos, end = m_values.end();
324
325 for (pos = m_values.begin(); pos != end; ++pos)
326 {
327 strm.Printf("%s=", pos->first.GetCString());
328 pos->second->DumpValue (strm);
329 }
330}
331
Greg Clayton17cd9952011-04-22 03:55:06 +0000332Error
Greg Clayton7406c392011-04-20 01:33:38 +0000333OptionValueDictionary::SetValueFromCString (const char *value_cstr)
334{
Greg Clayton17cd9952011-04-22 03:55:06 +0000335 Error error;
336 error.SetErrorStringWithFormat ("dictionary option values don't yet support being set by string: '%s'\n", value_cstr);
337 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000338}
339
Greg Claytone9728452011-04-21 17:46:10 +0000340lldb::OptionValueSP
341OptionValueDictionary::GetValueForKey (const ConstString &key) const
342{
343 lldb::OptionValueSP value_sp;
344 collection::const_iterator pos = m_values.find (key);
345 if (pos != m_values.end())
346 value_sp = pos->second;
347 return value_sp;
348}
349
350const char *
351OptionValueDictionary::GetStringValueForKey (const ConstString &key)
352{
353 collection::const_iterator pos = m_values.find (key);
354 if (pos != m_values.end())
355 {
356 if (pos->second->GetType() == OptionValue::eTypeString)
357 return static_cast<OptionValueString *>(pos->second.get())->GetCurrentValue();
358 }
359 return NULL;
360}
361
362
363bool
364OptionValueDictionary::SetStringValueForKey (const ConstString &key,
365 const char *value,
366 bool can_replace)
367{
368 collection::const_iterator pos = m_values.find (key);
369 if (pos != m_values.end())
370 {
371 if (!can_replace)
372 return false;
373 if (pos->second->GetType() == OptionValue::eTypeString)
374 {
375 pos->second->SetValueFromCString(value);
376 return true;
377 }
378 }
379 m_values[key] = OptionValueSP (new OptionValueString (value));
380 return true;
381
382}
383
384bool
385OptionValueDictionary::SetValueForKey (const ConstString &key,
386 const lldb::OptionValueSP &value_sp,
387 bool can_replace)
388{
389 // Make sure the value_sp object is allowed to contain
390 // values of the type passed in...
391 if (value_sp && (m_type_mask & value_sp->GetTypeAsMask()))
392 {
393 if (!can_replace)
394 {
395 collection::const_iterator pos = m_values.find (key);
396 if (pos != m_values.end())
397 return false;
398 }
399 m_values[key] = value_sp;
400 return true;
401 }
402 return false;
403}
404
405bool
406OptionValueDictionary::DeleteValueForKey (const ConstString &key)
407{
408 collection::iterator pos = m_values.find (key);
409 if (pos != m_values.end())
410 {
411 m_values.erase(pos);
412 return true;
413 }
414 return false;
415}
416
Greg Clayton7406c392011-04-20 01:33:38 +0000417