blob: 53a2bb47ecde1a320d48229acb236407b9cb8f2d [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
15#ifdef _WIN32
16#include <direct.h>
17#include <io.h>
epoger@google.come8ebeb12012-10-29 16:42:11 +000018#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000019
jvanverth44dcb8a2015-10-02 09:12:05 -070020#ifdef SK_BUILD_FOR_IOS
21#import <CoreFoundation/CoreFoundation.h>
22
23static 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, kCFStringEncodingUTF8);
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, kCFURLPOSIXPathStyle);
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
bungeman@google.com6cab1a42013-05-29 13:43:31 +000048SkFILE* sk_fopen(const char path[], SkFILE_Flags flags) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 char perm[4];
50 char* p = perm;
51
bungeman@google.com6cab1a42013-05-29 13:43:31 +000052 if (flags & kRead_SkFILE_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 *p++ = 'r';
bungeman@google.com6cab1a42013-05-29 13:43:31 +000054 }
55 if (flags & kWrite_SkFILE_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 *p++ = 'w';
bungeman@google.com6cab1a42013-05-29 13:43:31 +000057 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 *p++ = 'b';
59 *p = 0;
jvanverth44dcb8a2015-10-02 09:12:05 -070060
commit-bot@chromium.org9711e442013-04-24 20:03:00 +000061 //TODO: on Windows fopen is just ASCII or the current code page,
62 //convert to utf16 and use _wfopen
jvanverth44dcb8a2015-10-02 09:12:05 -070063 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
bungeman0881b952015-09-02 12:41:35 -070076 if (nullptr == file && (flags & kWrite_SkFILE_Flag)) {
77 SkDEBUGF(("sk_fopen: fopen(\"%s\", \"%s\") returned NULL (errno:%d): %s\n",
78 path, perm, errno, strerror(errno)));
79 }
80 return file;
reed@android.com8a1c16f2008-12-17 15:59:43 +000081}
82
humper@google.com18a48c32013-01-14 19:42:08 +000083char* sk_fgets(char* str, int size, SkFILE* f) {
84 return ::fgets(str, size, (FILE *)f);
humper@google.com7af56be2013-01-14 18:49:19 +000085}
86
87int sk_feof(SkFILE *f) {
humper@google.comd386b0e2013-01-14 22:01:14 +000088 // no :: namespace qualifier because it breaks android
89 return feof((FILE *)f);
humper@google.com7af56be2013-01-14 18:49:19 +000090}
91
bungeman@google.com6cab1a42013-05-29 13:43:31 +000092size_t sk_fgetsize(SkFILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 SkASSERT(f);
94
bungeman@google.com6cab1a42013-05-29 13:43:31 +000095 long curr = ::ftell((FILE*)f); // remember where we are
vandebo@chromium.org6390c722012-03-28 21:03:22 +000096 if (curr < 0) {
97 return 0;
98 }
bungeman@google.com6cab1a42013-05-29 13:43:31 +000099
100 ::fseek((FILE*)f, 0, SEEK_END); // go to the end
101 long size = ::ftell((FILE*)f); // record the size
vandebo@chromium.org6390c722012-03-28 21:03:22 +0000102 if (size < 0) {
103 size = 0;
104 }
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000105
106 ::fseek((FILE*)f, curr, SEEK_SET); // go back to our prev location
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 return size;
108}
109
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000110bool sk_frewind(SkFILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 SkASSERT(f);
112 ::rewind((FILE*)f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 return true;
114}
115
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000116size_t sk_fread(void* buffer, size_t byteCount, SkFILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 SkASSERT(f);
halcanary96fcdcc2015-08-27 07:41:13 -0700118 if (buffer == nullptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 size_t curr = ::ftell((FILE*)f);
120 if ((long)curr == -1) {
121 SkDEBUGF(("sk_fread: ftell(%p) returned -1 feof:%d ferror:%d\n", f, feof((FILE*)f), ferror((FILE*)f)));
122 return 0;
123 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 int err = ::fseek((FILE*)f, (long)byteCount, SEEK_CUR);
125 if (err != 0) {
126 SkDEBUGF(("sk_fread: fseek(%d) tell:%d failed with feof:%d ferror:%d returned:%d\n",
127 byteCount, curr, feof((FILE*)f), ferror((FILE*)f), err));
128 return 0;
129 }
130 return byteCount;
131 }
132 else
133 return ::fread(buffer, 1, byteCount, (FILE*)f);
134}
135
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000136size_t sk_fwrite(const void* buffer, size_t byteCount, SkFILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 SkASSERT(f);
138 return ::fwrite(buffer, 1, byteCount, (FILE*)f);
139}
140
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000141void sk_fflush(SkFILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 SkASSERT(f);
143 ::fflush((FILE*)f);
144}
145
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000146bool sk_fseek(SkFILE* f, size_t byteCount) {
147 int err = ::fseek((FILE*)f, (long)byteCount, SEEK_SET);
148 return err == 0;
149}
150
151bool sk_fmove(SkFILE* f, long byteCount) {
152 int err = ::fseek((FILE*)f, byteCount, SEEK_CUR);
153 return err == 0;
154}
155
156size_t sk_ftell(SkFILE* f) {
157 long curr = ::ftell((FILE*)f);
158 if (curr < 0) {
159 return 0;
160 }
161 return curr;
162}
163
164void sk_fclose(SkFILE* f) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 SkASSERT(f);
166 ::fclose((FILE*)f);
167}
168
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000169bool sk_isdir(const char *path) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000170 struct stat status;
171 if (0 != stat(path, &status)) {
172 return false;
173 }
scroggo@google.com6e725162012-11-01 16:28:23 +0000174 return SkToBool(status.st_mode & S_IFDIR);
epoger@google.come8ebeb12012-10-29 16:42:11 +0000175}
176
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000177bool sk_mkdir(const char* path) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000178 if (sk_isdir(path)) {
179 return true;
180 }
181 if (sk_exists(path)) {
182 fprintf(stderr,
183 "sk_mkdir: path '%s' already exists but is not a directory\n",
184 path);
185 return false;
186 }
187
188 int retval;
189#ifdef _WIN32
190 retval = _mkdir(path);
191#else
192 retval = mkdir(path, 0777);
193#endif
194 if (0 == retval) {
195 return true;
196 } else {
197 fprintf(stderr, "sk_mkdir: error %d creating dir '%s'\n", errno, path);
198 return false;
199 }
200}