2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2012-10-29 16:42:11 +00:00
|
|
|
// TODO: add unittests for all these operations
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifndef SkOSFile_DEFINED
|
|
|
|
#define SkOSFile_DEFINED
|
|
|
|
|
2015-11-20 21:47:49 +00:00
|
|
|
#include <stdio.h>
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2015-11-20 21:47:49 +00:00
|
|
|
#include "SkString.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
enum SkFILE_Flags {
|
|
|
|
kRead_SkFILE_Flag = 0x01,
|
|
|
|
kWrite_SkFILE_Flag = 0x02
|
|
|
|
};
|
|
|
|
|
2015-11-20 21:47:49 +00:00
|
|
|
FILE* sk_fopen(const char path[], SkFILE_Flags);
|
|
|
|
void sk_fclose(FILE*);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2015-11-20 21:47:49 +00:00
|
|
|
size_t sk_fgetsize(FILE*);
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return true if the file could seek back to the beginning
|
|
|
|
*/
|
2015-11-20 21:47:49 +00:00
|
|
|
bool sk_frewind(FILE*);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2015-11-20 21:47:49 +00:00
|
|
|
size_t sk_fread(void* buffer, size_t byteCount, FILE*);
|
|
|
|
size_t sk_fwrite(const void* buffer, size_t byteCount, FILE*);
|
2013-01-14 19:42:08 +00:00
|
|
|
|
2015-11-20 21:47:49 +00:00
|
|
|
char* sk_fgets(char* str, int size, FILE* f);
|
2013-01-14 19:42:08 +00:00
|
|
|
|
2015-11-20 21:47:49 +00:00
|
|
|
void sk_fflush(FILE*);
|
2015-12-16 21:41:23 +00:00
|
|
|
void sk_fsync(FILE*);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2015-11-20 21:47:49 +00:00
|
|
|
bool sk_fseek(FILE*, size_t);
|
|
|
|
bool sk_fmove(FILE*, long);
|
|
|
|
size_t sk_ftell(FILE*);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2013-05-29 13:43:31 +00:00
|
|
|
/** Maps a file into memory. Returns the address and length on success, NULL otherwise.
|
|
|
|
* The mapping is read only.
|
2013-06-03 17:10:35 +00:00
|
|
|
* When finished with the mapping, free the returned pointer with sk_fmunmap.
|
2013-05-29 13:43:31 +00:00
|
|
|
*/
|
2015-11-20 21:47:49 +00:00
|
|
|
void* sk_fmmap(FILE* f, size_t* length);
|
2013-05-29 13:43:31 +00:00
|
|
|
|
2013-06-03 17:10:35 +00:00
|
|
|
/** Maps a file descriptor into memory. Returns the address and length on success, NULL otherwise.
|
|
|
|
* The mapping is read only.
|
|
|
|
* When finished with the mapping, free the returned pointer with sk_fmunmap.
|
|
|
|
*/
|
|
|
|
void* sk_fdmmap(int fd, size_t* length);
|
|
|
|
|
|
|
|
/** Unmaps a file previously mapped by sk_fmmap or sk_fdmmap.
|
2013-05-29 13:43:31 +00:00
|
|
|
* The length parameter must be the same as returned from sk_fmmap.
|
|
|
|
*/
|
|
|
|
void sk_fmunmap(const void* addr, size_t length);
|
|
|
|
|
|
|
|
/** Returns true if the two point at the exact same filesystem object. */
|
2015-11-20 21:47:49 +00:00
|
|
|
bool sk_fidentical(FILE* a, FILE* b);
|
2013-05-29 13:43:31 +00:00
|
|
|
|
2013-06-03 17:10:35 +00:00
|
|
|
/** Returns the underlying file descriptor for the given file.
|
|
|
|
* The return value will be < 0 on failure.
|
|
|
|
*/
|
2015-11-20 21:47:49 +00:00
|
|
|
int sk_fileno(FILE* f);
|
2013-06-03 17:10:35 +00:00
|
|
|
|
2014-07-10 22:18:23 +00:00
|
|
|
/** Returns true if something (file, directory, ???) exists at this path,
|
|
|
|
* and has the specified access flags.
|
|
|
|
*/
|
|
|
|
bool sk_exists(const char *path, SkFILE_Flags = (SkFILE_Flags)0);
|
2012-10-29 16:42:11 +00:00
|
|
|
|
|
|
|
// Returns true if a directory exists at this path.
|
|
|
|
bool sk_isdir(const char *path);
|
|
|
|
|
2013-01-14 18:49:19 +00:00
|
|
|
// Have we reached the end of the file?
|
2015-11-20 21:47:49 +00:00
|
|
|
int sk_feof(FILE *);
|
2013-01-14 18:49:19 +00:00
|
|
|
|
|
|
|
|
2012-10-29 16:42:11 +00:00
|
|
|
// Create a new directory at this path; returns true if successful.
|
|
|
|
// If the directory already existed, this will return true.
|
|
|
|
// Description of the error, if any, will be written to stderr.
|
|
|
|
bool sk_mkdir(const char* path);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
class SkOSFile {
|
|
|
|
public:
|
|
|
|
class Iter {
|
|
|
|
public:
|
|
|
|
Iter();
|
|
|
|
Iter(const char path[], const char suffix[] = NULL);
|
|
|
|
~Iter();
|
|
|
|
|
|
|
|
void reset(const char path[], const char suffix[] = NULL);
|
2011-07-14 13:15:55 +00:00
|
|
|
/** If getDir is true, only returns directories.
|
|
|
|
Results are undefined if true and false calls are
|
|
|
|
interleaved on a single iterator.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
bool next(SkString* name, bool getDir = false);
|
|
|
|
|
2015-02-12 15:18:27 +00:00
|
|
|
static const size_t kStorageSize = 40;
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2015-02-12 15:18:27 +00:00
|
|
|
SkAlignedSStorage<kStorageSize> fSelf;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|