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