1 /*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkOSFile_ios_DEFINED
9 #define SkOSFile_ios_DEFINED
10
11 #include "include/core/SkString.h"
12
13 #ifdef SK_BUILD_FOR_IOS
14 #import <CoreFoundation/CoreFoundation.h>
15
16 #include "include/ports/SkCFObject.h"
17
ios_get_path_in_bundle(const char path[],SkString * result)18 static bool ios_get_path_in_bundle(const char path[], SkString* result) {
19 // Get a reference to the main bundle
20 CFBundleRef mainBundle = CFBundleGetMainBundle();
21
22 // Get a reference to the file's URL
23 // Use this to normalize the path
24 sk_cfp<CFURLRef> pathURL(CFURLCreateFromFileSystemRepresentation(/*allocator=*/nullptr,
25 (const UInt8*)path,
26 strlen(path),
27 /*isDirectory=*/false));
28 sk_cfp<CFStringRef> pathRef(CFURLCopyFileSystemPath(pathURL.get(), kCFURLPOSIXPathStyle));
29 // We use "data" as our subdirectory to match {{bundle_resources_dir}}/data in GN
30 // Unfortunately "resources" is not a valid top-level name in iOS, so we push it one level down
31 sk_cfp<CFURLRef> fileURL(CFBundleCopyResourceURL(mainBundle, pathRef.get(),
32 /*resourceType=*/nullptr, CFSTR("data")));
33 if (!fileURL) {
34 return false;
35 }
36 if (!result) {
37 return true;
38 }
39
40 // Convert the URL reference into a string reference
41 sk_cfp<CFStringRef> filePath(CFURLCopyFileSystemPath(fileURL.get(), kCFURLPOSIXPathStyle));
42
43 // Get the system encoding method
44 CFStringEncoding encodingMethod = CFStringGetSystemEncoding();
45
46 // Convert the string reference into an SkString
47 result->set(CFStringGetCStringPtr(filePath.get(), encodingMethod));
48 return true;
49 }
50 #endif
51
52 #endif // SkOSFile_ios_DEFINED
53