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