blob: df047bd9899686f00a9d606628c3ef1e72e351d2 [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;
60 if (value_cstr)
61 {
62 switch (value_cstr[0])
63 {
64 case '"':
65 case '\'':
66 {
67 size_t len = strlen(value_cstr);
68 if (len <= 1 || value_cstr[len-1] != value_cstr[0])
69 {
70 error.SetErrorString("mismatched quotes");
71 return error;
72 }
73 value_str_no_quotes.assign (value_cstr + 1, len - 2);
74 value_cstr = value_str_no_quotes.c_str();
75 }
76 break;
77 }
78 }
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 {
88 error = m_validator(value_cstr,m_validator_baton);
89 if (error.Fail())
90 return error;
91 }
Greg Clayton67cc0632012-08-22 17:17:09 +000092 error = OptionValue::SetValueFromCString (value_cstr, op);
93 break;
94
95 case eVarSetOperationAppend:
Enrico Granata9d140842012-12-11 22:42:19 +000096 {
97 std::string new_value(m_current_value);
Greg Clayton67cc0632012-08-22 17:17:09 +000098 if (value_cstr && value_cstr[0])
Greg Clayton4c054102012-09-01 00:38:36 +000099 {
100 if (m_options.Test (eOptionEncodeCharacterEscapeSequences))
101 {
102 std::string str;
103 Args::EncodeEscapeSequences (value_cstr, str);
Enrico Granata9d140842012-12-11 22:42:19 +0000104 new_value.append(str);
Greg Clayton4c054102012-09-01 00:38:36 +0000105 }
106 else
Enrico Granata9d140842012-12-11 22:42:19 +0000107 new_value.append(value_cstr);
108 }
109 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);
Greg Clayton4c054102012-09-01 00:38:36 +0000116 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000117 break;
118
119 case eVarSetOperationClear:
120 Clear ();
121 break;
122
123 case eVarSetOperationReplace:
124 case eVarSetOperationAssign:
Enrico Granata9d140842012-12-11 22:42:19 +0000125 if (m_validator)
126 {
127 error = m_validator(value_cstr,m_validator_baton);
128 if (error.Fail())
129 return error;
130 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000131 m_value_was_set = true;
Greg Clayton4c054102012-09-01 00:38:36 +0000132 if (m_options.Test (eOptionEncodeCharacterEscapeSequences))
133 {
134 Args::EncodeEscapeSequences (value_cstr, m_current_value);
135 }
136 else
137 {
138 SetCurrentValue (value_cstr);
139 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000140 break;
141 }
142 return error;
143}
144
145
146lldb::OptionValueSP
147OptionValueString::DeepCopy () const
148{
149 return OptionValueSP(new OptionValueString(*this));
150}
Enrico Granata9d140842012-12-11 22:42:19 +0000151
152Error
153OptionValueString::SetCurrentValue (const char *value)
154{
155 if (m_validator)
156 {
157 Error error(m_validator(value,m_validator_baton));
158 if (error.Fail())
159 return error;
160 }
161 if (value && value[0])
162 m_current_value.assign (value);
163 else
164 m_current_value.clear();
165 return Error();
166}
167
168Error
169OptionValueString::AppendToCurrentValue (const char *value)
170{
171 if (value && value[0])
172 {
173 if (m_validator)
174 {
175 std::string new_value(m_current_value);
176 new_value.append(value);
177 Error error(m_validator(value,m_validator_baton));
178 if (error.Fail())
179 return error;
180 m_current_value.assign(new_value);
181 }
182 else
183 m_current_value.append (value);
184 }
185 return Error();
186}