Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
osx_bundle.mm
Go to the documentation of this file.
1 /* Copyright (c) 2012 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #import <AvailabilityMacros.h> // MAC_OS_X_VERSION_MIN_REQUIRED
24 #import <Foundation/Foundation.h>
25 #import <string>
26 
27 #import "osx_bundle.h"
28 
29 #define STRINGIZE2(id) # id
30 #define STRINGIZE(id) STRINGIZE2(id)
31 
32 // Pass the bundle identifier string as a build option
33 #ifdef BUNDLE_IDENTIFIER
34 static const char* BUNDLE_ID_STR = STRINGIZE(BUNDLE_IDENTIFIER);
35 #else
36 static const char* BUNDLE_ID_STR = "";
37 #endif
38 
39 
41 {
42  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
43 
44  // Check for the existence of bundle with correct identifier property
45  // (can't just use mainBundle because that can return a bundle reference
46  // even for a loose binary!)
47  NSBundle *bundle = [NSBundle bundleWithIdentifier: [NSString stringWithUTF8String: BUNDLE_ID_STR]];
48 
49  [pool drain];
50  return bundle != nil;
51 }
52 
53 std::string osx_GetBundlePath()
54 {
55  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
56  std::string path;
57 
58  NSBundle *bundle = [NSBundle bundleWithIdentifier: [NSString stringWithUTF8String: BUNDLE_ID_STR]];
59  if (bundle != nil)
60  {
61 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
62  // Retrieve NSURL and convert to POSIX path, then get C-string
63  // encoded as UTF-8, and use it to construct std::string
64  // NSURL:path "If the receiver does not conform to RFC 1808, returns nil."
65  NSString *pathStr = [[bundle bundleURL] path];
66 #else
67  NSString *pathStr = [bundle bundlePath];
68 #endif
69  if (pathStr != nil)
70  {
71  path = std::string([pathStr UTF8String]);
72  }
73  }
74 
75  [pool drain];
76  return path;
77 }
78 
80 {
81  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
82  std::string path;
83 
84  NSBundle *bundle = [NSBundle bundleWithIdentifier: [NSString stringWithUTF8String: BUNDLE_ID_STR]];
85  if (bundle != nil)
86  {
87 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
88  // Retrieve NSURL and convert to POSIX path, then get C-string
89  // encoded as UTF-8, and use it to construct std::string
90  // NSURL:path "If the receiver does not conform to RFC 1808, returns nil."
91  NSString *pathStr = [[bundle resourceURL] path];
92 #else
93  NSString *pathStr = [bundle resourcePath];
94 #endif
95  if (pathStr != nil)
96  {
97  path = std::string([pathStr UTF8String]);
98  }
99  }
100 
101  [pool drain];
102  return path;
103 }
104 
106 {
107  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
108  std::string path;
109 
110  NSBundle *bundle = [NSBundle bundleWithIdentifier: [NSString stringWithUTF8String: BUNDLE_ID_STR]];
111  if (bundle != nil)
112  {
113 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
114  // Retrieve NSURL and convert to POSIX path, then get C-string
115  // encoded as UTF-8, and use it to construct std::string
116  // NSURL:path "If the receiver does not conform to RFC 1808, returns nil."
117  NSString *pathStr = [[bundle privateFrameworksURL] path];
118 #else
119  NSString *pathStr = [bundle privateFrameworksPath];
120 #endif
121  if (pathStr != nil)
122  {
123  path = std::string([pathStr UTF8String]);
124  }
125  }
126 
127  [pool drain];
128  return path;
129 }
bool osx_IsAppBundleValid()
Check if app is running in a valid bundle.
Definition: osx_bundle.mm:40
static const char * BUNDLE_ID_STR
Definition: osx_bundle.mm:36
C++ interface to Cocoa implementation for getting bundle information.
std::string osx_GetBundleFrameworksPath()
Get the system path to the bundle&#39;s Frameworks directory.
Definition: osx_bundle.mm:105
#define STRINGIZE(id)
Definition: osx_bundle.mm:30
std::string osx_GetBundleResourcesPath()
Get the system path to the bundle&#39;s Resources directory.
Definition: osx_bundle.mm:79
std::string osx_GetBundlePath()
Get the system path to the bundle itself.
Definition: osx_bundle.mm:53