blob: fec7905108c7dcaa1383ddd80ce8738477005654 [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//-------------------------------------------------------------------------
26OptionValueBoolean *
27OptionValue::GetAsBooleanValue ()
28{
29 if (GetType () == OptionValue::eTypeBoolean)
30 return static_cast<OptionValueBoolean *>(this);
31 return NULL;
32}
33
34OptionValueSInt64 *
35OptionValue::GetAsSInt64Value ()
36{
37 if (GetType () == OptionValue::eTypeSInt64)
38 return static_cast<OptionValueSInt64 *>(this);
39 return NULL;
40}
41
42OptionValueUInt64 *
43OptionValue::GetAsUInt64Value ()
44{
45 if (GetType () == OptionValue::eTypeUInt64)
46 return static_cast<OptionValueUInt64 *>(this);
47 return NULL;
48}
49
50OptionValueString *
51OptionValue::GetAsStringValue ()
52{
53 if (GetType () == OptionValue::eTypeString)
54 return static_cast<OptionValueString *>(this);
55 return NULL;
56}
57
58OptionValueFileSpec *
59OptionValue::GetAsFileSpecValue ()
60{
61 if (GetType () == OptionValue::eTypeFileSpec)
62 return static_cast<OptionValueFileSpec *>(this);
63 return NULL;
64}
65
66OptionValueArray *
67OptionValue::GetAsArrayValue ()
68{
69 if (GetType () == OptionValue::eTypeArray)
70 return static_cast<OptionValueArray *>(this);
71 return NULL;
72}
73
74OptionValueDictionary *
75OptionValue::GetAsDictionaryValue ()
76{
77 if (GetType () == OptionValue::eTypeDictionary)
78 return static_cast<OptionValueDictionary *>(this);
79 return NULL;
80}
81
82
Greg Clayton7406c392011-04-20 01:33:38 +000083//-------------------------------------------------------------------------
84// NamedOptionValue
85//-------------------------------------------------------------------------
86
87void
88NamedOptionValue::GetQualifiedName (Stream &strm)
89{
90 if (m_parent)
91 {
92 m_parent->GetQualifiedName (strm);
93 strm.PutChar('.');
94 }
95 strm << m_name;
96}
97
98OptionValue::Type
99NamedOptionValue::GetValueType ()
100{
101 if (m_value_sp)
102 return m_value_sp->GetType();
103 return OptionValue::eTypeInvalid;
104}
105
106bool
107NamedOptionValue::DumpValue (Stream &strm)
108{
109 if (m_value_sp)
110 {
111 m_value_sp->DumpValue (strm);
112 return true;
113 }
114 return false;
115}
116
117bool
118NamedOptionValue::SetValueFromCString (const char *value_cstr)
119{
120 if (m_value_sp)
121 return m_value_sp->SetValueFromCString (value_cstr);
122 return false;
123}
124
125bool
126NamedOptionValue::ResetValueToDefault ()
127{
128 if (m_value_sp)
129 return m_value_sp->ResetValueToDefault ();
130 return false;
131}
132
133
Greg Clayton7406c392011-04-20 01:33:38 +0000134//-------------------------------------------------------------------------
135// OptionValueBoolean
136//-------------------------------------------------------------------------
137void
138OptionValueBoolean::DumpValue (Stream &strm)
139{
140 strm.PutCString (m_current_value ? "true" : "false");
141}
142
143bool
144OptionValueBoolean::SetValueFromCString (const char *value_cstr)
145{
146 bool success = false;
147 bool value = Args::StringToBoolean(value_cstr, false, &success);
148 if (success)
149 {
150 m_current_value = value;
151 return true;
152 }
153 return false;
154}
155
156//-------------------------------------------------------------------------
157// OptionValueSInt64
158//-------------------------------------------------------------------------
159void
160OptionValueSInt64::DumpValue (Stream &strm)
161{
162 strm.Printf ("%lli", m_current_value);
163}
164
165bool
166OptionValueSInt64::SetValueFromCString (const char *value_cstr)
167{
168 bool success = false;
169 int64_t value = Args::StringToSInt64 (value_cstr, 0, 0, &success);
170 if (success)
171 {
172 m_current_value = value;
173 return true;
174 }
175 return false;
176}
177
178//-------------------------------------------------------------------------
179// OptionValueUInt64
180//-------------------------------------------------------------------------
181void
182OptionValueUInt64::DumpValue (Stream &strm)
183{
184 strm.Printf ("0x%llx", m_current_value);
185}
186
187bool
188OptionValueUInt64::SetValueFromCString (const char *value_cstr)
189{
190 bool success = false;
191 uint64_t value = Args::StringToUInt64 (value_cstr, 0, 0, &success);
192 if (success)
193 {
194 m_current_value = value;
195 return true;
196 }
197 return false;
198}
199
200//-------------------------------------------------------------------------
Greg Claytond12aeab2011-04-20 16:37:46 +0000201// OptionValueDictionary
202//-------------------------------------------------------------------------
203void
204OptionValueString::DumpValue (Stream &strm)
205{
206 strm.Printf ("\"%s\"", m_current_value.c_str());
207}
208
209bool
210OptionValueString::SetValueFromCString (const char *value_cstr)
211{
212 SetCurrentValue (value_cstr);
213 return true;
214}
215
216
217
218//-------------------------------------------------------------------------
Greg Clayton7406c392011-04-20 01:33:38 +0000219// OptionValueFileSpec
220//-------------------------------------------------------------------------
221void
222OptionValueFileSpec::DumpValue (Stream &strm)
223{
224 if (m_current_value)
225 {
226 if (m_current_value.GetDirectory())
227 {
228 strm << '"' << m_current_value.GetDirectory();
229 if (m_current_value.GetFilename())
230 strm << '/' << m_current_value.GetFilename();
231 strm << '"';
232 }
233 else
234 {
235 strm << '"' << m_current_value.GetFilename() << '"';
236 }
237 }
238}
239
240bool
241OptionValueFileSpec::SetValueFromCString (const char *value_cstr)
242{
243 if (value_cstr && value_cstr[0])
244 m_current_value.SetFile(value_cstr, false);
245 else
246 m_current_value.Clear();
247 return true;
248}
249
250
251//-------------------------------------------------------------------------
252// OptionValueArray
253//-------------------------------------------------------------------------
254void
255OptionValueArray::DumpValue (Stream &strm)
256{
257 const uint32_t size = m_values.size();
258 for (uint32_t i = 0; i<size; ++i)
259 {
260 strm.Printf("[%u] ", i);
261 m_values[i]->DumpValue (strm);
262 }
263}
264
265bool
266OptionValueArray::SetValueFromCString (const char *value_cstr)
267{
268 // We must be able to set this using the array specific functions
269 return false;
270}
271
272//-------------------------------------------------------------------------
273// OptionValueDictionary
274//-------------------------------------------------------------------------
275void
276OptionValueDictionary::DumpValue (Stream &strm)
277{
278 collection::iterator pos, end = m_values.end();
279
280 for (pos = m_values.begin(); pos != end; ++pos)
281 {
282 strm.Printf("%s=", pos->first.GetCString());
283 pos->second->DumpValue (strm);
284 }
285}
286
287bool
288OptionValueDictionary::SetValueFromCString (const char *value_cstr)
289{
290 // We must be able to set this using the array specific functions
291 return false;
292}
293
Greg Claytone9728452011-04-21 17:46:10 +0000294lldb::OptionValueSP
295OptionValueDictionary::GetValueForKey (const ConstString &key) const
296{
297 lldb::OptionValueSP value_sp;
298 collection::const_iterator pos = m_values.find (key);
299 if (pos != m_values.end())
300 value_sp = pos->second;
301 return value_sp;
302}
303
304const char *
305OptionValueDictionary::GetStringValueForKey (const ConstString &key)
306{
307 collection::const_iterator pos = m_values.find (key);
308 if (pos != m_values.end())
309 {
310 if (pos->second->GetType() == OptionValue::eTypeString)
311 return static_cast<OptionValueString *>(pos->second.get())->GetCurrentValue();
312 }
313 return NULL;
314}
315
316
317bool
318OptionValueDictionary::SetStringValueForKey (const ConstString &key,
319 const char *value,
320 bool can_replace)
321{
322 collection::const_iterator pos = m_values.find (key);
323 if (pos != m_values.end())
324 {
325 if (!can_replace)
326 return false;
327 if (pos->second->GetType() == OptionValue::eTypeString)
328 {
329 pos->second->SetValueFromCString(value);
330 return true;
331 }
332 }
333 m_values[key] = OptionValueSP (new OptionValueString (value));
334 return true;
335
336}
337
338bool
339OptionValueDictionary::SetValueForKey (const ConstString &key,
340 const lldb::OptionValueSP &value_sp,
341 bool can_replace)
342{
343 // Make sure the value_sp object is allowed to contain
344 // values of the type passed in...
345 if (value_sp && (m_type_mask & value_sp->GetTypeAsMask()))
346 {
347 if (!can_replace)
348 {
349 collection::const_iterator pos = m_values.find (key);
350 if (pos != m_values.end())
351 return false;
352 }
353 m_values[key] = value_sp;
354 return true;
355 }
356 return false;
357}
358
359bool
360OptionValueDictionary::DeleteValueForKey (const ConstString &key)
361{
362 collection::iterator pos = m_values.find (key);
363 if (pos != m_values.end())
364 {
365 m_values.erase(pos);
366 return true;
367 }
368 return false;
369}
370
Greg Clayton7406c392011-04-20 01:33:38 +0000371