blob: e541b3ce72401815b60d474ef7e733c2eface56d [file] [log] [blame]
Jim Van Verth329c5a62017-11-29 11:42:33 -05001/*
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
Daniel Bratell7db91db2018-12-20 16:00:44 +01008#ifndef SkOSFile_ios_DEFINED
9#define SkOSFile_ios_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkString.h"
Jim Van Verth329c5a62017-11-29 11:42:33 -050012
13#ifdef SK_BUILD_FOR_IOS
14#import <CoreFoundation/CoreFoundation.h>
15
16static bool ios_get_path_in_bundle(const char path[], SkString* result) {
17 // Get a reference to the main bundle
18 CFBundleRef mainBundle = CFBundleGetMainBundle();
19
20 // Get a reference to the file's URL
21 CFStringRef pathRef = CFStringCreateWithCString(nullptr, path, kCFStringEncodingUTF8);
22 // We use "data" as our subdirectory to match {{bundle_resources_dir}}/data in GN
23 // Unfortunately "resources" is not a valid top-level name in iOS, so we push it one level down
24 CFURLRef imageURL = CFBundleCopyResourceURL(mainBundle, pathRef, nullptr, CFSTR("data"));
25 CFRelease(pathRef);
26 if (!imageURL) {
27 return false;
28 }
29 if (!result) {
30 return true;
31 }
32
33 // Convert the URL reference into a string reference
34 CFStringRef imagePath = CFURLCopyFileSystemPath(imageURL, kCFURLPOSIXPathStyle);
35 CFRelease(imageURL);
36
37 // Get the system encoding method
38 CFStringEncoding encodingMethod = CFStringGetSystemEncoding();
39
40 // Convert the string reference into an SkString
41 result->set(CFStringGetCStringPtr(imagePath, encodingMethod));
Jim Van Verthf16e0742019-04-02 14:33:03 -040042 CFRelease(imagePath);
Jim Van Verth329c5a62017-11-29 11:42:33 -050043 return true;
44}
45#endif
Daniel Bratell7db91db2018-12-20 16:00:44 +010046
47#endif // SkOSFile_ios_DEFINED