Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 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" |
Greg Clayton | 754a936 | 2012-08-23 00:22:02 +0000 | [diff] [blame] | 17 | #include "lldb/Core/StringList.h" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 18 | #include "lldb/Interpreter/Args.h" |
Saleem Abdulrasool | 2860695 | 2014-06-27 05:17:41 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace lldb; |
| 22 | using namespace lldb_private; |
| 23 | |
| 24 | void |
| 25 | OptionValueBoolean::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 | |
| 39 | Error |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 40 | OptionValueBoolean::SetValueFromString (llvm::StringRef value_str, |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 41 | VarSetOperationType op) |
| 42 | { |
| 43 | Error error; |
| 44 | switch (op) |
| 45 | { |
| 46 | case eVarSetOperationClear: |
| 47 | Clear(); |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 48 | NotifyValueChanged(); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 49 | break; |
| 50 | |
| 51 | case eVarSetOperationReplace: |
| 52 | case eVarSetOperationAssign: |
| 53 | { |
| 54 | bool success = false; |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 55 | bool value = Args::StringToBoolean(value_str.str().c_str(), false, &success); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 56 | if (success) |
| 57 | { |
| 58 | m_value_was_set = true; |
| 59 | m_current_value = value; |
Greg Clayton | 332e8b1 | 2015-01-13 21:13:08 +0000 | [diff] [blame] | 60 | NotifyValueChanged(); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 61 | } |
| 62 | else |
| 63 | { |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 64 | if (value_str.size() == 0) |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 65 | error.SetErrorString ("invalid boolean string value <empty>"); |
| 66 | else |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 67 | error.SetErrorStringWithFormat ("invalid boolean string value: '%s'", |
| 68 | value_str.str().c_str()); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | break; |
| 72 | |
| 73 | case eVarSetOperationInsertBefore: |
| 74 | case eVarSetOperationInsertAfter: |
| 75 | case eVarSetOperationRemove: |
| 76 | case eVarSetOperationAppend: |
| 77 | case eVarSetOperationInvalid: |
Pavel Labath | c95f7e2 | 2015-02-20 11:14:59 +0000 | [diff] [blame] | 78 | error = OptionValue::SetValueFromString (value_str, op); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 79 | break; |
| 80 | } |
| 81 | return error; |
| 82 | } |
| 83 | |
| 84 | lldb::OptionValueSP |
| 85 | OptionValueBoolean::DeepCopy () const |
| 86 | { |
| 87 | return OptionValueSP(new OptionValueBoolean(*this)); |
| 88 | } |
| 89 | |
Greg Clayton | 754a936 | 2012-08-23 00:22:02 +0000 | [diff] [blame] | 90 | size_t |
| 91 | OptionValueBoolean::AutoComplete (CommandInterpreter &interpreter, |
| 92 | const char *s, |
| 93 | int match_start_point, |
| 94 | int max_return_elements, |
| 95 | bool &word_complete, |
| 96 | StringList &matches) |
| 97 | { |
| 98 | word_complete = false; |
| 99 | matches.Clear(); |
| 100 | struct StringEntry { |
| 101 | const char *string; |
| 102 | const size_t length; |
| 103 | }; |
| 104 | static const StringEntry g_autocomplete_entries[] = |
| 105 | { |
| 106 | { "true" , 4 }, |
| 107 | { "false", 5 }, |
| 108 | { "on" , 2 }, |
| 109 | { "off" , 3 }, |
| 110 | { "yes" , 3 }, |
| 111 | { "no" , 2 }, |
| 112 | { "1" , 1 }, |
| 113 | { "0" , 1 }, |
| 114 | }; |
Saleem Abdulrasool | 2860695 | 2014-06-27 05:17:41 +0000 | [diff] [blame] | 115 | const size_t k_num_autocomplete_entries = llvm::array_lengthof(g_autocomplete_entries); |
Greg Clayton | 754a936 | 2012-08-23 00:22:02 +0000 | [diff] [blame] | 116 | |
| 117 | if (s && s[0]) |
| 118 | { |
| 119 | const size_t s_len = strlen(s); |
| 120 | for (size_t i=0; i<k_num_autocomplete_entries; ++i) |
| 121 | { |
| 122 | if (s_len <= g_autocomplete_entries[i].length) |
| 123 | if (::strncasecmp(s, g_autocomplete_entries[i].string, s_len) == 0) |
| 124 | matches.AppendString(g_autocomplete_entries[i].string); |
| 125 | } |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | // only suggest "true" or "false" by default |
| 130 | for (size_t i=0; i<2; ++i) |
| 131 | matches.AppendString(g_autocomplete_entries[i].string); |
| 132 | } |
| 133 | return matches.GetSize(); |
| 134 | } |
| 135 | |
| 136 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 137 | |