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
...@@ -25,89 +25,102 @@ ...@@ -25,89 +25,102 @@
25 25
26 struct _file_stream 26 struct _file_stream
27 { 27 {
28 FILE *file; 28 FILE *file;
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 {
47 fs->offset = offset; 50 fseek (fs->file, offset, SEEK_SET);
48 } 51 fs->offset = offset;
49 *nbytes = fread( optr, osize, 1, fs->file); 52 }
50 if ( *nbytes == 0 ) { 53 *nbytes = fread (optr, osize, 1, fs->file);
51 if ( ferror( fs->file ) ) 54 if (*nbytes == 0)
52 return errno; 55 {
53 } else 56 if (ferror(fs->file))
54 fs->offset += *nbytes; 57 return errno;
55 return 0; 58 }
59 else
60 fs->offset += *nbytes;
61 return 0;
56 } 62 }
57
58 63
59 static int _file_write(stream_t stream, const char *iptr, size_t isize, off_t offset, size_t *nbytes) 64 static int
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 {
65 fs->offset = offset; 72 fseek (fs->file, offset, SEEK_SET);
66 } 73 fs->offset = offset;
67 *nbytes = fwrite( iptr, isize, 1, fs->file); 74 }
68 if ( *nbytes == 0 ) { 75 *nbytes = fwrite (iptr, isize, 1, fs->file);
69 if ( ferror( fs->file ) ) 76 if (*nbytes == 0)
70 return errno; 77 {
71 } else 78 if (ferror (fs->file))
72 fs->offset += *nbytes; 79 return errno;
73 return 0; 80 }
81 else
82 fs->offset += *nbytes;
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)
98 ret = errno; 109 {
99 free( fs ); 110 ret = errno;
100 return ret; 111 free (fs);
101 } 112 return ret;
102 if ( ( ret = stream_create(stream, flags, fs) ) != 0 ) { 113 }
103 fclose( fs->file ); 114 if ((ret = stream_create (stream, flags, fs)) != 0)
104 free( fs ); 115 {
105 return ret; 116 fclose (fs->file);
106 } 117 free (fs);
107 118 return ret;
108 stream_set_read(*stream, _file_read, fs ); 119 }
109 stream_set_write(*stream, _file_write, fs ); 120
110 stream_set_destroy(*stream, _file_destroy, fs ); 121 stream_set_read(*stream, _file_read, fs );
111 return 0; 122 stream_set_write(*stream, _file_write, fs );
123 stream_set_destroy(*stream, _file_destroy, fs );
124 return 0;
112 } 125 }
113 126
......
...@@ -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 }
......