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/DataFormatters/FormatManager.h"
13 #include "lldb/Host/FileSystem.h"
14 #include "lldb/Interpreter/CommandCompletions.h"
15 #include "lldb/Interpreter/CommandInterpreter.h"
16 #include "lldb/Utility/Args.h"
17 #include "lldb/Utility/State.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 Status 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
69 // around the file path to indicate that internal spaces are not word
70 // breaks. Strip off any ws & quotes from the start and end of the file
71 // path - we aren't doing any word // breaking here so the quoting is
72 // unnecessary. NB this will cause a problem if someone tries to specify
73 // a file path that legitimately begins or ends with a " or ' character,
75 value = value.trim("\"' \t");
76 m_value_was_set = true;
77 m_current_value.SetFile(value.str(), FileSpec::Style::native);
79 FileSystem::Instance().Resolve(m_current_value);
81 m_data_mod_time = llvm::sys::TimePoint<>();
84 error.SetErrorString("invalid value string");
88 case eVarSetOperationInsertBefore:
89 case eVarSetOperationInsertAfter:
90 case eVarSetOperationRemove:
91 case eVarSetOperationAppend:
92 case eVarSetOperationInvalid:
93 error = OptionValue::SetValueFromString(value, op);
99 lldb::OptionValueSP OptionValueFileSpec::DeepCopy() const {
100 return OptionValueSP(new OptionValueFileSpec(*this));
103 size_t OptionValueFileSpec::AutoComplete(CommandInterpreter &interpreter,
104 CompletionRequest &request) {
105 request.SetWordComplete(false);
106 CommandCompletions::InvokeCommonCompletionCallbacks(
107 interpreter, m_completion_mask, request, nullptr);
108 return request.GetNumberOfMatches();
111 const lldb::DataBufferSP &OptionValueFileSpec::GetFileContents() {
112 if (m_current_value) {
113 const auto file_mod_time = FileSystem::Instance().GetModificationTime(m_current_value);
114 if (m_data_sp && m_data_mod_time == file_mod_time)
117 FileSystem::Instance().CreateDataBuffer(m_current_value.GetPath());
118 m_data_mod_time = file_mod_time;