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