2014-01-04 14:02:53 +00:00
|
|
|
/*
|
|
|
|
* This file is subject to the terms of the GFX License. If a copy of
|
|
|
|
* the license was not distributed with this file, you can obtain one at:
|
|
|
|
*
|
|
|
|
* http://ugfx.org/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file include/gfile/gfile.h
|
|
|
|
* @brief GFILE - File IO Routines header file.
|
|
|
|
*
|
|
|
|
* @addtogroup GFILE
|
|
|
|
*
|
|
|
|
* @brief Module which contains Operating system independent FILEIO
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _GFILE_H
|
|
|
|
#define _GFILE_H
|
|
|
|
|
|
|
|
#include "gfx.h"
|
|
|
|
|
2014-02-06 15:34:38 +00:00
|
|
|
#if GFX_USE_GFILE || defined(__DOXYGEN__)
|
2014-01-04 14:02:53 +00:00
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Type definitions */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief A file pointer
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct GFILE {
|
|
|
|
const struct GFILEVMT * vmt;
|
|
|
|
uint16_t flags;
|
2014-01-24 09:33:28 +00:00
|
|
|
#define GFILEFLG_OPEN 0x0001 // File is open
|
|
|
|
#define GFILEFLG_READ 0x0002 // Read the file
|
|
|
|
#define GFILEFLG_WRITE 0x0004 // Write the file
|
|
|
|
#define GFILEFLG_APPEND 0x0008 // Append on each write
|
|
|
|
#define GFILEFLG_BINARY 0x0010 // Treat as a binary file
|
|
|
|
#define GFILEFLG_DELONCLOSE 0x0020 // Delete on close
|
|
|
|
#define GFILEFLG_CANSEEK 0x0040 // Seek operations are valid
|
|
|
|
#define GFILEFLG_FAILONBLOCK 0x0080 // Fail on a blocking call
|
|
|
|
#define GFILEFLG_MUSTEXIST 0x0100 // On open file must exist
|
|
|
|
#define GFILEFLG_MUSTNOTEXIST 0x0200 // On open file must not exist
|
|
|
|
#define GFILEFLG_TRUNC 0x0400 // On open truncate the file
|
2014-01-04 14:02:53 +00:00
|
|
|
void * obj;
|
|
|
|
long int pos;
|
|
|
|
} GFILE;
|
|
|
|
|
|
|
|
typedef struct GFILEVMT {
|
|
|
|
const struct GFILEVMT * next;
|
2014-01-24 09:33:28 +00:00
|
|
|
uint8_t flags;
|
2014-01-04 14:02:53 +00:00
|
|
|
#define GFSFLG_WRITEABLE 0x0001
|
|
|
|
#define GFSFLG_CASESENSITIVE 0x0002
|
|
|
|
#define GFSFLG_SEEKABLE 0x0004
|
|
|
|
#define GFSFLG_FAST 0x0010
|
|
|
|
#define GFSFLG_SMALL 0x0020
|
2014-01-28 14:37:16 +00:00
|
|
|
#define GFSFLG_TEXTMODES 0x0040
|
2014-01-24 09:33:28 +00:00
|
|
|
char prefix;
|
2014-01-04 14:02:53 +00:00
|
|
|
bool_t del(const char *fname);
|
|
|
|
bool_t exists(const char *fname);
|
|
|
|
long int filesize(const char *fname);
|
|
|
|
bool_t ren(const char *oldname, const char *newname);
|
2014-01-24 09:33:28 +00:00
|
|
|
bool_t open(GFILE *f, const char *fname);
|
2014-01-04 14:02:53 +00:00
|
|
|
void close(GFILE *f);
|
|
|
|
int read(GFILE *f, char *buf, int size);
|
2014-01-28 14:37:16 +00:00
|
|
|
int write(GFILE *f, const char *buf, int size);
|
2014-01-04 14:02:53 +00:00
|
|
|
bool_t setpos(GFILE *f, long int pos);
|
|
|
|
long int getsize(GFILE *f);
|
|
|
|
bool_t eof(GFILE *f);
|
|
|
|
} GFILEVMT;
|
|
|
|
|
|
|
|
typedef void GFILE;
|
|
|
|
|
|
|
|
extern GFILE *gfileStdIn;
|
|
|
|
extern GFILE *gfileStdErr;
|
|
|
|
extern GFILE *gfileStdOut;
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* External declarations. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2014-01-28 14:37:16 +00:00
|
|
|
bool_t gfileExists(const char *fname);
|
|
|
|
bool_t gfileDelete(const char *fname);
|
|
|
|
long int gfileGetFilesize(const char *fname);
|
|
|
|
bool_t gfileRename(const char *oldname, const char *newname);
|
|
|
|
GFILE * gfileOpen(const char *fname, const char *mode);
|
|
|
|
void gfileClose(GFILE *f);
|
|
|
|
size_t gfileRead(GFILE *f, char *buf, size_t len);
|
|
|
|
size_t gfileWrite(GFILE *f, const char *buf, size_t len);
|
2014-01-04 14:02:53 +00:00
|
|
|
long int gfileGetPos(GFILE *f);
|
2014-01-28 14:37:16 +00:00
|
|
|
bool_t gfileSetPos(GFILE *f, long int pos);
|
2014-01-04 14:02:53 +00:00
|
|
|
long int gfileGetSize(GFILE *f);
|
2014-01-28 14:37:16 +00:00
|
|
|
bool_t gfileEOF(GFILE *f);
|
|
|
|
|
2014-02-06 15:34:38 +00:00
|
|
|
#if GFILE_NEED_CHIBIOSFS && GFX_USE_OS_CHIBIOS
|
|
|
|
GFILE * gfileOpenBaseFileStream(void *BaseFileStreamPtr, const char *mode);
|
|
|
|
#endif
|
|
|
|
#if GFILE_NEED_MEMFS
|
|
|
|
GFILE * gfileOpenMemory(void *memptr, const char *mode);
|
|
|
|
#endif
|
|
|
|
|
2014-01-28 14:37:16 +00:00
|
|
|
#if GFILE_NEED_PRINTG
|
|
|
|
int vfnprintg(GFILE *f, int maxlen, const char *fmt, va_list arg);
|
|
|
|
int fnprintg(GFILE *f, int maxlen, const char *fmt, ...);
|
|
|
|
#define vfprintg(f,m,a) vfnprintg(f,0,m,a)
|
|
|
|
#define fprintg(f,m,...) fnprintg(f,0,m,...)
|
|
|
|
#define vprintg(m,a) vfnprintg(gfileStdOut,0,m,a)
|
|
|
|
#define printg(m,...) fnprintg(gfileStdOut,0,m,...)
|
|
|
|
|
|
|
|
#if GFILE_NEED_STRINGS
|
|
|
|
int vsnprintg(char *buf, int maxlen, const char *fmt, va_list arg);
|
|
|
|
int snprintg(char *buf, int maxlen, const char *fmt, ...);
|
2014-02-03 08:16:22 +00:00
|
|
|
#define vsprintg(s,m,a) vsnprintg(s,0,m,a)
|
|
|
|
#define sprintg(s,m,...) snprintg(s,0,m,...)
|
2014-01-28 14:37:16 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
2014-01-04 14:02:53 +00:00
|
|
|
|
2014-02-03 08:16:22 +00:00
|
|
|
#if GFILE_NEED_SCANG
|
|
|
|
int vfscang(GFILE *f, const char *fmt, va_list arg);
|
|
|
|
int fscang(GFILE *f, const char *fmt, ...);
|
|
|
|
#define vscang(f,a) vfscang(gfileStdIn,f,a)
|
|
|
|
#define scang(f,...) fscang(gfileStdIn,f,...)
|
|
|
|
|
|
|
|
#if GFILE_NEED_STRINGS
|
|
|
|
int vsscang(const char *buf, const char *fmt, va_list arg);
|
|
|
|
int sscang(const char *buf, const char *fmt, ...);
|
|
|
|
#endif
|
|
|
|
#endif
|
2014-01-04 14:02:53 +00:00
|
|
|
|
|
|
|
#if GFILE_NEED_STDIO && !defined(GFILE_IMPLEMENTATION)
|
2014-02-03 08:16:22 +00:00
|
|
|
#define stdin gfileStdIn
|
|
|
|
#define stdout gfileStdOut
|
|
|
|
#define stderr gfileStdErr
|
|
|
|
#define FILENAME_MAX 256 // Use a relatively small number for an embedded platform
|
|
|
|
#define L_tmpnam FILENAME_MAX
|
|
|
|
#define FOPEN_MAX GFILE_MAX_GFILES
|
|
|
|
#define TMP_MAX GFILE_MAX_GFILES
|
|
|
|
#define P_tmpdir "/tmp/"
|
2014-01-04 14:02:53 +00:00
|
|
|
#define FILE GFILE
|
|
|
|
#define fopen(n,m) gfileOpen(n,m)
|
|
|
|
#define fclose(f) gfileClose(f)
|
2014-01-28 14:37:16 +00:00
|
|
|
size_t gstdioRead(void * ptr, size_t size, size_t count, FILE *f);
|
|
|
|
size_t gstdioWrite(const void * ptr, size_t size, size_t count, FILE *f);
|
|
|
|
#define fread(p,sz,cnt,f) gstdioRead(p,sz,cnt,f)
|
|
|
|
#define fwrite(p,sz,cnt,f) gstdioWrite(p,sz,cnt,f)
|
|
|
|
int gstdioSeek(FILE *f, size_t offset, int origin);
|
|
|
|
#define fseek(f,ofs,org) gstdioSeek(f,ofs,org)
|
2014-01-04 14:02:53 +00:00
|
|
|
#define SEEK_SET 0
|
|
|
|
#define SEEK_CUR 1
|
|
|
|
#define SEEK_END 2
|
|
|
|
#define remove(n) (!gfileDelete(n))
|
|
|
|
#define rename(o,n) (!gfileRename(o,n))
|
|
|
|
#define fflush(f) (0)
|
|
|
|
#define ftell(f) gfileGetPos(f)
|
2014-01-28 14:37:16 +00:00
|
|
|
#define fpos_t long int
|
|
|
|
int gstdioGetpos(FILE *f, long int *pos);
|
|
|
|
#define fgetpos(f,pos) gstdioGetpos(f,pos)
|
2014-01-04 14:02:53 +00:00
|
|
|
#define fsetpos(f, pos) (!gfileSetPos(f, *pos))
|
|
|
|
#define rewind(f) gfileSetPos(f, 0);
|
2014-01-28 14:37:16 +00:00
|
|
|
#define feof(f) gfileEOF(f)
|
2014-01-04 14:02:53 +00:00
|
|
|
|
|
|
|
#define vfprintf(f,m,a) vfnprintg(f,0,m,a)
|
|
|
|
#define fprintf(f,m,...) fnprintg(f,0,m,...)
|
|
|
|
#define vprintf(m,a) vfnprintg(gfileStdOut,0,m,a)
|
|
|
|
#define printf(m,...) fnprintg(gfileStdOut,0,m,...)
|
|
|
|
#define vsnprintf(s,n,m,a) vsnprintg(s,n,m,a)
|
|
|
|
#define snprintf(s,n,m,...) snprintg(s,n,m,...)
|
|
|
|
#define vsprintf(s,m,a) vsnprintg(s,0,m,a)
|
|
|
|
#define sprintf(s,m,...) snprintg(s,0,m,...)
|
2014-02-03 08:16:22 +00:00
|
|
|
//TODO
|
|
|
|
//void clearerr ( FILE * stream );
|
|
|
|
//int ferror ( FILE * stream );
|
|
|
|
//FILE * tmpfile ( void ); // Auto-deleting
|
|
|
|
//char * tmpnam ( char * str );
|
|
|
|
//char * mktemp (char *template);
|
|
|
|
//FILE * freopen ( const char * filename, const char * mode, FILE * stream );
|
|
|
|
//setbuf
|
|
|
|
//setvbuf
|
|
|
|
//fflush
|
|
|
|
//fgetc
|
|
|
|
//fgets
|
|
|
|
//fputc
|
|
|
|
//fputs
|
|
|
|
//getc
|
|
|
|
//getchar
|
|
|
|
//puts
|
|
|
|
//ungetc
|
|
|
|
//void perror (const char * str);
|
2014-01-04 14:02:53 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-06 15:34:38 +00:00
|
|
|
#endif /* GFX_USE_GFILE */
|
2014-01-04 14:02:53 +00:00
|
|
|
|
2014-02-06 15:34:38 +00:00
|
|
|
#endif /* _GFILE_H */
|
2014-01-04 14:02:53 +00:00
|
|
|
/** @} */
|
|
|
|
|