xref: /openbsd-src/gnu/llvm/lldb/source/Interpreter/OptionValueChar.cpp (revision be691f3bb6417f04a68938fadbcaee2d5795e764)
1*dda28197Spatrick //===-- OptionValueChar.cpp -----------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include "lldb/Interpreter/OptionValueChar.h"
10061da546Spatrick 
11061da546Spatrick #include "lldb/Interpreter/OptionArgParser.h"
12061da546Spatrick #include "lldb/Utility/Stream.h"
13061da546Spatrick #include "lldb/Utility/StringList.h"
14061da546Spatrick #include "llvm/ADT/STLExtras.h"
15061da546Spatrick 
16061da546Spatrick using namespace lldb;
17061da546Spatrick using namespace lldb_private;
18061da546Spatrick 
DumpValue(const ExecutionContext * exe_ctx,Stream & strm,uint32_t dump_mask)19061da546Spatrick void OptionValueChar::DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
20061da546Spatrick                                 uint32_t dump_mask) {
21061da546Spatrick   if (dump_mask & eDumpOptionType)
22061da546Spatrick     strm.Printf("(%s)", GetTypeAsCString());
23061da546Spatrick 
24061da546Spatrick   if (dump_mask & eDumpOptionValue) {
25061da546Spatrick     if (dump_mask & eDumpOptionType)
26061da546Spatrick       strm.PutCString(" = ");
27061da546Spatrick     if (m_current_value != '\0')
28061da546Spatrick       strm.PutChar(m_current_value);
29061da546Spatrick     else
30061da546Spatrick       strm.PutCString("(null)");
31061da546Spatrick   }
32061da546Spatrick }
33061da546Spatrick 
SetValueFromString(llvm::StringRef value,VarSetOperationType op)34061da546Spatrick Status OptionValueChar::SetValueFromString(llvm::StringRef value,
35061da546Spatrick                                            VarSetOperationType op) {
36061da546Spatrick   Status error;
37061da546Spatrick   switch (op) {
38061da546Spatrick   case eVarSetOperationClear:
39061da546Spatrick     Clear();
40061da546Spatrick     break;
41061da546Spatrick 
42061da546Spatrick   case eVarSetOperationReplace:
43061da546Spatrick   case eVarSetOperationAssign: {
44061da546Spatrick     bool success = false;
45061da546Spatrick     char char_value = OptionArgParser::ToChar(value, '\0', &success);
46061da546Spatrick     if (success) {
47061da546Spatrick       m_current_value = char_value;
48061da546Spatrick       m_value_was_set = true;
49061da546Spatrick     } else
50061da546Spatrick       error.SetErrorStringWithFormat("'%s' cannot be longer than 1 character",
51061da546Spatrick                                      value.str().c_str());
52061da546Spatrick   } break;
53061da546Spatrick 
54061da546Spatrick   default:
55061da546Spatrick     error = OptionValue::SetValueFromString(value, op);
56061da546Spatrick     break;
57061da546Spatrick   }
58061da546Spatrick   return error;
59061da546Spatrick }
60