| 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 | |
| 9 | // TODO: add unittests for all these operations |
| 10 | |
| 11 | #ifndef SkOSFile_DEFINED |
| 12 | #define SkOSFile_DEFINED |
| 13 | |
| 14 | #include <stdio.h> |
| 15 | |
| 16 | #include "include/core/SkString.h" |
| 17 | |
| 18 | enum SkFILE_Flags { |
| 19 | kRead_SkFILE_Flag = 0x01, |
| 20 | kWrite_SkFILE_Flag = 0x02, |
| 21 | kAppend_SkFILE_Flag = 0x04 |
| 22 | }; |
| 23 | |
| 24 | FILE* sk_fopen(const char path[], SkFILE_Flags); |
| 25 | void sk_fclose(FILE*); |
| 26 | |
| 27 | size_t sk_fgetsize(FILE*); |
| 28 | |
| 29 | size_t sk_fwrite(const void* buffer, size_t byteCount, FILE*); |
| 30 | |
| 31 | void sk_fflush(FILE*); |
| 32 | void sk_fsync(FILE*); |
| 33 | |
| 34 | size_t sk_ftell(FILE*); |
| 35 | |
| 36 | /** Maps a file into memory. Returns the address and length on success, NULL otherwise. |
| 37 | * The mapping is read only. |
| 38 | * When finished with the mapping, free the returned pointer with sk_fmunmap. |
| 39 | */ |
| 40 | void* sk_fmmap(FILE* f, size_t* length); |
| 41 | |
| 42 | /** Maps a file descriptor into memory. Returns the address and length on success, NULL otherwise. |
| 43 | * The mapping is read only. |
| 44 | * When finished with the mapping, free the returned pointer with sk_fmunmap. |
| 45 | */ |
| 46 | void* sk_fdmmap(int fd, size_t* length); |
| 47 | |
| 48 | /** Unmaps a file previously mapped by sk_fmmap or sk_fdmmap. |
| 49 | * The length parameter must be the same as returned from sk_fmmap. |
| 50 | */ |
| 51 | void sk_fmunmap(const void* addr, size_t length); |
| 52 | |
| 53 | /** Returns true if the two point at the exact same filesystem object. */ |
| 54 | bool sk_fidentical(FILE* a, FILE* b); |
| 55 | |
| 56 | /** Returns the underlying file descriptor for the given file. |
| 57 | * The return value will be < 0 on failure. |
| 58 | */ |
| 59 | int sk_fileno(FILE* f); |
| 60 | |
| 61 | /** Returns true if something (file, directory, ???) exists at this path, |
| 62 | * and has the specified access flags. |
| 63 | */ |
| 64 | bool sk_exists(const char *path, SkFILE_Flags = (SkFILE_Flags)0); |
| 65 | |
| 66 | // Returns true if a directory exists at this path. |
| 67 | bool sk_isdir(const char *path); |
| 68 | |
| 69 | // Like pread, but may affect the file position marker. |
| 70 | // Returns the number of bytes read or SIZE_MAX if failed. |
| 71 | size_t sk_qread(FILE*, void* buffer, size_t count, size_t offset); |
| 72 | |
| 73 | |
| 74 | // Create a new directory at this path; returns true if successful. |
| 75 | // If the directory already existed, this will return true. |
| 76 | // Description of the error, if any, will be written to stderr. |
| 77 | bool sk_mkdir(const char* path); |
| 78 | |
| 79 | class SkOSFile { |
| 80 | public: |
| 81 | class Iter { |
| 82 | public: |
| 83 | Iter(); |
| 84 | Iter(const char path[], const char suffix[] = nullptr); |
| 85 | ~Iter(); |
| 86 | |
| 87 | void reset(const char path[], const char suffix[] = nullptr); |
| 88 | /** If getDir is true, only returns directories. |
| 89 | Results are undefined if true and false calls are |
| 90 | interleaved on a single iterator. |
| 91 | */ |
| 92 | bool next(SkString* name, bool getDir = false); |
| 93 | |
| 94 | static const size_t kStorageSize = 40; |
| 95 | private: |
| 96 | SkAlignedSStorage<kStorageSize> fSelf; |
| 97 | }; |
| 98 | }; |
| 99 | |
| 100 | #endif |
| 101 | |