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