Home | History | Annotate | Download | only in core
      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 // TODO: add unittests for all these operations
     11 
     12 #ifndef SkOSFile_DEFINED
     13 #define SkOSFile_DEFINED
     14 
     15 #include "SkString.h"
     16 
     17 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS)
     18     #include <dirent.h>
     19 #endif
     20 
     21 #include <stddef.h> // ptrdiff_t
     22 
     23 struct SkFILE;
     24 
     25 enum SkFILE_Flags {
     26     kRead_SkFILE_Flag   = 0x01,
     27     kWrite_SkFILE_Flag  = 0x02
     28 };
     29 
     30 #ifdef _WIN32
     31 const static char SkPATH_SEPARATOR = '\\';
     32 #else
     33 const static char SkPATH_SEPARATOR = '/';
     34 #endif
     35 
     36 SkFILE* sk_fopen(const char path[], SkFILE_Flags);
     37 void    sk_fclose(SkFILE*);
     38 
     39 size_t  sk_fgetsize(SkFILE*);
     40 /** Return true if the file could seek back to the beginning
     41 */
     42 bool    sk_frewind(SkFILE*);
     43 
     44 size_t  sk_fread(void* buffer, size_t byteCount, SkFILE*);
     45 size_t  sk_fwrite(const void* buffer, size_t byteCount, SkFILE*);
     46 
     47 char*   sk_fgets(char* str, int size, SkFILE* f);
     48 
     49 void    sk_fflush(SkFILE*);
     50 
     51 bool    sk_fseek(SkFILE*, size_t);
     52 bool    sk_fmove(SkFILE*, long);
     53 size_t  sk_ftell(SkFILE*);
     54 
     55 /** Maps a file into memory. Returns the address and length on success, NULL otherwise.
     56  *  The mapping is read only.
     57  *  When finished with the mapping, free the returned pointer with sk_fmunmap.
     58  */
     59 void*   sk_fmmap(SkFILE* f, size_t* length);
     60 
     61 /** Maps a file descriptor into memory. Returns the address and length on success, NULL otherwise.
     62  *  The mapping is read only.
     63  *  When finished with the mapping, free the returned pointer with sk_fmunmap.
     64  */
     65 void*   sk_fdmmap(int fd, size_t* length);
     66 
     67 /** Unmaps a file previously mapped by sk_fmmap or sk_fdmmap.
     68  *  The length parameter must be the same as returned from sk_fmmap.
     69  */
     70 void    sk_fmunmap(const void* addr, size_t length);
     71 
     72 /** Returns true if the two point at the exact same filesystem object. */
     73 bool    sk_fidentical(SkFILE* a, SkFILE* b);
     74 
     75 /** Returns the underlying file descriptor for the given file.
     76  *  The return value will be < 0 on failure.
     77  */
     78 int     sk_fileno(SkFILE* f);
     79 
     80 /** Returns true if something (file, directory, ???) exists at this path,
     81  *  and has the specified access flags.
     82  */
     83 bool    sk_exists(const char *path, SkFILE_Flags = (SkFILE_Flags)0);
     84 
     85 // Returns true if a directory exists at this path.
     86 bool    sk_isdir(const char *path);
     87 
     88 // Have we reached the end of the file?
     89 int sk_feof(SkFILE *);
     90 
     91 
     92 // Create a new directory at this path; returns true if successful.
     93 // If the directory already existed, this will return true.
     94 // Description of the error, if any, will be written to stderr.
     95 bool    sk_mkdir(const char* path);
     96 
     97 class SkOSFile {
     98 public:
     99     class Iter {
    100     public:
    101         Iter();
    102         Iter(const char path[], const char suffix[] = NULL);
    103         ~Iter();
    104 
    105         void reset(const char path[], const char suffix[] = NULL);
    106         /** If getDir is true, only returns directories.
    107             Results are undefined if true and false calls are
    108             interleaved on a single iterator.
    109         */
    110         bool next(SkString* name, bool getDir = false);
    111 
    112     private:
    113 #ifdef SK_BUILD_FOR_WIN
    114         HANDLE      fHandle;
    115         uint16_t*   fPath16;
    116 #elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS)
    117         DIR*        fDIR;
    118         SkString    fPath, fSuffix;
    119 #endif
    120     };
    121 };
    122 
    123 /**
    124  *  Functions for modifying SkStrings which represent paths on the filesystem.
    125  */
    126 class SkOSPath   {
    127 public:
    128     /**
    129      * Assembles rootPath and relativePath into a single path, like this:
    130      * rootPath/relativePath.
    131      * It is okay to call with a NULL rootPath and/or relativePath. A path
    132      * separator will still be inserted.
    133      *
    134      * Uses SkPATH_SEPARATOR, to work on all platforms.
    135      */
    136     static SkString Join(const char* rootPath, const char* relativePath);
    137 
    138     /**
    139      *  Return the name of the file, ignoring the directory structure.
    140      *  Behaves like python's os.path.basename. If the fullPath is
    141      *  /dir/subdir/, an empty string is returned.
    142      *  @param fullPath Full path to the file.
    143      *  @return SkString The basename of the file - anything beyond the
    144      *      final slash, or the full name if there is no slash.
    145      */
    146     static SkString Basename(const char* fullPath);
    147 
    148     /**
    149      *  Given a qualified file name returns the directory.
    150      *  Behaves like python's os.path.dirname. If the fullPath is
    151      *  /dir/subdir/ the return will be /dir/subdir/
    152      *  @param fullPath Full path to the file.
    153      *  @return SkString The dir containing the file - anything preceding the
    154      *      final slash, or the full name if ending in a slash.
    155      */
    156     static SkString Dirname(const char* fullPath);
    157 
    158 };
    159 
    160 #endif
    161