blob: 9012ab178e3cf7c70c5edde5f0f83f81cd725651 [file] [log] [blame]
Greg Clayton67cc0632012-08-22 17:17:09 +00001//===-- 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 Clayton4c054102012-09-01 00:38:36 +000017#include "lldb/Interpreter/Args.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000018
19using namespace lldb;
20using namespace lldb_private;
21
22void
23OptionValueString::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 Clayton4c054102012-09-01 00:38:36 +000033 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 Clayton67cc0632012-08-22 17:17:09 +000042 else
Greg Clayton4c054102012-09-01 00:38:36 +000043 {
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 Clayton67cc0632012-08-22 17:17:09 +000049 }
50 }
51}
52
53Error
54OptionValueString::SetValueFromCString (const char *value_cstr,
55 VarSetOperationType op)
56{
57 Error error;
Greg Clayton21c895e2013-03-06 02:19:38 +000058
59 std::string value_str_no_quotes;
Pavel Labathdf50f942015-02-16 13:13:39 +000060 llvm::StringRef trimmed = value_cstr ? llvm::StringRef(value_cstr).trim() : llvm::StringRef();
61 if (trimmed.size() > 0)
Greg Clayton21c895e2013-03-06 02:19:38 +000062 {
Pavel Labathdf50f942015-02-16 13:13:39 +000063 switch (trimmed.front())
Greg Clayton21c895e2013-03-06 02:19:38 +000064 {
65 case '"':
66 case '\'':
67 {
Pavel Labathdf50f942015-02-16 13:13:39 +000068 if (trimmed.size() <= 1 || trimmed.back() != trimmed.front())
Greg Clayton21c895e2013-03-06 02:19:38 +000069 {
70 error.SetErrorString("mismatched quotes");
71 return error;
72 }
Pavel Labathdf50f942015-02-16 13:13:39 +000073 trimmed = trimmed.drop_front().drop_back().str();
Greg Clayton21c895e2013-03-06 02:19:38 +000074 }
75 break;
76 }
Pavel Labathdf50f942015-02-16 13:13:39 +000077 value_str_no_quotes = trimmed.str();
78 value_cstr = value_str_no_quotes.c_str();
Greg Clayton21c895e2013-03-06 02:19:38 +000079 }
80
Greg Clayton67cc0632012-08-22 17:17:09 +000081 switch (op)
82 {
83 case eVarSetOperationInvalid:
84 case eVarSetOperationInsertBefore:
85 case eVarSetOperationInsertAfter:
86 case eVarSetOperationRemove:
Enrico Granata9d140842012-12-11 22:42:19 +000087 if (m_validator)
88 {
89 error = m_validator(value_cstr,m_validator_baton);
90 if (error.Fail())
91 return error;
92 }
Greg Clayton67cc0632012-08-22 17:17:09 +000093 error = OptionValue::SetValueFromCString (value_cstr, op);
94 break;
95
96 case eVarSetOperationAppend:
Enrico Granata9d140842012-12-11 22:42:19 +000097 {
Greg Clayton332e8b12015-01-13 21:13:08 +000098 std::string new_value(m_current_value);
99 if (value_cstr && value_cstr[0])
Greg Clayton4c054102012-09-01 00:38:36 +0000100 {
Greg Clayton332e8b12015-01-13 21:13:08 +0000101 if (m_options.Test (eOptionEncodeCharacterEscapeSequences))
102 {
103 std::string str;
104 Args::EncodeEscapeSequences (value_cstr, str);
105 new_value.append(str);
106 }
107 else
108 new_value.append(value_cstr);
Greg Clayton4c054102012-09-01 00:38:36 +0000109 }
Greg Clayton332e8b12015-01-13 21:13:08 +0000110 if (m_validator)
111 {
112 error = m_validator(new_value.c_str(),m_validator_baton);
113 if (error.Fail())
114 return error;
115 }
116 m_current_value.assign(new_value);
117 NotifyValueChanged();
Greg Clayton4c054102012-09-01 00:38:36 +0000118 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000119 break;
120
121 case eVarSetOperationClear:
122 Clear ();
Greg Clayton332e8b12015-01-13 21:13:08 +0000123 NotifyValueChanged();
Greg Clayton67cc0632012-08-22 17:17:09 +0000124 break;
125
126 case eVarSetOperationReplace:
127 case eVarSetOperationAssign:
Enrico Granata9d140842012-12-11 22:42:19 +0000128 if (m_validator)
129 {
130 error = m_validator(value_cstr,m_validator_baton);
131 if (error.Fail())
132 return error;
133 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000134 m_value_was_set = true;
Greg Clayton4c054102012-09-01 00:38:36 +0000135 if (m_options.Test (eOptionEncodeCharacterEscapeSequences))
136 {
137 Args::EncodeEscapeSequences (value_cstr, m_current_value);
138 }
139 else
140 {
141 SetCurrentValue (value_cstr);
142 }
Greg Clayton332e8b12015-01-13 21:13:08 +0000143 NotifyValueChanged();
Greg Clayton67cc0632012-08-22 17:17:09 +0000144 break;
145 }
146 return error;
147}
148
149
150lldb::OptionValueSP
151OptionValueString::DeepCopy () const
152{
153 return OptionValueSP(new OptionValueString(*this));
154}
Enrico Granata9d140842012-12-11 22:42:19 +0000155
156Error
157OptionValueString::SetCurrentValue (const char *value)
158{
159 if (m_validator)
160 {
161 Error error(m_validator(value,m_validator_baton));
162 if (error.Fail())
163 return error;
164 }
165 if (value && value[0])
166 m_current_value.assign (value);
167 else
168 m_current_value.clear();
169 return Error();
170}
171
172Error
173OptionValueString::AppendToCurrentValue (const char *value)
174{
175 if (value && value[0])
176 {
177 if (m_validator)
178 {
179 std::string new_value(m_current_value);
180 new_value.append(value);
181 Error error(m_validator(value,m_validator_baton));
182 if (error.Fail())
183 return error;
184 m_current_value.assign(new_value);
185 }
186 else
187 m_current_value.append (value);
188 }
189 return Error();
190}