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 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 OptionValueEnumeration::OptionValueEnumeration (const OptionEnumValueElement *enumerators,
21                                                 enum_type value) :
22     OptionValue(),
23     m_current_value (value),
24     m_default_value (value),
25     m_enumerations ()
26 {
27     SetEnumerations(enumerators);
28 }
29 
30 OptionValueEnumeration::~OptionValueEnumeration()
31 {
32 }
33 
34 void
35 OptionValueEnumeration::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
36 {
37     if (dump_mask & eDumpOptionType)
38         strm.Printf ("(%s)", GetTypeAsCString ());
39     if (dump_mask & eDumpOptionValue)
40     {
41         if (dump_mask & eDumpOptionType)
42             strm.PutCString (" = ");
43         const size_t count = m_enumerations.GetSize ();
44         for (size_t i=0; i<count; ++i)
45         {
46             if (m_enumerations.GetValueAtIndexUnchecked(i).value == m_current_value)
47             {
48                 strm.PutCString(m_enumerations.GetCStringAtIndex(i));
49                 return;
50             }
51         }
52         strm.Printf("%llu", (uint64_t)m_current_value);
53     }
54 }
55 
56 Error
57 OptionValueEnumeration::SetValueFromCString (const char *value, VarSetOperationType op)
58 {
59     Error error;
60     switch (op)
61     {
62         case eVarSetOperationClear:
63             Clear ();
64             break;
65 
66         case eVarSetOperationReplace:
67         case eVarSetOperationAssign:
68             if (value && value[0])
69             {
70                 ConstString const_enumerator_name(value);
71                 const EnumerationMapEntry *enumerator_entry = m_enumerations.FindFirstValueForName (const_enumerator_name.GetCString());
72                 if (enumerator_entry)
73                 {
74                     m_current_value = enumerator_entry->value.value;
75                 }
76                 else
77                 {
78                     StreamString error_strm;
79                     error_strm.Printf("invalid enumeration value '%s'", value);
80                     const size_t count = m_enumerations.GetSize ();
81                     if (count)
82                     {
83                         error_strm.Printf(", valid values are: %s", m_enumerations.GetCStringAtIndex(0));
84                         for (size_t i=1; i<count; ++i)
85                         {
86                             error_strm.Printf (", %s", m_enumerations.GetCStringAtIndex(i));
87                         }
88                     }
89                     error.SetErrorString(error_strm.GetData());
90                 }
91             }
92             else
93             {
94                 error.SetErrorString("invalid enumeration value");
95             }
96             break;
97 
98         case eVarSetOperationInsertBefore:
99         case eVarSetOperationInsertAfter:
100         case eVarSetOperationRemove:
101         case eVarSetOperationAppend:
102         case eVarSetOperationInvalid:
103             error = OptionValue::SetValueFromCString (value, op);
104             break;
105     }
106     return error;
107 }
108 
109 void
110 OptionValueEnumeration::SetEnumerations (const OptionEnumValueElement *enumerators)
111 {
112     m_enumerations.Clear();
113     if (enumerators)
114     {
115         for (size_t i=0; enumerators[i].string_value != NULL; ++i)
116         {
117             ConstString const_enumerator_name(enumerators[i].string_value);
118             EnumeratorInfo enumerator_info = { enumerators[i].value, enumerators[i].usage };
119             m_enumerations.Append (const_enumerator_name.GetCString(), enumerator_info);
120         }
121         m_enumerations.Sort();
122     }
123 }
124 
125 
126 lldb::OptionValueSP
127 OptionValueEnumeration::DeepCopy () const
128 {
129     return OptionValueSP(new OptionValueEnumeration(*this));
130 }
131 
132