home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / MiscKit1.2.6 / Source / MiscBundleLoader.m < prev    next >
Encoding:
Text File  |  1994-03-16  |  2.2 KB  |  71 lines

  1. //
  2. //    MiscBundleLoader.m -- an object category to load .bundle files with
  3. //            code for a specific ObjC class in them
  4. //        Written by Mike Ferris (c) 1994 by Mike Ferris.
  5. //        Unburied from original MOKit by Don Yacktman.
  6. //                Version 1.0.  All rights reserved.
  7. //
  8. //        This notice may not be removed from this source code.
  9. //
  10. //    This object is included in the MiscKit by permission from the author
  11. //    and its use is governed by the MiscKit license, found in the file
  12. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  13. //    for a list of all applicable permissions and restrictions.
  14. //    
  15.  
  16.  
  17. // Category to load bundle-ized objects into apps dynamically.  Just
  18. // ask for the object by name...
  19. // This was originally in Mike Ferris' MOKit code, where it was well hidden...
  20.  
  21. #import <misckit/MiscBundleLoader.h>
  22. #import <appkit/nextstd.h>
  23. #import <objc/NXBundle.h>
  24. #import <objc/objc-runtime.h>
  25. #import <sys/param.h>        // for MAXPATHLEN
  26.  
  27. #define BUNDLE_TYPE                            "bundle"
  28.  
  29. @implementation Object(MiscBundleLoader)
  30.  
  31. + (Class)loadClassBundle:(const char *)className
  32. // Finds the bundle of the same name as className, grabs it and loads the
  33. // class from it and returns the named class.
  34. {
  35.     char pathBuff[MAXPATHLEN + 1];
  36.     id classBundle = nil;
  37.     Class class = nil;
  38.     
  39.     // Load the bundle
  40.     if ((class = objc_lookUpClass(className)) == nil)  {
  41.         // class is not already loaded... load it.
  42.         
  43.         // Look for the bundle in the main bundle first, 
  44.         // else try in this class's bundle.
  45.         if (![[NXBundle mainBundle] getPath:pathBuff forResource:className 
  46.                     ofType:BUNDLE_TYPE]) {
  47.             if (![[NXBundle bundleForClass:[self class]] getPath:pathBuff 
  48.                         forResource:className ofType:BUNDLE_TYPE]) {
  49.                 NXLogError("[%s loadClassBundle] failed to "
  50.                         "find %s class bundle.", [self name], className);
  51.                 return nil;
  52.             }
  53.         }
  54.         classBundle = [[NXBundle allocFromZone:[self zone]] 
  55.                     initForDirectory:pathBuff];
  56.         if (!classBundle) {
  57.             NXLogError("[%s loadClassBundle] failed to "
  58.                         "create bundle for class %s.", [self name], className);
  59.             return nil;
  60.         }
  61.         if ((class = [classBundle classNamed:className]) == nil) {
  62.             NXLogError("[%s loadClassBundle] failed to "
  63.                         "load %s class from bundle.", [self name], className);
  64.             return nil;
  65.         }
  66.     }
  67.     return class;
  68. }
  69.  
  70. @end
  71.