BeRTOS
|
00001 00104 #ifndef KERN_KFILE_H 00105 #define KERN_KFILE_H 00106 00107 #include <cfg/compiler.h> 00108 #include <cfg/debug.h> 00109 #include <cfg/macros.h> 00110 00111 /* fwd decl */ 00112 struct KFile; 00113 00114 typedef int32_t kfile_off_t; 00115 00121 typedef enum KSeekMode 00122 { 00123 KSM_SEEK_SET, 00124 KSM_SEEK_CUR, 00125 KSM_SEEK_END, 00126 } KSeekMode; 00127 00128 /* 00129 * Prototypes for KFile access functions. 00130 * I/O file functions must be ANSI compliant. 00131 * \note A KFile user can choose which function subset to implement, 00132 * but has to set to NULL unimplemented features. 00133 */ 00134 00135 /* 00136 * Read from file. 00137 * \return the number of bytes read. 00138 */ 00139 typedef size_t (*ReadFunc_t) (struct KFile *fd, void *buf, size_t size); 00140 00141 /* 00142 * Write to file. 00143 * \return the number of bytes written. 00144 */ 00145 typedef size_t (*WriteFunc_t) (struct KFile *fd, const void *buf, size_t size); 00146 00147 /* 00148 * Seek into file (if seekable). 00149 * \return the new file offset or EOF on errors. 00150 */ 00151 typedef kfile_off_t (*SeekFunc_t) (struct KFile *fd, kfile_off_t offset, KSeekMode whence); 00152 00153 /* 00154 * Close and reopen file \a fd. 00155 * The reopening is done with the former file parameters and access modes. 00156 */ 00157 typedef struct KFile * (*ReOpenFunc_t) (struct KFile *fd); 00158 00159 /* 00160 * Close file. 00161 * \return 0 on success, EOF on errors. 00162 */ 00163 typedef int (*CloseFunc_t) (struct KFile *fd); 00164 00165 /* 00166 * Flush file I/O. 00167 * \return 0 on success, EOF on errors. 00168 */ 00169 typedef int (*FlushFunc_t) (struct KFile *fd); 00170 00171 /* 00172 * Get file error mask. 00173 * \return 0 on success or file error code, device specific. 00174 */ 00175 typedef int (*ErrorFunc_t) (struct KFile *fd); 00176 00177 /* 00178 * Clear errors. 00179 */ 00180 typedef void (*ClearErrFunc_t) (struct KFile *fd); 00181 00189 typedef struct KFile 00190 { 00191 ReadFunc_t read; 00192 WriteFunc_t write; 00193 ReOpenFunc_t reopen; 00194 CloseFunc_t close; 00195 SeekFunc_t seek; 00196 FlushFunc_t flush; 00197 ErrorFunc_t error; 00198 ClearErrFunc_t clearerr; 00199 DB(id_t _type); // Used to keep track, at runtime, of the class type. 00200 00201 /* NOTE: these must _NOT_ be size_t on 16bit CPUs! */ 00202 kfile_off_t seek_pos; 00203 kfile_off_t size; 00204 } KFile; 00205 00206 /* 00207 * Generic implementation of kfile_seek. 00208 */ 00209 kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode whence); 00210 00211 /* 00212 * Generic implementation of kfile_reopen. 00213 */ 00214 struct KFile * kfile_genericReopen(struct KFile *fd); 00215 00216 int kfile_genericClose(struct KFile *fd); 00217 00240 INLINE size_t kfile_read(struct KFile *fd, void *buf, size_t size) 00241 { 00242 ASSERT(fd->read); 00243 return fd->read(fd, buf, size); 00244 } 00245 int kfile_gets(struct KFile *fd, char *buf, int size); 00246 int kfile_gets_echo(struct KFile *fd, char *buf, int size, bool echo); 00247 00256 kfile_off_t kfile_copy(KFile *src, KFile *dst, kfile_off_t size); 00257 00268 INLINE size_t kfile_write(struct KFile *fd, const void *buf, size_t size) 00269 { 00270 ASSERT(fd->write); 00271 return fd->write(fd, buf, size); 00272 } 00273 00274 int kfile_printf(struct KFile *fd, const char *format, ...); 00275 int kfile_print(struct KFile *fd, const char *s); 00276 00287 INLINE kfile_off_t kfile_seek(struct KFile *fd, kfile_off_t offset, KSeekMode whence) 00288 { 00289 ASSERT(fd->seek); 00290 return fd->seek(fd, offset, whence); 00291 } 00292 00297 INLINE KFile * kfile_reopen(struct KFile *fd) 00298 { 00299 ASSERT(fd->reopen); 00300 return fd->reopen(fd); 00301 } 00302 00307 INLINE int kfile_close(struct KFile *fd) 00308 { 00309 ASSERT(fd->close); 00310 return fd->close(fd); 00311 } 00312 00317 INLINE int kfile_flush(struct KFile *fd) 00318 { 00319 ASSERT(fd->flush); 00320 return fd->flush(fd); 00321 } 00322 00327 INLINE int kfile_error(struct KFile *fd) 00328 { 00329 ASSERT(fd->error); 00330 return fd->error(fd); 00331 } 00332 00336 INLINE void kfile_clearerr(struct KFile *fd) 00337 { 00338 ASSERT(fd->clearerr); 00339 fd->clearerr(fd); 00340 } 00341 00342 int kfile_putc(int c, struct KFile *fd); 00343 int kfile_getc(struct KFile *fd); 00344 void kfile_resync(KFile *fd, mtime_t delay); 00345 void kfile_init(struct KFile *fd); 00346 /* @} */ 00347 //Defgroup io_kfile 00349 00350 /* 00351 * Kfile test function. 00352 */ 00353 int kfile_testSetup(void); 00354 int kfile_testRun(void); 00355 int kfile_testRunGeneric(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size); 00356 int kfile_testTearDown(void); 00357 00358 00359 #endif /* KERN_KFILE_H */