Home | History | Annotate | Download | only in ports
      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 #include "SkTypes.h"
     10 
     11 #include <errno.h>
     12 #include <stdio.h>
     13 #include <sys/stat.h>
     14 
     15 #ifdef _WIN32
     16 #include <direct.h>
     17 #include <io.h>
     18 #endif
     19 
     20 #ifdef SK_BUILD_FOR_IOS
     21 #import <CoreFoundation/CoreFoundation.h>
     22 
     23 static FILE* ios_open_from_bundle(const char path[], const char* perm) {
     24     // Get a reference to the main bundle
     25     CFBundleRef mainBundle = CFBundleGetMainBundle();
     26 
     27     // Get a reference to the file's URL
     28     CFStringRef pathRef = CFStringCreateWithCString(NULL, path, kCFStringEncodingUTF8);
     29     CFURLRef imageURL = CFBundleCopyResourceURL(mainBundle, pathRef, NULL, NULL);
     30     if (!imageURL) {
     31         return nullptr;
     32     }
     33 
     34     // Convert the URL reference into a string reference
     35     CFStringRef imagePath = CFURLCopyFileSystemPath(imageURL, kCFURLPOSIXPathStyle);
     36 
     37     // Get the system encoding method
     38     CFStringEncoding encodingMethod = CFStringGetSystemEncoding();
     39 
     40     // Convert the string reference into a C string
     41     const char *finalPath = CFStringGetCStringPtr(imagePath, encodingMethod);
     42 
     43     return fopen(finalPath, perm);
     44 }
     45 #endif
     46 
     47 
     48 FILE* sk_fopen(const char path[], SkFILE_Flags flags) {
     49     char    perm[4];
     50     char*   p = perm;
     51 
     52     if (flags & kRead_SkFILE_Flag) {
     53         *p++ = 'r';
     54     }
     55     if (flags & kWrite_SkFILE_Flag) {
     56         *p++ = 'w';
     57     }
     58     *p++ = 'b';
     59     *p = 0;
     60 
     61     //TODO: on Windows fopen is just ASCII or the current code page,
     62     //convert to utf16 and use _wfopen
     63     FILE* file = nullptr;
     64 #ifdef SK_BUILD_FOR_IOS
     65     // if read-only, try to open from bundle first
     66     if (kRead_SkFILE_Flag == flags) {
     67         file = ios_open_from_bundle(path, perm);
     68     }
     69     // otherwise just read from the Documents directory (default)
     70     if (!file) {
     71 #endif
     72         file = fopen(path, perm);
     73 #ifdef SK_BUILD_FOR_IOS
     74     }
     75 #endif
     76     if (nullptr == file && (flags & kWrite_SkFILE_Flag)) {
     77         SkDEBUGF(("sk_fopen: fopen(\"%s\", \"%s\") returned NULL (errno:%d): %s\n",
     78                   path, perm, errno, strerror(errno)));
     79     }
     80     return file;
     81 }
     82 
     83 char* sk_fgets(char* str, int size, FILE* f) {
     84     return fgets(str, size, (FILE *)f);
     85 }
     86 
     87 int sk_feof(FILE *f) {
     88     // no :: namespace qualifier because it breaks android
     89     return feof((FILE *)f);
     90 }
     91 
     92 size_t sk_fgetsize(FILE* f) {
     93     SkASSERT(f);
     94 
     95     long curr = ftell(f); // remember where we are
     96     if (curr < 0) {
     97         return 0;
     98     }
     99 
    100     fseek(f, 0, SEEK_END); // go to the end
    101     long size = ftell(f); // record the size
    102     if (size < 0) {
    103         size = 0;
    104     }
    105 
    106     fseek(f, curr, SEEK_SET); // go back to our prev location
    107     return size;
    108 }
    109 
    110 bool sk_frewind(FILE* f) {
    111     SkASSERT(f);
    112     ::rewind(f);
    113     return true;
    114 }
    115 
    116 size_t sk_fread(void* buffer, size_t byteCount, FILE* f) {
    117     SkASSERT(f);
    118     if (buffer == nullptr) {
    119         size_t curr = ftell(f);
    120         if ((long)curr == -1) {
    121             SkDEBUGF(("sk_fread: ftell(%p) returned -1 feof:%d ferror:%d\n", f, feof(f), ferror(f)));
    122             return 0;
    123         }
    124         int err = fseek(f, (long)byteCount, SEEK_CUR);
    125         if (err != 0) {
    126             SkDEBUGF(("sk_fread: fseek(%d) tell:%d failed with feof:%d ferror:%d returned:%d\n",
    127                         byteCount, curr, feof(f), ferror(f), err));
    128             return 0;
    129         }
    130         return byteCount;
    131     }
    132     else
    133         return fread(buffer, 1, byteCount, f);
    134 }
    135 
    136 size_t sk_fwrite(const void* buffer, size_t byteCount, FILE* f) {
    137     SkASSERT(f);
    138     return fwrite(buffer, 1, byteCount, f);
    139 }
    140 
    141 void sk_fflush(FILE* f) {
    142     SkASSERT(f);
    143     fflush(f);
    144 }
    145 
    146 void sk_fsync(FILE* f) {
    147 #if !defined(_WIN32) && !defined(SK_BUILD_FOR_ANDROID) && !defined(__UCLIBC__) \
    148         && !defined(_NEWLIB_VERSION)
    149     int fd = fileno(f);
    150     fsync(fd);
    151 #endif
    152 }
    153 
    154 bool sk_fseek(FILE* f, size_t byteCount) {
    155     int err = fseek(f, (long)byteCount, SEEK_SET);
    156     return err == 0;
    157 }
    158 
    159 bool sk_fmove(FILE* f, long byteCount) {
    160     int err = fseek(f, byteCount, SEEK_CUR);
    161     return err == 0;
    162 }
    163 
    164 size_t sk_ftell(FILE* f) {
    165     long curr = ftell(f);
    166     if (curr < 0) {
    167         return 0;
    168     }
    169     return curr;
    170 }
    171 
    172 void sk_fclose(FILE* f) {
    173     SkASSERT(f);
    174     fclose(f);
    175 }
    176 
    177 bool sk_isdir(const char *path) {
    178     struct stat status;
    179     if (0 != stat(path, &status)) {
    180         return false;
    181     }
    182     return SkToBool(status.st_mode & S_IFDIR);
    183 }
    184 
    185 bool sk_mkdir(const char* path) {
    186     if (sk_isdir(path)) {
    187         return true;
    188     }
    189     if (sk_exists(path)) {
    190         fprintf(stderr,
    191                 "sk_mkdir: path '%s' already exists but is not a directory\n",
    192                 path);
    193         return false;
    194     }
    195 
    196     int retval;
    197 #ifdef _WIN32
    198     retval = _mkdir(path);
    199 #else
    200     retval = mkdir(path, 0777);
    201 #endif
    202     if (0 == retval) {
    203         return true;
    204     } else {
    205         fprintf(stderr, "sk_mkdir: error %d creating dir '%s'\n", errno, path);
    206         return false;
    207     }
    208 }
    209