home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / OPENSTEP / UNIX / Programming / class-dump-2.1.1-MIS / src / ObjcClass.m < prev    next >
Encoding:
Text File  |  1997-12-07  |  4.7 KB  |  187 lines

  1. //
  2. // $Id: ObjcClass.m,v 1.3 1997/12/07 22:31:43 nygard Exp $
  3. //
  4.  
  5. //
  6. //  This file is a part of class-dump v2, a utility for examining the
  7. //  Objective-C segment of Mach-O files.
  8. //  Copyright (C) 1997  Steve Nygard
  9. //
  10. //  This program is free software; you can redistribute it and/or modify
  11. //  it under the terms of the GNU General Public License as published by
  12. //  the Free Software Foundation; either version 2 of the License, or
  13. //  (at your option) any later version.
  14. //
  15. //  This program is distributed in the hope that it will be useful,
  16. //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. //  GNU General Public License for more details.
  19. //
  20. //  You should have received a copy of the GNU General Public License
  21. //  along with this program; if not, write to the Free Software
  22. //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. //
  24. //  You may contact the author by:
  25. //     e-mail:  nygard@telusplanet.net
  26. //
  27.  
  28. #import "ObjcClass.h"
  29. #include "datatypes.h"
  30. #import "ObjcIvar.h"
  31. #import "ObjcMethod.h"
  32. #if NS_TARGET_MAJOR < 4
  33. #import <foundation/NSUtilities.h>
  34. #endif
  35. #import <stdio.h>
  36.  
  37. @implementation ObjcClass
  38.  
  39. - initWithClassName:(NSString *)className superClassName:(NSString *)superClassName
  40. {
  41.     if ([super init] == nil)
  42.         return nil;
  43.  
  44.     class_name = [className retain];
  45.     super_class_name = [superClassName retain];
  46.     ivars = [[NSMutableArray array] retain];
  47.     class_methods = [[NSMutableArray array] retain];
  48.     instance_methods = [[NSMutableArray array] retain];
  49.     protocol_names = [[NSMutableArray array] retain];
  50.  
  51.     return self;
  52. }
  53.  
  54. - (void) dealloc
  55. {
  56.     [class_name release];
  57.     [super_class_name release];
  58.     [ivars release];
  59.     [class_methods release];
  60.     [instance_methods release];
  61.     [protocol_names release];
  62.     
  63.     [super dealloc];
  64. }
  65.  
  66. - (void) addIvars:(NSArray *)newIvars
  67. {
  68.     [ivars addObjectsFromArray:newIvars];
  69. }
  70.  
  71. - (NSString *) description
  72. {
  73.     return [NSString stringWithFormat:@"@interface %@:%@ {\n%@\n}\n%@\n%@",
  74.                      class_name, super_class_name, ivars, class_methods, instance_methods];
  75. }
  76.  
  77. - (NSString *) className
  78. {
  79.     return class_name;
  80. }
  81.  
  82. - (NSArray *) protocolNames
  83. {
  84.     return protocol_names;
  85. }
  86.  
  87. - (NSString *) sortableName
  88. {
  89.     return class_name;
  90. }
  91.  
  92. - (void) addClassMethods:(NSArray *)newClassMethods
  93. {
  94.     [class_methods addObjectsFromArray:newClassMethods];
  95. }
  96.  
  97. - (void) addInstanceMethods:(NSArray *)newInstanceMethods
  98. {
  99.     [instance_methods addObjectsFromArray:newInstanceMethods];
  100. }
  101.  
  102. - (void) addProtocolNames:(NSArray *)newProtocolNames
  103. {
  104.     [protocol_names addObjectsFromArray:newProtocolNames];
  105. }
  106.  
  107. - (void) showDefinition:(int)flags
  108. {
  109.     NSEnumerator *enumerator;
  110.     ObjcIvar *ivar;
  111.     ObjcMethod *method;
  112.     NSString *protocolName;
  113.  
  114.     printf ("@interface %s", [class_name cString]);
  115.     if (super_class_name != nil)
  116.         printf (":%s", [super_class_name cString]);
  117.  
  118.     if ([protocol_names count] > 0)
  119.     {
  120.         enumerator = [protocol_names objectEnumerator];
  121.         printf (" <");
  122.         protocolName = [enumerator nextObject];
  123.         if (protocolName != nil)
  124.         {
  125.             printf ("%s", [protocolName cString]);
  126.             
  127.             while (protocolName = [enumerator nextObject])
  128.             {
  129.                 printf (", %s", [protocolName cString]);
  130.             }
  131.         }
  132.  
  133.         printf (">");
  134.     }
  135.  
  136.     printf ("\n{\n");
  137.  
  138.     enumerator = [ivars objectEnumerator];
  139.     while (ivar = [enumerator nextObject])
  140.     {
  141.         [ivar showIvarAtLevel:2];
  142.         if (flags & F_SHOW_IVAR_OFFSET)
  143.         {
  144.             printf ("\t// %ld = 0x%lx", [ivar offset], [ivar offset]);
  145.         }
  146.         printf ("\n");
  147.     }
  148.  
  149.     //printf ("%s\n", [[ivars description] cString]);
  150.     printf ("}\n\n");
  151.  
  152.     //NSLog (@"class_methods: %@", class_methods);
  153.  
  154.     if (flags & F_SORT_METHODS)
  155.         enumerator = [[class_methods sortedArrayUsingSelector:@selector (orderByMethodName:)] objectEnumerator];
  156.     else
  157.         enumerator = [class_methods reverseObjectEnumerator];
  158.  
  159.     while (method = [enumerator nextObject])
  160.     {
  161.         [method showMethod:'+'];
  162.         if (flags & F_SHOW_METHOD_ADDRESS)
  163.         {
  164.             printf ("\t// IMP=0x%08lx", [method address]);
  165.         }
  166.         printf ("\n");
  167.     }
  168.  
  169.     if (flags & F_SORT_METHODS)
  170.         enumerator = [[instance_methods sortedArrayUsingSelector:@selector (orderByMethodName:)] objectEnumerator];
  171.     else
  172.         enumerator = [instance_methods reverseObjectEnumerator];
  173.     while (method = [enumerator nextObject])
  174.     {
  175.         [method showMethod:'-'];
  176.         if (flags & F_SHOW_METHOD_ADDRESS)
  177.         {
  178.             printf ("\t// IMP=0x%08lx", [method address]);
  179.         }
  180.         printf ("\n");
  181.     }
  182.  
  183.     printf ("\n@end\n\n");
  184. }
  185.  
  186. @end
  187.