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 | 20 #ifdef SK_BUILD_FOR_IOS |
21 #import <CoreFoundation/CoreFoundation.h> | 21 #import <CoreFoundation/CoreFoundation.h> |
22 | 22 |
23 static FILE* ios_open_from_bundle(const char path[], const char* perm) { | 23 static FILE* ios_open_from_bundle(const char path[], const char* perm) { |
24 // Get a reference to the main bundle | 24 // Get a reference to the main bundle |
25 CFBundleRef mainBundle = CFBundleGetMainBundle(); | 25 CFBundleRef mainBundle = CFBundleGetMainBundle(); |
26 | 26 |
27 // Get a reference to the file's URL | 27 // Get a reference to the file's URL |
28 CFStringRef pathRef = CFStringCreateWithCString(NULL, path, kCFStringEncodin
gUTF8); | 28 CFStringRef pathRef = CFStringCreateWithCString(NULL, path, kCFStringEncodin
gUTF8); |
29 CFURLRef imageURL = CFBundleCopyResourceURL(mainBundle, pathRef, NULL, NULL)
; | 29 CFURLRef imageURL = CFBundleCopyResourceURL(mainBundle, pathRef, NULL, NULL)
; |
30 if (!imageURL) { | 30 if (!imageURL) { |
31 return nullptr; | 31 return nullptr; |
32 } | 32 } |
33 | 33 |
34 // Convert the URL reference into a string reference | 34 // Convert the URL reference into a string reference |
35 CFStringRef imagePath = CFURLCopyFileSystemPath(imageURL, kCFURLPOSIXPathSty
le); | 35 CFStringRef imagePath = CFURLCopyFileSystemPath(imageURL, kCFURLPOSIXPathSty
le); |
36 | 36 |
37 // Get the system encoding method | 37 // Get the system encoding method |
38 CFStringEncoding encodingMethod = CFStringGetSystemEncoding(); | 38 CFStringEncoding encodingMethod = CFStringGetSystemEncoding(); |
39 | 39 |
40 // Convert the string reference into a C string | 40 // Convert the string reference into a C string |
41 const char *finalPath = CFStringGetCStringPtr(imagePath, encodingMethod); | 41 const char *finalPath = CFStringGetCStringPtr(imagePath, encodingMethod); |
42 | 42 |
43 return fopen(finalPath, perm); | 43 return fopen(finalPath, perm); |
44 } | 44 } |
45 #endif | 45 #endif |
46 | 46 |
47 | 47 |
48 FILE* sk_fopen(const char path[], SkFILE_Flags flags) { | 48 FILE* sk_fopen(const char path[], SkFILE_Flags flags) { |
49 char perm[4]; | 49 char perm[4]; |
50 char* p = perm; | 50 char* p = perm; |
51 | 51 |
52 if (flags & kRead_SkFILE_Flag) { | 52 if (flags & kRead_SkFILE_Flag) { |
53 *p++ = 'r'; | 53 *p++ = 'r'; |
54 } | 54 } |
55 if (flags & kWrite_SkFILE_Flag) { | 55 if (flags & kWrite_SkFILE_Flag) { |
56 *p++ = 'w'; | 56 *p++ = 'w'; |
57 } | 57 } |
58 *p++ = 'b'; | 58 *p++ = 'b'; |
59 *p = 0; | 59 *p = 0; |
60 | 60 |
61 //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, |
62 //convert to utf16 and use _wfopen | 62 //convert to utf16 and use _wfopen |
63 FILE* file = nullptr; | 63 FILE* file = nullptr; |
64 #ifdef SK_BUILD_FOR_IOS | 64 #ifdef SK_BUILD_FOR_IOS |
65 // if read-only, try to open from bundle first | 65 // if read-only, try to open from bundle first |
66 if (kRead_SkFILE_Flag == flags) { | 66 if (kRead_SkFILE_Flag == flags) { |
67 file = ios_open_from_bundle(path, perm); | 67 file = ios_open_from_bundle(path, perm); |
68 } | 68 } |
69 // otherwise just read from the Documents directory (default) | 69 // otherwise just read from the Documents directory (default) |
70 if (!file) { | 70 if (!file) { |
71 #endif | 71 #endif |
72 file = ::fopen(path, perm); | 72 file = fopen(path, perm); |
73 #ifdef SK_BUILD_FOR_IOS | 73 #ifdef SK_BUILD_FOR_IOS |
74 } | 74 } |
75 #endif | 75 #endif |
76 if (nullptr == file && (flags & kWrite_SkFILE_Flag)) { | 76 if (nullptr == file && (flags & kWrite_SkFILE_Flag)) { |
77 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", |
78 path, perm, errno, strerror(errno))); | 78 path, perm, errno, strerror(errno))); |
79 } | 79 } |
80 return file; | 80 return file; |
81 } | 81 } |
82 | 82 |
83 char* sk_fgets(char* str, int size, FILE* f) { | 83 char* sk_fgets(char* str, int size, FILE* f) { |
84 return ::fgets(str, size, (FILE *)f); | 84 return fgets(str, size, (FILE *)f); |
85 } | 85 } |
86 | 86 |
87 int sk_feof(FILE *f) { | 87 int sk_feof(FILE *f) { |
88 // no :: namespace qualifier because it breaks android | 88 // no :: namespace qualifier because it breaks android |
89 return feof((FILE *)f); | 89 return feof((FILE *)f); |
90 } | 90 } |
91 | 91 |
92 size_t sk_fgetsize(FILE* f) { | 92 size_t sk_fgetsize(FILE* f) { |
93 SkASSERT(f); | 93 SkASSERT(f); |
94 | 94 |
95 long curr = ::ftell(f); // remember where we are | 95 long curr = ftell(f); // remember where we are |
96 if (curr < 0) { | 96 if (curr < 0) { |
97 return 0; | 97 return 0; |
98 } | 98 } |
99 | 99 |
100 ::fseek(f, 0, SEEK_END); // go to the end | 100 fseek(f, 0, SEEK_END); // go to the end |
101 long size = ::ftell(f); // record the size | 101 long size = ftell(f); // record the size |
102 if (size < 0) { | 102 if (size < 0) { |
103 size = 0; | 103 size = 0; |
104 } | 104 } |
105 | 105 |
106 ::fseek(f, curr, SEEK_SET); // go back to our prev location | 106 fseek(f, curr, SEEK_SET); // go back to our prev location |
107 return size; | 107 return size; |
108 } | 108 } |
109 | 109 |
110 bool sk_frewind(FILE* f) { | 110 bool sk_frewind(FILE* f) { |
111 SkASSERT(f); | 111 SkASSERT(f); |
112 ::rewind(f); | 112 ::rewind(f); |
113 return true; | 113 return true; |
114 } | 114 } |
115 | 115 |
116 size_t sk_fread(void* buffer, size_t byteCount, FILE* f) { | 116 size_t sk_fread(void* buffer, size_t byteCount, FILE* f) { |
117 SkASSERT(f); | 117 SkASSERT(f); |
118 if (buffer == nullptr) { | 118 if (buffer == nullptr) { |
119 size_t curr = ::ftell(f); | 119 size_t curr = ftell(f); |
120 if ((long)curr == -1) { | 120 if ((long)curr == -1) { |
121 SkDEBUGF(("sk_fread: ftell(%p) returned -1 feof:%d ferror:%d\n", f,
feof(f), ferror(f))); | 121 SkDEBUGF(("sk_fread: ftell(%p) returned -1 feof:%d ferror:%d\n", f,
feof(f), ferror(f))); |
122 return 0; | 122 return 0; |
123 } | 123 } |
124 int err = ::fseek(f, (long)byteCount, SEEK_CUR); | 124 int err = fseek(f, (long)byteCount, SEEK_CUR); |
125 if (err != 0) { | 125 if (err != 0) { |
126 SkDEBUGF(("sk_fread: fseek(%d) tell:%d failed with feof:%d ferror:%d
returned:%d\n", | 126 SkDEBUGF(("sk_fread: fseek(%d) tell:%d failed with feof:%d ferror:%d
returned:%d\n", |
127 byteCount, curr, feof(f), ferror(f), err)); | 127 byteCount, curr, feof(f), ferror(f), err)); |
128 return 0; | 128 return 0; |
129 } | 129 } |
130 return byteCount; | 130 return byteCount; |
131 } | 131 } |
132 else | 132 else |
133 return ::fread(buffer, 1, byteCount, f); | 133 return fread(buffer, 1, byteCount, f); |
134 } | 134 } |
135 | 135 |
136 size_t sk_fwrite(const void* buffer, size_t byteCount, FILE* f) { | 136 size_t sk_fwrite(const void* buffer, size_t byteCount, FILE* f) { |
137 SkASSERT(f); | 137 SkASSERT(f); |
138 return ::fwrite(buffer, 1, byteCount, f); | 138 return fwrite(buffer, 1, byteCount, f); |
139 } | 139 } |
140 | 140 |
141 void sk_fflush(FILE* f) { | 141 void sk_fflush(FILE* f) { |
142 SkASSERT(f); | 142 SkASSERT(f); |
143 ::fflush(f); | 143 fflush(f); |
144 } | 144 } |
145 | 145 |
146 void sk_fsync(FILE* f) { | 146 void sk_fsync(FILE* f) { |
147 #if !defined(_WIN32) && !defined(SK_BUILD_FOR_ANDROID) && !defined(__UCLIBC__) \ | 147 #if !defined(_WIN32) && !defined(SK_BUILD_FOR_ANDROID) && !defined(__UCLIBC__) \ |
148 && !defined(_NEWLIB_VERSION) | 148 && !defined(_NEWLIB_VERSION) |
149 int fd = ::fileno(f); | 149 int fd = fileno(f); |
150 ::fsync(fd); | 150 fsync(fd); |
151 #endif | 151 #endif |
152 } | 152 } |
153 | 153 |
154 bool sk_fseek(FILE* f, size_t byteCount) { | 154 bool sk_fseek(FILE* f, size_t byteCount) { |
155 int err = ::fseek(f, (long)byteCount, SEEK_SET); | 155 int err = fseek(f, (long)byteCount, SEEK_SET); |
156 return err == 0; | 156 return err == 0; |
157 } | 157 } |
158 | 158 |
159 bool sk_fmove(FILE* f, long byteCount) { | 159 bool sk_fmove(FILE* f, long byteCount) { |
160 int err = ::fseek(f, byteCount, SEEK_CUR); | 160 int err = fseek(f, byteCount, SEEK_CUR); |
161 return err == 0; | 161 return err == 0; |
162 } | 162 } |
163 | 163 |
164 size_t sk_ftell(FILE* f) { | 164 size_t sk_ftell(FILE* f) { |
165 long curr = ::ftell(f); | 165 long curr = ftell(f); |
166 if (curr < 0) { | 166 if (curr < 0) { |
167 return 0; | 167 return 0; |
168 } | 168 } |
169 return curr; | 169 return curr; |
170 } | 170 } |
171 | 171 |
172 void sk_fclose(FILE* f) { | 172 void sk_fclose(FILE* f) { |
173 SkASSERT(f); | 173 SkASSERT(f); |
174 ::fclose(f); | 174 fclose(f); |
175 } | 175 } |
176 | 176 |
177 bool sk_isdir(const char *path) { | 177 bool sk_isdir(const char *path) { |
178 struct stat status; | 178 struct stat status; |
179 if (0 != stat(path, &status)) { | 179 if (0 != stat(path, &status)) { |
180 return false; | 180 return false; |
181 } | 181 } |
182 return SkToBool(status.st_mode & S_IFDIR); | 182 return SkToBool(status.st_mode & S_IFDIR); |
183 } | 183 } |
184 | 184 |
(...skipping 14 matching lines...) Expand all Loading... |
199 #else | 199 #else |
200 retval = mkdir(path, 0777); | 200 retval = mkdir(path, 0777); |
201 #endif | 201 #endif |
202 if (0 == retval) { | 202 if (0 == retval) { |
203 return true; | 203 return true; |
204 } else { | 204 } else { |
205 fprintf(stderr, "sk_mkdir: error %d creating dir '%s'\n", errno, path); | 205 fprintf(stderr, "sk_mkdir: error %d creating dir '%s'\n", errno, path); |
206 return false; | 206 return false; |
207 } | 207 } |
208 } | 208 } |
OLD | NEW |