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