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 |
(...skipping 27 matching lines...) Expand all Loading... |
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 SkFILE* 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 SkFILE* 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 = (SkFILE*)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 = (SkFILE*)::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, SkFILE* 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(SkFILE *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(SkFILE* f) { | 92 size_t sk_fgetsize(FILE* f) { |
93 SkASSERT(f); | 93 SkASSERT(f); |
94 | 94 |
95 long curr = ::ftell((FILE*)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((FILE*)f, 0, SEEK_END); // go to the end | 100 ::fseek(f, 0, SEEK_END); // go to the end |
101 long size = ::ftell((FILE*)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((FILE*)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(SkFILE* f) { | 110 bool sk_frewind(FILE* f) { |
111 SkASSERT(f); | 111 SkASSERT(f); |
112 ::rewind((FILE*)f); | 112 ::rewind(f); |
113 return true; | 113 return true; |
114 } | 114 } |
115 | 115 |
116 size_t sk_fread(void* buffer, size_t byteCount, SkFILE* 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((FILE*)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((FILE*)f), ferror((FILE*)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((FILE*)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((FILE*)f), ferror((FILE*)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, (FILE*)f); | 133 return ::fread(buffer, 1, byteCount, f); |
134 } | 134 } |
135 | 135 |
136 size_t sk_fwrite(const void* buffer, size_t byteCount, SkFILE* 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, (FILE*)f); | 138 return ::fwrite(buffer, 1, byteCount, f); |
139 } | 139 } |
140 | 140 |
141 void sk_fflush(SkFILE* f) { | 141 void sk_fflush(FILE* f) { |
142 SkASSERT(f); | 142 SkASSERT(f); |
143 ::fflush((FILE*)f); | 143 ::fflush(f); |
144 } | 144 } |
145 | 145 |
146 bool sk_fseek(SkFILE* f, size_t byteCount) { | 146 bool sk_fseek(FILE* f, size_t byteCount) { |
147 int err = ::fseek((FILE*)f, (long)byteCount, SEEK_SET); | 147 int err = ::fseek(f, (long)byteCount, SEEK_SET); |
148 return err == 0; | 148 return err == 0; |
149 } | 149 } |
150 | 150 |
151 bool sk_fmove(SkFILE* f, long byteCount) { | 151 bool sk_fmove(FILE* f, long byteCount) { |
152 int err = ::fseek((FILE*)f, byteCount, SEEK_CUR); | 152 int err = ::fseek(f, byteCount, SEEK_CUR); |
153 return err == 0; | 153 return err == 0; |
154 } | 154 } |
155 | 155 |
156 size_t sk_ftell(SkFILE* f) { | 156 size_t sk_ftell(FILE* f) { |
157 long curr = ::ftell((FILE*)f); | 157 long curr = ::ftell(f); |
158 if (curr < 0) { | 158 if (curr < 0) { |
159 return 0; | 159 return 0; |
160 } | 160 } |
161 return curr; | 161 return curr; |
162 } | 162 } |
163 | 163 |
164 void sk_fclose(SkFILE* f) { | 164 void sk_fclose(FILE* f) { |
165 SkASSERT(f); | 165 SkASSERT(f); |
166 ::fclose((FILE*)f); | 166 ::fclose(f); |
167 } | 167 } |
168 | 168 |
169 bool sk_isdir(const char *path) { | 169 bool sk_isdir(const char *path) { |
170 struct stat status; | 170 struct stat status; |
171 if (0 != stat(path, &status)) { | 171 if (0 != stat(path, &status)) { |
172 return false; | 172 return false; |
173 } | 173 } |
174 return SkToBool(status.st_mode & S_IFDIR); | 174 return SkToBool(status.st_mode & S_IFDIR); |
175 } | 175 } |
176 | 176 |
(...skipping 14 matching lines...) Expand all Loading... |
191 #else | 191 #else |
192 retval = mkdir(path, 0777); | 192 retval = mkdir(path, 0777); |
193 #endif | 193 #endif |
194 if (0 == retval) { | 194 if (0 == retval) { |
195 return true; | 195 return true; |
196 } else { | 196 } else { |
197 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); |
198 return false; | 198 return false; |
199 } | 199 } |
200 } | 200 } |
OLD | NEW |