1254721Semaste//===-- OptionValueFormat.cpp -----------------------------------*- C++ -*-===//
2254721Semaste//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6254721Semaste//
7254721Semaste//===----------------------------------------------------------------------===//
8254721Semaste
9254721Semaste#include "lldb/Interpreter/OptionValueFormat.h"
10254721Semaste
11254721Semaste#include "lldb/DataFormatters/FormatManager.h"
12341825Sdim#include "lldb/Interpreter/OptionArgParser.h"
13321369Sdim#include "lldb/Utility/Stream.h"
14254721Semaste
15254721Semasteusing namespace lldb;
16254721Semasteusing namespace lldb_private;
17254721Semaste
18314564Sdimvoid OptionValueFormat::DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
19314564Sdim                                  uint32_t dump_mask) {
20314564Sdim  if (dump_mask & eDumpOptionType)
21314564Sdim    strm.Printf("(%s)", GetTypeAsCString());
22314564Sdim  if (dump_mask & eDumpOptionValue) {
23254721Semaste    if (dump_mask & eDumpOptionType)
24314564Sdim      strm.PutCString(" = ");
25314564Sdim    strm.PutCString(FormatManager::GetFormatAsCString(m_current_value));
26314564Sdim  }
27254721Semaste}
28254721Semaste
29321369SdimStatus OptionValueFormat::SetValueFromString(llvm::StringRef value,
30321369Sdim                                             VarSetOperationType op) {
31321369Sdim  Status error;
32314564Sdim  switch (op) {
33314564Sdim  case eVarSetOperationClear:
34314564Sdim    Clear();
35314564Sdim    NotifyValueChanged();
36314564Sdim    break;
37314564Sdim
38314564Sdim  case eVarSetOperationReplace:
39314564Sdim  case eVarSetOperationAssign: {
40314564Sdim    Format new_format;
41341825Sdim    error = OptionArgParser::ToFormat(value.str().c_str(), new_format, nullptr);
42314564Sdim    if (error.Success()) {
43314564Sdim      m_value_was_set = true;
44314564Sdim      m_current_value = new_format;
45314564Sdim      NotifyValueChanged();
46254721Semaste    }
47314564Sdim  } break;
48314564Sdim
49314564Sdim  case eVarSetOperationInsertBefore:
50314564Sdim  case eVarSetOperationInsertAfter:
51314564Sdim  case eVarSetOperationRemove:
52314564Sdim  case eVarSetOperationAppend:
53314564Sdim  case eVarSetOperationInvalid:
54314564Sdim    error = OptionValue::SetValueFromString(value, op);
55314564Sdim    break;
56314564Sdim  }
57314564Sdim  return error;
58254721Semaste}
59254721Semaste
60314564Sdimlldb::OptionValueSP OptionValueFormat::DeepCopy() const {
61314564Sdim  return OptionValueSP(new OptionValueFormat(*this));
62254721Semaste}
63