OptionValueUUID.cpp revision 254721
1169695Skan//===-- OptionValueUUID.cpp ------------------------------------*- C++ -*-===//
2169695Skan//
3169695Skan//                     The LLVM Compiler Infrastructure
4169695Skan//
5169695Skan// This file is distributed under the University of Illinois Open Source
6169695Skan// License. See LICENSE.TXT for details.
7169695Skan//
8169695Skan//===----------------------------------------------------------------------===//
9169695Skan
10169695Skan#include "lldb/lldb-python.h"
11169695Skan
12169695Skan#include "lldb/Interpreter/OptionValueUUID.h"
13169695Skan
14169695Skan// C Includes
15169695Skan// C++ Includes
16169695Skan// Other libraries and framework includes
17169695Skan// Project includes
18169695Skan#include "lldb/Core/Module.h"
19169695Skan#include "lldb/Core/Stream.h"
20169695Skan#include "lldb/Core/StringList.h"
21169695Skan#include "lldb/Interpreter/CommandInterpreter.h"
22169695Skan
23169695Skanusing namespace lldb;
24169695Skanusing namespace lldb_private;
25169695Skan
26169695Skanvoid
27169695SkanOptionValueUUID::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
28169695Skan{
29169695Skan    if (dump_mask & eDumpOptionType)
30169695Skan        strm.Printf ("(%s)", GetTypeAsCString ());
31169695Skan    if (dump_mask & eDumpOptionValue)
32169695Skan    {
33169695Skan        if (dump_mask & eDumpOptionType)
34169695Skan            strm.PutCString (" = ");
35169695Skan        m_uuid.Dump (&strm);
36169695Skan    }
37169695Skan}
38169695Skan
39169695SkanError
40169695SkanOptionValueUUID::SetValueFromCString (const char *value_cstr,
41169695Skan                                      VarSetOperationType op)
42169695Skan{
43169695Skan    Error error;
44169695Skan    switch (op)
45169695Skan    {
46169695Skan        case eVarSetOperationClear:
47169695Skan            Clear();
48169695Skan            break;
49169695Skan
50169695Skan        case eVarSetOperationReplace:
51169695Skan        case eVarSetOperationAssign:
52169695Skan            {
53169695Skan                if (m_uuid.SetFromCString(value_cstr) == 0)
54169695Skan                    error.SetErrorStringWithFormat ("invalid uuid string value '%s'", value_cstr);
55169695Skan                else
56169695Skan                    m_value_was_set = true;
57169695Skan            }
58169695Skan            break;
59169695Skan
60169695Skan        case eVarSetOperationInsertBefore:
61169695Skan        case eVarSetOperationInsertAfter:
62169695Skan        case eVarSetOperationRemove:
63169695Skan        case eVarSetOperationAppend:
64169695Skan        case eVarSetOperationInvalid:
65169695Skan            error = OptionValue::SetValueFromCString (value_cstr, op);
66169695Skan            break;
67    }
68    return error;
69}
70
71lldb::OptionValueSP
72OptionValueUUID::DeepCopy () const
73{
74    return OptionValueSP(new OptionValueUUID(*this));
75}
76
77size_t
78OptionValueUUID::AutoComplete (CommandInterpreter &interpreter,
79                               const char *s,
80                               int match_start_point,
81                               int max_return_elements,
82                               bool &word_complete,
83                               StringList &matches)
84{
85    word_complete = false;
86    matches.Clear();
87    ExecutionContext exe_ctx(interpreter.GetExecutionContext());
88    Target *target = exe_ctx.GetTargetPtr();
89    if (target)
90    {
91        const size_t num_modules = target->GetImages().GetSize();
92        if (num_modules > 0)
93        {
94            UUID::ValueType uuid_bytes;
95            const size_t num_bytes_decoded = UUID::DecodeUUIDBytesFromCString(s, uuid_bytes, NULL);
96            for (size_t i=0; i<num_modules; ++i)
97            {
98                ModuleSP module_sp (target->GetImages().GetModuleAtIndex(i));
99                if (module_sp)
100                {
101                    const UUID &module_uuid = module_sp->GetUUID();
102                    if (module_uuid.IsValid())
103                    {
104                        bool add_uuid = false;
105                        if (num_bytes_decoded == 0)
106                            add_uuid = true;
107                        else
108                            add_uuid = ::memcmp(module_uuid.GetBytes(), uuid_bytes, num_bytes_decoded) == 0;
109                        if (add_uuid)
110                        {
111                            std::string uuid_str;
112                            uuid_str = module_uuid.GetAsString();
113                            if (!uuid_str.empty())
114                                matches.AppendString(uuid_str.c_str());
115                        }
116                    }
117                }
118            }
119        }
120    }
121    return matches.GetSize();
122}
123
124