OptionValueFileSpec.cpp revision 276479
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
23using namespace lldb;
24using namespace lldb_private;
25
26
27OptionValueFileSpec::OptionValueFileSpec () :
28    OptionValue(),
29    m_current_value (),
30    m_default_value (),
31    m_data_sp(),
32    m_completion_mask (CommandCompletions::eDiskFileCompletion)
33{
34}
35
36OptionValueFileSpec::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
45OptionValueFileSpec::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
55void
56OptionValueFileSpec::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            strm << '"' << m_current_value.GetPath().c_str() << '"';
68        }
69    }
70}
71
72Error
73OptionValueFileSpec::SetValueFromCString (const char *value_cstr,
74                                          VarSetOperationType op)
75{
76    Error error;
77    switch (op)
78    {
79    case eVarSetOperationClear:
80        Clear ();
81        break;
82
83    case eVarSetOperationReplace:
84    case eVarSetOperationAssign:
85        if (value_cstr && value_cstr[0])
86        {
87            // The setting value may have whitespace, double-quotes, or single-quotes around the file
88            // path to indicate that internal spaces are not word breaks.  Strip off any ws & quotes
89            // from the start and end of the file path - we aren't doing any word // breaking here so
90            // the quoting is unnecessary.  NB this will cause a problem if someone tries to specify
91            // a file path that legitimately begins or ends with a " or ' character, or whitespace.
92            std::string filepath(value_cstr);
93            auto prefix_chars_to_trim = filepath.find_first_not_of ("\"' \t");
94            if (prefix_chars_to_trim != std::string::npos && prefix_chars_to_trim > 0)
95                filepath.erase(0, prefix_chars_to_trim);
96            auto suffix_chars_to_trim = filepath.find_last_not_of ("\"' \t");
97            if (suffix_chars_to_trim != std::string::npos && suffix_chars_to_trim < filepath.size())
98                filepath.erase (suffix_chars_to_trim + 1);
99
100            m_value_was_set = true;
101            m_current_value.SetFile(filepath.c_str(), true);
102            m_data_sp.reset();
103        }
104        else
105        {
106            error.SetErrorString("invalid value string");
107        }
108        break;
109
110    case eVarSetOperationInsertBefore:
111    case eVarSetOperationInsertAfter:
112    case eVarSetOperationRemove:
113    case eVarSetOperationAppend:
114    case eVarSetOperationInvalid:
115        error = OptionValue::SetValueFromCString (value_cstr, op);
116        break;
117    }
118    return error;
119}
120
121lldb::OptionValueSP
122OptionValueFileSpec::DeepCopy () const
123{
124    return OptionValueSP(new OptionValueFileSpec(*this));
125}
126
127
128size_t
129OptionValueFileSpec::AutoComplete (CommandInterpreter &interpreter,
130                                   const char *s,
131                                   int match_start_point,
132                                   int max_return_elements,
133                                   bool &word_complete,
134                                   StringList &matches)
135{
136    word_complete = false;
137    matches.Clear();
138    CommandCompletions::InvokeCommonCompletionCallbacks (interpreter,
139                                                         m_completion_mask,
140                                                         s,
141                                                         match_start_point,
142                                                         max_return_elements,
143                                                         nullptr,
144                                                         word_complete,
145                                                         matches);
146    return matches.GetSize();
147}
148
149
150
151const lldb::DataBufferSP &
152OptionValueFileSpec::GetFileContents(bool null_terminate)
153{
154    if (!m_data_sp && m_current_value)
155    {
156        if (null_terminate)
157            m_data_sp = m_current_value.ReadFileContentsAsCString();
158        else
159            m_data_sp = m_current_value.ReadFileContents();
160    }
161    return m_data_sp;
162}
163
164
165