blob: 6d39b3afd19a44a48cdb310523d1e4571d54ef59 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
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
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkOSFile.h"
bungemanf20488b2015-07-29 11:49:40 -07009#include "SkTypes.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include <errno.h>
epoger@google.come8ebeb12012-10-29 16:42:11 +000012#include <stdio.h>
13#include <sys/stat.h>
epoger@google.come8ebeb12012-10-29 16:42:11 +000014
abarth6fc8ff02016-07-15 15:15:15 -070015#ifdef SK_BUILD_FOR_UNIX
16#include <unistd.h>
17#endif
18
epoger@google.come8ebeb12012-10-29 16:42:11 +000019#ifdef _WIN32
20#include <direct.h>
21#include <io.h>
epoger@google.come8ebeb12012-10-29 16:42:11 +000022#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000023
jvanverth44dcb8a2015-10-02 09:12:05 -070024#ifdef SK_BUILD_FOR_IOS
25#import <CoreFoundation/CoreFoundation.h>
26
27static FILE* ios_open_from_bundle(const char path[], const char* perm) {
28 // Get a reference to the main bundle
29 CFBundleRef mainBundle = CFBundleGetMainBundle();
mtklein944c2d92016-02-16 08:10:34 -080030
jvanverth44dcb8a2015-10-02 09:12:05 -070031 // Get a reference to the file's URL
32 CFStringRef pathRef = CFStringCreateWithCString(NULL, path, kCFStringEncodingUTF8);
33 CFURLRef imageURL = CFBundleCopyResourceURL(mainBundle, pathRef, NULL, NULL);
sdefresne388127f2016-09-20 08:53:14 -070034 CFRelease(pathRef);
jvanverth44dcb8a2015-10-02 09:12:05 -070035 if (!imageURL) {
36 return nullptr;
37 }
mtklein944c2d92016-02-16 08:10:34 -080038
jvanverth44dcb8a2015-10-02 09:12:05 -070039 // Convert the URL reference into a string reference
40 CFStringRef imagePath = CFURLCopyFileSystemPath(imageURL, kCFURLPOSIXPathStyle);
mtklein944c2d92016-02-16 08:10:34 -080041
jvanverth44dcb8a2015-10-02 09:12:05 -070042 // Get the system encoding method
43 CFStringEncoding encodingMethod = CFStringGetSystemEncoding();
mtklein944c2d92016-02-16 08:10:34 -080044
jvanverth44dcb8a2015-10-02 09:12:05 -070045 // Convert the string reference into a C string
46 const char *finalPath = CFStringGetCStringPtr(imagePath, encodingMethod);
mtklein944c2d92016-02-16 08:10:34 -080047
jvanverth44dcb8a2015-10-02 09:12:05 -070048 return fopen(finalPath, perm);
49}
50#endif
51
52
halcanaryd76be9c2015-11-20 13:47:49 -080053FILE* sk_fopen(const char path[], SkFILE_Flags flags) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 char perm[4];
55 char* p = perm;
56
bungeman@google.com6cab1a42013-05-29 13:43:31 +000057 if (flags & kRead_SkFILE_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 *p++ = 'r';
bungeman@google.com6cab1a42013-05-29 13:43:31 +000059 }
60 if (flags & kWrite_SkFILE_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 *p++ = 'w';
bungeman@google.com6cab1a42013-05-29 13:43:31 +000062 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 *p++ = 'b';
64 *p = 0;
mtklein944c2d92016-02-16 08:10:34 -080065
commit-bot@chromium.org9711e442013-04-24 20:03:00 +000066 //TODO: on Windows fopen is just ASCII or the current code page,
67 //convert to utf16 and use _wfopen
halcanaryd76be9c2015-11-20 13:47:49 -080068 FILE* file = nullptr;
jvanverth44dcb8a2015-10-02 09:12:05 -070069#ifdef SK_BUILD_FOR_IOS
70 // if read-only, try to open from bundle first
71 if (kRead_SkFILE_Flag == flags) {
halcanaryd76be9c2015-11-20 13:47:49 -080072 file = ios_open_from_bundle(path, perm);
jvanverth44dcb8a2015-10-02 09:12:05 -070073 }
74 // otherwise just read from the Documents directory (default)
75 if (!file) {
76#endif
mtklein944c2d92016-02-16 08:10:34 -080077 file = fopen(path, perm);
jvanverth44dcb8a2015-10-02 09:12:05 -070078#ifdef SK_BUILD_FOR_IOS
79 }
80#endif
bungeman0881b952015-09-02 12:41:35 -070081 if (nullptr == file && (flags & kWrite_SkFILE_Flag)) {
82 SkDEBUGF(("sk_fopen: fopen(\"%s\", \"%s\") returned NULL (errno:%d): %s\n",
83 path, perm, errno, strerror(errno)));
84 }
85 return file;
reed@android.com8a1c16f2008-12-17 15:59:43 +000086}
87
halcanaryd76be9c2015-11-20 13:47:49 -080088char* sk_fgets(char* str, int size, FILE* f) {
mtklein944c2d92016-02-16 08:10:34 -080089 return fgets(str, size, (FILE *)f);
humper@google.com7af56be2013-01-14 18:49:19 +000090}
91
halcanaryd76be9c2015-11-20 13:47:49 -080092int sk_feof(FILE *f) {
humper@google.comd386b0e2013-01-14 22:01:14 +000093 // no :: namespace qualifier because it breaks android
94 return feof((FILE *)f);
humper@google.com7af56be2013-01-14 18:49:19 +000095}
96
halcanaryd76be9c2015-11-20 13:47:49 -080097size_t sk_fgetsize(FILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 SkASSERT(f);
99
mtklein944c2d92016-02-16 08:10:34 -0800100 long curr = ftell(f); // remember where we are
vandebo@chromium.org6390c722012-03-28 21:03:22 +0000101 if (curr < 0) {
102 return 0;
103 }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000104
mtklein944c2d92016-02-16 08:10:34 -0800105 fseek(f, 0, SEEK_END); // go to the end
106 long size = ftell(f); // record the size
vandebo@chromium.org6390c722012-03-28 21:03:22 +0000107 if (size < 0) {
108 size = 0;
109 }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000110
mtklein944c2d92016-02-16 08:10:34 -0800111 fseek(f, curr, SEEK_SET); // go back to our prev location
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 return size;
113}
114
halcanaryd76be9c2015-11-20 13:47:49 -0800115bool sk_frewind(FILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 SkASSERT(f);
halcanaryd76be9c2015-11-20 13:47:49 -0800117 ::rewind(f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 return true;
119}
120
halcanaryd76be9c2015-11-20 13:47:49 -0800121size_t sk_fread(void* buffer, size_t byteCount, FILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 SkASSERT(f);
halcanary96fcdcc2015-08-27 07:41:13 -0700123 if (buffer == nullptr) {
mtklein944c2d92016-02-16 08:10:34 -0800124 size_t curr = ftell(f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 if ((long)curr == -1) {
halcanaryd76be9c2015-11-20 13:47:49 -0800126 SkDEBUGF(("sk_fread: ftell(%p) returned -1 feof:%d ferror:%d\n", f, feof(f), ferror(f)));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 return 0;
128 }
mtklein944c2d92016-02-16 08:10:34 -0800129 int err = fseek(f, (long)byteCount, SEEK_CUR);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 if (err != 0) {
131 SkDEBUGF(("sk_fread: fseek(%d) tell:%d failed with feof:%d ferror:%d returned:%d\n",
halcanaryd76be9c2015-11-20 13:47:49 -0800132 byteCount, curr, feof(f), ferror(f), err));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 return 0;
134 }
135 return byteCount;
136 }
137 else
mtklein944c2d92016-02-16 08:10:34 -0800138 return fread(buffer, 1, byteCount, f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139}
140
halcanaryd76be9c2015-11-20 13:47:49 -0800141size_t sk_fwrite(const void* buffer, size_t byteCount, FILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 SkASSERT(f);
mtklein944c2d92016-02-16 08:10:34 -0800143 return fwrite(buffer, 1, byteCount, f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144}
145
halcanaryd76be9c2015-11-20 13:47:49 -0800146void sk_fflush(FILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 SkASSERT(f);
mtklein944c2d92016-02-16 08:10:34 -0800148 fflush(f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149}
150
caryclark7471fa42015-12-16 13:41:23 -0800151void sk_fsync(FILE* f) {
152#if !defined(_WIN32) && !defined(SK_BUILD_FOR_ANDROID) && !defined(__UCLIBC__) \
153 && !defined(_NEWLIB_VERSION)
mtklein944c2d92016-02-16 08:10:34 -0800154 int fd = fileno(f);
155 fsync(fd);
caryclark7471fa42015-12-16 13:41:23 -0800156#endif
157}
158
halcanaryd76be9c2015-11-20 13:47:49 -0800159bool sk_fseek(FILE* f, size_t byteCount) {
mtklein944c2d92016-02-16 08:10:34 -0800160 int err = fseek(f, (long)byteCount, SEEK_SET);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000161 return err == 0;
162}
163
halcanaryd76be9c2015-11-20 13:47:49 -0800164bool sk_fmove(FILE* f, long byteCount) {
mtklein944c2d92016-02-16 08:10:34 -0800165 int err = fseek(f, byteCount, SEEK_CUR);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000166 return err == 0;
167}
168
halcanaryd76be9c2015-11-20 13:47:49 -0800169size_t sk_ftell(FILE* f) {
mtklein944c2d92016-02-16 08:10:34 -0800170 long curr = ftell(f);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000171 if (curr < 0) {
172 return 0;
173 }
174 return curr;
175}
176
halcanaryd76be9c2015-11-20 13:47:49 -0800177void sk_fclose(FILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 SkASSERT(f);
mtklein944c2d92016-02-16 08:10:34 -0800179 fclose(f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180}
181
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000182bool sk_isdir(const char *path) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000183 struct stat status;
184 if (0 != stat(path, &status)) {
185 return false;
186 }
scroggo@google.com6e725162012-11-01 16:28:23 +0000187 return SkToBool(status.st_mode & S_IFDIR);
epoger@google.come8ebeb12012-10-29 16:42:11 +0000188}
189
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000190bool sk_mkdir(const char* path) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000191 if (sk_isdir(path)) {
192 return true;
193 }
194 if (sk_exists(path)) {
195 fprintf(stderr,
196 "sk_mkdir: path '%s' already exists but is not a directory\n",
197 path);
198 return false;
199 }
200
201 int retval;
202#ifdef _WIN32
203 retval = _mkdir(path);
204#else
205 retval = mkdir(path, 0777);
206#endif
207 if (0 == retval) {
208 return true;
209 } else {
210 fprintf(stderr, "sk_mkdir: error %d creating dir '%s'\n", errno, path);
211 return false;
212 }
213}