1 //===-- OptionValueBoolean.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/OptionValueBoolean.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Core/StringList.h"
18 #include "lldb/Interpreter/Args.h"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 void
DumpValue(const ExecutionContext * exe_ctx,Stream & strm,uint32_t dump_mask)24 OptionValueBoolean::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
25 {
26     if (dump_mask & eDumpOptionType)
27         strm.Printf ("(%s)", GetTypeAsCString ());
28 //    if (dump_mask & eDumpOptionName)
29 //        DumpQualifiedName (strm);
30     if (dump_mask & eDumpOptionValue)
31     {
32         if (dump_mask & eDumpOptionType)
33             strm.PutCString (" = ");
34         strm.PutCString (m_current_value ? "true" : "false");
35     }
36 }
37 
38 Error
SetValueFromCString(const char * value_cstr,VarSetOperationType op)39 OptionValueBoolean::SetValueFromCString (const char *value_cstr,
40                                          VarSetOperationType op)
41 {
42     Error error;
43     switch (op)
44     {
45     case eVarSetOperationClear:
46         Clear();
47         break;
48 
49     case eVarSetOperationReplace:
50     case eVarSetOperationAssign:
51         {
52             bool success = false;
53             bool value = Args::StringToBoolean(value_cstr, false, &success);
54             if (success)
55             {
56                 m_value_was_set = true;
57                 m_current_value = value;
58             }
59             else
60             {
61                 if (value_cstr == NULL)
62                     error.SetErrorString ("invalid boolean string value: NULL");
63                 else if (value_cstr[0] == '\0')
64                     error.SetErrorString ("invalid boolean string value <empty>");
65                 else
66                     error.SetErrorStringWithFormat ("invalid boolean string value: '%s'", value_cstr);
67             }
68         }
69         break;
70 
71     case eVarSetOperationInsertBefore:
72     case eVarSetOperationInsertAfter:
73     case eVarSetOperationRemove:
74     case eVarSetOperationAppend:
75     case eVarSetOperationInvalid:
76         error = OptionValue::SetValueFromCString (value_cstr, op);
77         break;
78     }
79     return error;
80 }
81 
82 lldb::OptionValueSP
DeepCopy() const83 OptionValueBoolean::DeepCopy () const
84 {
85     return OptionValueSP(new OptionValueBoolean(*this));
86 }
87 
88 size_t
AutoComplete(CommandInterpreter & interpreter,const char * s,int match_start_point,int max_return_elements,bool & word_complete,StringList & matches)89 OptionValueBoolean::AutoComplete (CommandInterpreter &interpreter,
90                                   const char *s,
91                                   int match_start_point,
92                                   int max_return_elements,
93                                   bool &word_complete,
94                                   StringList &matches)
95 {
96     word_complete = false;
97     matches.Clear();
98     struct StringEntry {
99         const char *string;
100         const size_t length;
101     };
102     static const StringEntry g_autocomplete_entries[] =
103     {
104         { "true" , 4 },
105         { "false", 5 },
106         { "on"   , 2 },
107         { "off"  , 3 },
108         { "yes"  , 3 },
109         { "no"   , 2 },
110         { "1"    , 1 },
111         { "0"    , 1 },
112     };
113     const size_t k_num_autocomplete_entries = sizeof(g_autocomplete_entries)/sizeof(StringEntry);
114 
115     if (s && s[0])
116     {
117         const size_t s_len = strlen(s);
118         for (size_t i=0; i<k_num_autocomplete_entries; ++i)
119         {
120             if (s_len <= g_autocomplete_entries[i].length)
121                 if (::strncasecmp(s, g_autocomplete_entries[i].string, s_len) == 0)
122                     matches.AppendString(g_autocomplete_entries[i].string);
123         }
124     }
125     else
126     {
127         // only suggest "true" or "false" by default
128         for (size_t i=0; i<2; ++i)
129             matches.AppendString(g_autocomplete_entries[i].string);
130     }
131     return matches.GetSize();
132 }
133 
134 
135 
136