Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
osx_paths.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_paths.h"
28 
29 // Helper function
30 static std::string getUserDirectoryPath(NSSearchPathDirectory directory)
31 {
32  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
33  std::string result;
34 
35 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
36  // Returns array of NSURL objects which are preferred for file paths
37  NSArray* paths = [[NSFileManager defaultManager] URLsForDirectory:directory inDomains:NSUserDomainMask];
38 #else
39  NSArray* paths = NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, true);
40 #endif
41  if ([paths count] > 0)
42  {
43 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
44  // Retrieve first NSURL and convert to POSIX path, then get C-string
45  // encoded as UTF-8, and use it to construct std::string
46  // NSURL:path "If the receiver does not conform to RFC 1808, returns nil."
47  NSString* pathStr = [[paths objectAtIndex:0] path];
48 #else
49  NSString* pathStr = [paths objectAtIndex:0];
50 #endif
51  if (pathStr != nil)
52  result = std::string([pathStr UTF8String]);
53  }
54 
55  [pool drain];
56  return result;
57 }
58 
59 std::string osx_GetAppSupportPath()
60 {
61  return getUserDirectoryPath(NSApplicationSupportDirectory);
62 }
63 
64 std::string osx_GetCachesPath()
65 {
66  return getUserDirectoryPath(NSCachesDirectory);
67 }
std::string osx_GetCachesPath()
Get the user&#39;s Caches path (typically ~/Library/Caches)
Definition: osx_paths.mm:64
std::string osx_GetAppSupportPath()
Get the user&#39;s Application Support path (typically ~/Library/Application Support) ...
Definition: osx_paths.mm:59
static std::string getUserDirectoryPath(NSSearchPathDirectory directory)
Definition: osx_paths.mm:30
C++ interface to Cocoa implementation for retrieving standard OS X paths.