Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 79 lines (65 sloc) 3.493 kb
dc5be1c0 »
2009-07-04 Initial check-in.
1 //
2 // UKDebugNames.m
3 // filebrowser
4 //
5 // Created by Uli Kusterer on 01.05.05.
720b0e7b »
2010-03-04 Added licensing information to the source files.
6 // Copyright 2005 Uli Kusterer.
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
dc5be1c0 »
2009-07-04 Initial check-in.
26 //
27
28 #import "UKDebugNames.h"
29
30
31 NSMutableDictionary* gUKDebugNamesObjectToNameMap = nil; // Object pointer -> human-readable name map. Keys are hex strings ("0x00123456").
32 NSArray* gUKDebugNamesArray = nil; // Array of human-readable names we pick object names from.
33 int gUKDebugNamesNextNameIndex = 0; // Counter of next name from debug names array we'll use. May be out of range, in which case we wrap it before look-up.
34 int gUKDebugNamesReuseNumber = 0; // Counter to allow reusing names when we have used up all names in the debug names array.
35
36
37
38 NSString* UKDebugNameFor( id obj )
39 {
40 if( obj == nil )
41 return @"(null)";
42
43 // Lazily instantiate our mapping table and our list of human-readable names to choose from:
44 if( !gUKDebugNamesObjectToNameMap )
45 gUKDebugNamesObjectToNameMap = [[NSMutableDictionary alloc] init];
46 if( !gUKDebugNamesArray )
47 gUKDebugNamesArray = [[NSArray alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"UKDebugNames" ofType: @"plist"]];
48
49 // Generate a key and try to look up an existing mapping table entry:
50 NSString* key = [NSString stringWithFormat: @"%lx", obj];
51 NSString* value = [gUKDebugNamesObjectToNameMap objectForKey: key];
52
53 if( !value ) // No entry yet?
54 {
55 // Verify the "next name" index is still in range:
56 if( gUKDebugNamesNextNameIndex >= [gUKDebugNamesArray count] )
57 {
58 gUKDebugNamesReuseNumber++; // If not, increase the number appended to each name by one...
59 gUKDebugNamesNextNameIndex = 0; // ... and wrap back to the start of the name list.
60 }
61
62 // Get the next name and increment our "next name" counter:
63 value = [gUKDebugNamesArray objectAtIndex: gUKDebugNamesNextNameIndex++];
64 if( !value )
65 return @"(null-name)";
66
67 // If we're out of fresh names (reuse > 0), append the number to the name:
68 if( gUKDebugNamesReuseNumber > 0 )
69 value = [value stringByAppendingFormat: @" %d", gUKDebugNamesReuseNumber +1];
70 if( !value )
71 return @"(null-name2)";
72
73 // Add name to our mapping table so next time we can return the same name for this object:
74 [gUKDebugNamesObjectToNameMap setObject: value forKey: key];
75 }
76
77 return value; // Let the caller have the name.
78 }
Something went wrong with that request. Please try again.