Commit b10caa79 b10caa79762f864e2d2eee70de62428f4cf93c2d by Alain Magloire

file_stream.c message.c include/private/message0.h

first attempt at message_write().
1 parent 62e07501
...@@ -87,7 +87,7 @@ int ...@@ -87,7 +87,7 @@ int
87 file_stream_create (stream_t *stream, const char *filename, int flags) 87 file_stream_create (stream_t *stream, const char *filename, int flags)
88 { 88 {
89 struct _file_stream *fs; 89 struct _file_stream *fs;
90 char *mode; 90 const char *mode;
91 int ret; 91 int ret;
92 92
93 if (stream == NULL || filename == NULL) 93 if (stream == NULL || filename == NULL)
......
...@@ -53,6 +53,11 @@ struct _message ...@@ -53,6 +53,11 @@ struct _message
53 event_t event; 53 event_t event;
54 size_t event_num; 54 size_t event_num;
55 55
56 /* for message_write */
57 char *hdr_buf;
58 size_t hdr_buflen;
59 int hdr_done;
60
56 int (*_from) __P ((message_t msg, char *, size_t, size_t *)); 61 int (*_from) __P ((message_t msg, char *, size_t, size_t *));
57 int (*_received) __P ((message_t msg, char *, size_t, size_t *)); 62 int (*_received) __P ((message_t msg, char *, size_t, size_t *));
58 63
......
...@@ -168,6 +168,7 @@ message_get_stream (message_t msg, stream_t *pstream) ...@@ -168,6 +168,7 @@ message_get_stream (message_t msg, stream_t *pstream)
168 stream_set_read (stream, message_read, msg); 168 stream_set_read (stream, message_read, msg);
169 stream_set_write (stream, message_write, msg); 169 stream_set_write (stream, message_write, msg);
170 stream_set_fd (stream, message_get_fd, msg); 170 stream_set_fd (stream, message_get_fd, msg);
171 stream_set_flags (stream, MU_STREAM_RDWR, msg);
171 msg->stream = stream; 172 msg->stream = stream;
172 } 173 }
173 174
...@@ -457,11 +458,74 @@ message_write (stream_t os, const char *buf, size_t buflen, ...@@ -457,11 +458,74 @@ message_write (stream_t os, const char *buf, size_t buflen,
457 off_t off, size_t *pnwrite) 458 off_t off, size_t *pnwrite)
458 { 459 {
459 message_t msg; 460 message_t msg;
460 (void)buf; (void)buflen; (void)off; (void)pnwrite; 461 int status;
461 462
462 if (os == NULL || (msg = os->owner) == NULL) 463 if (os == NULL || (msg = os->owner) == NULL)
463 return EINVAL; 464 return EINVAL;
464 return ENOSYS; 465
466 /* skip the obvious */
467 if (buf == NULL || *buf == '\0' || buflen == 0)
468 {
469 if (pnwrite)
470 *pnwrite = 0;
471 return 0;
472 }
473
474 if (!msg->hdr_done)
475 {
476 size_t len;
477 char *nl;
478 char *thdr;
479 while (!msg->hdr_done && (nl = memchr (buf, '\n', buflen)) != NULL)
480 {
481 len = nl - buf + 1;
482 thdr = realloc (msg->hdr_buf, msg->hdr_buflen + len);
483 if (thdr == NULL)
484 {
485 free (msg->hdr_buf);
486 msg->hdr_buf = NULL;
487 msg->hdr_buflen = 0;
488 return ENOMEM;
489 }
490 else
491 msg->hdr_buf = thdr;
492 memcpy (msg->hdr_buf + msg->hdr_buflen, buf, len);
493 msg->hdr_buflen += len;
494 if (buf == nl)
495 {
496 header_destroy (&(msg->header), msg);
497 status = header_create (&(msg->header), msg->hdr_buf,
498 msg->hdr_buflen, msg);
499 free (msg->hdr_buf);
500 msg->hdr_buf = NULL;
501 if (status != 0)
502 {
503 msg->hdr_buflen = 0;
504 return status;
505 }
506 msg->hdr_done = 1;
507 }
508 buf = nl + 1;
509 buflen -= len;
510 }
511 }
512 if (buflen)
513 {
514 stream_t bs;
515 body_t body;
516 if ((status = message_get_body (msg, &body)) != 0 ||
517 (status = body_get_stream (msg->body, &bs)) != 0)
518 {
519 free (msg->hdr_buf);
520 msg->hdr_buf = NULL;
521 msg->hdr_buflen = msg->hdr_done = 0;
522 return status;
523 }
524 if (off > (off_t)msg->hdr_buflen)
525 off -= msg->hdr_buflen;
526 return stream_write (bs, buf, buflen, off, pnwrite);
527 }
528 return 0;
465 } 529 }
466 530
467 static int 531 static int
......