1//===-- BreakpointID.cpp ----------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10
11// C Includes
12#include <stdio.h>
13
14// C++ Includes
15// Other libraries and framework includes
16// Project includes
17
18#include "lldb/Breakpoint/BreakpointID.h"
19#include "lldb/Breakpoint/Breakpoint.h"
20#include "lldb/Core/Stream.h"
21
22using namespace lldb;
23using namespace lldb_private;
24
25BreakpointID::BreakpointID (break_id_t bp_id, break_id_t loc_id) :
26    m_break_id (bp_id),
27    m_location_id (loc_id)
28{
29}
30
31BreakpointID::~BreakpointID ()
32{
33}
34
35const char *BreakpointID::g_range_specifiers[] = { "-", "to", "To", "TO", NULL };
36
37// Tells whether or not STR is valid to use between two strings representing breakpoint IDs, to
38// indicate a range of breakpoint IDs.  This is broken out into a separate function so that we can
39// easily change or add to the format for specifying ID ranges at a later date.
40
41bool
42BreakpointID::IsRangeIdentifier (const char *str)
43{
44    int specifier_count = 0;
45    for (int i = 0; g_range_specifiers[i] != NULL; ++i)
46      ++specifier_count;
47
48    for (int i = 0; i < specifier_count; ++i)
49    {
50      if (strcmp (g_range_specifiers[i], str) == 0)
51            return true;
52    }
53
54  return false;
55}
56
57bool
58BreakpointID::IsValidIDExpression (const char *str)
59{
60    break_id_t bp_id;
61    break_id_t loc_id;
62    BreakpointID::ParseCanonicalReference (str, &bp_id, &loc_id);
63
64    if (bp_id == LLDB_INVALID_BREAK_ID)
65        return false;
66    else
67        return true;
68}
69
70void
71BreakpointID::GetDescription (Stream *s, lldb::DescriptionLevel level)
72{
73    if (level == eDescriptionLevelVerbose)
74        s->Printf("%p BreakpointID:", this);
75
76    if (m_break_id == LLDB_INVALID_BREAK_ID)
77        s->PutCString ("<invalid>");
78    else if (m_location_id == LLDB_INVALID_BREAK_ID)
79        s->Printf("%i", m_break_id);
80    else
81        s->Printf("%i.%i", m_break_id, m_location_id);
82}
83
84void
85BreakpointID::GetCanonicalReference (Stream *s, break_id_t bp_id, break_id_t loc_id)
86{
87    if (bp_id == LLDB_INVALID_BREAK_ID)
88        s->PutCString ("<invalid>");
89    else if (loc_id == LLDB_INVALID_BREAK_ID)
90        s->Printf("%i", bp_id);
91    else
92        s->Printf("%i.%i", bp_id, loc_id);
93}
94
95bool
96BreakpointID::ParseCanonicalReference (const char *input, break_id_t *break_id_ptr, break_id_t *break_loc_id_ptr)
97{
98    *break_id_ptr = LLDB_INVALID_BREAK_ID;
99    *break_loc_id_ptr = LLDB_INVALID_BREAK_ID;
100
101    if (input == NULL || *input == '\0')
102        return false;
103
104    const char *format = "%i%n.%i%n";
105    int chars_consumed_1 = 0;
106    int chars_consumed_2 = 0;
107    int n_items_parsed = ::sscanf (input,
108                                   format,
109                                   break_id_ptr,        // %i   parse the breakpoint ID
110                                   &chars_consumed_1,   // %n   gets the number of characters parsed so far
111                                   break_loc_id_ptr,    // %i   parse the breakpoint location ID
112                                   &chars_consumed_2);  // %n   gets the number of characters parsed so far
113
114    if ((n_items_parsed == 1 && input[chars_consumed_1] == '\0') ||
115        (n_items_parsed == 2 && input[chars_consumed_2] == '\0'))
116        return true;
117
118    // Badly formatted canonical reference.
119    *break_id_ptr = LLDB_INVALID_BREAK_ID;
120    *break_loc_id_ptr = LLDB_INVALID_BREAK_ID;
121    return false;
122}
123
124