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