From: asterix Date: Wed, 4 Aug 2010 13:38:45 +0000 (+0000) Subject: Rename kblock on posix file to appropriate name. X-Git-Tag: 2.6.0~249 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=8391dbcd70180ffb6ea99ec894b98cb4280b10c3;p=bertos.git Rename kblock on posix file to appropriate name. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@4141 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/bertos/io/kblock_file.c b/bertos/io/kblock_file.c deleted file mode 100644 index cd18431d..00000000 --- a/bertos/io/kblock_file.c +++ /dev/null @@ -1,183 +0,0 @@ -/** - * \file - * - * - * \author Francesco Sacchi - * - * \brief KBlock interface over libc files. - * - * notest: avr - * notest: arm - */ - - -#include "kblock_file.h" -#include -#include - - -static int kblockfile_load(KBlock *b, block_idx_t index) -{ - KBlockFile *f = KBLOCKFILE_CAST(b); - fseek(f->fp, index * b->blk_size, SEEK_SET); - return (fread(f->b.priv.buf, 1, b->blk_size, f->fp) == b->blk_size) ? 0 : EOF; -} - -static int kblockfile_store(struct KBlock *b, block_idx_t index) -{ - KBlockFile *f = KBLOCKFILE_CAST(b); - fseek(f->fp, index * b->blk_size, SEEK_SET); - return (fwrite(f->b.priv.buf, 1, b->blk_size, f->fp) == b->blk_size) ? 0 : EOF; -} - -static size_t kblockfile_readBuf(struct KBlock *b, void *buf, size_t offset, size_t size) -{ - KBlockFile *f = KBLOCKFILE_CAST(b); - memcpy(buf, (uint8_t *)f->b.priv.buf + offset, size); - return size; -} - -static size_t kblockfile_readDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size) -{ - KBlockFile *f = KBLOCKFILE_CAST(b); - fseek(f->fp, index * b->blk_size + offset, SEEK_SET); - return fread(buf, 1, size, f->fp); -} - -static size_t kblockfile_writeBuf(struct KBlock *b, const void *buf, size_t offset, size_t size) -{ - KBlockFile *f = KBLOCKFILE_CAST(b); - memcpy((uint8_t *)f->b.priv.buf + offset, buf, size); - return size; -} - -static size_t kblockfile_writeDirect(struct KBlock *b, block_idx_t index, const void *buf, size_t offset, size_t size) -{ - KBlockFile *f = KBLOCKFILE_CAST(b); - ASSERT(buf); - ASSERT(index < b->blk_cnt); - fseek(f->fp, index * b->blk_size + offset, SEEK_SET); - return fwrite(buf, 1, size, f->fp); -} - -static int kblockfile_error(struct KBlock *b) -{ - KBlockFile *f = KBLOCKFILE_CAST(b); - return ferror(f->fp); -} - - -static int kblockfile_claererr(struct KBlock *b) -{ - KBlockFile *f = KBLOCKFILE_CAST(b); - clearerr(f->fp); - return 0; -} - - -static int kblockfile_close(struct KBlock *b) -{ - KBlockFile *f = KBLOCKFILE_CAST(b); - - return fflush(f->fp) | fclose(f->fp); -} - - -static const KBlockVTable kblockfile_hwbuffered_vt = -{ - .readDirect = kblockfile_readDirect, - - .readBuf = kblockfile_readBuf, - .writeBuf = kblockfile_writeBuf, - .load = kblockfile_load, - .store = kblockfile_store, - - .error = kblockfile_error, - .clearerr = kblockfile_claererr, - .close = kblockfile_close, -}; - -static const KBlockVTable kblockfile_swbuffered_vt = -{ - .readDirect = kblockfile_readDirect, - .writeDirect =kblockfile_writeDirect, - - .readBuf = kblock_swReadBuf, - .writeBuf = kblock_swWriteBuf, - .load = kblock_swLoad, - .store = kblock_swStore, - - .error = kblockfile_error, - .clearerr = kblockfile_claererr, - .close = kblockfile_close, -}; - -static const KBlockVTable kblockfile_unbuffered_vt = -{ - .readDirect = kblockfile_readDirect, - .writeDirect =kblockfile_writeDirect, - - .error = kblockfile_error, - .clearerr = kblockfile_claererr, - .close = kblockfile_close, -}; - - - -void kblockfile_init(KBlockFile *f, FILE *fp, bool hwbuf, void *buf, size_t block_size, block_idx_t block_count) -{ - ASSERT(f); - ASSERT(fp); - ASSERT(block_size); - - memset(f, 0, sizeof(*f)); - - DB(f->b.priv.type = KBT_KBLOCKFILE); - - f->fp = fp; - f->b.blk_size = block_size; - f->b.blk_cnt = block_count; - - f->b.priv.flags |= KB_PARTIAL_WRITE; - if (buf) - { - f->b.priv.flags |= KB_BUFFERED; - f->b.priv.buf = buf; - if (hwbuf) - f->b.priv.vt = &kblockfile_hwbuffered_vt; - else - f->b.priv.vt = &kblockfile_swbuffered_vt; - kblockfile_load(&f->b, 0); - f->b.priv.curr_blk = 0; - } - else - f->b.priv.vt = &kblockfile_unbuffered_vt; -} diff --git a/bertos/io/kblock_file.h b/bertos/io/kblock_file.h deleted file mode 100644 index 77e52756..00000000 --- a/bertos/io/kblock_file.h +++ /dev/null @@ -1,62 +0,0 @@ -/** - * \file - * - * - * \author Francesco Sacchi - * - * \brief KBlock interface - */ - -#ifndef KBLOCK_FILE_H -#define KBLOCK_FILE_H - -#include "kblock.h" - -#include - -typedef struct KBlockFile -{ - KBlock b; - FILE *fp; -} KBlockFile; - -#define KBT_KBLOCKFILE MAKE_ID('K', 'B', 'F', 'L') - - -INLINE KBlockFile *KBLOCKFILE_CAST(KBlock *b) -{ - ASSERT(b->priv.type == KBT_KBLOCKFILE); - return (KBlockFile *)b; -} - -void kblockfile_init(KBlockFile *f, FILE *fp, bool hwbuf, void *buf, size_t block_size, block_idx_t block_count); - -#endif /* KBLOCK_FILE_H */ diff --git a/bertos/io/kblock_posix.c b/bertos/io/kblock_posix.c new file mode 100644 index 00000000..cd18431d --- /dev/null +++ b/bertos/io/kblock_posix.c @@ -0,0 +1,183 @@ +/** + * \file + * + * + * \author Francesco Sacchi + * + * \brief KBlock interface over libc files. + * + * notest: avr + * notest: arm + */ + + +#include "kblock_file.h" +#include +#include + + +static int kblockfile_load(KBlock *b, block_idx_t index) +{ + KBlockFile *f = KBLOCKFILE_CAST(b); + fseek(f->fp, index * b->blk_size, SEEK_SET); + return (fread(f->b.priv.buf, 1, b->blk_size, f->fp) == b->blk_size) ? 0 : EOF; +} + +static int kblockfile_store(struct KBlock *b, block_idx_t index) +{ + KBlockFile *f = KBLOCKFILE_CAST(b); + fseek(f->fp, index * b->blk_size, SEEK_SET); + return (fwrite(f->b.priv.buf, 1, b->blk_size, f->fp) == b->blk_size) ? 0 : EOF; +} + +static size_t kblockfile_readBuf(struct KBlock *b, void *buf, size_t offset, size_t size) +{ + KBlockFile *f = KBLOCKFILE_CAST(b); + memcpy(buf, (uint8_t *)f->b.priv.buf + offset, size); + return size; +} + +static size_t kblockfile_readDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size) +{ + KBlockFile *f = KBLOCKFILE_CAST(b); + fseek(f->fp, index * b->blk_size + offset, SEEK_SET); + return fread(buf, 1, size, f->fp); +} + +static size_t kblockfile_writeBuf(struct KBlock *b, const void *buf, size_t offset, size_t size) +{ + KBlockFile *f = KBLOCKFILE_CAST(b); + memcpy((uint8_t *)f->b.priv.buf + offset, buf, size); + return size; +} + +static size_t kblockfile_writeDirect(struct KBlock *b, block_idx_t index, const void *buf, size_t offset, size_t size) +{ + KBlockFile *f = KBLOCKFILE_CAST(b); + ASSERT(buf); + ASSERT(index < b->blk_cnt); + fseek(f->fp, index * b->blk_size + offset, SEEK_SET); + return fwrite(buf, 1, size, f->fp); +} + +static int kblockfile_error(struct KBlock *b) +{ + KBlockFile *f = KBLOCKFILE_CAST(b); + return ferror(f->fp); +} + + +static int kblockfile_claererr(struct KBlock *b) +{ + KBlockFile *f = KBLOCKFILE_CAST(b); + clearerr(f->fp); + return 0; +} + + +static int kblockfile_close(struct KBlock *b) +{ + KBlockFile *f = KBLOCKFILE_CAST(b); + + return fflush(f->fp) | fclose(f->fp); +} + + +static const KBlockVTable kblockfile_hwbuffered_vt = +{ + .readDirect = kblockfile_readDirect, + + .readBuf = kblockfile_readBuf, + .writeBuf = kblockfile_writeBuf, + .load = kblockfile_load, + .store = kblockfile_store, + + .error = kblockfile_error, + .clearerr = kblockfile_claererr, + .close = kblockfile_close, +}; + +static const KBlockVTable kblockfile_swbuffered_vt = +{ + .readDirect = kblockfile_readDirect, + .writeDirect =kblockfile_writeDirect, + + .readBuf = kblock_swReadBuf, + .writeBuf = kblock_swWriteBuf, + .load = kblock_swLoad, + .store = kblock_swStore, + + .error = kblockfile_error, + .clearerr = kblockfile_claererr, + .close = kblockfile_close, +}; + +static const KBlockVTable kblockfile_unbuffered_vt = +{ + .readDirect = kblockfile_readDirect, + .writeDirect =kblockfile_writeDirect, + + .error = kblockfile_error, + .clearerr = kblockfile_claererr, + .close = kblockfile_close, +}; + + + +void kblockfile_init(KBlockFile *f, FILE *fp, bool hwbuf, void *buf, size_t block_size, block_idx_t block_count) +{ + ASSERT(f); + ASSERT(fp); + ASSERT(block_size); + + memset(f, 0, sizeof(*f)); + + DB(f->b.priv.type = KBT_KBLOCKFILE); + + f->fp = fp; + f->b.blk_size = block_size; + f->b.blk_cnt = block_count; + + f->b.priv.flags |= KB_PARTIAL_WRITE; + if (buf) + { + f->b.priv.flags |= KB_BUFFERED; + f->b.priv.buf = buf; + if (hwbuf) + f->b.priv.vt = &kblockfile_hwbuffered_vt; + else + f->b.priv.vt = &kblockfile_swbuffered_vt; + kblockfile_load(&f->b, 0); + f->b.priv.curr_blk = 0; + } + else + f->b.priv.vt = &kblockfile_unbuffered_vt; +} diff --git a/bertos/io/kblock_posix.h b/bertos/io/kblock_posix.h new file mode 100644 index 00000000..5cbd5434 --- /dev/null +++ b/bertos/io/kblock_posix.h @@ -0,0 +1,62 @@ +/** + * \file + * + * + * \author Francesco Sacchi + * + * \brief KBlock interface + */ + +#ifndef KBLOCK_POSIX_H +#define KBLOCK_POSIX_H + +#include "kblock.h" + +#include + +typedef struct KBlockFile +{ + KBlock b; + FILE *fp; +} KBlockFile; + +#define KBT_KBLOCKFILE MAKE_ID('K', 'B', 'F', 'L') + + +INLINE KBlockFile *KBLOCKFILE_CAST(KBlock *b) +{ + ASSERT(b->priv.type == KBT_KBLOCKFILE); + return (KBlockFile *)b; +} + +void kblockfile_init(KBlockFile *f, FILE *fp, bool hwbuf, void *buf, size_t block_size, block_idx_t block_count); + +#endif /* KBLOCK_POSIX_H */