OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkOSFile.h" | 8 #include "SkOSFile.h" |
9 #include "SkTypes.h" | 9 #include "SkTypes.h" |
10 | 10 |
11 #include <errno.h> | 11 #include <errno.h> |
12 #include <stdio.h> | 12 #include <stdio.h> |
13 #include <sys/stat.h> | 13 #include <sys/stat.h> |
14 | 14 |
15 #ifdef _WIN32 | 15 #ifdef _WIN32 |
16 #include <direct.h> | 16 #include <direct.h> |
17 #include <io.h> | 17 #include <io.h> |
18 #endif | 18 #endif |
19 | 19 |
| 20 #ifdef SK_BUILD_FOR_IOS |
| 21 #import <CoreFoundation/CoreFoundation.h> |
| 22 |
| 23 static FILE* ios_open_from_bundle(const char path[], const char* perm) { |
| 24 // Get a reference to the main bundle |
| 25 CFBundleRef mainBundle = CFBundleGetMainBundle(); |
| 26 |
| 27 // Get a reference to the file's URL |
| 28 CFStringRef pathRef = CFStringCreateWithCString(NULL, path, kCFStringEncodin
gUTF8); |
| 29 CFURLRef imageURL = CFBundleCopyResourceURL(mainBundle, pathRef, NULL, NULL)
; |
| 30 if (!imageURL) { |
| 31 return nullptr; |
| 32 } |
| 33 |
| 34 // Convert the URL reference into a string reference |
| 35 CFStringRef imagePath = CFURLCopyFileSystemPath(imageURL, kCFURLPOSIXPathSty
le); |
| 36 |
| 37 // Get the system encoding method |
| 38 CFStringEncoding encodingMethod = CFStringGetSystemEncoding(); |
| 39 |
| 40 // Convert the string reference into a C string |
| 41 const char *finalPath = CFStringGetCStringPtr(imagePath, encodingMethod); |
| 42 |
| 43 return fopen(finalPath, perm); |
| 44 } |
| 45 #endif |
| 46 |
| 47 |
20 SkFILE* sk_fopen(const char path[], SkFILE_Flags flags) { | 48 SkFILE* sk_fopen(const char path[], SkFILE_Flags flags) { |
21 char perm[4]; | 49 char perm[4]; |
22 char* p = perm; | 50 char* p = perm; |
23 | 51 |
24 if (flags & kRead_SkFILE_Flag) { | 52 if (flags & kRead_SkFILE_Flag) { |
25 *p++ = 'r'; | 53 *p++ = 'r'; |
26 } | 54 } |
27 if (flags & kWrite_SkFILE_Flag) { | 55 if (flags & kWrite_SkFILE_Flag) { |
28 *p++ = 'w'; | 56 *p++ = 'w'; |
29 } | 57 } |
30 *p++ = 'b'; | 58 *p++ = 'b'; |
31 *p = 0; | 59 *p = 0; |
32 | 60 |
33 //TODO: on Windows fopen is just ASCII or the current code page, | 61 //TODO: on Windows fopen is just ASCII or the current code page, |
34 //convert to utf16 and use _wfopen | 62 //convert to utf16 and use _wfopen |
35 SkFILE* file = (SkFILE*)::fopen(path, perm); | 63 SkFILE* file = nullptr; |
| 64 #ifdef SK_BUILD_FOR_IOS |
| 65 // if read-only, try to open from bundle first |
| 66 if (kRead_SkFILE_Flag == flags) { |
| 67 file = (SkFILE*)ios_open_from_bundle(path, perm); |
| 68 } |
| 69 // otherwise just read from the Documents directory (default) |
| 70 if (!file) { |
| 71 #endif |
| 72 file = (SkFILE*)::fopen(path, perm); |
| 73 #ifdef SK_BUILD_FOR_IOS |
| 74 } |
| 75 #endif |
36 if (nullptr == file && (flags & kWrite_SkFILE_Flag)) { | 76 if (nullptr == file && (flags & kWrite_SkFILE_Flag)) { |
37 SkDEBUGF(("sk_fopen: fopen(\"%s\", \"%s\") returned NULL (errno:%d): %s\
n", | 77 SkDEBUGF(("sk_fopen: fopen(\"%s\", \"%s\") returned NULL (errno:%d): %s\
n", |
38 path, perm, errno, strerror(errno))); | 78 path, perm, errno, strerror(errno))); |
39 } | 79 } |
40 return file; | 80 return file; |
41 } | 81 } |
42 | 82 |
43 char* sk_fgets(char* str, int size, SkFILE* f) { | 83 char* sk_fgets(char* str, int size, SkFILE* f) { |
44 return ::fgets(str, size, (FILE *)f); | 84 return ::fgets(str, size, (FILE *)f); |
45 } | 85 } |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 #else | 191 #else |
152 retval = mkdir(path, 0777); | 192 retval = mkdir(path, 0777); |
153 #endif | 193 #endif |
154 if (0 == retval) { | 194 if (0 == retval) { |
155 return true; | 195 return true; |
156 } else { | 196 } else { |
157 fprintf(stderr, "sk_mkdir: error %d creating dir '%s'\n", errno, path); | 197 fprintf(stderr, "sk_mkdir: error %d creating dir '%s'\n", errno, path); |
158 return false; | 198 return false; |
159 } | 199 } |
160 } | 200 } |
OLD | NEW |