1 //===-- OptionValueEnumeration.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/OptionValueEnumeration.h" 11 12 // C Includes 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 #include "lldb/Core/StringList.h" 17 18 using namespace lldb; 19 using namespace lldb_private; 20 21 OptionValueEnumeration::OptionValueEnumeration (const OptionEnumValueElement *enumerators, 22 enum_type value) : 23 OptionValue(), 24 m_current_value (value), 25 m_default_value (value), 26 m_enumerations () 27 { 28 SetEnumerations(enumerators); 29 } 30 31 OptionValueEnumeration::~OptionValueEnumeration() 32 { 33 } 34 35 void 36 OptionValueEnumeration::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask) 37 { 38 if (dump_mask & eDumpOptionType) 39 strm.Printf ("(%s)", GetTypeAsCString ()); 40 if (dump_mask & eDumpOptionValue) 41 { 42 if (dump_mask & eDumpOptionType) 43 strm.PutCString (" = "); 44 const size_t count = m_enumerations.GetSize (); 45 for (size_t i=0; i<count; ++i) 46 { 47 if (m_enumerations.GetValueAtIndexUnchecked(i).value == m_current_value) 48 { 49 strm.PutCString(m_enumerations.GetCStringAtIndex(i)); 50 return; 51 } 52 } 53 strm.Printf("%" PRIu64, (uint64_t)m_current_value); 54 } 55 } 56 57 Error 58 OptionValueEnumeration::SetValueFromCString (const char *value, VarSetOperationType op) 59 { 60 Error error; 61 switch (op) 62 { 63 case eVarSetOperationClear: 64 Clear (); 65 break; 66 67 case eVarSetOperationReplace: 68 case eVarSetOperationAssign: 69 if (value && value[0]) 70 { 71 ConstString const_enumerator_name(value); 72 const EnumerationMapEntry *enumerator_entry = m_enumerations.FindFirstValueForName (const_enumerator_name.GetCString()); 73 if (enumerator_entry) 74 { 75 m_current_value = enumerator_entry->value.value; 76 } 77 else 78 { 79 StreamString error_strm; 80 error_strm.Printf("invalid enumeration value '%s'", value); 81 const size_t count = m_enumerations.GetSize (); 82 if (count) 83 { 84 error_strm.Printf(", valid values are: %s", m_enumerations.GetCStringAtIndex(0)); 85 for (size_t i=1; i<count; ++i) 86 { 87 error_strm.Printf (", %s", m_enumerations.GetCStringAtIndex(i)); 88 } 89 } 90 error.SetErrorString(error_strm.GetData()); 91 } 92 } 93 else 94 { 95 error.SetErrorString("invalid enumeration value"); 96 } 97 break; 98 99 case eVarSetOperationInsertBefore: 100 case eVarSetOperationInsertAfter: 101 case eVarSetOperationRemove: 102 case eVarSetOperationAppend: 103 case eVarSetOperationInvalid: 104 error = OptionValue::SetValueFromCString (value, op); 105 break; 106 } 107 return error; 108 } 109 110 void 111 OptionValueEnumeration::SetEnumerations (const OptionEnumValueElement *enumerators) 112 { 113 m_enumerations.Clear(); 114 if (enumerators) 115 { 116 for (size_t i=0; enumerators[i].string_value != NULL; ++i) 117 { 118 ConstString const_enumerator_name(enumerators[i].string_value); 119 EnumeratorInfo enumerator_info = { enumerators[i].value, enumerators[i].usage }; 120 m_enumerations.Append (const_enumerator_name.GetCString(), enumerator_info); 121 } 122 m_enumerations.Sort(); 123 } 124 } 125 126 127 lldb::OptionValueSP 128 OptionValueEnumeration::DeepCopy () const 129 { 130 return OptionValueSP(new OptionValueEnumeration(*this)); 131 } 132 133 size_t 134 OptionValueEnumeration::AutoComplete (CommandInterpreter &interpreter, 135 const char *s, 136 int match_start_point, 137 int max_return_elements, 138 bool &word_complete, 139 StringList &matches) 140 { 141 word_complete = false; 142 matches.Clear(); 143 144 const uint32_t num_enumerators = m_enumerations.GetSize(); 145 if (s && s[0]) 146 { 147 const size_t s_len = strlen(s); 148 for (size_t i=0; i<num_enumerators; ++i) 149 { 150 const char *name = m_enumerations.GetCStringAtIndex(i); 151 if (::strncmp(s, name, s_len) == 0) 152 matches.AppendString(name); 153 } 154 } 155 else 156 { 157 // only suggest "true" or "false" by default 158 for (size_t i=0; i<num_enumerators; ++i) 159 matches.AppendString(m_enumerations.GetCStringAtIndex(i)); 160 } 161 return matches.GetSize(); 162 } 163 164 165 166 167