kfile.h

Go to the documentation of this file.
00001 
00100 #ifndef KERN_KFILE_H
00101 #define KERN_KFILE_H
00102 
00103 #include <cfg/compiler.h>
00104 #include <cfg/debug.h>
00105 #include <cfg/macros.h>
00106 
00107 /* fwd decl */
00108 struct KFile;
00109 
00110 typedef int32_t kfile_off_t;     
00111 
00117 typedef enum KSeekMode
00118 {
00119     KSM_SEEK_SET, 
00120     KSM_SEEK_CUR, 
00121     KSM_SEEK_END, 
00122 } KSeekMode;
00123 
00136 typedef size_t (*ReadFunc_t) (struct KFile *fd, void *buf, size_t size);
00137 
00142 typedef size_t (*WriteFunc_t) (struct KFile *fd, const void *buf, size_t size);
00143 
00148 typedef kfile_off_t (*SeekFunc_t) (struct KFile *fd, kfile_off_t offset, KSeekMode whence);
00149 
00154 typedef struct KFile * (*ReOpenFunc_t) (struct KFile *fd);
00155 
00160 typedef int (*CloseFunc_t) (struct KFile *fd);
00161 
00166 typedef int (*FlushFunc_t) (struct KFile *fd);
00167 
00172 typedef int (*ErrorFunc_t) (struct KFile *fd);
00173 
00177 typedef void (*ClearErrFunc_t) (struct KFile *fd);
00178 /* \} */
00179 
00187 typedef struct KFile
00188 {
00189     ReadFunc_t     read;
00190     WriteFunc_t    write;
00191     ReOpenFunc_t   reopen;
00192     CloseFunc_t    close;
00193     SeekFunc_t     seek;
00194     FlushFunc_t    flush;
00195     ErrorFunc_t    error;
00196     ClearErrFunc_t clearerr;
00197     DB(id_t _type); 
00198 
00199     /* NOTE: these must _NOT_ be size_t on 16bit CPUs! */
00200     kfile_off_t    seek_pos;
00201     kfile_off_t    size;
00202 } KFile;
00203 
00207 kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode whence);
00208 
00212 struct KFile * kfile_genericReopen(struct KFile *fd);
00213 
00214 int kfile_genericClose(struct KFile *fd);
00215 
00216 int kfile_putc(int c, struct KFile *fd); 
00217 int kfile_getc(struct KFile *fd);  
00218 int kfile_printf(struct KFile *fd, const char *format, ...);
00219 int kfile_print(struct KFile *fd, const char *s);
00220 int kfile_gets(struct KFile *fd, char *buf, int size);
00221 int kfile_gets_echo(struct KFile *fd, char *buf, int size, bool echo);
00222 void kfile_resync(KFile *fd, mtime_t delay);
00223 
00229 INLINE size_t kfile_read(struct KFile *fd, void *buf, size_t size)
00230 {
00231     ASSERT(fd->read);
00232     return fd->read(fd, buf, size);
00233 }
00234 
00235 INLINE size_t kfile_write(struct KFile *fd, const void *buf, size_t size)
00236 {
00237     ASSERT(fd->write);
00238     return fd->write(fd, buf, size);
00239 }
00240 
00241 INLINE KFile * kfile_reopen(struct KFile *fd)
00242 {
00243     ASSERT(fd->reopen);
00244     return fd->reopen(fd);
00245 }
00246 
00247 INLINE int kfile_close(struct KFile *fd)
00248 {
00249     ASSERT(fd->close);
00250     return fd->close(fd);
00251 }
00252 
00253 INLINE kfile_off_t kfile_seek(struct KFile *fd, kfile_off_t offset, KSeekMode whence)
00254 {
00255     ASSERT(fd->seek);
00256     return fd->seek(fd, offset, whence);
00257 }
00258 
00259 INLINE int kfile_flush(struct KFile *fd)
00260 {
00261     ASSERT(fd->flush);
00262     return fd->flush(fd);
00263 }
00264 
00265 INLINE int kfile_error(struct KFile *fd)
00266 {
00267     ASSERT(fd->error);
00268     return fd->error(fd);
00269 }
00270 
00271 INLINE void kfile_clearerr(struct KFile *fd)
00272 {
00273     ASSERT(fd->clearerr);
00274     fd->clearerr(fd);
00275 }
00276 /* \} */
00277 
00281 int kfile_testSetup(void);
00282 int kfile_testRun(void);
00283 int kfile_testRunGeneric(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size);
00284 int kfile_testTearDown(void);
00285 
00286 #endif /* KERN_KFILE_H */