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 / MappedFile.m < prev    next >
Encoding:
Text File  |  1997-12-07  |  3.3 KB  |  132 lines

  1. //
  2. // $Id: MappedFile.m,v 1.4 1997/12/07 22:31:41 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 "MappedFile.h"
  29. #if NS_TARGET_MAJOR < 4
  30. #import <foundation/NSArray.h>
  31. #import <foundation/NSException.h>
  32. #import <foundation/NSPathUtilities.h>
  33. #import <foundation/NSUtilities.h>
  34.  
  35. @interface NSString (Foundation4PathCompatibility)
  36. - (NSArray *)pathComponents;
  37. @end
  38.  
  39. @implementation NSString (Foundation4PathCompatibility)
  40. - (NSArray *)pathComponents
  41. {
  42.    return [self componentsSeparatedByString:@"/"];
  43. }
  44. @end
  45.  
  46. #endif
  47.  
  48. #include <stdio.h>
  49. #include <libc.h>
  50. #include <ctype.h>
  51.  
  52. @implementation MappedFile
  53.  
  54. // Will map filename into memory.  If filename is a directory with specific suffixes, treat the directory as a wrapper.
  55.  
  56. - initWithFilename:(NSString *)aFilename
  57. {
  58.     NSString *standardPath;
  59. #if (NS_TARGET_MAJOR >= 4)
  60.     NSMutableSet *wrappers = [NSMutableSet set];
  61. #else
  62.     // for foundation 3.x (less efficient than a set but at least it works...)
  63.     NSMutableArray *wrappers = [NSMutableArray array];
  64. #endif
  65.     if ([super init] == nil)
  66.         return nil;
  67.  
  68.     standardPath = [aFilename stringByStandardizingPath];
  69.  
  70.     [wrappers addObject:@"app"];
  71.     [wrappers addObject:@"framework"];
  72.     [wrappers addObject:@"bundle"];
  73.     [wrappers addObject:@"palette"];
  74.  
  75.     if ([wrappers containsObject:[standardPath pathExtension]] == YES)
  76.     {
  77.         standardPath = [self pathToMainFileOfWrapper:standardPath];
  78.     }
  79.  
  80.     data = [[NSData dataWithContentsOfMappedFile:standardPath] retain];
  81.     if (data == nil)
  82.     {
  83.         NSLog (@"Couldn't map file: %@", standardPath);
  84.         return nil;
  85.     }
  86.  
  87.     filename = [standardPath retain];
  88.  
  89.     return self;
  90. }
  91.  
  92. - (void) dealloc
  93. {
  94.     [data release];
  95.     [filename release];
  96.  
  97.     [super dealloc];
  98. }
  99.  
  100. - (NSString *) filename
  101. {
  102.     return filename;
  103. }
  104.  
  105. - (const void *) data
  106. {
  107.     return [data bytes];
  108. }
  109.  
  110. // How does this handle something ending in "/"?
  111.  
  112. - (NSString *) pathToMainFileOfWrapper:(NSString *)path
  113. {
  114.     NSRange range;
  115.     NSMutableString *tmp;
  116.     NSString *extension;
  117.     NSString *base;
  118.  
  119.     base = [[path pathComponents] lastObject];
  120.     NSAssert (base != nil, @"No base.");
  121.     
  122.     extension = [NSString stringWithFormat:@".%@", [base pathExtension]];
  123.  
  124.     tmp = [NSMutableString stringWithFormat:@"%@/%@", path, base];
  125.     range = [tmp rangeOfString:extension options:NSBackwardsSearch];
  126.     [tmp deleteCharactersInRange:range];
  127.  
  128.     return tmp;
  129. }
  130.  
  131. @end
  132.