blob: c095ba32f62c7899dbc37744fad3563e8b44ddac [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
Jim Van Verth329c5a62017-11-29 11:42:33 -050011#include "SkString.h"
12
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));
42 return true;
43}
44#endif
Daniel Bratell7db91db2018-12-20 16:00:44 +010045
46#endif // SkOSFile_ios_DEFINED