1254721Semaste//===-- OptionValueEnumeration.cpp ------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#include "lldb/Interpreter/OptionValueEnumeration.h"
11254721Semaste
12254721Semaste// C Includes
13254721Semaste// C++ Includes
14254721Semaste// Other libraries and framework includes
15254721Semaste// Project includes
16254721Semaste#include "lldb/Core/StringList.h"
17254721Semaste
18254721Semasteusing namespace lldb;
19254721Semasteusing namespace lldb_private;
20254721Semaste
21254721SemasteOptionValueEnumeration::OptionValueEnumeration (const OptionEnumValueElement *enumerators,
22254721Semaste                                                enum_type value) :
23254721Semaste    OptionValue(),
24254721Semaste    m_current_value (value),
25254721Semaste    m_default_value (value),
26254721Semaste    m_enumerations ()
27254721Semaste{
28254721Semaste    SetEnumerations(enumerators);
29254721Semaste}
30254721Semaste
31254721SemasteOptionValueEnumeration::~OptionValueEnumeration()
32254721Semaste{
33254721Semaste}
34254721Semaste
35254721Semastevoid
36254721SemasteOptionValueEnumeration::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
37254721Semaste{
38254721Semaste    if (dump_mask & eDumpOptionType)
39254721Semaste        strm.Printf ("(%s)", GetTypeAsCString ());
40254721Semaste    if (dump_mask & eDumpOptionValue)
41254721Semaste    {
42254721Semaste        if (dump_mask & eDumpOptionType)
43254721Semaste            strm.PutCString (" = ");
44254721Semaste        const size_t count = m_enumerations.GetSize ();
45254721Semaste        for (size_t i=0; i<count; ++i)
46254721Semaste        {
47254721Semaste            if (m_enumerations.GetValueAtIndexUnchecked(i).value == m_current_value)
48254721Semaste            {
49254721Semaste                strm.PutCString(m_enumerations.GetCStringAtIndex(i));
50254721Semaste                return;
51254721Semaste            }
52254721Semaste        }
53254721Semaste        strm.Printf("%" PRIu64, (uint64_t)m_current_value);
54254721Semaste    }
55254721Semaste}
56254721Semaste
57254721SemasteError
58254721SemasteOptionValueEnumeration::SetValueFromCString (const char *value, VarSetOperationType op)
59254721Semaste{
60254721Semaste    Error error;
61254721Semaste    switch (op)
62254721Semaste    {
63254721Semaste        case eVarSetOperationClear:
64254721Semaste            Clear ();
65254721Semaste            break;
66254721Semaste
67254721Semaste        case eVarSetOperationReplace:
68254721Semaste        case eVarSetOperationAssign:
69254721Semaste            if (value && value[0])
70254721Semaste            {
71254721Semaste                ConstString const_enumerator_name(value);
72254721Semaste                const EnumerationMapEntry *enumerator_entry = m_enumerations.FindFirstValueForName (const_enumerator_name.GetCString());
73254721Semaste                if (enumerator_entry)
74254721Semaste                {
75254721Semaste                    m_current_value = enumerator_entry->value.value;
76254721Semaste                }
77254721Semaste                else
78254721Semaste                {
79254721Semaste                    StreamString error_strm;
80254721Semaste                    error_strm.Printf("invalid enumeration value '%s'", value);
81254721Semaste                    const size_t count = m_enumerations.GetSize ();
82254721Semaste                    if (count)
83254721Semaste                    {
84254721Semaste                        error_strm.Printf(", valid values are: %s", m_enumerations.GetCStringAtIndex(0));
85254721Semaste                        for (size_t i=1; i<count; ++i)
86254721Semaste                        {
87254721Semaste                            error_strm.Printf (", %s", m_enumerations.GetCStringAtIndex(i));
88254721Semaste                        }
89254721Semaste                    }
90254721Semaste                    error.SetErrorString(error_strm.GetData());
91254721Semaste                }
92254721Semaste            }
93254721Semaste            else
94254721Semaste            {
95254721Semaste                error.SetErrorString("invalid enumeration value");
96254721Semaste            }
97254721Semaste            break;
98254721Semaste
99254721Semaste        case eVarSetOperationInsertBefore:
100254721Semaste        case eVarSetOperationInsertAfter:
101254721Semaste        case eVarSetOperationRemove:
102254721Semaste        case eVarSetOperationAppend:
103254721Semaste        case eVarSetOperationInvalid:
104254721Semaste            error = OptionValue::SetValueFromCString (value, op);
105254721Semaste            break;
106254721Semaste    }
107254721Semaste    return error;
108254721Semaste}
109254721Semaste
110254721Semastevoid
111254721SemasteOptionValueEnumeration::SetEnumerations (const OptionEnumValueElement *enumerators)
112254721Semaste{
113254721Semaste    m_enumerations.Clear();
114254721Semaste    if (enumerators)
115254721Semaste    {
116254721Semaste        for (size_t i=0; enumerators[i].string_value != NULL; ++i)
117254721Semaste        {
118254721Semaste            ConstString const_enumerator_name(enumerators[i].string_value);
119254721Semaste            EnumeratorInfo enumerator_info = { enumerators[i].value, enumerators[i].usage };
120254721Semaste            m_enumerations.Append (const_enumerator_name.GetCString(), enumerator_info);
121254721Semaste        }
122254721Semaste        m_enumerations.Sort();
123254721Semaste    }
124254721Semaste}
125254721Semaste
126254721Semaste
127254721Semastelldb::OptionValueSP
128254721SemasteOptionValueEnumeration::DeepCopy () const
129254721Semaste{
130254721Semaste    return OptionValueSP(new OptionValueEnumeration(*this));
131254721Semaste}
132254721Semaste
133254721Semastesize_t
134254721SemasteOptionValueEnumeration::AutoComplete (CommandInterpreter &interpreter,
135254721Semaste                                      const char *s,
136254721Semaste                                      int match_start_point,
137254721Semaste                                      int max_return_elements,
138254721Semaste                                      bool &word_complete,
139254721Semaste                                      StringList &matches)
140254721Semaste{
141254721Semaste    word_complete = false;
142254721Semaste    matches.Clear();
143254721Semaste
144254721Semaste    const uint32_t num_enumerators = m_enumerations.GetSize();
145254721Semaste    if (s && s[0])
146254721Semaste    {
147254721Semaste        const size_t s_len = strlen(s);
148254721Semaste        for (size_t i=0; i<num_enumerators; ++i)
149254721Semaste        {
150254721Semaste            const char *name = m_enumerations.GetCStringAtIndex(i);
151254721Semaste            if (::strncmp(s, name, s_len) == 0)
152254721Semaste                matches.AppendString(name);
153254721Semaste        }
154254721Semaste    }
155254721Semaste    else
156254721Semaste    {
157254721Semaste        // only suggest "true" or "false" by default
158254721Semaste        for (size_t i=0; i<num_enumerators; ++i)
159254721Semaste            matches.AppendString(m_enumerations.GetCStringAtIndex(i));
160254721Semaste    }
161254721Semaste    return matches.GetSize();
162254721Semaste}
163254721Semaste
164254721Semaste
165254721Semaste
166254721Semaste
167