blob: 63f006e643f9c525a957d8ddc58c0c664e502a2e [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
Pavel Labathc95f7e22015-02-20 11:14:59 +000054OptionValueString::SetValueFromString (llvm::StringRef value,
Greg Clayton67cc0632012-08-22 17:17:09 +000055 VarSetOperationType op)
56{
57 Error error;
Greg Clayton21c895e2013-03-06 02:19:38 +000058
Pavel Labathc95f7e22015-02-20 11:14:59 +000059 std::string value_str = value.str();
60 value = value.trim();
61 if (value.size() > 0)
Greg Clayton21c895e2013-03-06 02:19:38 +000062 {
Pavel Labathc95f7e22015-02-20 11:14:59 +000063 switch (value.front())
Greg Clayton21c895e2013-03-06 02:19:38 +000064 {
65 case '"':
66 case '\'':
67 {
Pavel Labathc95f7e22015-02-20 11:14:59 +000068 if (value.size() <= 1 || value.back() != value.front())
Greg Clayton21c895e2013-03-06 02:19:38 +000069 {
70 error.SetErrorString("mismatched quotes");
71 return error;
72 }
Zachary Turner4f2a9722015-05-22 19:33:54 +000073 value = value.drop_front().drop_back();
Greg Clayton21c895e2013-03-06 02:19:38 +000074 }
75 break;
76 }
Pavel Labathc95f7e22015-02-20 11:14:59 +000077 value_str = value.str();
Greg Clayton21c895e2013-03-06 02:19:38 +000078 }
79
Greg Clayton67cc0632012-08-22 17:17:09 +000080 switch (op)
81 {
82 case eVarSetOperationInvalid:
83 case eVarSetOperationInsertBefore:
84 case eVarSetOperationInsertAfter:
85 case eVarSetOperationRemove:
Enrico Granata9d140842012-12-11 22:42:19 +000086 if (m_validator)
87 {
Pavel Labathc95f7e22015-02-20 11:14:59 +000088 error = m_validator(value_str.c_str(),m_validator_baton);
Enrico Granata9d140842012-12-11 22:42:19 +000089 if (error.Fail())
90 return error;
91 }
Pavel Labathc95f7e22015-02-20 11:14:59 +000092 error = OptionValue::SetValueFromString (value, op);
Greg Clayton67cc0632012-08-22 17:17:09 +000093 break;
94
95 case eVarSetOperationAppend:
Enrico Granata9d140842012-12-11 22:42:19 +000096 {
Greg Clayton332e8b12015-01-13 21:13:08 +000097 std::string new_value(m_current_value);
Pavel Labathc95f7e22015-02-20 11:14:59 +000098 if (value.size() > 0)
Greg Clayton4c054102012-09-01 00:38:36 +000099 {
Greg Clayton332e8b12015-01-13 21:13:08 +0000100 if (m_options.Test (eOptionEncodeCharacterEscapeSequences))
101 {
102 std::string str;
Pavel Labathc95f7e22015-02-20 11:14:59 +0000103 Args::EncodeEscapeSequences (value_str.c_str(), str);
Greg Clayton332e8b12015-01-13 21:13:08 +0000104 new_value.append(str);
105 }
106 else
Pavel Labathc95f7e22015-02-20 11:14:59 +0000107 new_value.append(value);
Greg Clayton4c054102012-09-01 00:38:36 +0000108 }
Greg Clayton332e8b12015-01-13 21:13:08 +0000109 if (m_validator)
110 {
111 error = m_validator(new_value.c_str(),m_validator_baton);
112 if (error.Fail())
113 return error;
114 }
115 m_current_value.assign(new_value);
116 NotifyValueChanged();
Greg Clayton4c054102012-09-01 00:38:36 +0000117 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000118 break;
119
120 case eVarSetOperationClear:
121 Clear ();
Greg Clayton332e8b12015-01-13 21:13:08 +0000122 NotifyValueChanged();
Greg Clayton67cc0632012-08-22 17:17:09 +0000123 break;
124
125 case eVarSetOperationReplace:
126 case eVarSetOperationAssign:
Enrico Granata9d140842012-12-11 22:42:19 +0000127 if (m_validator)
128 {
Pavel Labathc95f7e22015-02-20 11:14:59 +0000129 error = m_validator(value_str.c_str(), m_validator_baton);
Enrico Granata9d140842012-12-11 22:42:19 +0000130 if (error.Fail())
131 return error;
132 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000133 m_value_was_set = true;
Greg Clayton4c054102012-09-01 00:38:36 +0000134 if (m_options.Test (eOptionEncodeCharacterEscapeSequences))
135 {
Pavel Labathc95f7e22015-02-20 11:14:59 +0000136 Args::EncodeEscapeSequences (value_str.c_str(), m_current_value);
Greg Clayton4c054102012-09-01 00:38:36 +0000137 }
138 else
139 {
Pavel Labathc95f7e22015-02-20 11:14:59 +0000140 SetCurrentValue (value_str.c_str());
Greg Clayton4c054102012-09-01 00:38:36 +0000141 }
Greg Clayton332e8b12015-01-13 21:13:08 +0000142 NotifyValueChanged();
Greg Clayton67cc0632012-08-22 17:17:09 +0000143 break;
144 }
145 return error;
146}
147
148
149lldb::OptionValueSP
150OptionValueString::DeepCopy () const
151{
152 return OptionValueSP(new OptionValueString(*this));
153}
Enrico Granata9d140842012-12-11 22:42:19 +0000154
155Error
156OptionValueString::SetCurrentValue (const char *value)
157{
158 if (m_validator)
159 {
160 Error error(m_validator(value,m_validator_baton));
161 if (error.Fail())
162 return error;
163 }
164 if (value && value[0])
165 m_current_value.assign (value);
166 else
167 m_current_value.clear();
168 return Error();
169}
170
171Error
172OptionValueString::AppendToCurrentValue (const char *value)
173{
174 if (value && value[0])
175 {
176 if (m_validator)
177 {
178 std::string new_value(m_current_value);
179 new_value.append(value);
180 Error error(m_validator(value,m_validator_baton));
181 if (error.Fail())
182 return error;
183 m_current_value.assign(new_value);
184 }
185 else
186 m_current_value.append (value);
187 }
188 return Error();
189}