xref: /aosp_15_r20/external/skia/src/ports/SkOSFile_ios.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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     if (!mainBundle) {
22         return false;
23     }
24 
25     // Get a reference to the file's URL
26     // Use this to normalize the path
27     sk_cfp<CFURLRef> pathURL(CFURLCreateFromFileSystemRepresentation(/*allocator=*/nullptr,
28                                                                      (const UInt8*)path,
29                                                                      strlen(path),
30                                                                      /*isDirectory=*/false));
31 
32     sk_cfp<CFStringRef> pathRef(CFURLCopyFileSystemPath(pathURL.get(), kCFURLPOSIXPathStyle));
33 
34     // We use "data" as our subdirectory to match {{bundle_resources_dir}}/data in GN
35     // Unfortunately "resources" is not a valid top-level name in iOS, so we push it one level down
36     sk_cfp<CFURLRef> fileURL(CFBundleCopyResourceURL(mainBundle, pathRef.get(),
37                                                      /*resourceType=*/nullptr, CFSTR("data")));
38     if (!fileURL) {
39         return false;
40     }
41     if (!result) {
42         return true;
43     }
44 
45     // Convert the URL reference into a string reference
46     sk_cfp<CFStringRef> filePath(CFURLCopyFileSystemPath(fileURL.get(), kCFURLPOSIXPathStyle));
47 
48     // Get the system encoding method
49     CFStringEncoding encodingMethod = CFStringGetSystemEncoding();
50 
51     // Convert the string reference into an SkString
52     result->set(CFStringGetCStringPtr(filePath.get(), encodingMethod));
53     return true;
54 }
55 #endif
56 
57 #endif  // SkOSFile_ios_DEFINED
58