1 //===-- OptionValueDictionary.cpp -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "lldb/Interpreter/OptionValueDictionary.h"
16 // Other libraries and framework includes
17 #include "llvm/ADT/StringRef.h"
19 #include "lldb/Core/State.h"
20 #include "lldb/DataFormatters/FormatManager.h"
21 #include "lldb/Interpreter/Args.h"
22 #include "lldb/Interpreter/OptionValueString.h"
25 using namespace lldb_private;
28 OptionValueDictionary::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
30 const Type dict_type = ConvertTypeMaskToType (m_type_mask);
31 if (dump_mask & eDumpOptionType)
33 if (m_type_mask != eTypeInvalid)
34 strm.Printf ("(%s of %ss)", GetTypeAsCString(), GetBuiltinTypeAsCString(dict_type));
36 strm.Printf ("(%s)", GetTypeAsCString());
38 if (dump_mask & eDumpOptionValue)
40 if (dump_mask & eDumpOptionType)
41 strm.PutCString (" =");
43 collection::iterator pos, end = m_values.end();
47 for (pos = m_values.begin(); pos != end; ++pos)
49 OptionValue *option_value = pos->second.get();
51 strm.Indent(pos->first.GetCString());
53 const uint32_t extra_dump_options = m_raw_value_dump ? eDumpOptionRaw : 0;
60 case eTypeFileSpecList:
63 option_value->DumpValue(exe_ctx, strm, dump_mask | extra_dump_options);
74 // No need to show the type for dictionaries of simple items
76 option_value->DumpValue(exe_ctx, strm, (dump_mask & (~eDumpOptionType)) | extra_dump_options);
86 OptionValueDictionary::GetArgs (Args &args) const
89 collection::const_iterator pos, end = m_values.end();
90 for (pos = m_values.begin(); pos != end; ++pos)
93 strm.Printf("%s=", pos->first.GetCString());
94 pos->second->DumpValue(NULL, strm, eDumpOptionValue|eDumpOptionRaw);
95 args.AppendArgument(strm.GetString().c_str());
97 return args.GetArgumentCount();
101 OptionValueDictionary::SetArgs (const Args &args, VarSetOperationType op)
104 const size_t argc = args.GetArgumentCount();
107 case eVarSetOperationClear:
111 case eVarSetOperationAppend:
112 case eVarSetOperationReplace:
113 case eVarSetOperationAssign:
116 for (size_t i=0; i<argc; ++i)
118 llvm::StringRef key_and_value(args.GetArgumentAtIndex(i));
119 if (!key_and_value.empty())
121 std::pair<llvm::StringRef, llvm::StringRef> kvp(key_and_value.split('='));
122 llvm::StringRef key = kvp.first;
123 bool key_valid = false;
126 if (key.front() == '[')
128 // Key name starts with '[', so the the key value must be in single or double quotes like:
131 if ((key.size() > 2) && (key.back() == ']'))
133 // Strip leading '[' and trailing ']'
134 key = key.substr(1, key.size()-2);
135 const char quote_char = key.front();
136 if ((quote_char == '\'') || (quote_char == '"'))
138 if ((key.size() > 2) && (key.back() == quote_char))
141 key = key.substr(1, key.size()-2);
147 // square brackets, no quotes
154 // No square brackets or quotes
160 error.SetErrorStringWithFormat("invalid key \"%s\", the key must be a bare string or surrounded by brackets with optional quotes: [<key>] or ['<key>'] or [\"<key>\"]", kvp.first.str().c_str());
164 lldb::OptionValueSP value_sp (CreateValueFromCStringForTypeMask (kvp.second.data(),
171 m_value_was_set = true;
172 SetValueForKey (ConstString(key), value_sp, true);
176 error.SetErrorString("dictionaries that can contain multiple types must subclass OptionValueArray");
181 error.SetErrorString("empty argument");
187 error.SetErrorString("assign operation takes one or more key=value arguments");
191 case eVarSetOperationRemove:
194 for (size_t i=0; i<argc; ++i)
196 ConstString key(args.GetArgumentAtIndex(i));
197 if (!DeleteValueForKey(key))
199 error.SetErrorStringWithFormat("no value found named '%s', aborting remove operation", key.GetCString());
206 error.SetErrorString("remove operation takes one or more key arguments");
210 case eVarSetOperationInsertBefore:
211 case eVarSetOperationInsertAfter:
212 case eVarSetOperationInvalid:
213 error = OptionValue::SetValueFromCString (NULL, op);
220 OptionValueDictionary::SetValueFromCString (const char *value_cstr, VarSetOperationType op)
222 Args args(value_cstr);
223 return SetArgs (args, op);
227 OptionValueDictionary::GetSubValue (const ExecutionContext *exe_ctx, const char *name, bool will_modify, Error &error) const
229 lldb::OptionValueSP value_sp;
233 const char *sub_name = NULL;
235 const char *open_bracket = ::strchr (name, '[');
239 const char *key_start = open_bracket + 1;
240 const char *key_end = NULL;
241 switch (open_bracket[1])
245 key_end = strchr(key_start, '\'');
248 if (key_end[1] == ']')
251 sub_name = key_end + 2;
255 error.SetErrorStringWithFormat ("invalid value path '%s', single quoted key names must be formatted as ['<key>'] where <key> is a string that doesn't contain quotes", name);
261 error.SetErrorString ("missing '] key name terminator, key name started with ['");
267 key_end = strchr(key_start, '"');
270 if (key_end[1] == ']')
273 sub_name = key_end + 2;
276 error.SetErrorStringWithFormat ("invalid value path '%s', double quoted key names must be formatted as [\"<key>\"] where <key> is a string that doesn't contain quotes", name);
281 error.SetErrorString ("missing \"] key name terminator, key name started with [\"");
287 key_end = strchr(key_start, ']');
291 sub_name = key_end + 1;
295 error.SetErrorString ("missing ] key name terminator, key name started with [");
301 if (key_start && key_end)
303 key.SetCStringWithLength (key_start, key_end - key_start);
305 value_sp = GetValueForKey (key);
309 return value_sp->GetSubValue (exe_ctx, sub_name, will_modify, error);
313 error.SetErrorStringWithFormat("dictionary does not contain a value for the key name '%s'", key.GetCString());
317 if (!value_sp && error.AsCString() == NULL)
319 error.SetErrorStringWithFormat ("invalid value path '%s', %s values only support '[<key>]' subvalues where <key> a string value optionally delimitted by single or double quotes",
328 OptionValueDictionary::SetSubValue (const ExecutionContext *exe_ctx, VarSetOperationType op, const char *name, const char *value)
331 const bool will_modify = true;
332 lldb::OptionValueSP value_sp (GetSubValue (exe_ctx, name, will_modify, error));
334 error = value_sp->SetValueFromCString(value, op);
337 if (error.AsCString() == NULL)
338 error.SetErrorStringWithFormat("invalid value path '%s'", name);
345 OptionValueDictionary::GetValueForKey (const ConstString &key) const
347 lldb::OptionValueSP value_sp;
348 collection::const_iterator pos = m_values.find (key);
349 if (pos != m_values.end())
350 value_sp = pos->second;
355 OptionValueDictionary::GetStringValueForKey (const ConstString &key)
357 collection::const_iterator pos = m_values.find (key);
358 if (pos != m_values.end())
360 OptionValueString *string_value = pos->second->GetAsString();
362 return string_value->GetCurrentValue();
369 OptionValueDictionary::SetStringValueForKey (const ConstString &key,
373 collection::const_iterator pos = m_values.find (key);
374 if (pos != m_values.end())
378 if (pos->second->GetType() == OptionValue::eTypeString)
380 pos->second->SetValueFromCString(value);
384 m_values[key] = OptionValueSP (new OptionValueString (value));
390 OptionValueDictionary::SetValueForKey (const ConstString &key,
391 const lldb::OptionValueSP &value_sp,
394 // Make sure the value_sp object is allowed to contain
395 // values of the type passed in...
396 if (value_sp && (m_type_mask & value_sp->GetTypeAsMask()))
400 collection::const_iterator pos = m_values.find (key);
401 if (pos != m_values.end())
404 m_values[key] = value_sp;
411 OptionValueDictionary::DeleteValueForKey (const ConstString &key)
413 collection::iterator pos = m_values.find (key);
414 if (pos != m_values.end())
423 OptionValueDictionary::DeepCopy () const
425 OptionValueDictionary *copied_dict = new OptionValueDictionary (m_type_mask, m_raw_value_dump);
426 lldb::OptionValueSP copied_value_sp(copied_dict);
427 collection::const_iterator pos, end = m_values.end();
428 for (pos = m_values.begin(); pos != end; ++pos)
431 strm.Printf("%s=", pos->first.GetCString());
432 copied_dict->SetValueForKey (pos->first, pos->second->DeepCopy(), true);
434 return copied_value_sp;