1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
10e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com// TODO: add unittests for all these operations
11e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkOSFile_DEFINED
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkOSFile_DEFINED
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkString.h"
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstruct SkFILE;
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comenum SkFILE_Flags {
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    kRead_SkFILE_Flag   = 0x01,
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    kWrite_SkFILE_Flag  = 0x02
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com#ifdef _WIN32
25e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.comconst static char SkPATH_SEPARATOR = '\\';
26e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com#else
27e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.comconst static char SkPATH_SEPARATOR = '/';
28e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com#endif
29e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkFILE* sk_fopen(const char path[], SkFILE_Flags);
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid    sk_fclose(SkFILE*);
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comsize_t  sk_fgetsize(SkFILE*);
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Return true if the file could seek back to the beginning
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool    sk_frewind(SkFILE*);
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comsize_t  sk_fread(void* buffer, size_t byteCount, SkFILE*);
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comsize_t  sk_fwrite(const void* buffer, size_t byteCount, SkFILE*);
4018a48c3c1c1aa183a1b1ab033be34329685199f6humper@google.com
4118a48c3c1c1aa183a1b1ab033be34329685199f6humper@google.comchar*   sk_fgets(char* str, int size, SkFILE* f);
4218a48c3c1c1aa183a1b1ab033be34329685199f6humper@google.com
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid    sk_fflush(SkFILE*);
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
456cab1a4b6a68aa81237731308ff37a646d48f51cbungeman@google.combool    sk_fseek(SkFILE*, size_t);
466cab1a4b6a68aa81237731308ff37a646d48f51cbungeman@google.combool    sk_fmove(SkFILE*, long);
477af56bee17764a0c118c8856a035bb3d27766969humper@google.comsize_t  sk_ftell(SkFILE*);
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
496cab1a4b6a68aa81237731308ff37a646d48f51cbungeman@google.com/** Maps a file into memory. Returns the address and length on success, NULL otherwise.
506cab1a4b6a68aa81237731308ff37a646d48f51cbungeman@google.com *  The mapping is read only.
5111c9a55afd95078d14ab8cd7c1c5c0032af2a498bungeman@google.com *  When finished with the mapping, free the returned pointer with sk_fmunmap.
526cab1a4b6a68aa81237731308ff37a646d48f51cbungeman@google.com */
536cab1a4b6a68aa81237731308ff37a646d48f51cbungeman@google.comvoid*   sk_fmmap(SkFILE* f, size_t* length);
546cab1a4b6a68aa81237731308ff37a646d48f51cbungeman@google.com
5511c9a55afd95078d14ab8cd7c1c5c0032af2a498bungeman@google.com/** Maps a file descriptor into memory. Returns the address and length on success, NULL otherwise.
5611c9a55afd95078d14ab8cd7c1c5c0032af2a498bungeman@google.com *  The mapping is read only.
5711c9a55afd95078d14ab8cd7c1c5c0032af2a498bungeman@google.com *  When finished with the mapping, free the returned pointer with sk_fmunmap.
5811c9a55afd95078d14ab8cd7c1c5c0032af2a498bungeman@google.com */
5911c9a55afd95078d14ab8cd7c1c5c0032af2a498bungeman@google.comvoid*   sk_fdmmap(int fd, size_t* length);
6011c9a55afd95078d14ab8cd7c1c5c0032af2a498bungeman@google.com
6111c9a55afd95078d14ab8cd7c1c5c0032af2a498bungeman@google.com/** Unmaps a file previously mapped by sk_fmmap or sk_fdmmap.
626cab1a4b6a68aa81237731308ff37a646d48f51cbungeman@google.com *  The length parameter must be the same as returned from sk_fmmap.
636cab1a4b6a68aa81237731308ff37a646d48f51cbungeman@google.com */
646cab1a4b6a68aa81237731308ff37a646d48f51cbungeman@google.comvoid    sk_fmunmap(const void* addr, size_t length);
656cab1a4b6a68aa81237731308ff37a646d48f51cbungeman@google.com
666cab1a4b6a68aa81237731308ff37a646d48f51cbungeman@google.com/** Returns true if the two point at the exact same filesystem object. */
676cab1a4b6a68aa81237731308ff37a646d48f51cbungeman@google.combool    sk_fidentical(SkFILE* a, SkFILE* b);
686cab1a4b6a68aa81237731308ff37a646d48f51cbungeman@google.com
6911c9a55afd95078d14ab8cd7c1c5c0032af2a498bungeman@google.com/** Returns the underlying file descriptor for the given file.
7011c9a55afd95078d14ab8cd7c1c5c0032af2a498bungeman@google.com *  The return value will be < 0 on failure.
7111c9a55afd95078d14ab8cd7c1c5c0032af2a498bungeman@google.com */
7211c9a55afd95078d14ab8cd7c1c5c0032af2a498bungeman@google.comint     sk_fileno(SkFILE* f);
7311c9a55afd95078d14ab8cd7c1c5c0032af2a498bungeman@google.com
74bf0b9ced0b93d9684b044e0880691768f9aa4394bungeman/** Returns true if something (file, directory, ???) exists at this path,
75bf0b9ced0b93d9684b044e0880691768f9aa4394bungeman *  and has the specified access flags.
76bf0b9ced0b93d9684b044e0880691768f9aa4394bungeman */
77bf0b9ced0b93d9684b044e0880691768f9aa4394bungemanbool    sk_exists(const char *path, SkFILE_Flags = (SkFILE_Flags)0);
78e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com
79e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com// Returns true if a directory exists at this path.
80e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.combool    sk_isdir(const char *path);
81e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com
827af56bee17764a0c118c8856a035bb3d27766969humper@google.com// Have we reached the end of the file?
837af56bee17764a0c118c8856a035bb3d27766969humper@google.comint sk_feof(SkFILE *);
847af56bee17764a0c118c8856a035bb3d27766969humper@google.com
857af56bee17764a0c118c8856a035bb3d27766969humper@google.com
86e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com// Create a new directory at this path; returns true if successful.
87e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com// If the directory already existed, this will return true.
88e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com// Description of the error, if any, will be written to stderr.
89e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.combool    sk_mkdir(const char* path);
90e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkOSFile {
928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    class Iter {
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    public:
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        Iter();
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        Iter(const char path[], const char suffix[] = NULL);
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        ~Iter();
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        void reset(const char path[], const char suffix[] = NULL);
1007d04280a68430d6b472ee67dc8f6092127625e73tomhudson@google.com        /** If getDir is true, only returns directories.
1017d04280a68430d6b472ee67dc8f6092127625e73tomhudson@google.com            Results are undefined if true and false calls are
1027d04280a68430d6b472ee67dc8f6092127625e73tomhudson@google.com            interleaved on a single iterator.
1037d04280a68430d6b472ee67dc8f6092127625e73tomhudson@google.com        */
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bool next(SkString* name, bool getDir = false);
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
106e998b7ff3a398eaa2f359d4af1a1898ea2824938bungeman        static const size_t kStorageSize = 40;
1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    private:
108e998b7ff3a398eaa2f359d4af1a1898ea2824938bungeman        SkAlignedSStorage<kStorageSize> fSelf;
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
112ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com/**
113ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com *  Functions for modifying SkStrings which represent paths on the filesystem.
114ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com */
1156eb03cc06d0bc60da5277a83aa0251a475794b04bsalomonclass SkOSPath   {
116ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.compublic:
117ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    /**
118ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com     * Assembles rootPath and relativePath into a single path, like this:
119c76218d5edf08f1b73dc06a92e3af52ed268e7bascroggo@google.com     * rootPath/relativePath.
120c76218d5edf08f1b73dc06a92e3af52ed268e7bascroggo@google.com     * It is okay to call with a NULL rootPath and/or relativePath. A path
121c76218d5edf08f1b73dc06a92e3af52ed268e7bascroggo@google.com     * separator will still be inserted.
122ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com     *
123ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com     * Uses SkPATH_SEPARATOR, to work on all platforms.
124ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com     */
125a8e2e1504b9af6ba791637f228debaa23953064atfarina    static SkString Join(const char* rootPath, const char* relativePath);
126ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com
127ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com    /**
128ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com     *  Return the name of the file, ignoring the directory structure.
129ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com     *  Behaves like python's os.path.basename. If the fullPath is
130ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com     *  /dir/subdir/, an empty string is returned.
131ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com     *  @param fullPath Full path to the file.
132ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com     *  @return SkString The basename of the file - anything beyond the
133ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com     *      final slash, or the full name if there is no slash.
134ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com     */
135a8e2e1504b9af6ba791637f228debaa23953064atfarina    static SkString Basename(const char* fullPath);
1366eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon
1376eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon    /**
1386eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon     *  Given a qualified file name returns the directory.
1396eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon     *  Behaves like python's os.path.dirname. If the fullPath is
1406eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon     *  /dir/subdir/ the return will be /dir/subdir/
1416eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon     *  @param fullPath Full path to the file.
1426eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon     *  @return SkString The dir containing the file - anything preceding the
1436eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon     *      final slash, or the full name if ending in a slash.
1446eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon     */
1456eb03cc06d0bc60da5277a83aa0251a475794b04bsalomon    static SkString Dirname(const char* fullPath);
146ccd7afb6fb2df9774e57fb4d7f62f9504cabf03escroggo@google.com};
147a8e2e1504b9af6ba791637f228debaa23953064atfarina
1488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
149