Permalink
Browse files

made er_*Resource use NSBundles URLForResource, added er_stringResource

  • Loading branch information...
1 parent dabcbee commit fd63f69e7f7499963f677a464af5c9312c9924bd @bkrpub committed Nov 23, 2011
Showing with 37 additions and 12 deletions.
  1. +6 −0 EngineRoom/ERUtility/NSBundle_ERExtensions.h
  2. +31 −12 EngineRoom/ERUtility/NSBundle_ERExtensions.m
@@ -5,8 +5,14 @@ extern NSString *kERBundleBuildConfiguration;
@interface NSBundle (ResourceExtensions)
+- (NSURL *) er_URLForResource: (NSString *) name withExtension: (NSString *) extension error: (NSError **) outError;
+
- (NSData *) er_dataResource: (NSString *) name ofType: (NSString *) type options: (NSUInteger) dataOptions error: (NSError **) outError;
+- (NSString *) er_stringResource: (NSString *) name ofType: (NSString *) type encoding: (NSStringEncoding) encoding error: (NSError **) outError;
+
+- (NSString *) er_stringResource: (NSString *) name ofType: (NSString *) type error: (NSError **) outError;
+
- (id) er_propertyListResource: (NSString *) name ofType: (NSString *) type options: (NSUInteger) plistOptions error: (NSError **) outError;
@end
@@ -9,29 +9,48 @@
@implementation NSBundle (ERResourceExtensions)
+- (NSURL *) er_URLForResource: (NSString *) name withExtension: (NSString *) extension error: (NSError **) outError
+{
+ ER_PRECONDITION_NSERROR_RETURN_NIL(name != nil, outError);
+
+ NSURL *URL = [self URLForResource: name withExtension: extension ?: @""];
+
+ ER_CHECK_NSERROR_REASON_RETURN_NIL(nil != URL, outError, NSPOSIXErrorDomain, ENOENT, @"NO_SUCH_RESOURCE: %@%@%@", name, extension ? @"." : @"", extension ?: @"");
+
+ return URL;
+}
+
- (NSData *) er_dataResource: (NSString *) name ofType: (NSString *) type options: (NSUInteger) dataOptions error: (NSError **) outError
{
- ER_PRECONDITION_NSERROR_RETURN_NIL(name != nil, outError);
-
- NSURL *resourceURL = [self resourceURL];
-
- NSURL *URL = [resourceURL URLByAppendingPathComponent: name];
-
- if( nil != type ) {
- URL = [URL URLByAppendingPathExtension: type];
- }
+ NSURL *URL = [self er_URLForResource: name withExtension: type error: outError];
+
+ if( nil == URL ) {
+ return nil;
+ }
return [NSData dataWithContentsOfURL: URL options: dataOptions error: outError];
}
- (id) er_propertyListResource: (NSString *) name ofType: (NSString *) type options: (NSUInteger) plistOptions error: (NSError **) outError
-{
- ER_PRECONDITION_NSERROR_RETURN_NIL(name != nil, outError);
-
+{
NSData *data = [self er_dataResource: name ofType: type options: 0 error: outError];
return data ? [NSPropertyListSerialization er_propertyListWithData: data options: plistOptions error: outError] : nil;
}
+- (NSString *) er_stringResource: (NSString *) name ofType: (NSString *) type encoding: (NSStringEncoding) encoding error: (NSError **) outError
+{
+ NSURL *URL = [self er_URLForResource: name withExtension: type error: outError];
+ NSString *string = URL ? [NSString stringWithContentsOfURL: URL encoding: encoding error: outError] : nil;
+ return string;
+}
+
+- (NSString *) er_stringResource: (NSString *) name ofType: (NSString *) type error: (NSError **) outError
+{
+ NSURL *URL = [self er_URLForResource: name withExtension: type error: outError];
+ NSString *string = URL ? [NSString stringWithContentsOfURL: URL usedEncoding: nil error: outError] : nil;
+ return string;
+}
+
@end
@implementation NSBundle (ERInfoDictionaryExtensions)

0 comments on commit fd63f69

Please sign in to comment.