1 /* libs/graphics/ports/SkOSFile_stdio.cpp 2 ** 3 ** Copyright 2006, The Android Open Source Project 4 ** 5 ** Licensed under the Apache License, Version 2.0 (the "License"); 6 ** you may not use this file except in compliance with the License. 7 ** You may obtain a copy of the License at 8 ** 9 ** http://www.apache.org/licenses/LICENSE-2.0 10 ** 11 ** Unless required by applicable law or agreed to in writing, software 12 ** distributed under the License is distributed on an "AS IS" BASIS, 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 ** See the License for the specific language governing permissions and 15 ** limitations under the License. 16 */ 17 18 #include "SkOSFile.h" 19 20 #include <stdio.h> 21 #include <errno.h> 22 23 SkFILE* sk_fopen(const char path[], SkFILE_Flags flags) 24 { 25 char perm[4]; 26 char* p = perm; 27 28 if (flags & kRead_SkFILE_Flag) 29 *p++ = 'r'; 30 if (flags & kWrite_SkFILE_Flag) 31 *p++ = 'w'; 32 *p++ = 'b'; 33 *p = 0; 34 35 SkFILE* f = (SkFILE*)::fopen(path, perm); 36 #if 0 37 if (NULL == f) 38 SkDebugf("sk_fopen failed for %s (%s), errno=%s\n", path, perm, strerror(errno)); 39 #endif 40 return f; 41 } 42 43 size_t sk_fgetsize(SkFILE* f) 44 { 45 SkASSERT(f); 46 47 size_t curr = ::ftell((FILE*)f); // remember where we are 48 ::fseek((FILE*)f, 0, SEEK_END); // go to the end 49 size_t size = ::ftell((FILE*)f); // record the size 50 ::fseek((FILE*)f, (long)curr, SEEK_SET); // go back to our prev loc 51 return size; 52 } 53 54 bool sk_frewind(SkFILE* f) 55 { 56 SkASSERT(f); 57 ::rewind((FILE*)f); 58 // ::fseek((FILE*)f, 0, SEEK_SET); 59 return true; 60 } 61 62 size_t sk_fread(void* buffer, size_t byteCount, SkFILE* f) 63 { 64 SkASSERT(f); 65 if (buffer == NULL) 66 { 67 size_t curr = ::ftell((FILE*)f); 68 if ((long)curr == -1) { 69 SkDEBUGF(("sk_fread: ftell(%p) returned -1 feof:%d ferror:%d\n", f, feof((FILE*)f), ferror((FILE*)f))); 70 return 0; 71 } 72 // ::fseek((FILE*)f, (long)(curr + byteCount), SEEK_SET); 73 int err = ::fseek((FILE*)f, (long)byteCount, SEEK_CUR); 74 if (err != 0) { 75 SkDEBUGF(("sk_fread: fseek(%d) tell:%d failed with feof:%d ferror:%d returned:%d\n", 76 byteCount, curr, feof((FILE*)f), ferror((FILE*)f), err)); 77 return 0; 78 } 79 return byteCount; 80 } 81 else 82 return ::fread(buffer, 1, byteCount, (FILE*)f); 83 } 84 85 size_t sk_fwrite(const void* buffer, size_t byteCount, SkFILE* f) 86 { 87 SkASSERT(f); 88 return ::fwrite(buffer, 1, byteCount, (FILE*)f); 89 } 90 91 void sk_fflush(SkFILE* f) 92 { 93 SkASSERT(f); 94 ::fflush((FILE*)f); 95 } 96 97 void sk_fclose(SkFILE* f) 98 { 99 SkASSERT(f); 100 ::fclose((FILE*)f); 101 } 102 103