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"
17 #include "lldb/Interpreter/Args.h"
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         {
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             }
42             else
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             }
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:
64         if (m_validator)
65         {
66             error = m_validator(value_cstr,m_validator_baton);
67             if (error.Fail())
68                 return error;
69         }
70         error = OptionValue::SetValueFromCString (value_cstr, op);
71         break;
72 
73     case eVarSetOperationAppend:
74         {
75         std::string new_value(m_current_value);
76         if (value_cstr && value_cstr[0])
77         {
78             if (m_options.Test (eOptionEncodeCharacterEscapeSequences))
79             {
80                 std::string str;
81                 Args::EncodeEscapeSequences (value_cstr, str);
82                 new_value.append(str);
83             }
84             else
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);
94         }
95         break;
96 
97     case eVarSetOperationClear:
98         Clear ();
99         break;
100 
101     case eVarSetOperationReplace:
102     case eVarSetOperationAssign:
103         if (m_validator)
104         {
105             error = m_validator(value_cstr,m_validator_baton);
106             if (error.Fail())
107                 return error;
108         }
109         m_value_was_set = true;
110         if (m_options.Test (eOptionEncodeCharacterEscapeSequences))
111         {
112             Args::EncodeEscapeSequences (value_cstr, m_current_value);
113         }
114         else
115         {
116             SetCurrentValue (value_cstr);
117         }
118         break;
119     }
120     return error;
121 }
122 
123 
124 lldb::OptionValueSP
125 OptionValueString::DeepCopy () const
126 {
127     return OptionValueSP(new OptionValueString(*this));
128 }
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 }
165