FileReader

Offers a simple interface for reading and buffering the content of a file.

Summary
FileReaderOffers a simple interface for reading and buffering the content of a file.
CopyrightThis program is free software.
Files
C-kern/api/io/reader/filereader.hHeader file FileReader.
C-kern/io/reader/filereader.cImplementation file FileReader impl.
Types
struct filereader_tExport filereader_t into global namespace.
Functions
test
unittest_io_reader_filereaderTest filereader_t functionality.
filereader_tReads file data into internal memory buffers.
ioerrorSafes status of last read access to file.
unreadsizeThe size of buffered data for which readnext_filereader is not called.
nextindexIndex into mmfile.
nrfreebufferNumber of released or unread buffers.
fileoffsetOffset into file where the next read operation begins.
filesizeThe size of the io-stream file refers to.
fileThe file from which is read.
mmfileThe buffered input of the file.
static configuration
filereader_SYS_BUFFER_SIZEThe sum of the size the two allocated buffers.
lifetime
filereader_FREEStatic initializer.
initsingle_filereaderOpens file for reading into a single buffer.
init_filereaderOpens file for reading into a double buffer.
free_filereaderCloses file and frees allocated buffers.
query
sizebuffer_filereaderReturns the buffer size in bytes.
ioerror_filereaderReturns the I/0 error (>0) or 0 if no error occurred.
iseof_filereaderReturns true if end of file is reached.
isfree_filereaderReturns true in case frd == filereader_FREE.
isnext_filereaderReturns true if there is a free buffer available.
setter
setioerror_filereaderSets ioerror of frd.
read
readnext_filereaderReturns buffer containing the next block of input data.
release_filereaderReleases the oldest read buffer.
unread_filereaderThe last buffer returned by readnext_filereader is marked as unread.
inline implementation
Macros
ioerror_filereaderImplements filereader_t.ioerror_filereader.
iseof_filereaderImplements filereader_t.iseof_filereader.
isnext_filereaderImplements filereader_t.isnext_filereader.
setioerror_filereaderImplements filereader_t.setioerror_filereader.

Copyright

This program is free software.  You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Author

© 2013 Jörg Seebohn

Files

C-kern/api/io/reader/filereader.h

Header file FileReader.

C-kern/io/reader/filereader.c

Implementation file FileReader impl.

Types

struct filereader_t

typedef struct filereader_t filereader_t

Export filereader_t into global namespace.

Functions

test

unittest_io_reader_filereader

int unittest_io_reader_filereader(void)

Test filereader_t functionality.

filereader_t

struct filereader_t

Reads file data into internal memory buffers.  At least two buffers are supported.  If one buffer is in use the other could be filled with new data from the file.  The function readnext_filereader returns a buffer containing the next read data.  Use release_filereader if you do not longer need it.  For every called readnext_filereader you need to call release_filereader.  Always the oldest read buffer is released.

Summary
ioerrorSafes status of last read access to file.
unreadsizeThe size of buffered data for which readnext_filereader is not called.
nextindexIndex into mmfile.
nrfreebufferNumber of released or unread buffers.
fileoffsetOffset into file where the next read operation begins.
filesizeThe size of the io-stream file refers to.
fileThe file from which is read.
mmfileThe buffered input of the file.
static configuration
filereader_SYS_BUFFER_SIZEThe sum of the size the two allocated buffers.
lifetime
filereader_FREEStatic initializer.
initsingle_filereaderOpens file for reading into a single buffer.
init_filereaderOpens file for reading into a double buffer.
free_filereaderCloses file and frees allocated buffers.
query
sizebuffer_filereaderReturns the buffer size in bytes.
ioerror_filereaderReturns the I/0 error (>0) or 0 if no error occurred.
iseof_filereaderReturns true if end of file is reached.
isfree_filereaderReturns true in case frd == filereader_FREE.
isnext_filereaderReturns true if there is a free buffer available.
setter
setioerror_filereaderSets ioerror of frd.
read
readnext_filereaderReturns buffer containing the next block of input data.
release_filereaderReleases the oldest read buffer.
unread_filereaderThe last buffer returned by readnext_filereader is marked as unread.

ioerror

int ioerror

Safes status of last read access to file.  In case ioerror != 0 no more call is made to the underlying file.

unreadsize

size_t unreadsize

The size of buffered data for which readnext_filereader is not called.

nextindex

uint8_t nextindex

Index into mmfile.  It is the index of the buffer which must be returned during the next call to readnext_filereader.

nrfreebuffer

uint8_t nrfreebuffer

Number of released or unread buffers.  This value can range from 0 to 2.

fileoffset

off_t fileoffset

Offset into file where the next read operation begins.

filesize

off_t filesize

The size of the io-stream file refers to.

file

sys_iochannel_t file

The file from which is read.

mmfile

struct { uint8_t* addr ; size_t size ; } mmfile[2]

The buffered input of the file.

static configuration

filereader_SYS_BUFFER_SIZE

#define filereader_SYS_BUFFER_SIZE (4*4096)

The sum of the size the two allocated buffers.  Every buffer is allocated half the size in bytes of this value.  This value can be overwritten in C-kern/resource/config/modulevalues.

lifetime

filereader_FREE

#define filereader_FREE { 0, 0, 0, 0, 0, 0, sys_iochannel_FREE, { {0, 0}, {0, 0} } }

Static initializer.

initsingle_filereader

int initsingle_filereader(/*out*/filereader_t *frd,  
const char *filepath,  
const struct directory_t *relative_to/*0 = > current working dir*/)

Opens file for reading into a single buffer.  Works only on files < 2GB on 32 bit systems.

init_filereader

int init_filereader(/*out*/filereader_t *frd,  
const char *filepath,  
const struct directory_t *relative_to/*0 = > current working dir*/)

Opens file for reading into a double buffer.  Works also for files > 2GB on 32 bit systems.

free_filereader

int free_filereader(filereader_t *frd)

Closes file and frees allocated buffers.

query

sizebuffer_filereader

size_t sizebuffer_filereader(void)

Returns the buffer size in bytes.  See also filereader_t.filereader_SYS_BUFFER_SIZE.  The size is aligned to value (2 * pagesize_vm()).  Therefore the two buffers of the double buffer configuration are aligned to pagesize_vm().

ioerror_filereader

int ioerror_filereader(const filereader_t *frd)

Returns the I/0 error (>0) or 0 if no error occurred.  If an error occurred every call to readnext_filereader returns this error code (EIO, ENOMEM, ...).

iseof_filereader

bool iseof_filereader(const filereader_t *frd)

Returns true if end of file is reached.  If there is no more data to read readnext_filereader will also return ENODATA.

isfree_filereader

bool isfree_filereader(const filereader_t *frd)

Returns true in case frd == filereader_FREE.

isnext_filereader

bool isnext_filereader(const filereader_t *frd)

Returns true if there is a free buffer available.  Therefore readnext_filereader will not return ENOBUFS or ENODATA.

setter

setioerror_filereader

void setioerror_filereader(filereader_t *frd,
int ioerr)

Sets ioerror of frd.  After an error occurred (or is simulated by another component) the function readnext_filereader returns this error.  Call free and init on frd again to clear the error or call this function with ioerr set to 0.

read

readnext_filereader

int readnext_filereader(filereader_t *frd,
/*out*/struct stringstream_t *buffer)

Returns buffer containing the next block of input data.  If you do not longer need it use release_filereader to release the oldest buffer.  The current implementation supports only two unreleased buffers.

Returns

0Read new buffer.
ENODATAAll data read.
ENOBUFSNo more buffer available.  Call release_filereader first before calling this function.
EIOInput/Output error (ENOMEM or other error codes are also possible).

release_filereader

void release_filereader(filereader_t *frd)

Releases the oldest read buffer.  If no buffer was read this function does nothing.

unread_filereader

void unread_filereader(filereader_t *frd)

The last buffer returned by readnext_filereader is marked as unread.  The next call to readnext_filereader returns the same buffer.  If no buffer was read this function does nothing.

Macros

ioerror_filereader

#define ioerror_filereader(frd) ((frd)->ioerror)

Implements filereader_t.ioerror_filereader.

iseof_filereader

#define iseof_filereader(
   frd
) ( __extension__ ({ const filereader_t * _f ; _f = (frd) ; (_f->unreadsize == 0 && _f->fileoffset == _f->filesize) ; }))

Implements filereader_t.iseof_filereader.

isnext_filereader

#define isnext_filereader(
   frd
) ( __extension__ ({ const filereader_t * _f ; _f = (frd) ; (_f->unreadsize != 0) ; }))

Implements filereader_t.isnext_filereader.

setioerror_filereader

#define setioerror_filereader(frd,
ioerr) ((void)((frd)->ioerror = (ioerr)))

Implements filereader_t.setioerror_filereader.

Offers a simple interface for reading and buffering the content of a file.
Implements FileReader.
typedef struct filereader_t filereader_t
Export filereader_t into global namespace.
struct filereader_t
Reads file data into internal memory buffers.
int unittest_io_reader_filereader(void)
Test filereader_t functionality.
int ioerror
Safes status of last read access to file.
sys_iochannel_t file
The file from which is read.
size_t unreadsize
The size of buffered data for which readnext_filereader is not called.
int readnext_filereader(filereader_t *frd,
/*out*/struct stringstream_t *buffer)
Returns buffer containing the next block of input data.
uint8_t nextindex
Index into mmfile.
struct { uint8_t* addr ; size_t size ; } mmfile[2]
The buffered input of the file.
uint8_t nrfreebuffer
Number of released or unread buffers.
off_t fileoffset
Offset into file where the next read operation begins.
off_t filesize
The size of the io-stream file refers to.
#define filereader_SYS_BUFFER_SIZE (4*4096)
The sum of the size the two allocated buffers.
#define filereader_FREE { 0, 0, 0, 0, 0, 0, sys_iochannel_FREE, { {0, 0}, {0, 0} } }
Static initializer.
int initsingle_filereader(/*out*/filereader_t *frd,  
const char *filepath,  
const struct directory_t *relative_to/*0 = > current working dir*/)
Opens file for reading into a single buffer.
int init_filereader(/*out*/filereader_t *frd,  
const char *filepath,  
const struct directory_t *relative_to/*0 = > current working dir*/)
Opens file for reading into a double buffer.
int free_filereader(filereader_t *frd)
Closes file and frees allocated buffers.
size_t sizebuffer_filereader(void)
Returns the buffer size in bytes.
int ioerror_filereader(const filereader_t *frd)
Returns the I/0 error (>0) or 0 if no error occurred.
bool iseof_filereader(const filereader_t *frd)
Returns true if end of file is reached.
bool isfree_filereader(const filereader_t *frd)
Returns true in case frd == filereader_FREE.
bool isnext_filereader(const filereader_t *frd)
Returns true if there is a free buffer available.
void setioerror_filereader(filereader_t *frd,
int ioerr)
Sets ioerror of frd.
void release_filereader(filereader_t *frd)
Releases the oldest read buffer.
void unread_filereader(filereader_t *frd)
The last buffer returned by readnext_filereader is marked as unread.
#define ioerror_filereader(frd) ((frd)->ioerror)
Implements filereader_t.ioerror_filereader.
#define iseof_filereader(
   frd
) ( __extension__ ({ const filereader_t * _f ; _f = (frd) ; (_f->unreadsize == 0 && _f->fileoffset == _f->filesize) ; }))
Implements filereader_t.iseof_filereader.
#define isnext_filereader(
   frd
) ( __extension__ ({ const filereader_t * _f ; _f = (frd) ; (_f->unreadsize != 0) ; }))
Implements filereader_t.isnext_filereader.
#define setioerror_filereader(frd,
ioerr) ((void)((frd)->ioerror = (ioerr)))
Implements filereader_t.setioerror_filereader.
Close