annotate src/lib/istream.c @ 3241:b79853b4b005 HEAD

Replaced i_stream_get_size() with i_stream_stat(). Added i_stream_sync().
author Timo Sirainen <tss@iki.fi>
date Tue, 29 Mar 2005 13:28:06 +0300
parents a6af023db10d
children e05a1af4bbc7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1741
9df02b1533b3 Removed most of the license comments from src/lib/*.c. It's just fine to
Timo Sirainen <tss@iki.fi>
parents: 1401
diff changeset
1 /* Copyright (c) 2002-2003 Timo Sirainen */
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 #include "lib.h"
3241
b79853b4b005 Replaced i_stream_get_size() with i_stream_stat(). Added i_stream_sync().
Timo Sirainen <tss@iki.fi>
parents: 3240
diff changeset
4 #include "ioloop.h"
3240
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
5 #include "str.h"
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
6 #include "istream-internal.h"
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
7
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
8 void i_stream_ref(struct istream *stream)
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9 {
1027
29f09c05945d More type safety to i/o streams
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
10 _io_stream_ref(&stream->real_stream->iostream);
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11 }
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
12
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
13 void i_stream_unref(struct istream *stream)
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
14 {
3240
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
15 struct _istream *_stream = stream->real_stream;
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
16
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
17 if (_stream->iostream.refcount == 1) {
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
18 if (_stream->line_str != NULL)
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
19 str_free(_stream->line_str);
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
20 }
1027
29f09c05945d More type safety to i/o streams
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
21 _io_stream_unref(&stream->real_stream->iostream);
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
22 }
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
23
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
24 int i_stream_get_fd(struct istream *stream)
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
25 {
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
26 struct _istream *_stream = stream->real_stream;
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
27
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
28 return _stream->fd;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
29 }
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
30
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
31 void i_stream_close(struct istream *stream)
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
32 {
1027
29f09c05945d More type safety to i/o streams
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
33 _io_stream_close(&stream->real_stream->iostream);
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
34 stream->closed = TRUE;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
35 }
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
36
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
37 void i_stream_set_max_buffer_size(struct istream *stream, size_t max_size)
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
38 {
1027
29f09c05945d More type safety to i/o streams
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
39 _io_stream_set_max_buffer_size(&stream->real_stream->iostream,
29f09c05945d More type safety to i/o streams
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
40 max_size);
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
41 }
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
42
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
43 ssize_t i_stream_read(struct istream *stream)
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
44 {
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
45 struct _istream *_stream = stream->real_stream;
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
46
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
47 if (stream->closed)
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
48 return -1;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
49
2431
8e9d8cb1e674 Removed istream.disconnected, it's too much of a special case and the only
Timo Sirainen <tss@iki.fi>
parents: 2421
diff changeset
50 stream->eof = FALSE;
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
51 return _stream->read(_stream);
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
52 }
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
53
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
54 void i_stream_skip(struct istream *stream, uoff_t count)
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
55 {
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
56 struct _istream *_stream = stream->real_stream;
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
57 size_t data_size;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
58
1853
0fd7dbda6704 cleanup
Timo Sirainen <tss@iki.fi>
parents: 1846
diff changeset
59 data_size = _stream->pos - _stream->skip;
0fd7dbda6704 cleanup
Timo Sirainen <tss@iki.fi>
parents: 1846
diff changeset
60 if (count <= data_size) {
1870
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
61 /* within buffer */
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
62 stream->v_offset += count;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
63 _stream->skip += count;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
64 return;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
65 }
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
66
1870
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
67 /* have to seek forward */
1853
0fd7dbda6704 cleanup
Timo Sirainen <tss@iki.fi>
parents: 1846
diff changeset
68 count -= data_size;
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
69 _stream->skip = _stream->pos;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
70 stream->v_offset += data_size;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
71
1870
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
72 if (stream->closed)
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
73 return;
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
74
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
75 _stream->seek(_stream, stream->v_offset + count);
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
76 }
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
77
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
78 void i_stream_seek(struct istream *stream, uoff_t v_offset)
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
79 {
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
80 struct _istream *_stream = stream->real_stream;
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
81
2186
6eb96c8aaac3 seek optimization
Timo Sirainen <tss@iki.fi>
parents: 2165
diff changeset
82 if (v_offset >= stream->v_offset) {
1874
Timo Sirainen <tss@iki.fi>
parents: 1870
diff changeset
83 i_stream_skip(stream, v_offset - stream->v_offset);
Timo Sirainen <tss@iki.fi>
parents: 1870
diff changeset
84 return;
Timo Sirainen <tss@iki.fi>
parents: 1870
diff changeset
85 }
Timo Sirainen <tss@iki.fi>
parents: 1870
diff changeset
86
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
87 if (stream->closed)
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
88 return;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
89
2431
8e9d8cb1e674 Removed istream.disconnected, it's too much of a special case and the only
Timo Sirainen <tss@iki.fi>
parents: 2421
diff changeset
90 stream->eof = FALSE;
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
91 _stream->seek(_stream, v_offset);
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
92 }
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
93
3241
b79853b4b005 Replaced i_stream_get_size() with i_stream_stat(). Added i_stream_sync().
Timo Sirainen <tss@iki.fi>
parents: 3240
diff changeset
94 void i_stream_sync(struct istream *stream)
1870
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
95 {
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
96 struct _istream *_stream = stream->real_stream;
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
97
3241
b79853b4b005 Replaced i_stream_get_size() with i_stream_stat(). Added i_stream_sync().
Timo Sirainen <tss@iki.fi>
parents: 3240
diff changeset
98 if (!stream->closed && _stream->sync != NULL)
b79853b4b005 Replaced i_stream_get_size() with i_stream_stat(). Added i_stream_sync().
Timo Sirainen <tss@iki.fi>
parents: 3240
diff changeset
99 _stream->sync(_stream);
b79853b4b005 Replaced i_stream_get_size() with i_stream_stat(). Added i_stream_sync().
Timo Sirainen <tss@iki.fi>
parents: 3240
diff changeset
100 }
b79853b4b005 Replaced i_stream_get_size() with i_stream_stat(). Added i_stream_sync().
Timo Sirainen <tss@iki.fi>
parents: 3240
diff changeset
101
b79853b4b005 Replaced i_stream_get_size() with i_stream_stat(). Added i_stream_sync().
Timo Sirainen <tss@iki.fi>
parents: 3240
diff changeset
102 const struct stat *i_stream_stat(struct istream *stream)
b79853b4b005 Replaced i_stream_get_size() with i_stream_stat(). Added i_stream_sync().
Timo Sirainen <tss@iki.fi>
parents: 3240
diff changeset
103 {
b79853b4b005 Replaced i_stream_get_size() with i_stream_stat(). Added i_stream_sync().
Timo Sirainen <tss@iki.fi>
parents: 3240
diff changeset
104 struct _istream *_stream = stream->real_stream;
b79853b4b005 Replaced i_stream_get_size() with i_stream_stat(). Added i_stream_sync().
Timo Sirainen <tss@iki.fi>
parents: 3240
diff changeset
105
b79853b4b005 Replaced i_stream_get_size() with i_stream_stat(). Added i_stream_sync().
Timo Sirainen <tss@iki.fi>
parents: 3240
diff changeset
106 return _stream->stat(_stream);
1870
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
107 }
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
108
2788
f322b24da429 Added i_stream_have_bytes_left().
Timo Sirainen <tss@iki.fi>
parents: 2626
diff changeset
109 int i_stream_have_bytes_left(struct istream *stream)
f322b24da429 Added i_stream_have_bytes_left().
Timo Sirainen <tss@iki.fi>
parents: 2626
diff changeset
110 {
f322b24da429 Added i_stream_have_bytes_left().
Timo Sirainen <tss@iki.fi>
parents: 2626
diff changeset
111 struct _istream *_stream = stream->real_stream;
f322b24da429 Added i_stream_have_bytes_left().
Timo Sirainen <tss@iki.fi>
parents: 2626
diff changeset
112
f322b24da429 Added i_stream_have_bytes_left().
Timo Sirainen <tss@iki.fi>
parents: 2626
diff changeset
113 return !stream->eof || _stream->skip != _stream->pos;
f322b24da429 Added i_stream_have_bytes_left().
Timo Sirainen <tss@iki.fi>
parents: 2626
diff changeset
114 }
f322b24da429 Added i_stream_have_bytes_left().
Timo Sirainen <tss@iki.fi>
parents: 2626
diff changeset
115
3240
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
116 static char *i_stream_next_line_finish(struct _istream *stream, size_t i)
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
117 {
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
118 char *ret;
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
119 size_t end;
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
120
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
121 if (i > 0 && stream->buffer[i-1] == '\r')
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
122 end = i - 1;
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
123 else
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
124 end = i;
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
125
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
126 if (stream->w_buffer != NULL) {
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
127 /* modify the buffer directly */
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
128 stream->w_buffer[end] = '\0';
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
129 ret = (char *)stream->w_buffer + stream->skip;
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
130 } else {
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
131 /* use a temporary string to return it */
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
132 if (stream->line_str == NULL)
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
133 stream->line_str = str_new(default_pool, 256);
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
134 str_truncate(stream->line_str, 0);
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
135 str_append_n(stream->line_str, stream->buffer + stream->skip,
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
136 end - stream->skip);
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
137 ret = str_c_modifyable(stream->line_str);
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
138 }
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
139
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
140 i++;
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
141 stream->istream.v_offset += i - stream->skip;
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
142 stream->skip = i;
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
143 return ret;
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
144 }
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
145
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
146 char *i_stream_next_line(struct istream *stream)
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
147 {
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
148 struct _istream *_stream = stream->real_stream;
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
149 char *ret_buf;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
150 size_t i;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
151
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
152 i_assert(stream != NULL);
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
153
1846
f5d7407d33a0 reset stream_errno
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
154 if (_stream->skip >= _stream->pos) {
f5d7407d33a0 reset stream_errno
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
155 stream->stream_errno = 0;
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
156 return NULL;
1846
f5d7407d33a0 reset stream_errno
Timo Sirainen <tss@iki.fi>
parents: 1741
diff changeset
157 }
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
158
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
159 if (_stream->w_buffer == NULL) {
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
160 i_error("i_stream_next_line() called for unmodifyable stream");
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
161 return NULL;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
162 }
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
163
805
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 764
diff changeset
164 /* @UNSAFE */
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
165 ret_buf = NULL;
918
23b42ffe8756 Removed cr_lookup_pos optimization with i_stream_next_line(). It breaks if
Timo Sirainen <tss@iki.fi>
parents: 903
diff changeset
166 for (i = _stream->skip; i < _stream->pos; i++) {
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
167 if (_stream->buffer[i] == 10) {
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
168 /* got it */
3240
a6af023db10d i_stream_next_line() works now even if the stream buffer can't be directly
Timo Sirainen <tss@iki.fi>
parents: 2788
diff changeset
169 ret_buf = i_stream_next_line_finish(_stream, i);
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
170 break;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
171 }
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
172 }
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
173
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
174 return ret_buf;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
175 }
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
176
1293
2f2c6335ed6d Added i_stream_read_next_line()
Timo Sirainen <tss@iki.fi>
parents: 1027
diff changeset
177 char *i_stream_read_next_line(struct istream *stream)
2f2c6335ed6d Added i_stream_read_next_line()
Timo Sirainen <tss@iki.fi>
parents: 1027
diff changeset
178 {
2f2c6335ed6d Added i_stream_read_next_line()
Timo Sirainen <tss@iki.fi>
parents: 1027
diff changeset
179 char *line;
2f2c6335ed6d Added i_stream_read_next_line()
Timo Sirainen <tss@iki.fi>
parents: 1027
diff changeset
180
2f2c6335ed6d Added i_stream_read_next_line()
Timo Sirainen <tss@iki.fi>
parents: 1027
diff changeset
181 line = i_stream_next_line(stream);
2f2c6335ed6d Added i_stream_read_next_line()
Timo Sirainen <tss@iki.fi>
parents: 1027
diff changeset
182 if (line != NULL)
2f2c6335ed6d Added i_stream_read_next_line()
Timo Sirainen <tss@iki.fi>
parents: 1027
diff changeset
183 return line;
2f2c6335ed6d Added i_stream_read_next_line()
Timo Sirainen <tss@iki.fi>
parents: 1027
diff changeset
184
2f2c6335ed6d Added i_stream_read_next_line()
Timo Sirainen <tss@iki.fi>
parents: 1027
diff changeset
185 if (i_stream_read(stream) > 0)
2f2c6335ed6d Added i_stream_read_next_line()
Timo Sirainen <tss@iki.fi>
parents: 1027
diff changeset
186 line = i_stream_next_line(stream);
2f2c6335ed6d Added i_stream_read_next_line()
Timo Sirainen <tss@iki.fi>
parents: 1027
diff changeset
187 return line;
2f2c6335ed6d Added i_stream_read_next_line()
Timo Sirainen <tss@iki.fi>
parents: 1027
diff changeset
188 }
2f2c6335ed6d Added i_stream_read_next_line()
Timo Sirainen <tss@iki.fi>
parents: 1027
diff changeset
189
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
190 const unsigned char *i_stream_get_data(struct istream *stream, size_t *size)
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
191 {
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
192 struct _istream *_stream = stream->real_stream;
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
193
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
194 if (_stream->skip >= _stream->pos) {
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
195 *size = 0;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
196 return NULL;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
197 }
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
198
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
199 *size = _stream->pos - _stream->skip;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
200 return _stream->buffer + _stream->skip;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
201 }
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
202
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
203 unsigned char *i_stream_get_modifyable_data(struct istream *stream,
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
204 size_t *size)
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
205 {
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
206 struct _istream *_stream = stream->real_stream;
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
207
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
208 if (_stream->skip >= _stream->pos || _stream->w_buffer == NULL) {
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
209 *size = 0;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
210 return NULL;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
211 }
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
212
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
213 *size = _stream->pos - _stream->skip;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
214 return _stream->w_buffer + _stream->skip;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
215 }
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
216
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
217 int i_stream_read_data(struct istream *stream, const unsigned char **data,
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
218 size_t *size, size_t threshold)
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
219 {
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
220 ssize_t ret = 0;
2573
d10ad1e5a5aa i_stream_read_data(): return -1 if we didn't read more data, even if buffer
Timo Sirainen <tss@iki.fi>
parents: 2459
diff changeset
221 int read_more = FALSE;
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
222
2165
4c5732a9e2bc i_stream_read_data(): don't call read() unless we have to
Timo Sirainen <tss@iki.fi>
parents: 2160
diff changeset
223 do {
4c5732a9e2bc i_stream_read_data(): don't call read() unless we have to
Timo Sirainen <tss@iki.fi>
parents: 2160
diff changeset
224 *data = i_stream_get_data(stream, size);
4c5732a9e2bc i_stream_read_data(): don't call read() unless we have to
Timo Sirainen <tss@iki.fi>
parents: 2160
diff changeset
225 if (*size > threshold)
4c5732a9e2bc i_stream_read_data(): don't call read() unless we have to
Timo Sirainen <tss@iki.fi>
parents: 2160
diff changeset
226 return 1;
4c5732a9e2bc i_stream_read_data(): don't call read() unless we have to
Timo Sirainen <tss@iki.fi>
parents: 2160
diff changeset
227
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
228 /* we need more data */
1312
c893ba64db07 Fixes for resetting stream limits. Should fix some APPEND problems.
Timo Sirainen <tss@iki.fi>
parents: 1293
diff changeset
229 ret = i_stream_read(stream);
2573
d10ad1e5a5aa i_stream_read_data(): return -1 if we didn't read more data, even if buffer
Timo Sirainen <tss@iki.fi>
parents: 2459
diff changeset
230 if (ret > 0)
d10ad1e5a5aa i_stream_read_data(): return -1 if we didn't read more data, even if buffer
Timo Sirainen <tss@iki.fi>
parents: 2459
diff changeset
231 read_more = TRUE;
2452
963d95591b49 i_stream_read_data(): don't loop if read() returns 0.
Timo Sirainen <tss@iki.fi>
parents: 2431
diff changeset
232 } while (ret > 0);
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
233
2265
1def5ca25efd i_stream_read_data() could have returned invalid data at EOF (notable with
Timo Sirainen <tss@iki.fi>
parents: 2186
diff changeset
234 *data = i_stream_get_data(stream, size);
2165
4c5732a9e2bc i_stream_read_data(): don't call read() unless we have to
Timo Sirainen <tss@iki.fi>
parents: 2160
diff changeset
235 return ret == -2 ? -2 :
2626
a2b910e21225 i_stream_read_data(): return 0 if i_stream_read() returned 0.
Timo Sirainen <tss@iki.fi>
parents: 2573
diff changeset
236 (read_more || ret == 0 ? 0 : -1);
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
237 }
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
238
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
239 struct istream *_i_stream_create(struct _istream *_stream, pool_t pool, int fd,
1870
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
240 uoff_t abs_start_offset)
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
241 {
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
242 _stream->fd = fd;
1870
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
243 _stream->abs_start_offset = abs_start_offset;
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
244 _stream->istream.real_stream = _stream;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
245
3241
b79853b4b005 Replaced i_stream_get_size() with i_stream_stat(). Added i_stream_sync().
Timo Sirainen <tss@iki.fi>
parents: 3240
diff changeset
246 memset(&_stream->statbuf, 0, sizeof(_stream->statbuf));
b79853b4b005 Replaced i_stream_get_size() with i_stream_stat(). Added i_stream_sync().
Timo Sirainen <tss@iki.fi>
parents: 3240
diff changeset
247 _stream->statbuf.st_size = -1;
b79853b4b005 Replaced i_stream_get_size() with i_stream_stat(). Added i_stream_sync().
Timo Sirainen <tss@iki.fi>
parents: 3240
diff changeset
248 _stream->statbuf.st_atime =
b79853b4b005 Replaced i_stream_get_size() with i_stream_stat(). Added i_stream_sync().
Timo Sirainen <tss@iki.fi>
parents: 3240
diff changeset
249 _stream->statbuf.st_mtime =
b79853b4b005 Replaced i_stream_get_size() with i_stream_stat(). Added i_stream_sync().
Timo Sirainen <tss@iki.fi>
parents: 3240
diff changeset
250 _stream->statbuf.st_ctime = ioloop_time;
b79853b4b005 Replaced i_stream_get_size() with i_stream_stat(). Added i_stream_sync().
Timo Sirainen <tss@iki.fi>
parents: 3240
diff changeset
251
764
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
252 _io_stream_init(pool, &_stream->iostream);
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
253 return &_stream->istream;
f57c52738f90 Renamed IBuffer and OBuffer to IStream and OStream which describes their
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
254 }
1870
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
255
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
256 #ifdef STREAM_TEST
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
257 /* gcc istream.c -o teststream liblib.a -Wall -DHAVE_CONFIG_H -DSTREAM_TEST -g */
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
258
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
259 #include <fcntl.h>
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
260 #include <unistd.h>
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
261 #include "ostream.h"
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
262
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
263 #define BUF_VALUE(offset) \
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
264 (((offset) % 256) ^ ((offset) / 256))
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
265
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
266 static void check_buffer(const unsigned char *data, size_t size, size_t offset)
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
267 {
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
268 size_t i;
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
269
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
270 for (i = 0; i < size; i++)
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
271 i_assert(data[i] == BUF_VALUE(i+offset));
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
272 }
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
273
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
274 int main(void)
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
275 {
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
276 struct istream *input, *l_input;
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
277 struct ostream *output1, *output2;
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
278 int i, fd1, fd2;
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
279 unsigned char buf[1024];
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
280 const unsigned char *data;
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
281 size_t size;
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
282
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
283 lib_init();
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
284
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
285 fd1 = open("teststream.1", O_RDWR | O_CREAT | O_TRUNC, 0600);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
286 if (fd1 < 0)
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
287 i_fatal("open() failed: %m");
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
288 fd2 = open("teststream.2", O_RDWR | O_CREAT | O_TRUNC, 0600);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
289 if (fd2 < 0)
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
290 i_fatal("open() failed: %m");
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
291
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
292 /* write initial data */
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
293 for (i = 0; i < sizeof(buf); i++)
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
294 buf[i] = BUF_VALUE(i);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
295 write(fd1, buf, sizeof(buf));
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
296
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
297 /* test reading */
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
298 input = i_stream_create_file(fd1, default_pool, 512, FALSE);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
299 i_assert(i_stream_get_size(input) == sizeof(buf));
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
300
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
301 i_assert(i_stream_read_data(input, &data, &size, 0) > 0);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
302 i_assert(size == 512);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
303 check_buffer(data, size, 0);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
304
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
305 i_stream_seek(input, 256);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
306 i_assert(i_stream_read_data(input, &data, &size, 0) > 0);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
307 i_assert(size == 512);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
308 check_buffer(data, size, 256);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
309
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
310 i_stream_seek(input, 0);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
311 i_assert(i_stream_read_data(input, &data, &size, 512) == -2);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
312 i_assert(size == 512);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
313 check_buffer(data, size, 0);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
314
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
315 i_stream_skip(input, 900);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
316 i_assert(i_stream_read_data(input, &data, &size, 0) > 0);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
317 i_assert(size == sizeof(buf) - 900);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
318 check_buffer(data, size, 900);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
319
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
320 /* test moving data */
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
321 output1 = o_stream_create_file(fd1, default_pool, 512, FALSE);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
322 output2 = o_stream_create_file(fd2, default_pool, 512, FALSE);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
323
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
324 i_stream_seek(input, 1); size = sizeof(buf)-1;
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
325 i_assert(o_stream_send_istream(output2, input) == size);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
326 o_stream_flush(output2);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
327
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
328 lseek(fd2, 0, SEEK_SET);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
329 i_assert(read(fd2, buf, sizeof(buf)) == size);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
330 check_buffer(buf, size, 1);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
331
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
332 i_stream_seek(input, 0);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
333 o_stream_seek(output1, sizeof(buf));
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
334 i_assert(o_stream_send_istream(output1, input) == sizeof(buf));
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
335
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
336 /* test moving with limits */
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
337 l_input = i_stream_create_limit(default_pool, input,
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
338 sizeof(buf)/2, 512);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
339 i_stream_seek(l_input, 0);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
340 o_stream_seek(output1, 10);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
341 i_assert(o_stream_send_istream(output1, l_input) == 512);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
342
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
343 i_stream_set_max_buffer_size(input, sizeof(buf));
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
344
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
345 i_stream_seek(input, 0);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
346 i_assert(i_stream_read_data(input, &data, &size, sizeof(buf)-1) > 0);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
347 i_assert(size == sizeof(buf));
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
348 check_buffer(data, 10, 0);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
349 check_buffer(data + 10, 512, sizeof(buf)/2);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
350 check_buffer(data + 10 + 512,
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
351 size - (10 + 512), 10 + 512);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
352
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
353 /* reading within limits */
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
354 i_stream_seek(l_input, 0);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
355 i_assert(i_stream_read_data(l_input, &data, &size, 511) > 0);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
356 i_assert(size == 512);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
357 i_assert(i_stream_read_data(l_input, &data, &size, 512) == -2);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
358 i_assert(size == 512);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
359 i_stream_skip(l_input, 511);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
360 i_assert(i_stream_read_data(l_input, &data, &size, 0) > 0);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
361 i_assert(size == 1);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
362 i_stream_skip(l_input, 1);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
363 i_assert(i_stream_read_data(l_input, &data, &size, 0) == -1);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
364 i_assert(size == 0);
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
365
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
366 unlink("teststream.1");
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
367 unlink("teststream.2");
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
368 return 0;
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
369 }
c972ea085643 istream rewrite. instead of directly setting any limits to stream, you now
Timo Sirainen <tss@iki.fi>
parents: 1861
diff changeset
370 #endif