Add an initial breakpoints manager window and save the breakpoints to NSUserDefaults
[macgdbp.git] / Source / BreakpointManager.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2008, Blue Static <http://www.bluestatic.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17 #import "BreakpointManager.h"
18
19 NSString *kdBreakpoints = @"breakpoints";
20
21 @implementation BreakpointManager
22
23 /**
24 * Initializer
25 */
26 - (id)init
27 {
28 if (self = [super init])
29 {
30 breakpoints = (NSMutableDictionary *)[[NSUserDefaults standardUserDefaults] objectForKey:kdBreakpoints];
31 if (!breakpoints)
32 {
33 breakpoints = [NSMutableDictionary dictionary];
34 }
35 }
36 return self;
37 }
38
39 /**
40 * Returns the shared manager (singleton)
41 */
42 + (BreakpointManager *)sharedManager
43 {
44 static BreakpointManager *manager;
45 if (!manager)
46 {
47 manager = [[BreakpointManager alloc] init];
48 }
49 return manager;
50 }
51
52 /**
53 * Registers a breakpoint at a given line
54 */
55 - (void)addBreakpoint:(Breakpoint *)bp;
56 {
57 NSMutableSet *lines = [breakpoints valueForKey:[bp file]];
58 if (lines == nil)
59 {
60 lines = [NSMutableSet setWithObject:bp];
61 [breakpoints setValue:lines forKey:[bp file]];
62 }
63 else
64 {
65 [lines addObject:bp];
66 }
67 [[NSUserDefaults standardUserDefaults] setObject:breakpoints forKey:kdBreakpoints];
68 NSLog(@"breakpoints = %@", breakpoints);
69 }
70
71 /**
72 * Removes a breakpoint at a given line/file combination, or nil if nothing was removed
73 */
74 - (Breakpoint *)removeBreakpointAt:(int)line inFile:(NSString *)file
75 {
76 NSMutableSet *lines = [breakpoints valueForKey:file];
77 for (Breakpoint *b in lines)
78 {
79 if ([b line] == line)
80 {
81 [lines removeObject:b];
82 [[NSUserDefaults standardUserDefaults] setObject:breakpoints forKey:kdBreakpoints];
83 return b;
84 }
85 }
86 return nil;
87 }
88
89 /**
90 * Returns all the breakpoints for a given file
91 */
92 - (NSSet *)breakpointsForFile:(NSString *)file
93 {
94 return [breakpoints valueForKey:file];
95 }
96
97 /**
98 * Checks to see if a given file has a breakpoint on a given line
99 */
100 - (BOOL)hasBreakpointAt:(int)line inFile:(NSString *)file
101 {
102 NSSet *lines = [breakpoints valueForKey:file];
103 if (!lines)
104 {
105 return NO;
106 }
107 for (Breakpoint *b in lines)
108 {
109 if ([b line] == line)
110 {
111 return YES;
112 }
113 }
114 return NO;
115 }
116
117 @end