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