1 //===-- OptionValuePathMappings.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/OptionValuePathMappings.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 OptionValuePathMappings::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
25 if (dump_mask & eDumpOptionType)
26 strm.Printf ("(%s)", GetTypeAsCString ());
27 if (dump_mask & eDumpOptionValue)
29 if (dump_mask & eDumpOptionType)
30 strm.Printf (" =%s", (m_path_mappings.GetSize() > 0) ? "\n" : "");
31 m_path_mappings.Dump(&strm);
36 OptionValuePathMappings::SetValueFromCString (const char *value, VarSetOperationType op)
40 const size_t argc = args.GetArgumentCount();
44 case eVarSetOperationClear:
48 case eVarSetOperationReplace:
49 // Must be at least one index + 1 pair of paths, and the pair count must be even
50 if (argc >= 3 && (((argc - 1) & 1) == 0))
52 uint32_t idx = Args::StringToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX);
53 const uint32_t count = m_path_mappings.GetSize();
56 error.SetErrorStringWithFormat("invalid file list index %u, index must be 0 through %u", idx, count);
60 for (size_t i=1; i<argc; i += 2, ++idx)
62 ConstString a(args.GetArgumentAtIndex(i));
63 ConstString b(args.GetArgumentAtIndex(i+1));
64 if (!m_path_mappings.Replace (a, b, idx, m_notify_changes))
65 m_path_mappings.Append(a, b, m_notify_changes);
71 error.SetErrorString("replace operation takes an array index followed by one or more path pairs");
77 case eVarSetOperationAssign:
78 if (argc < 2 || (argc & 1))
80 error.SetErrorString("assign operation takes one or more path pairs");
83 m_path_mappings.Clear(m_notify_changes);
84 // Fall through to append case
85 case eVarSetOperationAppend:
86 if (argc < 2 || (argc & 1))
88 error.SetErrorString("append operation takes one or more path pairs");
93 for (size_t i=0; i<argc; i += 2)
95 ConstString a(args.GetArgumentAtIndex(i));
96 ConstString b(args.GetArgumentAtIndex(i+1));
97 m_path_mappings.Append(a, b, m_notify_changes);
98 m_value_was_set = true;
103 case eVarSetOperationInsertBefore:
104 case eVarSetOperationInsertAfter:
105 // Must be at least one index + 1 pair of paths, and the pair count must be even
106 if (argc >= 3 && (((argc - 1) & 1) == 0))
108 uint32_t idx = Args::StringToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX);
109 const uint32_t count = m_path_mappings.GetSize();
112 error.SetErrorStringWithFormat("invalid file list index %u, index must be 0 through %u", idx, count);
116 if (op == eVarSetOperationInsertAfter)
118 for (size_t i=1; i<argc; i += 2, ++idx)
120 ConstString a(args.GetArgumentAtIndex(i));
121 ConstString b(args.GetArgumentAtIndex(i+1));
122 m_path_mappings.Insert (a, b, idx, m_notify_changes);
128 error.SetErrorString("insert operation takes an array index followed by one or more path pairs");
132 case eVarSetOperationRemove:
135 std::vector<int> remove_indexes;
136 bool all_indexes_valid = true;
138 for (i=0; all_indexes_valid && i<argc; ++i)
140 const int idx = Args::StringToSInt32(args.GetArgumentAtIndex(i), INT32_MAX);
141 if (idx == INT32_MAX)
142 all_indexes_valid = false;
144 remove_indexes.push_back(idx);
147 if (all_indexes_valid)
149 size_t num_remove_indexes = remove_indexes.size();
150 if (num_remove_indexes)
152 // Sort and then erase in reverse so indexes are always valid
153 std::sort(remove_indexes.begin(), remove_indexes.end());
154 for (size_t j=num_remove_indexes-1; j<num_remove_indexes; ++j)
156 m_path_mappings.Remove (j, m_notify_changes);
162 error.SetErrorStringWithFormat("invalid array index '%s', aborting remove operation", args.GetArgumentAtIndex(i));
167 error.SetErrorString("remove operation takes one or more array index");
171 case eVarSetOperationInvalid:
172 error = OptionValue::SetValueFromCString (value, op);
177 m_value_was_set = true;
182 OptionValuePathMappings::DeepCopy () const
184 return OptionValueSP(new OptionValuePathMappings(*this));