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/Interpreter/OptionValueFileSpec.h"
12 #include "lldb/Core/State.h"
13 #include "lldb/DataFormatters/FormatManager.h"
14 #include "lldb/Host/FileSystem.h"
15 #include "lldb/Interpreter/Args.h"
16 #include "lldb/Interpreter/CommandCompletions.h"
17 #include "lldb/Interpreter/CommandInterpreter.h"
20 using namespace lldb_private;
22 OptionValueFileSpec::OptionValueFileSpec(bool resolve)
23 : OptionValue(), m_current_value(), m_default_value(), m_data_sp(),
25 m_completion_mask(CommandCompletions::eDiskFileCompletion),
28 OptionValueFileSpec::OptionValueFileSpec(const FileSpec &value, bool resolve)
29 : OptionValue(), m_current_value(value), m_default_value(value),
30 m_data_sp(), m_data_mod_time(),
31 m_completion_mask(CommandCompletions::eDiskFileCompletion),
34 OptionValueFileSpec::OptionValueFileSpec(const FileSpec ¤t_value,
35 const FileSpec &default_value,
37 : OptionValue(), m_current_value(current_value),
38 m_default_value(default_value), m_data_sp(), m_data_mod_time(),
39 m_completion_mask(CommandCompletions::eDiskFileCompletion),
42 void OptionValueFileSpec::DumpValue(const ExecutionContext *exe_ctx,
43 Stream &strm, uint32_t dump_mask) {
44 if (dump_mask & eDumpOptionType)
45 strm.Printf("(%s)", GetTypeAsCString());
46 if (dump_mask & eDumpOptionValue) {
47 if (dump_mask & eDumpOptionType)
48 strm.PutCString(" = ");
50 if (m_current_value) {
51 strm << '"' << m_current_value.GetPath().c_str() << '"';
56 Error OptionValueFileSpec::SetValueFromString(llvm::StringRef value,
57 VarSetOperationType op) {
60 case eVarSetOperationClear:
65 case eVarSetOperationReplace:
66 case eVarSetOperationAssign:
67 if (value.size() > 0) {
68 // The setting value may have whitespace, double-quotes, or single-quotes
70 // path to indicate that internal spaces are not word breaks. Strip off
72 // from the start and end of the file path - we aren't doing any word //
74 // the quoting is unnecessary. NB this will cause a problem if someone
76 // a file path that legitimately begins or ends with a " or ' character,
78 value = value.trim("\"' \t");
79 m_value_was_set = true;
80 m_current_value.SetFile(value.str(), m_resolve);
82 m_data_mod_time = llvm::sys::TimePoint<>();
85 error.SetErrorString("invalid value string");
89 case eVarSetOperationInsertBefore:
90 case eVarSetOperationInsertAfter:
91 case eVarSetOperationRemove:
92 case eVarSetOperationAppend:
93 case eVarSetOperationInvalid:
94 error = OptionValue::SetValueFromString(value, op);
100 lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const {
101 return OptionValueSP(new OptionValueFileSpec(*this));
104 size_t OptionValueFileSpec::AutoComplete(
105 CommandInterpreter &interpreter, llvm::StringRef s, int match_start_point,
106 int max_return_elements, bool &word_complete, StringList &matches) {
107 word_complete = false;
109 CommandCompletions::InvokeCommonCompletionCallbacks(
110 interpreter, m_completion_mask, s, match_start_point, max_return_elements,
111 nullptr, word_complete, matches);
112 return matches.GetSize();
115 const lldb::DataBufferSP &
116 OptionValueFileSpec::GetFileContents(bool null_terminate) {
117 if (m_current_value) {
118 const auto file_mod_time = FileSystem::GetModificationTime(m_current_value);
119 if (m_data_sp && m_data_mod_time == file_mod_time)
122 m_data_sp = m_current_value.ReadFileContentsAsCString();
124 m_data_sp = m_current_value.ReadFileContents();
125 m_data_mod_time = file_mod_time;