Greg Clayton | 73844aa | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 1 | //===-- OptionValueString.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/OptionValueString.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 | 3b1afc6 | 2012-09-01 00:38:36 +0000 | [diff] [blame] | 17 | #include "lldb/Interpreter/Args.h" |
Greg Clayton | 73844aa | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace lldb; |
| 20 | using namespace lldb_private; |
| 21 | |
| 22 | void |
| 23 | OptionValueString::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask) |
| 24 | { |
| 25 | if (dump_mask & eDumpOptionType) |
| 26 | strm.Printf ("(%s)", GetTypeAsCString ()); |
| 27 | if (dump_mask & eDumpOptionValue) |
| 28 | { |
| 29 | if (dump_mask & eDumpOptionType) |
| 30 | strm.PutCString (" = "); |
| 31 | if (!m_current_value.empty() || m_value_was_set) |
| 32 | { |
Greg Clayton | 3b1afc6 | 2012-09-01 00:38:36 +0000 | [diff] [blame] | 33 | if (m_options.Test (eOptionEncodeCharacterEscapeSequences)) |
| 34 | { |
| 35 | std::string expanded_escape_value; |
| 36 | Args::ExpandEscapedCharacters(m_current_value.c_str(), expanded_escape_value); |
| 37 | if (dump_mask & eDumpOptionRaw) |
| 38 | strm.Printf ("%s", expanded_escape_value.c_str()); |
| 39 | else |
| 40 | strm.Printf ("\"%s\"", expanded_escape_value.c_str()); |
| 41 | } |
Greg Clayton | 73844aa | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 42 | else |
Greg Clayton | 3b1afc6 | 2012-09-01 00:38:36 +0000 | [diff] [blame] | 43 | { |
| 44 | if (dump_mask & eDumpOptionRaw) |
| 45 | strm.Printf ("%s", m_current_value.c_str()); |
| 46 | else |
| 47 | strm.Printf ("\"%s\"", m_current_value.c_str()); |
| 48 | } |
Greg Clayton | 73844aa | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | Error |
| 54 | OptionValueString::SetValueFromCString (const char *value_cstr, |
| 55 | VarSetOperationType op) |
| 56 | { |
| 57 | Error error; |
| 58 | switch (op) |
| 59 | { |
| 60 | case eVarSetOperationInvalid: |
| 61 | case eVarSetOperationInsertBefore: |
| 62 | case eVarSetOperationInsertAfter: |
| 63 | case eVarSetOperationRemove: |
Enrico Granata | 729e99e | 2012-12-11 22:42:19 +0000 | [diff] [blame^] | 64 | if (m_validator) |
| 65 | { |
| 66 | error = m_validator(value_cstr,m_validator_baton); |
| 67 | if (error.Fail()) |
| 68 | return error; |
| 69 | } |
Greg Clayton | 73844aa | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 70 | error = OptionValue::SetValueFromCString (value_cstr, op); |
| 71 | break; |
| 72 | |
| 73 | case eVarSetOperationAppend: |
Enrico Granata | 729e99e | 2012-12-11 22:42:19 +0000 | [diff] [blame^] | 74 | { |
| 75 | std::string new_value(m_current_value); |
Greg Clayton | 73844aa | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 76 | if (value_cstr && value_cstr[0]) |
Greg Clayton | 3b1afc6 | 2012-09-01 00:38:36 +0000 | [diff] [blame] | 77 | { |
| 78 | if (m_options.Test (eOptionEncodeCharacterEscapeSequences)) |
| 79 | { |
| 80 | std::string str; |
| 81 | Args::EncodeEscapeSequences (value_cstr, str); |
Enrico Granata | 729e99e | 2012-12-11 22:42:19 +0000 | [diff] [blame^] | 82 | new_value.append(str); |
Greg Clayton | 3b1afc6 | 2012-09-01 00:38:36 +0000 | [diff] [blame] | 83 | } |
| 84 | else |
Enrico Granata | 729e99e | 2012-12-11 22:42:19 +0000 | [diff] [blame^] | 85 | new_value.append(value_cstr); |
| 86 | } |
| 87 | if (m_validator) |
| 88 | { |
| 89 | error = m_validator(new_value.c_str(),m_validator_baton); |
| 90 | if (error.Fail()) |
| 91 | return error; |
| 92 | } |
| 93 | m_current_value.assign(new_value); |
Greg Clayton | 3b1afc6 | 2012-09-01 00:38:36 +0000 | [diff] [blame] | 94 | } |
Greg Clayton | 73844aa | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 95 | break; |
| 96 | |
| 97 | case eVarSetOperationClear: |
| 98 | Clear (); |
| 99 | break; |
| 100 | |
| 101 | case eVarSetOperationReplace: |
| 102 | case eVarSetOperationAssign: |
Enrico Granata | 729e99e | 2012-12-11 22:42:19 +0000 | [diff] [blame^] | 103 | if (m_validator) |
| 104 | { |
| 105 | error = m_validator(value_cstr,m_validator_baton); |
| 106 | if (error.Fail()) |
| 107 | return error; |
| 108 | } |
Greg Clayton | 73844aa | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 109 | m_value_was_set = true; |
Greg Clayton | 3b1afc6 | 2012-09-01 00:38:36 +0000 | [diff] [blame] | 110 | if (m_options.Test (eOptionEncodeCharacterEscapeSequences)) |
| 111 | { |
| 112 | Args::EncodeEscapeSequences (value_cstr, m_current_value); |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | SetCurrentValue (value_cstr); |
| 117 | } |
Greg Clayton | 73844aa | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 118 | break; |
| 119 | } |
| 120 | return error; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | lldb::OptionValueSP |
| 125 | OptionValueString::DeepCopy () const |
| 126 | { |
| 127 | return OptionValueSP(new OptionValueString(*this)); |
| 128 | } |
Enrico Granata | 729e99e | 2012-12-11 22:42:19 +0000 | [diff] [blame^] | 129 | |
| 130 | Error |
| 131 | OptionValueString::SetCurrentValue (const char *value) |
| 132 | { |
| 133 | if (m_validator) |
| 134 | { |
| 135 | Error error(m_validator(value,m_validator_baton)); |
| 136 | if (error.Fail()) |
| 137 | return error; |
| 138 | } |
| 139 | if (value && value[0]) |
| 140 | m_current_value.assign (value); |
| 141 | else |
| 142 | m_current_value.clear(); |
| 143 | return Error(); |
| 144 | } |
| 145 | |
| 146 | Error |
| 147 | OptionValueString::AppendToCurrentValue (const char *value) |
| 148 | { |
| 149 | if (value && value[0]) |
| 150 | { |
| 151 | if (m_validator) |
| 152 | { |
| 153 | std::string new_value(m_current_value); |
| 154 | new_value.append(value); |
| 155 | Error error(m_validator(value,m_validator_baton)); |
| 156 | if (error.Fail()) |
| 157 | return error; |
| 158 | m_current_value.assign(new_value); |
| 159 | } |
| 160 | else |
| 161 | m_current_value.append (value); |
| 162 | } |
| 163 | return Error(); |
| 164 | } |