1 //===-- OptionValueFileSpec.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/OptionValueFileSpec.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Core/FormatManager.h"
17 #include "lldb/Core/State.h"
18 #include "lldb/Interpreter/Args.h"
19 #include "lldb/Interpreter/CommandCompletions.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 void
25 OptionValueFileSpec::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
26 {
27     if (dump_mask & eDumpOptionType)
28         strm.Printf ("(%s)", GetTypeAsCString ());
29     if (dump_mask & eDumpOptionValue)
30     {
31         if (dump_mask & eDumpOptionType)
32             strm.PutCString (" = ");
33 
34         if (m_current_value)
35         {
36             if (m_current_value.GetDirectory())
37             {
38                 strm << '"' << m_current_value.GetDirectory();
39                 if (m_current_value.GetFilename())
40                     strm << '/' << m_current_value.GetFilename();
41                 strm << '"';
42             }
43             else
44             {
45                 strm << '"' << m_current_value.GetFilename() << '"';
46             }
47         }
48     }
49 }
50 
51 Error
52 OptionValueFileSpec::SetValueFromCString (const char *value_cstr,
53                                           VarSetOperationType op)
54 {
55     Error error;
56     switch (op)
57     {
58     case eVarSetOperationClear:
59         Clear ();
60         break;
61 
62     case eVarSetOperationReplace:
63     case eVarSetOperationAssign:
64         if (value_cstr && value_cstr[0])
65         {
66             m_value_was_set = true;
67             m_current_value.SetFile(value_cstr, false);
68         }
69         else
70         {
71             error.SetErrorString("invalid value string");
72         }
73         break;
74 
75     case eVarSetOperationInsertBefore:
76     case eVarSetOperationInsertAfter:
77     case eVarSetOperationRemove:
78     case eVarSetOperationAppend:
79     case eVarSetOperationInvalid:
80         error = OptionValue::SetValueFromCString (value_cstr, op);
81         break;
82     }
83     return error;
84 }
85 
86 lldb::OptionValueSP
87 OptionValueFileSpec::DeepCopy () const
88 {
89     return OptionValueSP(new OptionValueFileSpec(*this));
90 }
91 
92 
93 size_t
94 OptionValueFileSpec::AutoComplete (CommandInterpreter &interpreter,
95                                    const char *s,
96                                    int match_start_point,
97                                    int max_return_elements,
98                                    bool &word_complete,
99                                    StringList &matches)
100 {
101     word_complete = false;
102     matches.Clear();
103     CommandCompletions::InvokeCommonCompletionCallbacks (interpreter,
104                                                          CommandCompletions::eDiskFileCompletion,
105                                                          s,
106                                                          match_start_point,
107                                                          max_return_elements,
108                                                          NULL,
109                                                          word_complete,
110                                                          matches);
111     return matches.GetSize();
112 }
113 
114 
115 
116 const lldb::DataBufferSP &
117 OptionValueFileSpec::GetFileContents(bool null_terminate)
118 {
119     if (!m_data_sp && m_current_value)
120     {
121         if (null_terminate)
122             m_data_sp = m_current_value.ReadFileContentsAsCString();
123         else
124             m_data_sp = m_current_value.ReadFileContents();
125     }
126     return m_data_sp;
127 }
128 
129 
130