epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
| 10 | #include "SkOSFile.h" |
| 11 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include <errno.h> |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 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 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | 9711e44 | 2013-04-24 20:03:00 +0000 | [diff] [blame^] | 36 | //TODO: on Windows fopen is just ASCII or the current code page, |
| 37 | //convert to utf16 and use _wfopen |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | SkFILE* f = (SkFILE*)::fopen(path, perm); |
| 39 | #if 0 |
| 40 | if (NULL == f) |
| 41 | SkDebugf("sk_fopen failed for %s (%s), errno=%s\n", path, perm, strerror(errno)); |
| 42 | #endif |
| 43 | return f; |
| 44 | } |
| 45 | |
humper@google.com | 18a48c3 | 2013-01-14 19:42:08 +0000 | [diff] [blame] | 46 | char* sk_fgets(char* str, int size, SkFILE* f) { |
| 47 | return ::fgets(str, size, (FILE *)f); |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 48 | } |
| 49 | |
humper@google.com | 18a48c3 | 2013-01-14 19:42:08 +0000 | [diff] [blame] | 50 | |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 51 | int sk_feof(SkFILE *f) { |
humper@google.com | d386b0e | 2013-01-14 22:01:14 +0000 | [diff] [blame] | 52 | // no :: namespace qualifier because it breaks android |
| 53 | return feof((FILE *)f); |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 54 | } |
| 55 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | size_t sk_fgetsize(SkFILE* f) |
| 57 | { |
| 58 | SkASSERT(f); |
| 59 | |
vandebo@chromium.org | 6390c72 | 2012-03-28 21:03:22 +0000 | [diff] [blame] | 60 | long curr = ::ftell((FILE*)f); // remember where we are |
| 61 | if (curr < 0) { |
| 62 | return 0; |
| 63 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | ::fseek((FILE*)f, 0, SEEK_END); // go to the end |
vandebo@chromium.org | 6390c72 | 2012-03-28 21:03:22 +0000 | [diff] [blame] | 65 | long size = ::ftell((FILE*)f); // record the size |
| 66 | if (size < 0) { |
| 67 | size = 0; |
| 68 | } |
| 69 | ::fseek((FILE*)f, curr, SEEK_SET); // go back to our prev loc |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | return size; |
| 71 | } |
| 72 | |
| 73 | bool sk_frewind(SkFILE* f) |
| 74 | { |
| 75 | SkASSERT(f); |
| 76 | ::rewind((FILE*)f); |
| 77 | // ::fseek((FILE*)f, 0, SEEK_SET); |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | size_t sk_fread(void* buffer, size_t byteCount, SkFILE* f) |
| 82 | { |
| 83 | SkASSERT(f); |
| 84 | if (buffer == NULL) |
| 85 | { |
| 86 | size_t curr = ::ftell((FILE*)f); |
| 87 | if ((long)curr == -1) { |
| 88 | SkDEBUGF(("sk_fread: ftell(%p) returned -1 feof:%d ferror:%d\n", f, feof((FILE*)f), ferror((FILE*)f))); |
| 89 | return 0; |
| 90 | } |
| 91 | // ::fseek((FILE*)f, (long)(curr + byteCount), SEEK_SET); |
| 92 | int err = ::fseek((FILE*)f, (long)byteCount, SEEK_CUR); |
| 93 | if (err != 0) { |
| 94 | SkDEBUGF(("sk_fread: fseek(%d) tell:%d failed with feof:%d ferror:%d returned:%d\n", |
| 95 | byteCount, curr, feof((FILE*)f), ferror((FILE*)f), err)); |
| 96 | return 0; |
| 97 | } |
| 98 | return byteCount; |
| 99 | } |
| 100 | else |
| 101 | return ::fread(buffer, 1, byteCount, (FILE*)f); |
| 102 | } |
| 103 | |
| 104 | size_t sk_fwrite(const void* buffer, size_t byteCount, SkFILE* f) |
| 105 | { |
| 106 | SkASSERT(f); |
| 107 | return ::fwrite(buffer, 1, byteCount, (FILE*)f); |
| 108 | } |
| 109 | |
| 110 | void sk_fflush(SkFILE* f) |
| 111 | { |
| 112 | SkASSERT(f); |
| 113 | ::fflush((FILE*)f); |
| 114 | } |
| 115 | |
| 116 | void sk_fclose(SkFILE* f) |
| 117 | { |
| 118 | SkASSERT(f); |
| 119 | ::fclose((FILE*)f); |
| 120 | } |
| 121 | |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 122 | bool sk_exists(const char *path) |
| 123 | { |
| 124 | #ifdef _WIN32 |
| 125 | return (0 == _access(path, 0)); |
| 126 | #else |
| 127 | return (0 == access(path, 0)); |
| 128 | #endif |
| 129 | } |
| 130 | |
| 131 | bool sk_isdir(const char *path) |
| 132 | { |
| 133 | struct stat status; |
| 134 | if (0 != stat(path, &status)) { |
| 135 | return false; |
| 136 | } |
scroggo@google.com | 6e72516 | 2012-11-01 16:28:23 +0000 | [diff] [blame] | 137 | return SkToBool(status.st_mode & S_IFDIR); |
epoger@google.com | e8ebeb1 | 2012-10-29 16:42:11 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | bool sk_mkdir(const char* path) |
| 141 | { |
| 142 | if (sk_isdir(path)) { |
| 143 | return true; |
| 144 | } |
| 145 | if (sk_exists(path)) { |
| 146 | fprintf(stderr, |
| 147 | "sk_mkdir: path '%s' already exists but is not a directory\n", |
| 148 | path); |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | int retval; |
| 153 | #ifdef _WIN32 |
| 154 | retval = _mkdir(path); |
| 155 | #else |
| 156 | retval = mkdir(path, 0777); |
| 157 | #endif |
| 158 | if (0 == retval) { |
| 159 | return true; |
| 160 | } else { |
| 161 | fprintf(stderr, "sk_mkdir: error %d creating dir '%s'\n", errno, path); |
| 162 | return false; |
| 163 | } |
| 164 | } |