Commit 89542f44 89542f44fd012eb63ed32e87a2b5a431b2b738e5 by Sergey Poznyakoff

(mu_stream_getline, mu_stream_sequential_getline): New function

1 parent 32405975
...@@ -74,6 +74,8 @@ extern int mu_stream_get_transport (mu_stream_t stream, mu_transport_t *pt); ...@@ -74,6 +74,8 @@ extern int mu_stream_get_transport (mu_stream_t stream, mu_transport_t *pt);
74 74
75 extern int mu_stream_read (mu_stream_t, char *, size_t, mu_off_t, size_t *); 75 extern int mu_stream_read (mu_stream_t, char *, size_t, mu_off_t, size_t *);
76 extern int mu_stream_readline (mu_stream_t, char *, size_t, mu_off_t, size_t *); 76 extern int mu_stream_readline (mu_stream_t, char *, size_t, mu_off_t, size_t *);
77 extern int mu_stream_getline (mu_stream_t is, char **pbuf, size_t *pbufsize,
78 mu_off_t offset, size_t *pnread);
77 extern int mu_stream_size (mu_stream_t, mu_off_t *); 79 extern int mu_stream_size (mu_stream_t, mu_off_t *);
78 extern int mu_stream_truncate (mu_stream_t, mu_off_t); 80 extern int mu_stream_truncate (mu_stream_t, mu_off_t);
79 extern int mu_stream_write (mu_stream_t, const char *, size_t, mu_off_t, 81 extern int mu_stream_write (mu_stream_t, const char *, size_t, mu_off_t,
...@@ -152,6 +154,9 @@ extern int mu_stream_sequential_read (mu_stream_t stream, ...@@ -152,6 +154,9 @@ extern int mu_stream_sequential_read (mu_stream_t stream,
152 extern int mu_stream_sequential_readline (mu_stream_t stream, 154 extern int mu_stream_sequential_readline (mu_stream_t stream,
153 char *buf, size_t size, size_t *nbytes); 155 char *buf, size_t size, size_t *nbytes);
154 156
157 extern int mu_stream_sequential_getline (mu_stream_t is,
158 char **pbuf, size_t *pbufsize, size_t *pnread);
159
155 extern int mu_stream_sequential_write (mu_stream_t stream, 160 extern int mu_stream_sequential_write (mu_stream_t stream,
156 const char *buf, size_t size); 161 const char *buf, size_t size);
157 extern int mu_stream_seek (mu_stream_t stream, mu_off_t off, int whence); 162 extern int mu_stream_seek (mu_stream_t stream, mu_off_t off, int whence);
......
...@@ -109,11 +109,12 @@ _mu_rfc822_open (mu_stream_t stream) ...@@ -109,11 +109,12 @@ _mu_rfc822_open (mu_stream_t stream)
109 { 109 {
110 struct _mu_rfc822_stream *s = mu_stream_get_owner (stream); 110 struct _mu_rfc822_stream *s = mu_stream_get_owner (stream);
111 size_t offset, len; 111 size_t offset, len;
112 char buffer[256]; 112 char *buffer = NULL;
113 size_t bufsize = 0;
113 int rc; 114 int rc;
114 115
115 offset = 0; 116 offset = 0;
116 while ((rc = mu_stream_readline (s->stream, buffer, sizeof buffer, 117 while ((rc = mu_stream_getline (s->stream, &buffer, &bufsize,
117 offset, &len)) == 0 118 offset, &len)) == 0
118 && len > 0) 119 && len > 0)
119 { 120 {
...@@ -126,6 +127,7 @@ _mu_rfc822_open (mu_stream_t stream) ...@@ -126,6 +127,7 @@ _mu_rfc822_open (mu_stream_t stream)
126 127
127 offset += len; 128 offset += len;
128 } 129 }
130 free (buffer);
129 return 0; 131 return 0;
130 } 132 }
131 133
...@@ -211,18 +213,17 @@ restore_envelope (mu_stream_t str, struct _mu_rfc822_message **pmenv) ...@@ -211,18 +213,17 @@ restore_envelope (mu_stream_t str, struct _mu_rfc822_message **pmenv)
211 char *env_from = NULL; 213 char *env_from = NULL;
212 char *env_date = NULL; 214 char *env_date = NULL;
213 int rc; 215 int rc;
214 char buffer[128]; 216 char *buffer = NULL;
217 size_t bufsize = 0;
215 size_t len; 218 size_t len;
216 mu_off_t body_start, body_end; 219 mu_off_t body_start, body_end;
217 220
218 while ((rc = mu_stream_readline (str, buffer, sizeof buffer, offset, &len)) 221 while ((rc = mu_stream_getline (str, &buffer, &bufsize, offset, &len)) == 0
219 == 0
220 && len > 0) 222 && len > 0)
221 { 223 {
222 if (buffer[0] == '\n') 224 if (buffer[0] == '\n')
223 break; 225 break;
224 buffer[len] = 0; 226 buffer[len] = 0;
225 offset += len;
226 if (strncasecmp (buffer, MU_HEADER_FROM, 227 if (strncasecmp (buffer, MU_HEADER_FROM,
227 sizeof (MU_HEADER_FROM) - 1) == 0) 228 sizeof (MU_HEADER_FROM) - 1) == 0)
228 from = strdup (skipws (buffer, sizeof (MU_HEADER_FROM))); 229 from = strdup (skipws (buffer, sizeof (MU_HEADER_FROM)));
...@@ -234,6 +235,8 @@ restore_envelope (mu_stream_t str, struct _mu_rfc822_message **pmenv) ...@@ -234,6 +235,8 @@ restore_envelope (mu_stream_t str, struct _mu_rfc822_message **pmenv)
234 env_date = strdup (skipws (buffer, sizeof (MU_HEADER_ENV_DATE))); 235 env_date = strdup (skipws (buffer, sizeof (MU_HEADER_ENV_DATE)));
235 } 236 }
236 237
238 free (buffer);
239
237 body_start = offset + 1; 240 body_start = offset + 1;
238 mu_stream_size (str, &body_end); 241 mu_stream_size (str, &body_end);
239 242
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail 1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2004, 2005 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2000, 2001, 2004, 2005,
3 2006 Free Software Foundation, Inc.
3 4
4 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public 6 modify it under the terms of the GNU Lesser General Public
...@@ -397,6 +398,69 @@ mu_stream_readline (mu_stream_t is, char *buf, size_t count, ...@@ -397,6 +398,69 @@ mu_stream_readline (mu_stream_t is, char *buf, size_t count,
397 } 398 }
398 399
399 int 400 int
401 mu_stream_getline (mu_stream_t is, char **pbuf, size_t *pbufsize,
402 mu_off_t offset, size_t *pnread)
403 {
404 char *buf = *pbuf;
405 size_t bufsize = *pbufsize;
406 size_t total = 0, off = 0;
407 int rc = 0;
408 #define DELTA 128
409
410 if (buf == NULL)
411 {
412 bufsize = DELTA;
413 buf = malloc (bufsize);
414 if (!buf)
415 return ENOMEM;
416 }
417
418 do
419 {
420 size_t nread;
421 int rc;
422
423 if (off + 1 == bufsize)
424 {
425 char *p;
426 p = realloc (buf, bufsize + DELTA);
427 if (!p)
428 {
429 rc = ENOMEM;
430 break;
431 }
432 bufsize += DELTA;
433 buf = p;
434 }
435
436 rc = mu_stream_readline (is, buf + off, bufsize - off, offset + off,
437 &nread);
438 if (rc)
439 {
440 if (*pbuf)
441 free (buf);
442 return rc;
443 }
444 if (nread == 0)
445 break;
446 off += nread;
447 total += nread;
448 }
449 while (buf[off - 1] != '\n');
450
451 if (rc && !*pbuf)
452 free (buf);
453 else
454 {
455 *pbuf = buf;
456 *pbufsize = bufsize;
457 if (pnread)
458 *pnread = total;
459 }
460 return rc;
461 }
462
463 int
400 mu_stream_write (mu_stream_t os, const char *buf, size_t count, 464 mu_stream_write (mu_stream_t os, const char *buf, size_t count,
401 mu_off_t offset, size_t *pnwrite) 465 mu_off_t offset, size_t *pnwrite)
402 { 466 {
...@@ -435,8 +499,7 @@ mu_stream_get_transport2 (mu_stream_t stream, ...@@ -435,8 +499,7 @@ mu_stream_get_transport2 (mu_stream_t stream,
435 } 499 }
436 500
437 int 501 int
438 mu_stream_get_transport (mu_stream_t stream, 502 mu_stream_get_transport (mu_stream_t stream, mu_transport_t *pt)
439 mu_transport_t *pt)
440 { 503 {
441 return mu_stream_get_transport2 (stream, pt, NULL); 504 return mu_stream_get_transport2 (stream, pt, NULL);
442 } 505 }
...@@ -519,7 +582,8 @@ mu_stream_get_state (mu_stream_t stream, int *pstate) ...@@ -519,7 +582,8 @@ mu_stream_get_state (mu_stream_t stream, int *pstate)
519 } 582 }
520 583
521 int 584 int
522 mu_stream_set_destroy (mu_stream_t stream, void (*_destroy) (mu_stream_t), void *owner) 585 mu_stream_set_destroy (mu_stream_t stream,
586 void (*_destroy) (mu_stream_t), void *owner)
523 { 587 {
524 if (stream == NULL) 588 if (stream == NULL)
525 return EINVAL; 589 return EINVAL;
...@@ -546,7 +610,8 @@ mu_stream_set_open (mu_stream_t stream, ...@@ -546,7 +610,8 @@ mu_stream_set_open (mu_stream_t stream,
546 } 610 }
547 611
548 int 612 int
549 mu_stream_set_close (mu_stream_t stream, int (*_close) (mu_stream_t), void *owner) 613 mu_stream_set_close (mu_stream_t stream,
614 int (*_close) (mu_stream_t), void *owner)
550 { 615 {
551 if (stream == NULL) 616 if (stream == NULL)
552 return EINVAL; 617 return EINVAL;
...@@ -560,7 +625,9 @@ mu_stream_set_close (mu_stream_t stream, int (*_close) (mu_stream_t), void *owne ...@@ -560,7 +625,9 @@ mu_stream_set_close (mu_stream_t stream, int (*_close) (mu_stream_t), void *owne
560 625
561 int 626 int
562 mu_stream_set_get_transport2 (mu_stream_t stream, 627 mu_stream_set_get_transport2 (mu_stream_t stream,
563 int (*_get_trans) (mu_stream_t, mu_transport_t *, mu_transport_t *), 628 int (*_get_trans) (mu_stream_t,
629 mu_transport_t *,
630 mu_transport_t *),
564 void *owner) 631 void *owner)
565 { 632 {
566 if (stream == NULL) 633 if (stream == NULL)
...@@ -574,8 +641,9 @@ mu_stream_set_get_transport2 (mu_stream_t stream, ...@@ -574,8 +641,9 @@ mu_stream_set_get_transport2 (mu_stream_t stream,
574 } 641 }
575 642
576 int 643 int
577 mu_stream_set_read (mu_stream_t stream, int (*_read) 644 mu_stream_set_read (mu_stream_t stream,
578 (mu_stream_t, char *, size_t, mu_off_t, size_t *), 645 int (*_read) (mu_stream_t, char *, size_t,
646 mu_off_t, size_t *),
579 void *owner) 647 void *owner)
580 { 648 {
581 if (stream == NULL) 649 if (stream == NULL)
...@@ -589,8 +657,9 @@ mu_stream_set_read (mu_stream_t stream, int (*_read) ...@@ -589,8 +657,9 @@ mu_stream_set_read (mu_stream_t stream, int (*_read)
589 } 657 }
590 658
591 int 659 int
592 mu_stream_set_readline (mu_stream_t stream, int (*_readline) 660 mu_stream_set_readline (mu_stream_t stream,
593 (mu_stream_t, char *, size_t, mu_off_t, size_t *), 661 int (*_readline) (mu_stream_t, char *, size_t,
662 mu_off_t, size_t *),
594 void *owner) 663 void *owner)
595 { 664 {
596 if (stream == NULL) 665 if (stream == NULL)
...@@ -604,8 +673,9 @@ mu_stream_set_readline (mu_stream_t stream, int (*_readline) ...@@ -604,8 +673,9 @@ mu_stream_set_readline (mu_stream_t stream, int (*_readline)
604 } 673 }
605 674
606 int 675 int
607 mu_stream_set_write (mu_stream_t stream, int (*_write) 676 mu_stream_set_write (mu_stream_t stream,
608 (mu_stream_t, const char *, size_t, mu_off_t, size_t *), 677 int (*_write) (mu_stream_t, const char *, size_t,
678 mu_off_t, size_t *),
609 void *owner) 679 void *owner)
610 { 680 {
611 if (stream == NULL) 681 if (stream == NULL)
...@@ -620,7 +690,9 @@ mu_stream_set_write (mu_stream_t stream, int (*_write) ...@@ -620,7 +690,9 @@ mu_stream_set_write (mu_stream_t stream, int (*_write)
620 690
621 691
622 int 692 int
623 mu_stream_set_size (mu_stream_t stream, int (*_size)(mu_stream_t, mu_off_t *), void *owner) 693 mu_stream_set_size (mu_stream_t stream,
694 int (*_size) (mu_stream_t, mu_off_t *),
695 void *owner)
624 { 696 {
625 if (stream == NULL) 697 if (stream == NULL)
626 return EINVAL; 698 return EINVAL;
...@@ -631,7 +703,8 @@ mu_stream_set_size (mu_stream_t stream, int (*_size)(mu_stream_t, mu_off_t *), v ...@@ -631,7 +703,8 @@ mu_stream_set_size (mu_stream_t stream, int (*_size)(mu_stream_t, mu_off_t *), v
631 } 703 }
632 704
633 int 705 int
634 mu_stream_set_truncate (mu_stream_t stream, int (*_truncate) (mu_stream_t, mu_off_t), 706 mu_stream_set_truncate (mu_stream_t stream,
707 int (*_truncate) (mu_stream_t, mu_off_t),
635 void *owner) 708 void *owner)
636 { 709 {
637 if (stream == NULL) 710 if (stream == NULL)
...@@ -643,7 +716,8 @@ mu_stream_set_truncate (mu_stream_t stream, int (*_truncate) (mu_stream_t, mu_of ...@@ -643,7 +716,8 @@ mu_stream_set_truncate (mu_stream_t stream, int (*_truncate) (mu_stream_t, mu_of
643 } 716 }
644 717
645 int 718 int
646 mu_stream_set_flush (mu_stream_t stream, int (*_flush) (mu_stream_t), void *owner) 719 mu_stream_set_flush (mu_stream_t stream,
720 int (*_flush) (mu_stream_t), void *owner)
647 { 721 {
648 if (stream == NULL) 722 if (stream == NULL)
649 return EINVAL; 723 return EINVAL;
...@@ -676,7 +750,8 @@ mu_stream_set_strerror (mu_stream_t stream, ...@@ -676,7 +750,8 @@ mu_stream_set_strerror (mu_stream_t stream,
676 750
677 int 751 int
678 mu_stream_set_wait (mu_stream_t stream, 752 mu_stream_set_wait (mu_stream_t stream,
679 int (*wait) (mu_stream_t, int *, struct timeval *), void *owner) 753 int (*wait) (mu_stream_t, int *, struct timeval *),
754 void *owner)
680 { 755 {
681 if (stream == NULL) 756 if (stream == NULL)
682 return EINVAL; 757 return EINVAL;
...@@ -717,6 +792,23 @@ mu_stream_sequential_readline (mu_stream_t stream, char *buf, size_t size, ...@@ -717,6 +792,23 @@ mu_stream_sequential_readline (mu_stream_t stream, char *buf, size_t size,
717 } 792 }
718 793
719 int 794 int
795 mu_stream_sequential_getline (mu_stream_t stream,
796 char **pbuf, size_t *pbufsize,
797 size_t *nbytes)
798 {
799 size_t rdbytes;
800 int rc = mu_stream_getline (stream, pbuf, pbufsize, stream->offset, &rdbytes);
801 if (!rc)
802 {
803 stream->offset += rdbytes;
804 if (nbytes)
805 *nbytes = rdbytes;
806 }
807 return rc;
808 }
809
810
811 int
720 mu_stream_sequential_write (mu_stream_t stream, const char *buf, size_t size) 812 mu_stream_sequential_write (mu_stream_t stream, const char *buf, size_t size)
721 { 813 {
722 if (stream == NULL) 814 if (stream == NULL)
......