blob: 4721b9d47ed18c703cb72715634f5b5cc8d6a4c1 [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 {
Greg Claytonff44ab42011-04-23 02:04:55 +0000159 if (value_cstr == NULL)
160 error.SetErrorString ("invalid boolean string value: NULL\n");
161 else if (value_cstr[0] == '\0')
162 error.SetErrorString ("invalid boolean string value <empty>\n");
163 else
164 error.SetErrorStringWithFormat ("invalid boolean string value: '%s'\n", value_cstr);
Greg Clayton17cd9952011-04-22 03:55:06 +0000165 }
166 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000167}
168
169//-------------------------------------------------------------------------
170// OptionValueSInt64
171//-------------------------------------------------------------------------
172void
173OptionValueSInt64::DumpValue (Stream &strm)
174{
175 strm.Printf ("%lli", m_current_value);
176}
177
Greg Clayton17cd9952011-04-22 03:55:06 +0000178Error
Greg Clayton7406c392011-04-20 01:33:38 +0000179OptionValueSInt64::SetValueFromCString (const char *value_cstr)
180{
Greg Clayton17cd9952011-04-22 03:55:06 +0000181
182 Error error;
Greg Clayton7406c392011-04-20 01:33:38 +0000183 bool success = false;
184 int64_t value = Args::StringToSInt64 (value_cstr, 0, 0, &success);
185 if (success)
186 {
Greg Clayton17cd9952011-04-22 03:55:06 +0000187 m_value_was_set = true;
Greg Clayton7406c392011-04-20 01:33:38 +0000188 m_current_value = value;
Greg Clayton7406c392011-04-20 01:33:38 +0000189 }
Greg Clayton17cd9952011-04-22 03:55:06 +0000190 else
191 {
192 error.SetErrorStringWithFormat ("invalid int64_t string value: '%s'\n", value_cstr);
193 }
194 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000195}
196
197//-------------------------------------------------------------------------
198// OptionValueUInt64
199//-------------------------------------------------------------------------
Greg Clayton17cd9952011-04-22 03:55:06 +0000200
201lldb::OptionValueSP
202OptionValueUInt64::Create (const char *value_cstr, Error &error)
203{
204 lldb::OptionValueSP value_sp (new OptionValueUInt64());
205 error = value_sp->SetValueFromCString (value_cstr);
206 if (error.Fail())
207 value_sp.reset();
208 return value_sp;
209}
210
211
Greg Clayton7406c392011-04-20 01:33:38 +0000212void
213OptionValueUInt64::DumpValue (Stream &strm)
214{
215 strm.Printf ("0x%llx", m_current_value);
216}
217
Greg Clayton17cd9952011-04-22 03:55:06 +0000218Error
Greg Clayton7406c392011-04-20 01:33:38 +0000219OptionValueUInt64::SetValueFromCString (const char *value_cstr)
220{
Greg Clayton17cd9952011-04-22 03:55:06 +0000221 Error error;
Greg Clayton7406c392011-04-20 01:33:38 +0000222 bool success = false;
223 uint64_t value = Args::StringToUInt64 (value_cstr, 0, 0, &success);
224 if (success)
225 {
Greg Clayton17cd9952011-04-22 03:55:06 +0000226 m_value_was_set = true;
Greg Clayton7406c392011-04-20 01:33:38 +0000227 m_current_value = value;
Greg Clayton7406c392011-04-20 01:33:38 +0000228 }
Greg Clayton17cd9952011-04-22 03:55:06 +0000229 else
230 {
231 error.SetErrorStringWithFormat ("invalid uint64_t string value: '%s'\n", value_cstr);
232 }
233 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000234}
235
236//-------------------------------------------------------------------------
Greg Claytond12aeab2011-04-20 16:37:46 +0000237// OptionValueDictionary
238//-------------------------------------------------------------------------
239void
240OptionValueString::DumpValue (Stream &strm)
241{
242 strm.Printf ("\"%s\"", m_current_value.c_str());
243}
244
Greg Clayton17cd9952011-04-22 03:55:06 +0000245Error
Greg Claytond12aeab2011-04-20 16:37:46 +0000246OptionValueString::SetValueFromCString (const char *value_cstr)
247{
Greg Clayton17cd9952011-04-22 03:55:06 +0000248 m_value_was_set = true;
Greg Claytond12aeab2011-04-20 16:37:46 +0000249 SetCurrentValue (value_cstr);
Greg Clayton17cd9952011-04-22 03:55:06 +0000250 return Error ();
Greg Claytond12aeab2011-04-20 16:37:46 +0000251}
252
253
254
255//-------------------------------------------------------------------------
Greg Clayton7406c392011-04-20 01:33:38 +0000256// OptionValueFileSpec
257//-------------------------------------------------------------------------
258void
259OptionValueFileSpec::DumpValue (Stream &strm)
260{
261 if (m_current_value)
262 {
263 if (m_current_value.GetDirectory())
264 {
265 strm << '"' << m_current_value.GetDirectory();
266 if (m_current_value.GetFilename())
267 strm << '/' << m_current_value.GetFilename();
268 strm << '"';
269 }
270 else
271 {
272 strm << '"' << m_current_value.GetFilename() << '"';
273 }
274 }
275}
276
Greg Clayton17cd9952011-04-22 03:55:06 +0000277Error
Greg Clayton7406c392011-04-20 01:33:38 +0000278OptionValueFileSpec::SetValueFromCString (const char *value_cstr)
279{
280 if (value_cstr && value_cstr[0])
281 m_current_value.SetFile(value_cstr, false);
282 else
283 m_current_value.Clear();
Greg Clayton17cd9952011-04-22 03:55:06 +0000284 m_value_was_set = true;
285 return Error();
Greg Clayton7406c392011-04-20 01:33:38 +0000286}
287
288
289//-------------------------------------------------------------------------
290// OptionValueArray
291//-------------------------------------------------------------------------
292void
293OptionValueArray::DumpValue (Stream &strm)
294{
295 const uint32_t size = m_values.size();
296 for (uint32_t i = 0; i<size; ++i)
297 {
298 strm.Printf("[%u] ", i);
299 m_values[i]->DumpValue (strm);
300 }
301}
302
Greg Clayton17cd9952011-04-22 03:55:06 +0000303Error
Greg Clayton7406c392011-04-20 01:33:38 +0000304OptionValueArray::SetValueFromCString (const char *value_cstr)
305{
Greg Clayton17cd9952011-04-22 03:55:06 +0000306 Error error;
307 error.SetErrorStringWithFormat ("array option values don't yet support being set by string: '%s'\n", value_cstr);
308 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000309}
310
311//-------------------------------------------------------------------------
312// OptionValueDictionary
313//-------------------------------------------------------------------------
314void
315OptionValueDictionary::DumpValue (Stream &strm)
316{
317 collection::iterator pos, end = m_values.end();
318
319 for (pos = m_values.begin(); pos != end; ++pos)
320 {
321 strm.Printf("%s=", pos->first.GetCString());
322 pos->second->DumpValue (strm);
323 }
324}
325
Greg Clayton17cd9952011-04-22 03:55:06 +0000326Error
Greg Clayton7406c392011-04-20 01:33:38 +0000327OptionValueDictionary::SetValueFromCString (const char *value_cstr)
328{
Greg Clayton17cd9952011-04-22 03:55:06 +0000329 Error error;
330 error.SetErrorStringWithFormat ("dictionary option values don't yet support being set by string: '%s'\n", value_cstr);
331 return error;
Greg Clayton7406c392011-04-20 01:33:38 +0000332}
333
Greg Claytone9728452011-04-21 17:46:10 +0000334lldb::OptionValueSP
335OptionValueDictionary::GetValueForKey (const ConstString &key) const
336{
337 lldb::OptionValueSP value_sp;
338 collection::const_iterator pos = m_values.find (key);
339 if (pos != m_values.end())
340 value_sp = pos->second;
341 return value_sp;
342}
343
344const char *
345OptionValueDictionary::GetStringValueForKey (const ConstString &key)
346{
347 collection::const_iterator pos = m_values.find (key);
348 if (pos != m_values.end())
349 {
350 if (pos->second->GetType() == OptionValue::eTypeString)
351 return static_cast<OptionValueString *>(pos->second.get())->GetCurrentValue();
352 }
353 return NULL;
354}
355
356
357bool
358OptionValueDictionary::SetStringValueForKey (const ConstString &key,
359 const char *value,
360 bool can_replace)
361{
362 collection::const_iterator pos = m_values.find (key);
363 if (pos != m_values.end())
364 {
365 if (!can_replace)
366 return false;
367 if (pos->second->GetType() == OptionValue::eTypeString)
368 {
369 pos->second->SetValueFromCString(value);
370 return true;
371 }
372 }
373 m_values[key] = OptionValueSP (new OptionValueString (value));
374 return true;
375
376}
377
378bool
379OptionValueDictionary::SetValueForKey (const ConstString &key,
380 const lldb::OptionValueSP &value_sp,
381 bool can_replace)
382{
383 // Make sure the value_sp object is allowed to contain
384 // values of the type passed in...
385 if (value_sp && (m_type_mask & value_sp->GetTypeAsMask()))
386 {
387 if (!can_replace)
388 {
389 collection::const_iterator pos = m_values.find (key);
390 if (pos != m_values.end())
391 return false;
392 }
393 m_values[key] = value_sp;
394 return true;
395 }
396 return false;
397}
398
399bool
400OptionValueDictionary::DeleteValueForKey (const ConstString &key)
401{
402 collection::iterator pos = m_values.find (key);
403 if (pos != m_values.end())
404 {
405 m_values.erase(pos);
406 return true;
407 }
408 return false;
409}
410
Greg Clayton7406c392011-04-20 01:33:38 +0000411