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/State.h"
19 #include "lldb/DataFormatters/FormatManager.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             Args args(value_cstr);
98             if (args.GetArgumentCount() == 1)
99             {
100                 const char *path = args.GetArgumentAtIndex(0);
101                 m_value_was_set = true;
102                 m_current_value.SetFile(path, true);
103             }
104             else
105             {
106                 error.SetErrorString("please supply a single path argument for this file or quote the path if it contains spaces");
107             }
108         }
109         else
110         {
111             error.SetErrorString("invalid value string");
112         }
113         break;
114 
115     case eVarSetOperationInsertBefore:
116     case eVarSetOperationInsertAfter:
117     case eVarSetOperationRemove:
118     case eVarSetOperationAppend:
119     case eVarSetOperationInvalid:
120         error = OptionValue::SetValueFromCString (value_cstr, op);
121         break;
122     }
123     return error;
124 }
125 
126 lldb::OptionValueSP
127 OptionValueFileSpec::DeepCopy () const
128 {
129     return OptionValueSP(new OptionValueFileSpec(*this));
130 }
131 
132 
133 size_t
134 OptionValueFileSpec::AutoComplete (CommandInterpreter &interpreter,
135                                    const char *s,
136                                    int match_start_point,
137                                    int max_return_elements,
138                                    bool &word_complete,
139                                    StringList &matches)
140 {
141     word_complete = false;
142     matches.Clear();
143     CommandCompletions::InvokeCommonCompletionCallbacks (interpreter,
144                                                          m_completion_mask,
145                                                          s,
146                                                          match_start_point,
147                                                          max_return_elements,
148                                                          NULL,
149                                                          word_complete,
150                                                          matches);
151     return matches.GetSize();
152 }
153 
154 
155 
156 const lldb::DataBufferSP &
157 OptionValueFileSpec::GetFileContents(bool null_terminate)
158 {
159     if (!m_data_sp && m_current_value)
160     {
161         if (null_terminate)
162             m_data_sp = m_current_value.ReadFileContentsAsCString();
163         else
164             m_data_sp = m_current_value.ReadFileContents();
165     }
166     return m_data_sp;
167 }
168 
169 
170