1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/include/core/SkOSFile.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,159 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2006 The Android Open Source Project 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 + 1.13 +// TODO: add unittests for all these operations 1.14 + 1.15 +#ifndef SkOSFile_DEFINED 1.16 +#define SkOSFile_DEFINED 1.17 + 1.18 +#include "SkString.h" 1.19 + 1.20 +#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS) 1.21 + #include <dirent.h> 1.22 +#endif 1.23 + 1.24 +#include <stddef.h> // ptrdiff_t 1.25 + 1.26 +struct SkFILE; 1.27 + 1.28 +enum SkFILE_Flags { 1.29 + kRead_SkFILE_Flag = 0x01, 1.30 + kWrite_SkFILE_Flag = 0x02 1.31 +}; 1.32 + 1.33 +#ifdef _WIN32 1.34 +const static char SkPATH_SEPARATOR = '\\'; 1.35 +#else 1.36 +const static char SkPATH_SEPARATOR = '/'; 1.37 +#endif 1.38 + 1.39 +SkFILE* sk_fopen(const char path[], SkFILE_Flags); 1.40 +void sk_fclose(SkFILE*); 1.41 + 1.42 +size_t sk_fgetsize(SkFILE*); 1.43 +/** Return true if the file could seek back to the beginning 1.44 +*/ 1.45 +bool sk_frewind(SkFILE*); 1.46 + 1.47 +size_t sk_fread(void* buffer, size_t byteCount, SkFILE*); 1.48 +size_t sk_fwrite(const void* buffer, size_t byteCount, SkFILE*); 1.49 + 1.50 +char* sk_fgets(char* str, int size, SkFILE* f); 1.51 + 1.52 +void sk_fflush(SkFILE*); 1.53 + 1.54 +bool sk_fseek(SkFILE*, size_t); 1.55 +bool sk_fmove(SkFILE*, long); 1.56 +size_t sk_ftell(SkFILE*); 1.57 + 1.58 +/** Maps a file into memory. Returns the address and length on success, NULL otherwise. 1.59 + * The mapping is read only. 1.60 + * When finished with the mapping, free the returned pointer with sk_fmunmap. 1.61 + */ 1.62 +void* sk_fmmap(SkFILE* f, size_t* length); 1.63 + 1.64 +/** Maps a file descriptor into memory. Returns the address and length on success, NULL otherwise. 1.65 + * The mapping is read only. 1.66 + * When finished with the mapping, free the returned pointer with sk_fmunmap. 1.67 + */ 1.68 +void* sk_fdmmap(int fd, size_t* length); 1.69 + 1.70 +/** Unmaps a file previously mapped by sk_fmmap or sk_fdmmap. 1.71 + * The length parameter must be the same as returned from sk_fmmap. 1.72 + */ 1.73 +void sk_fmunmap(const void* addr, size_t length); 1.74 + 1.75 +/** Returns true if the two point at the exact same filesystem object. */ 1.76 +bool sk_fidentical(SkFILE* a, SkFILE* b); 1.77 + 1.78 +/** Returns the underlying file descriptor for the given file. 1.79 + * The return value will be < 0 on failure. 1.80 + */ 1.81 +int sk_fileno(SkFILE* f); 1.82 + 1.83 +// Returns true if something (file, directory, ???) exists at this path. 1.84 +bool sk_exists(const char *path); 1.85 + 1.86 +// Returns true if a directory exists at this path. 1.87 +bool sk_isdir(const char *path); 1.88 + 1.89 +// Have we reached the end of the file? 1.90 +int sk_feof(SkFILE *); 1.91 + 1.92 + 1.93 +// Create a new directory at this path; returns true if successful. 1.94 +// If the directory already existed, this will return true. 1.95 +// Description of the error, if any, will be written to stderr. 1.96 +bool sk_mkdir(const char* path); 1.97 + 1.98 +class SkOSFile { 1.99 +public: 1.100 + class Iter { 1.101 + public: 1.102 + Iter(); 1.103 + Iter(const char path[], const char suffix[] = NULL); 1.104 + ~Iter(); 1.105 + 1.106 + void reset(const char path[], const char suffix[] = NULL); 1.107 + /** If getDir is true, only returns directories. 1.108 + Results are undefined if true and false calls are 1.109 + interleaved on a single iterator. 1.110 + */ 1.111 + bool next(SkString* name, bool getDir = false); 1.112 + 1.113 + private: 1.114 +#ifdef SK_BUILD_FOR_WIN 1.115 + HANDLE fHandle; 1.116 + uint16_t* fPath16; 1.117 +#elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS) 1.118 + DIR* fDIR; 1.119 + SkString fPath, fSuffix; 1.120 +#endif 1.121 + }; 1.122 +}; 1.123 + 1.124 +class SkUTF16_Str { 1.125 +public: 1.126 + SkUTF16_Str(const char src[]); 1.127 + ~SkUTF16_Str() 1.128 + { 1.129 + sk_free(fStr); 1.130 + } 1.131 + const uint16_t* get() const { return fStr; } 1.132 + 1.133 +private: 1.134 + uint16_t* fStr; 1.135 +}; 1.136 + 1.137 +/** 1.138 + * Functions for modifying SkStrings which represent paths on the filesystem. 1.139 + */ 1.140 +class SkOSPath { 1.141 +public: 1.142 + /** 1.143 + * Assembles rootPath and relativePath into a single path, like this: 1.144 + * rootPath/relativePath. 1.145 + * It is okay to call with a NULL rootPath and/or relativePath. A path 1.146 + * separator will still be inserted. 1.147 + * 1.148 + * Uses SkPATH_SEPARATOR, to work on all platforms. 1.149 + */ 1.150 + static SkString SkPathJoin(const char *rootPath, const char *relativePath); 1.151 + 1.152 + /** 1.153 + * Return the name of the file, ignoring the directory structure. 1.154 + * Behaves like python's os.path.basename. If the fullPath is 1.155 + * /dir/subdir/, an empty string is returned. 1.156 + * @param fullPath Full path to the file. 1.157 + * @return SkString The basename of the file - anything beyond the 1.158 + * final slash, or the full name if there is no slash. 1.159 + */ 1.160 + static SkString SkBasename(const char* fullPath); 1.161 +}; 1.162 +#endif