From b1c122a36a111f4bb088a8f000a7fc8f8975734e Mon Sep 17 00:00:00 2001 From: lottaviano Date: Fri, 5 Jun 2009 12:56:37 +0000 Subject: [PATCH] Added KFile interface emulation in posix systems git-svn-id: https://src.develer.com/svnoss/bertos/trunk@2705 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/emul/kfile_posix.c | 97 +++++++++++++++++++++++++++++++++++++++ bertos/emul/kfile_posix.h | 56 ++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 bertos/emul/kfile_posix.c create mode 100644 bertos/emul/kfile_posix.h diff --git a/bertos/emul/kfile_posix.c b/bertos/emul/kfile_posix.c new file mode 100644 index 00000000..3fce00bc --- /dev/null +++ b/bertos/emul/kfile_posix.c @@ -0,0 +1,97 @@ +/** + * \file + * + * + * \brief KFile interface implementation in Posix systems. + * + * \version $Id$ + * \author Luca Ottaviano + */ + +#include + +static size_t kfile_posix_read(struct KFile *_fd, void *buf, size_t size) +{ + KFilePosix *fd = KFILEPOSIX_CAST(_fd); + return fread(buf, sizeof(uint8_t), size, fd->fp); +} + +static size_t kfile_posix_write(struct KFile *_fd, const void *buf, size_t size) +{ + KFilePosix *fd = KFILEPOSIX_CAST(_fd); + return fwrite(buf, sizeof(uint8_t), size, fd->fp); +} + +static kfile_off_t kfile_posix_seek(struct KFile *_fd, kfile_off_t offset, KSeekMode whence) +{ + KFilePosix *fd = KFILEPOSIX_CAST(_fd); + int std_whence; + switch (whence) + { + case KSM_SEEK_CUR: + std_whence = SEEK_CUR; + break; + case KSM_SEEK_END: + std_whence = SEEK_END; + break; + case KSM_SEEK_SET: + /* fall */ + default: + std_whence = SEEK_SET; + } + + return fseek(fd->fp, offset, std_whence); +} + +static int kfile_posix_close(struct KFile *_fd) +{ + KFilePosix *fd = KFILEPOSIX_CAST(_fd); + return fclose(fd->fp); +} + +static int kfile_posix_flush(struct KFile *_fd) +{ + KFilePosix *fd = KFILEPOSIX_CAST(_fd); + return fflush(fd->fp); +} + +FILE* kfile_posix_init(KFilePosix *file, const char *filename, const char *mode) +{ + file->fd._type = KFT_KFILEPOSIX; + file->fd.read = kfile_posix_read; + file->fd.write = kfile_posix_write; + file->fd.close = kfile_posix_close; + file->fd.seek = kfile_posix_seek; + file->fd.flush = kfile_posix_flush; + + file->fp = fopen(filename, mode); + return file->fp; +} diff --git a/bertos/emul/kfile_posix.h b/bertos/emul/kfile_posix.h new file mode 100644 index 00000000..b55c048d --- /dev/null +++ b/bertos/emul/kfile_posix.h @@ -0,0 +1,56 @@ +/** + * \file + * + * + * \brief KFile interface implementation in Posix systems. + * + * \version $Id$ + * \author Luca Ottaviano + */ + +#include +#include + +typedef struct KFilePosix +{ + KFile fd; + FILE *fp; +} KFilePosix; + +#define KFT_KFILEPOSIX MAKE_ID('K', 'F', 'T', 'P') + +INLINE KFilePosix *KFILEPOSIX_CAST(KFile *fd) +{ + ASSERT(fd->_type == KFT_KFILEPOSIX); + return (KFilePosix *)fd; +} + +FILE *kfile_posix_init(KFilePosix *file, const char *filename, const char *mode); -- 2.25.1