1 //===-- OptionValueArray.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/Interpreter/OptionValueArray.h"
14 // Other libraries and framework includes
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Interpreter/Args.h"
20 using namespace lldb_private;
23 OptionValueArray::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
25 const Type array_element_type = ConvertTypeMaskToType (m_type_mask);
26 if (dump_mask & eDumpOptionType)
28 if ((GetType() == eTypeArray) && (m_type_mask != eTypeInvalid))
29 strm.Printf ("(%s of %ss)", GetTypeAsCString(), GetBuiltinTypeAsCString(array_element_type));
31 strm.Printf ("(%s)", GetTypeAsCString());
33 if (dump_mask & eDumpOptionValue)
35 if (dump_mask & eDumpOptionType)
36 strm.Printf (" =%s", (m_values.size() > 0) ? "\n" : "");
38 const uint32_t size = m_values.size();
39 for (uint32_t i = 0; i<size; ++i)
42 strm.Printf("[%u]: ", i);
43 const uint32_t extra_dump_options = m_raw_value_dump ? eDumpOptionRaw : 0;
44 switch (array_element_type)
50 case eTypeFileSpecList:
52 m_values[i]->DumpValue(exe_ctx, strm, dump_mask | extra_dump_options);
63 // No need to show the type for dictionaries of simple items
64 m_values[i]->DumpValue(exe_ctx, strm, (dump_mask & (~eDumpOptionType)) | extra_dump_options);
75 OptionValueArray::SetValueFromCString (const char *value, VarSetOperationType op)
78 return SetArgs (args, op);
83 OptionValueArray::GetSubValue (const ExecutionContext *exe_ctx,
88 if (name && name[0] == '[')
90 const char *end_bracket = strchr (name+1, ']');
93 const char *sub_value = NULL;
95 sub_value = end_bracket + 1;
96 std::string index_str (name+1, end_bracket);
97 const size_t array_count = m_values.size();
98 int32_t idx = Args::StringToSInt32(index_str.c_str(), INT32_MAX, 0, NULL);
102 uint32_t new_idx = UINT32_MAX;
105 // Access from the end of the array if the index is negative
106 new_idx = array_count - idx;
110 // Just a standard index
114 if (new_idx < array_count)
116 if (m_values[new_idx])
119 return m_values[new_idx]->GetSubValue (exe_ctx, sub_value, will_modify, error);
121 return m_values[new_idx];
126 if (array_count == 0)
127 error.SetErrorStringWithFormat("index %i is not valid for an empty array", idx);
129 error.SetErrorStringWithFormat("index %i out of range, valid values are 0 through %" PRIu64, idx, (uint64_t)(array_count - 1));
131 error.SetErrorStringWithFormat("negative index %i out of range, valid values are -1 through -%" PRIu64, idx, (uint64_t)array_count);
138 error.SetErrorStringWithFormat("invalid value path '%s', %s values only support '[<index>]' subvalues where <index> is a positive or negative array index", name, GetTypeAsCString());
140 return OptionValueSP();
145 OptionValueArray::GetArgs (Args &args) const
147 const uint32_t size = m_values.size();
148 std::vector<const char *> argv;
149 for (uint32_t i = 0; i<size; ++i)
151 const char *string_value = m_values[i]->GetStringValue ();
153 argv.push_back(string_value);
159 args.SetArguments(argv.size(), &argv[0]);
160 return args.GetArgumentCount();
164 OptionValueArray::SetArgs (const Args &args, VarSetOperationType op)
167 const size_t argc = args.GetArgumentCount();
170 case eVarSetOperationInvalid:
171 error.SetErrorString("unsupported operation");
174 case eVarSetOperationInsertBefore:
175 case eVarSetOperationInsertAfter:
178 uint32_t idx = Args::StringToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX);
179 const uint32_t count = GetSize();
182 error.SetErrorStringWithFormat("invalid insert array index %u, index must be 0 through %u", idx, count);
186 if (op == eVarSetOperationInsertAfter)
188 for (size_t i=1; i<argc; ++i, ++idx)
190 lldb::OptionValueSP value_sp (CreateValueFromCStringForTypeMask (args.GetArgumentAtIndex(i),
197 if (idx >= m_values.size())
198 m_values.push_back(value_sp);
200 m_values.insert(m_values.begin() + idx, value_sp);
204 error.SetErrorString("array of complex types must subclass OptionValueArray");
212 error.SetErrorString("insert operation takes an array index followed by one or more values");
216 case eVarSetOperationRemove:
219 const uint32_t size = m_values.size();
220 std::vector<int> remove_indexes;
221 bool all_indexes_valid = true;
223 for (i=0; i<argc; ++i)
225 const int idx = Args::StringToSInt32(args.GetArgumentAtIndex(i), INT32_MAX);
228 all_indexes_valid = false;
232 remove_indexes.push_back(idx);
235 if (all_indexes_valid)
237 size_t num_remove_indexes = remove_indexes.size();
238 if (num_remove_indexes)
240 // Sort and then erase in reverse so indexes are always valid
241 if (num_remove_indexes > 1)
243 std::sort(remove_indexes.begin(), remove_indexes.end());
244 for (std::vector<int>::const_reverse_iterator pos = remove_indexes.rbegin(), end = remove_indexes.rend(); pos != end; ++pos)
246 m_values.erase(m_values.begin() + *pos);
252 m_values.erase(m_values.begin() + remove_indexes.front());
258 error.SetErrorStringWithFormat("invalid array index '%s', aborting remove operation", args.GetArgumentAtIndex(i));
263 error.SetErrorString("remove operation takes one or more array indices");
267 case eVarSetOperationClear:
271 case eVarSetOperationReplace:
274 uint32_t idx = Args::StringToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX);
275 const uint32_t count = GetSize();
278 error.SetErrorStringWithFormat("invalid replace array index %u, index must be 0 through %u", idx, count);
282 for (size_t i=1; i<argc; ++i, ++idx)
284 lldb::OptionValueSP value_sp (CreateValueFromCStringForTypeMask (args.GetArgumentAtIndex(i),
292 m_values[idx] = value_sp;
294 m_values.push_back(value_sp);
298 error.SetErrorString("array of complex types must subclass OptionValueArray");
306 error.SetErrorString("replace operation takes an array index followed by one or more values");
310 case eVarSetOperationAssign:
312 // Fall through to append case
313 case eVarSetOperationAppend:
314 for (size_t i=0; i<argc; ++i)
316 lldb::OptionValueSP value_sp (CreateValueFromCStringForTypeMask (args.GetArgumentAtIndex(i),
323 m_value_was_set = true;
324 AppendValue(value_sp);
328 error.SetErrorString("array of complex types must subclass OptionValueArray");
337 OptionValueArray::DeepCopy () const
339 OptionValueArray *copied_array = new OptionValueArray (m_type_mask, m_raw_value_dump);
340 lldb::OptionValueSP copied_value_sp(copied_array);
341 const uint32_t size = m_values.size();
342 for (uint32_t i = 0; i<size; ++i)
344 copied_array->AppendValue (m_values[i]->DeepCopy());
346 return copied_value_sp;