Commit 62e07501 62e0750147b27bbe32810ca39958f6f8e46caca6 by Alain Magloire

file_stream.c mime.c

gnu coding standards fon file_stream.c
mime.c added stream_set_fd
1 parent b44590e7
......@@ -29,79 +29,92 @@ struct _file_stream
int offset;
};
static void _file_destroy(stream_t stream)
static void
_file_destroy (stream_t stream)
{
struct _file_stream *fs = stream->owner;
if ( fs->file )
fclose(fs->file);
free(fs);
if (fs->file)
fclose (fs->file);
free (fs);
}
static int _file_read(stream_t stream, char *optr, size_t osize, off_t offset, size_t *nbytes)
static int
_file_read (stream_t stream, char *optr, size_t osize,
off_t offset, size_t *nbytes)
{
struct _file_stream *fs = stream->owner;
if ( fs->offset != offset ) {
fseek( fs->file, offset, SEEK_SET );
if (fs->offset != offset)
{
fseek (fs->file, offset, SEEK_SET);
fs->offset = offset;
}
*nbytes = fread( optr, osize, 1, fs->file);
if ( *nbytes == 0 ) {
if ( ferror( fs->file ) )
*nbytes = fread (optr, osize, 1, fs->file);
if (*nbytes == 0)
{
if (ferror(fs->file))
return errno;
} else
}
else
fs->offset += *nbytes;
return 0;
}
static int _file_write(stream_t stream, const char *iptr, size_t isize, off_t offset, size_t *nbytes)
static int
_file_write (stream_t stream, const char *iptr, size_t isize,
off_t offset, size_t *nbytes)
{
struct _file_stream *fs = stream->owner;
if ( fs->offset != offset ) {
fseek( fs->file, offset, SEEK_SET );
if (fs->offset != offset)
{
fseek (fs->file, offset, SEEK_SET);
fs->offset = offset;
}
*nbytes = fwrite( iptr, isize, 1, fs->file);
if ( *nbytes == 0 ) {
if ( ferror( fs->file ) )
*nbytes = fwrite (iptr, isize, 1, fs->file);
if (*nbytes == 0)
{
if (ferror (fs->file))
return errno;
} else
}
else
fs->offset += *nbytes;
return 0;
}
int file_stream_create(stream_t *stream, const char *filename, int flags)
int
file_stream_create (stream_t *stream, const char *filename, int flags)
{
struct _file_stream *fs;
char *mode;
int ret;
if ( stream == NULL || filename == NULL )
if (stream == NULL || filename == NULL)
return EINVAL;
if ( ( fs = calloc(sizeof(struct _file_stream), 1) ) == NULL )
if ((fs = calloc(sizeof(struct _file_stream), 1)) == NULL)
return ENOMEM;
if ( ( flags & ( MU_STREAM_READ|MU_STREAM_WRITE ) ) == ( MU_STREAM_READ|MU_STREAM_WRITE ) )
if (flags & MU_STREAM_RDWR)
mode = "r+b";
else if ( flags & MU_STREAM_READ )
else if (flags & MU_STREAM_READ)
mode = "rb";
else if ( flags & MU_STREAM_WRITE )
else if (flags & MU_STREAM_WRITE)
mode = "wb";
else
return EINVAL;
if ( ( fs->file = fopen(filename, mode) ) == NULL ) {
if ((fs->file = fopen (filename, mode)) == NULL)
{
ret = errno;
free( fs );
free (fs);
return ret;
}
if ( ( ret = stream_create(stream, flags, fs) ) != 0 ) {
fclose( fs->file );
free( fs );
if ((ret = stream_create (stream, flags, fs)) != 0)
{
fclose (fs->file);
free (fs);
return ret;
}
......
......@@ -295,6 +295,13 @@ static int _mime_parse_mpart_message(mime_t mime)
return ret;
}
static int _mime_message_fd(stream_t stream, int *fd)
{
struct _mime_part *mime_part = stream->owner;
return stream_get_fd(mime_part->mime->stream, fd);
}
static int _mime_message_read(stream_t stream, char *buf, size_t buflen, off_t off, size_t *nbytes)
{
struct _mime_part *mime_part = stream->owner;
......@@ -448,7 +455,9 @@ int mime_get_part(mime_t mime, int part, message_t *msg)
if ( ( ret = stream_create(&stream, MU_STREAM_READ, mime_part) ) == 0 ) {
body_set_size (body, _mime_body_size, mime_part);
stream_set_read(stream, _mime_message_read, mime_part);
stream_set_fd(stream, _mime_message_fd, mime_part);
body_set_stream(body, stream, mime_part);
message_set_body(mime_part->msg, body, mime_part);
*msg = mime_part->msg;
return 0;
}
......