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 <errno.h> 13 #include <stdio.h> 14 #include <sys/stat.h> 15 #include <sys/types.h> 16 17 #ifdef _WIN32 18 #include <direct.h> 19 #include <io.h> 20 #else 21 #include <unistd.h> 22 #endif 23 24 SkFILE* sk_fopen(const char path[], SkFILE_Flags flags) 25 { 26 char perm[4]; 27 char* p = perm; 28 29 if (flags & kRead_SkFILE_Flag) 30 *p++ = 'r'; 31 if (flags & kWrite_SkFILE_Flag) 32 *p++ = 'w'; 33 *p++ = 'b'; 34 *p = 0; 35 36 SkFILE* f = (SkFILE*)::fopen(path, perm); 37 #if 0 38 if (NULL == f) 39 SkDebugf("sk_fopen failed for %s (%s), errno=%s\n", path, perm, strerror(errno)); 40 #endif 41 return f; 42 } 43 44 char* sk_fgets(char* str, int size, SkFILE* f) { 45 return ::fgets(str, size, (FILE *)f); 46 } 47 48 49 int sk_feof(SkFILE *f) { 50 // no :: namespace qualifier because it breaks android 51 return feof((FILE *)f); 52 } 53 54 size_t sk_fgetsize(SkFILE* f) 55 { 56 SkASSERT(f); 57 58 long curr = ::ftell((FILE*)f); // remember where we are 59 if (curr < 0) { 60 return 0; 61 } 62 ::fseek((FILE*)f, 0, SEEK_END); // go to the end 63 long size = ::ftell((FILE*)f); // record the size 64 if (size < 0) { 65 size = 0; 66 } 67 ::fseek((FILE*)f, curr, SEEK_SET); // go back to our prev loc 68 return size; 69 } 70 71 bool sk_frewind(SkFILE* f) 72 { 73 SkASSERT(f); 74 ::rewind((FILE*)f); 75 // ::fseek((FILE*)f, 0, SEEK_SET); 76 return true; 77 } 78 79 size_t sk_fread(void* buffer, size_t byteCount, SkFILE* f) 80 { 81 SkASSERT(f); 82 if (buffer == NULL) 83 { 84 size_t curr = ::ftell((FILE*)f); 85 if ((long)curr == -1) { 86 SkDEBUGF(("sk_fread: ftell(%p) returned -1 feof:%d ferror:%d\n", f, feof((FILE*)f), ferror((FILE*)f))); 87 return 0; 88 } 89 // ::fseek((FILE*)f, (long)(curr + byteCount), SEEK_SET); 90 int err = ::fseek((FILE*)f, (long)byteCount, SEEK_CUR); 91 if (err != 0) { 92 SkDEBUGF(("sk_fread: fseek(%d) tell:%d failed with feof:%d ferror:%d returned:%d\n", 93 byteCount, curr, feof((FILE*)f), ferror((FILE*)f), err)); 94 return 0; 95 } 96 return byteCount; 97 } 98 else 99 return ::fread(buffer, 1, byteCount, (FILE*)f); 100 } 101 102 size_t sk_fwrite(const void* buffer, size_t byteCount, SkFILE* f) 103 { 104 SkASSERT(f); 105 return ::fwrite(buffer, 1, byteCount, (FILE*)f); 106 } 107 108 void sk_fflush(SkFILE* f) 109 { 110 SkASSERT(f); 111 ::fflush((FILE*)f); 112 } 113 114 void sk_fclose(SkFILE* f) 115 { 116 SkASSERT(f); 117 ::fclose((FILE*)f); 118 } 119 120 bool sk_exists(const char *path) 121 { 122 #ifdef _WIN32 123 return (0 == _access(path, 0)); 124 #else 125 return (0 == access(path, 0)); 126 #endif 127 } 128 129 bool sk_isdir(const char *path) 130 { 131 struct stat status; 132 if (0 != stat(path, &status)) { 133 return false; 134 } 135 return SkToBool(status.st_mode & S_IFDIR); 136 } 137 138 bool sk_mkdir(const char* path) 139 { 140 if (sk_isdir(path)) { 141 return true; 142 } 143 if (sk_exists(path)) { 144 fprintf(stderr, 145 "sk_mkdir: path '%s' already exists but is not a directory\n", 146 path); 147 return false; 148 } 149 150 int retval; 151 #ifdef _WIN32 152 retval = _mkdir(path); 153 #else 154 retval = mkdir(path, 0777); 155 #endif 156 if (0 == retval) { 157 return true; 158 } else { 159 fprintf(stderr, "sk_mkdir: error %d creating dir '%s'\n", errno, path); 160 return false; 161 } 162 } 163