Jim Van Verth | 329c5a6 | 2017-11-29 11:42:33 -0500 | [diff] [blame] | 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 | |
Daniel Bratell | 7db91db | 2018-12-20 16:00:44 +0100 | [diff] [blame] | 8 | #ifndef SkOSFile_ios_DEFINED |
| 9 | #define SkOSFile_ios_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkString.h" |
Jim Van Verth | 329c5a6 | 2017-11-29 11:42:33 -0500 | [diff] [blame] | 12 | |
| 13 | #ifdef SK_BUILD_FOR_IOS |
| 14 | #import <CoreFoundation/CoreFoundation.h> |
| 15 | |
| 16 | static 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 Verth | f16e074 | 2019-04-02 14:33:03 -0400 | [diff] [blame] | 42 | CFRelease(imagePath); |
Jim Van Verth | 329c5a6 | 2017-11-29 11:42:33 -0500 | [diff] [blame] | 43 | return true; |
| 44 | } |
| 45 | #endif |
Daniel Bratell | 7db91db | 2018-12-20 16:00:44 +0100 | [diff] [blame] | 46 | |
| 47 | #endif // SkOSFile_ios_DEFINED |