Created
February 26, 2018 17:17
-
-
Save baarde/fc7b4d53723a946971ee6c2b981b6b4e to your computer and use it in GitHub Desktop.
NSBundle (XYZCurrent)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <execinfo.h> | |
#import <dlfcn.h> | |
static NSBundle *XYZBundleForAddressPointer(const void *pointer); | |
static NSBundle *XYZBundleForExecutablePath(NSString *execPath); | |
static NSString *XYZBundlePathFromExecutablePath(NSString *execPath); | |
static NSString *XYZFrameworkPathFromLibraryPath(NSString *libraryPath); | |
@implementation NSBundle (XYZCurrent) | |
+ (nonnull NSBundle *)current | |
{ | |
void *pointers[] = {NULL, NULL}; | |
backtrace(pointers, 2); | |
const void *pointer = pointers[1]; | |
return XYZBundleForAddressPointer(pointer) ?: [self mainBundle]; | |
} | |
@end | |
static NSBundle *XYZBundleForAddressPointer(const void *pointer) { | |
if (pointer == NULL) { | |
return nil; | |
} | |
Dl_info info; | |
int ret = dladdr(pointer, &info); | |
if (ret == 0 || info.dli_fname == NULL) { | |
return nil; | |
} | |
NSString *execPath = [NSString stringWithUTF8String:info.dli_fname]; | |
if (execPath == nil) { | |
return nil; | |
} | |
return XYZBundleForExecutablePath(execPath); | |
} | |
static NSBundle *XYZBundleForExecutablePath(NSString *execPath) { | |
NSString *bundlePath = | |
XYZFrameworkPathFromLibraryPath(execPath) ?: | |
XYZBundlePathFromExecutablePath(execPath); | |
if (bundlePath == nil) { | |
return nil; | |
} | |
return [NSBundle bundleWithPath:bundlePath]; | |
} | |
static NSString *XYZBundlePathFromExecutablePath(NSString *execPath) { | |
return nil; | |
} | |
static NSString *XYZFrameworkPathFromLibraryPath(NSString *libraryPath) { | |
return nil; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
something like: