1 //===-- OptionValueRegex.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/OptionValueRegex.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Core/Stream.h"
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 
21 void OptionValueRegex::DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
22                                  uint32_t dump_mask) {
23   if (dump_mask & eDumpOptionType)
24     strm.Printf("(%s)", GetTypeAsCString());
25   if (dump_mask & eDumpOptionValue) {
26     if (dump_mask & eDumpOptionType)
27       strm.PutCString(" = ");
28     if (m_regex.IsValid()) {
29       const char *regex_text = m_regex.GetText();
30       if (regex_text && regex_text[0])
31         strm.Printf("%s", regex_text);
32     } else {
33     }
34   }
35 }
36 
37 Error OptionValueRegex::SetValueFromString(llvm::StringRef value,
38                                            VarSetOperationType op) {
39   Error error;
40   switch (op) {
41   case eVarSetOperationInvalid:
42   case eVarSetOperationInsertBefore:
43   case eVarSetOperationInsertAfter:
44   case eVarSetOperationRemove:
45   case eVarSetOperationAppend:
46     error = OptionValue::SetValueFromString(value, op);
47     break;
48 
49   case eVarSetOperationClear:
50     Clear();
51     NotifyValueChanged();
52     break;
53 
54   case eVarSetOperationReplace:
55   case eVarSetOperationAssign:
56     if (m_regex.Compile(value.str().c_str())) {
57       m_value_was_set = true;
58       NotifyValueChanged();
59     } else {
60       char regex_error[1024];
61       if (m_regex.GetErrorAsCString(regex_error, sizeof(regex_error)))
62         error.SetErrorString(regex_error);
63       else
64         error.SetErrorStringWithFormat("regex error %u",
65                                        m_regex.GetErrorCode());
66     }
67     break;
68   }
69   return error;
70 }
71 
72 lldb::OptionValueSP OptionValueRegex::DeepCopy() const {
73   return OptionValueSP(new OptionValueRegex(m_regex.GetText()));
74 }
75