1/* $NetBSD$ */ 2 3/*++ 4/* NAME 5/* rec_streamlf 3 6/* SUMMARY 7/* record interface to stream-lf files 8/* SYNOPSIS 9/* #include <rec_streamlf.h> 10/* 11/* int rec_streamlf_get(stream, buf, maxlen) 12/* VSTREAM *stream; 13/* VSTRING *buf; 14/* int maxlen; 15/* 16/* int rec_streamlf_put(stream, type, data, len) 17/* VSTREAM *stream; 18/* int type; 19/* const char *data; 20/* int len; 21/* AUXILIARY FUNCTIONS 22/* int REC_STREAMLF_PUT_BUF(stream, type, buf) 23/* VSTREAM *stream; 24/* int type; 25/* VSTRING *buf; 26/* DESCRIPTION 27/* This module implements record I/O on top of stream-lf files. 28/* 29/* rec_streamlf_get() reads one record from the specified stream. 30/* The result is null-terminated and may contain embedded null 31/* characters. 32/* The \fImaxlen\fR argument specifies an upper bound to the amount 33/* of data read. The result is REC_TYPE_NORM when the record was 34/* terminated by newline, REC_TYPE_CONT when no terminating newline 35/* was found (the record was larger than \fImaxlen\fR characters or 36/* EOF was reached), and REC_TYPE_EOF when no data could be read or 37/* when an I/O error was detected. 38/* 39/* rec_streamlf_put() writes one record to the named stream. 40/* When the record type is REC_TYPE_NORM, a newline character is 41/* appended to the output. The result is the record type, or 42/* REC_TYPE_EOF in case of problems. 43/* 44/* REC_STREAMLF_PUT_BUF() is a wrapper for rec_streamlf_put() that 45/* makes it more convenient to output VSTRING buffers. 46/* REC_STREAMLF_PUT_BUF() is an unsafe macro that evaluates some 47/* arguments more than once. 48/* SEE ALSO 49/* record(3) typed records 50/* LICENSE 51/* .ad 52/* .fi 53/* The Secure Mailer license must be distributed with this software. 54/* AUTHOR(S) 55/* Wietse Venema 56/* IBM T.J. Watson Research 57/* P.O. Box 704 58/* Yorktown Heights, NY 10598, USA 59/*--*/ 60 61/* System library. */ 62 63#include <sys_defs.h> 64 65/* Utility library. */ 66 67#include <vstring.h> 68#include <vstream.h> 69 70/* Global library. */ 71 72#include <record.h> 73#include <rec_type.h> 74#include <rec_streamlf.h> 75 76/* rec_streamlf_get - read record from stream-lf file */ 77 78int rec_streamlf_get(VSTREAM *stream, VSTRING *buf, int maxlen) 79{ 80 int n = maxlen; 81 int ch; 82 83 /* 84 * If this one character ar a time code proves to be a performance 85 * bottleneck, switch to block search (memchr()) and to block move 86 * (memcpy()) operations. 87 */ 88 VSTRING_RESET(buf); 89 while (n-- > 0) { 90 if ((ch = VSTREAM_GETC(stream)) == VSTREAM_EOF) 91 return (VSTRING_LEN(buf) > 0 ? REC_TYPE_CONT : REC_TYPE_EOF); 92 if (ch == '\n') { 93 VSTRING_TERMINATE(buf); 94 return (REC_TYPE_NORM); 95 } 96 VSTRING_ADDCH(buf, ch); 97 } 98 VSTRING_TERMINATE(buf); 99 return (REC_TYPE_CONT); 100} 101 102/* rec_streamlf_put - write record to stream-lf file */ 103 104int rec_streamlf_put(VSTREAM *stream, int type, const char *data, int len) 105{ 106 if (len > 0) 107 (void) vstream_fwrite(stream, data, len); 108 if (type == REC_TYPE_NORM) 109 (void) VSTREAM_PUTC('\n', stream); 110 return (vstream_ferror(stream) ? REC_TYPE_EOF : type); 111} 112