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