aboutsummaryrefslogtreecommitdiffstats
path: root/src/gfile/sys_defs.h
blob: 0c5bac0c55e3355b096e217e56a2aedcb9dc7c50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * 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    src/gfile/sys_defs.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"

#if GFX_USE_GFILE || defined(__DOXYGEN__)

/*===========================================================================*/
/* Type definitions                                                          */
/*===========================================================================*/

/**
 * @brief	A file pointer
 */

#ifndef GFILE_IMPLEMENTATION
	typedef void GFILE;
#else
	typedef struct GFILE GFILE;
#endif

extern GFILE *gfileStdIn;
extern GFILE *gfileStdErr;
extern GFILE *gfileStdOut;

/*===========================================================================*/
/* External declarations.                                                    */
/*===========================================================================*/

#ifdef __cplusplus
extern "C" {
#endif

	/**
	 * @brief					Check if file exists
	 * 
	 * @param[in] fname			The file name
	 * 
	 * @return					TRUE if file exists, FALSE otherwise
	 * 
	 * @api
	 */
	bool_t		gfileExists(const char *fname);

	/**
	 * @brief					Delete file
	 * 
	 * @param[in] fname			The file name
	 * 
	 * @return					TRUE on success, FALSE otherwise
	 * 
	 * @api
	 */
	bool_t		gfileDelete(const char *fname);

	/**
	 * @brief					Get the size of a file
	 * @note					Please use @p gfileGetSize() if the file is opened
	 * 
	 * @param[in] fname			The file name
	 * 
	 * @return					File size on success, -1 on error
	 * 
	 * @api
	 */
	long int	gfileGetFilesize(const char *fname);

	/**
	 * @brief					Rename file
	 *
	 * @param[in] oldname		The current file name
	 * @param[in] newname		The new name of the file
	 *
	 * @return					TRUE on success, FALSE otherwise
	 *
	 * @api
	 */
	bool_t		gfileRename(const char *oldname, const char *newname);

	/**
	 * @brief					Open file
	 * @details					A file must be opened before it can be accessed
	 * @details					ToDo (document possible modes)
	 * @details					The resulting GFILE will be used for all functions that access the file.
	 *
	 * @param[in] fname			The file name
	 * @param[in] mode			The mode
	 *
	 * @return					Valid GFILE on success, 0 otherwise
	 *
	 * @api
	 */	
	GFILE *		gfileOpen(const char *fname, const char *mode);

	/**
	 * @brief					Close file
	 * @details					Closes a file after is has been opened using @p gfileOpen()
	 *
	 * @param[in] f				The file
	 *
	 * @api
	 */
	void		gfileClose(GFILE *f);

	/**
	 * @brief					Read from file
	 * @details					Reads a given amount of bytes from the file
	 * @details					The read/write cursor will not be reset when calling this function
	 *
	 * @param[in] f				The file
	 * @param[out] buf			The buffer in which to save the content that has been read from the file
	 * @param[in] len			Amount of bytes to read
	 *
	 * @return					Amount of bytes read
	 *
	 * @api
	 */
	size_t		gfileRead(GFILE *f, void *buf, size_t len);

	/**
	 * @brief					Write to file
	 * @details					Write a given amount of bytes to the file
	 * @details					The read/write cursor will not be reset when calling this function
	 *
	 * @param[in] f				The file
	 * @param[in] buf			The buffer which contains the content that will be written to the file
	 * @param[in] len			Amount of bytes to write
	 *
	 * @return					Amount of bytes written
	 *
	 * @api
	 */
	size_t		gfileWrite(GFILE *f, const void *buf, size_t len);

	/**
	 * @brief					Get the current position of the read/write cursor
	 *
	 * @param[in] f				The file
	 *
	 * @return					The current position in the file
	 *
	 * @api
	 */
	long int	gfileGetPos(GFILE *f);

	/**
	 * @brief					Set the position of the read/write cursor
	 *
	 * @param[in] f				The file
	 * @param[in] pos			The position to which the cursor will be set
	 *
	 * @return					TRUE on success, FALSE otherwise
	 *
	 * @api
	 */
	bool_t		gfileSetPos(GFILE *f, long int pos);

	/**
	 * @brief					Get the size of file
	 * @note					Please use @p gfileGetFilesize() if the file is not opened
	 *
	 * @param[in] f				The file
	 *
	 * @return					The size of the file
	 *
	 * @api
	 */
	long int	gfileGetSize(GFILE *f);

	/**
	 * @brief					Check for EOF
	 * @details					Checks if the cursor is at the end of the file
	 *
	 * @param[in] f				The file
	 *
	 * @return					TRUE if EOF, FALSE otherwise
	 *
	 * @api
	 */
	bool_t		gfileEOF(GFILE *f);

	/**
	 * @brief					Mount a logical drive (aka partition)
	 *
	 * @details					Not supported by every file system
	 * @details					Currently just one drive at one is supported.
	 *
	 * @param[in] fs			The file system (F for FatFS)
	 * @param[in] drive			The logical drive prefix
	 *
	 * @return					TRUE on success, FALSE otherwise
	 *
	 * @api
	 */
	bool_t gfileMount(char fs, const char *drive);

	/**
	 * @brief					Unmount a logical drive (aka partition)
	 *
	 * @details					Does have no effect if @p gfileMount() as been called before hand
	 *
	 * @param[in] fs			The file system (F for FatFS)
	 * @param[in] drive			The logical drive prefix
	 *
	 * @return					TRUE on success, FALSE otherwise
	 *
	 * @api
	 */
	bool_t gfileUnmount(char fs, const char *drive);

	/**
	 * @brief					Syncs the file object (flushes the buffer)
	 *
	 * @details					Not supported by every file system
	 *
	 * @param[in] f				The file
	 *
	 * @return					TRUE on success, FALSE otherwise
	 *
	 * @api
	 */
	bool_t gfileSync(GFILE *f);

	#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

	#if GFILE_NEED_PRINTG
		#include <stdarg.h>

		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, ...);
			#define vsprintg(s,m,a)		vsnprintg(s,0,m,a)
			#define sprintg(s,m,...)	snprintg(s,0,m,...)
		#endif
	#endif

	#if GFILE_NEED_SCANG
		#include <stdarg.h>

		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

	#if GFILE_NEED_STDIO && !defined(GFILE_IMPLEMENTATION)
		#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/"
		#define FILE					GFILE
		#define fopen(n,m)				gfileOpen(n,m)
		#define fclose(f)				gfileClose(f)
		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)
			#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)
		#define fpos_t					long int
		int gstdioGetpos(FILE *f, long int *pos);
		#define fgetpos(f,pos)			gstdioGetpos(f,pos)
		#define fsetpos(f, pos)			(!gfileSetPos(f, *pos))
		#define rewind(f)				gfileSetPos(f, 0);
		#define feof(f)					gfileEOF(f)

		#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,...)
		//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);
	#endif

#ifdef __cplusplus
}
#endif

#endif /* GFX_USE_GFILE */

#endif /* _GFILE_H */
/** @} */