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