1 2 /* 3 * Copyright 2006 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 #include "SkOSFile.h" 11 12 #include <stdio.h> 13 #include <errno.h> 14 15 SkFILE* sk_fopen(const char path[], SkFILE_Flags flags) 16 { 17 char perm[4]; 18 char* p = perm; 19 20 if (flags & kRead_SkFILE_Flag) 21 *p++ = 'r'; 22 if (flags & kWrite_SkFILE_Flag) 23 *p++ = 'w'; 24 *p++ = 'b'; 25 *p = 0; 26 27 SkFILE* f = (SkFILE*)::fopen(path, perm); 28 #if 0 29 if (NULL == f) 30 SkDebugf("sk_fopen failed for %s (%s), errno=%s\n", path, perm, strerror(errno)); 31 #endif 32 return f; 33 } 34 35 size_t sk_fgetsize(SkFILE* f) 36 { 37 SkASSERT(f); 38 39 size_t curr = ::ftell((FILE*)f); // remember where we are 40 ::fseek((FILE*)f, 0, SEEK_END); // go to the end 41 size_t size = ::ftell((FILE*)f); // record the size 42 ::fseek((FILE*)f, (long)curr, SEEK_SET); // go back to our prev loc 43 return size; 44 } 45 46 bool sk_frewind(SkFILE* f) 47 { 48 SkASSERT(f); 49 ::rewind((FILE*)f); 50 // ::fseek((FILE*)f, 0, SEEK_SET); 51 return true; 52 } 53 54 size_t sk_fread(void* buffer, size_t byteCount, SkFILE* f) 55 { 56 SkASSERT(f); 57 if (buffer == NULL) 58 { 59 size_t curr = ::ftell((FILE*)f); 60 if ((long)curr == -1) { 61 SkDEBUGF(("sk_fread: ftell(%p) returned -1 feof:%d ferror:%d\n", f, feof((FILE*)f), ferror((FILE*)f))); 62 return 0; 63 } 64 // ::fseek((FILE*)f, (long)(curr + byteCount), SEEK_SET); 65 int err = ::fseek((FILE*)f, (long)byteCount, SEEK_CUR); 66 if (err != 0) { 67 SkDEBUGF(("sk_fread: fseek(%d) tell:%d failed with feof:%d ferror:%d returned:%d\n", 68 byteCount, curr, feof((FILE*)f), ferror((FILE*)f), err)); 69 return 0; 70 } 71 return byteCount; 72 } 73 else 74 return ::fread(buffer, 1, byteCount, (FILE*)f); 75 } 76 77 size_t sk_fwrite(const void* buffer, size_t byteCount, SkFILE* f) 78 { 79 SkASSERT(f); 80 return ::fwrite(buffer, 1, byteCount, (FILE*)f); 81 } 82 83 void sk_fflush(SkFILE* f) 84 { 85 SkASSERT(f); 86 ::fflush((FILE*)f); 87 } 88 89 void sk_fclose(SkFILE* f) 90 { 91 SkASSERT(f); 92 ::fclose((FILE*)f); 93 } 94 95