SkOSFile.h revision d686ac77c2c485c4a3302eda9c1de597a6f8c568
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 "SkString.h"
16
17#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS)
18    #include <dirent.h>
19#endif
20
21#include <stddef.h> // ptrdiff_t
22
23struct SkFILE;
24
25enum SkFILE_Flags {
26    kRead_SkFILE_Flag   = 0x01,
27    kWrite_SkFILE_Flag  = 0x02
28};
29
30#ifdef _WIN32
31const static char SkPATH_SEPARATOR = '\\';
32#else
33const static char SkPATH_SEPARATOR = '/';
34#endif
35
36SkFILE* sk_fopen(const char path[], SkFILE_Flags);
37void    sk_fclose(SkFILE*);
38
39size_t  sk_fgetsize(SkFILE*);
40/** Return true if the file could seek back to the beginning
41*/
42bool    sk_frewind(SkFILE*);
43
44size_t  sk_fread(void* buffer, size_t byteCount, SkFILE*);
45size_t  sk_fwrite(const void* buffer, size_t byteCount, SkFILE*);
46
47char*   sk_fgets(char* str, int size, SkFILE* f);
48
49void    sk_fflush(SkFILE*);
50
51int     sk_fseek(SkFILE*, size_t, int);
52size_t  sk_ftell(SkFILE*);
53
54// Returns true if something (file, directory, ???) exists at this path.
55bool    sk_exists(const char *path);
56
57// Returns true if a directory exists at this path.
58bool    sk_isdir(const char *path);
59
60// Have we reached the end of the file?
61int sk_feof(SkFILE *);
62
63
64// Create a new directory at this path; returns true if successful.
65// If the directory already existed, this will return true.
66// Description of the error, if any, will be written to stderr.
67bool    sk_mkdir(const char* path);
68
69class SkOSFile {
70public:
71    class Iter {
72    public:
73        Iter();
74        Iter(const char path[], const char suffix[] = NULL);
75        ~Iter();
76
77        void reset(const char path[], const char suffix[] = NULL);
78        /** If getDir is true, only returns directories.
79            Results are undefined if true and false calls are
80            interleaved on a single iterator.
81        */
82        bool next(SkString* name, bool getDir = false);
83
84    private:
85#ifdef SK_BUILD_FOR_WIN
86        HANDLE      fHandle;
87        uint16_t*   fPath16;
88#elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS)
89        DIR*        fDIR;
90        SkString    fPath, fSuffix;
91#endif
92    };
93};
94
95class SkUTF16_Str {
96public:
97    SkUTF16_Str(const char src[]);
98    ~SkUTF16_Str()
99    {
100        sk_free(fStr);
101    }
102    const uint16_t* get() const { return fStr; }
103
104private:
105    uint16_t*   fStr;
106};
107
108#endif
109