1 //===-- OptionValueFileSpec.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "lldb/Interpreter/OptionValueFileSpec.h"
16 // Other libraries and framework 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"
24 using namespace lldb_private;
27 OptionValueFileSpec::OptionValueFileSpec () :
32 m_completion_mask (CommandCompletions::eDiskFileCompletion)
36 OptionValueFileSpec::OptionValueFileSpec (const FileSpec &value) :
38 m_current_value (value),
39 m_default_value (value),
41 m_completion_mask (CommandCompletions::eDiskFileCompletion)
45 OptionValueFileSpec::OptionValueFileSpec (const FileSpec ¤t_value,
46 const FileSpec &default_value) :
48 m_current_value (current_value),
49 m_default_value (default_value),
51 m_completion_mask (CommandCompletions::eDiskFileCompletion)
56 OptionValueFileSpec::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
58 if (dump_mask & eDumpOptionType)
59 strm.Printf ("(%s)", GetTypeAsCString ());
60 if (dump_mask & eDumpOptionValue)
62 if (dump_mask & eDumpOptionType)
63 strm.PutCString (" = ");
67 strm << '"' << m_current_value.GetPath().c_str() << '"';
73 OptionValueFileSpec::SetValueFromCString (const char *value_cstr,
74 VarSetOperationType op)
79 case eVarSetOperationClear:
83 case eVarSetOperationReplace:
84 case eVarSetOperationAssign:
85 if (value_cstr && value_cstr[0])
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);
100 m_value_was_set = true;
101 m_current_value.SetFile(filepath.c_str(), true);
105 error.SetErrorString("invalid value string");
109 case eVarSetOperationInsertBefore:
110 case eVarSetOperationInsertAfter:
111 case eVarSetOperationRemove:
112 case eVarSetOperationAppend:
113 case eVarSetOperationInvalid:
114 error = OptionValue::SetValueFromCString (value_cstr, op);
121 OptionValueFileSpec::DeepCopy () const
123 return OptionValueSP(new OptionValueFileSpec(*this));
128 OptionValueFileSpec::AutoComplete (CommandInterpreter &interpreter,
130 int match_start_point,
131 int max_return_elements,
135 word_complete = false;
137 CommandCompletions::InvokeCommonCompletionCallbacks (interpreter,
145 return matches.GetSize();
150 const lldb::DataBufferSP &
151 OptionValueFileSpec::GetFileContents(bool null_terminate)
153 if (!m_data_sp && m_current_value)
156 m_data_sp = m_current_value.ReadFileContentsAsCString();
158 m_data_sp = m_current_value.ReadFileContents();