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 ...@@ -29,79 +29,92 @@ struct _file_stream
29 int offset; 29 int offset;
30 }; 30 };
31 31
32 static void _file_destroy(stream_t stream) 32 static void
33 _file_destroy (stream_t stream)
33 { 34 {
34 struct _file_stream *fs = stream->owner; 35 struct _file_stream *fs = stream->owner;
35 36
36 if ( fs->file ) 37 if (fs->file)
37 fclose(fs->file); 38 fclose (fs->file);
38 free(fs); 39 free (fs);
39 } 40 }
40 41
41 static int _file_read(stream_t stream, char *optr, size_t osize, off_t offset, size_t *nbytes) 42 static int
43 _file_read (stream_t stream, char *optr, size_t osize,
44 off_t offset, size_t *nbytes)
42 { 45 {
43 struct _file_stream *fs = stream->owner; 46 struct _file_stream *fs = stream->owner;
44 47
45 if ( fs->offset != offset ) { 48 if (fs->offset != offset)
46 fseek( fs->file, offset, SEEK_SET ); 49 {
50 fseek (fs->file, offset, SEEK_SET);
47 fs->offset = offset; 51 fs->offset = offset;
48 } 52 }
49 *nbytes = fread( optr, osize, 1, fs->file); 53 *nbytes = fread (optr, osize, 1, fs->file);
50 if ( *nbytes == 0 ) { 54 if (*nbytes == 0)
51 if ( ferror( fs->file ) ) 55 {
56 if (ferror(fs->file))
52 return errno; 57 return errno;
53 } else 58 }
59 else
54 fs->offset += *nbytes; 60 fs->offset += *nbytes;
55 return 0; 61 return 0;
56 } 62 }
57 63
58 64 static int
59 static int _file_write(stream_t stream, const char *iptr, size_t isize, off_t offset, size_t *nbytes) 65 _file_write (stream_t stream, const char *iptr, size_t isize,
66 off_t offset, size_t *nbytes)
60 { 67 {
61 struct _file_stream *fs = stream->owner; 68 struct _file_stream *fs = stream->owner;
62 69
63 if ( fs->offset != offset ) { 70 if (fs->offset != offset)
64 fseek( fs->file, offset, SEEK_SET ); 71 {
72 fseek (fs->file, offset, SEEK_SET);
65 fs->offset = offset; 73 fs->offset = offset;
66 } 74 }
67 *nbytes = fwrite( iptr, isize, 1, fs->file); 75 *nbytes = fwrite (iptr, isize, 1, fs->file);
68 if ( *nbytes == 0 ) { 76 if (*nbytes == 0)
69 if ( ferror( fs->file ) ) 77 {
78 if (ferror (fs->file))
70 return errno; 79 return errno;
71 } else 80 }
81 else
72 fs->offset += *nbytes; 82 fs->offset += *nbytes;
73 return 0; 83 return 0;
74 } 84 }
75 85
76 int file_stream_create(stream_t *stream, const char *filename, int flags) 86 int
87 file_stream_create (stream_t *stream, const char *filename, int flags)
77 { 88 {
78 struct _file_stream *fs; 89 struct _file_stream *fs;
79 char *mode; 90 char *mode;
80 int ret; 91 int ret;
81 92
82 if ( stream == NULL || filename == NULL ) 93 if (stream == NULL || filename == NULL)
83 return EINVAL; 94 return EINVAL;
84 95
85 if ( ( fs = calloc(sizeof(struct _file_stream), 1) ) == NULL ) 96 if ((fs = calloc(sizeof(struct _file_stream), 1)) == NULL)
86 return ENOMEM; 97 return ENOMEM;
87 98
88 if ( ( flags & ( MU_STREAM_READ|MU_STREAM_WRITE ) ) == ( MU_STREAM_READ|MU_STREAM_WRITE ) ) 99 if (flags & MU_STREAM_RDWR)
89 mode = "r+b"; 100 mode = "r+b";
90 else if ( flags & MU_STREAM_READ ) 101 else if (flags & MU_STREAM_READ)
91 mode = "rb"; 102 mode = "rb";
92 else if ( flags & MU_STREAM_WRITE ) 103 else if (flags & MU_STREAM_WRITE)
93 mode = "wb"; 104 mode = "wb";
94 else 105 else
95 return EINVAL; 106 return EINVAL;
96 107
97 if ( ( fs->file = fopen(filename, mode) ) == NULL ) { 108 if ((fs->file = fopen (filename, mode)) == NULL)
109 {
98 ret = errno; 110 ret = errno;
99 free( fs ); 111 free (fs);
100 return ret; 112 return ret;
101 } 113 }
102 if ( ( ret = stream_create(stream, flags, fs) ) != 0 ) { 114 if ((ret = stream_create (stream, flags, fs)) != 0)
103 fclose( fs->file ); 115 {
104 free( fs ); 116 fclose (fs->file);
117 free (fs);
105 return ret; 118 return ret;
106 } 119 }
107 120
......
...@@ -295,6 +295,13 @@ static int _mime_parse_mpart_message(mime_t mime) ...@@ -295,6 +295,13 @@ static int _mime_parse_mpart_message(mime_t mime)
295 return ret; 295 return ret;
296 } 296 }
297 297
298 static int _mime_message_fd(stream_t stream, int *fd)
299 {
300 struct _mime_part *mime_part = stream->owner;
301
302 return stream_get_fd(mime_part->mime->stream, fd);
303 }
304
298 static int _mime_message_read(stream_t stream, char *buf, size_t buflen, off_t off, size_t *nbytes) 305 static int _mime_message_read(stream_t stream, char *buf, size_t buflen, off_t off, size_t *nbytes)
299 { 306 {
300 struct _mime_part *mime_part = stream->owner; 307 struct _mime_part *mime_part = stream->owner;
...@@ -448,7 +455,9 @@ int mime_get_part(mime_t mime, int part, message_t *msg) ...@@ -448,7 +455,9 @@ int mime_get_part(mime_t mime, int part, message_t *msg)
448 if ( ( ret = stream_create(&stream, MU_STREAM_READ, mime_part) ) == 0 ) { 455 if ( ( ret = stream_create(&stream, MU_STREAM_READ, mime_part) ) == 0 ) {
449 body_set_size (body, _mime_body_size, mime_part); 456 body_set_size (body, _mime_body_size, mime_part);
450 stream_set_read(stream, _mime_message_read, mime_part); 457 stream_set_read(stream, _mime_message_read, mime_part);
458 stream_set_fd(stream, _mime_message_fd, mime_part);
451 body_set_stream(body, stream, mime_part); 459 body_set_stream(body, stream, mime_part);
460 message_set_body(mime_part->msg, body, mime_part);
452 *msg = mime_part->msg; 461 *msg = mime_part->msg;
453 return 0; 462 return 0;
454 } 463 }
......