1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
/* Portions Copyright (C) 2001 artofcode LLC.
Portions Copyright (C) 1996, 2001 Artifex Software Inc.
Portions Copyright (C) 1988, 2000 Aladdin Enterprises.
This software is based in part on the work of the Independent JPEG Group.
All Rights Reserved.
This software is distributed under license and may not be copied, modified
or distributed except as expressly authorized under the terms of that
license. Refer to licensing information at http://www.artifex.com/ or
contact Artifex Software, Inc., 101 Lucas Valley Road #110,
San Rafael, CA 94903, (415)492-9861, for further information. */
/*$Id$ */
#include "string_.h"
#include "gdebug.h"
#include "gstypes.h"
#include "gsmemory.h"
#include "gsstruct.h"
#include "pltoputl.h"
/* -------------- Read stream cursor operations ---------- */
/* Initialize cursor */
int /* returns 0 ok, else -ve error code */
pl_top_cursor_init(
pl_top_cursor_t *cursor, /* cursor to init/open */
FILE *strm, /* open stream to read from */
byte *buffer, /* buffer to use for reading */
unsigned buffer_length /* length of *buffer */
)
{
int status;
cursor->strm = strm;
cursor->buffer = buffer;
cursor->buffer_length = buffer_length;
cursor->cursor.limit = cursor->cursor.ptr = buffer - 1;
cursor->status = 1; /* non-status */
status = pl_top_cursor_next(cursor);
return status < 0 ? status : 0; /* report errors, not EOF */
}
/* End of data condition. */
static bool
pl_cursor_EOD(pl_top_cursor_t *cursor)
{
/* NB review history of the second predicate. */
return (cursor->status <= 0 && cursor->cursor.ptr <= cursor->buffer);
}
void
pl_renew_cursor_status(pl_top_cursor_t *cursor)
{
if (pl_cursor_EOD(cursor)) {
cursor->status = 1;
}
}
/* Refill from input */
int /* rets 1 ok, else 0 EOF, -ve error */
pl_top_cursor_next(
pl_top_cursor_t *cursor /* cursor to operate on */
)
{
int len;
/* Declare EOF even if chars left in buffer if no chars were consumed */
if (pl_cursor_EOD(cursor))
return cursor->status;
/* Copy any remaining bytes to head of buffer */
len = cursor->cursor.limit - cursor->cursor.ptr;
if (len > 0)
memmove(cursor->buffer, cursor->cursor.ptr + 1, len);
cursor->cursor.ptr = cursor->buffer - 1;
cursor->cursor.limit = cursor->buffer + (len - 1);
/* Top off rest of buffer by reading stream */
if (cursor->status > 0 && len < cursor->buffer_length) {
cursor->status = fread((byte *)(cursor->cursor.limit + 1),
1, cursor->buffer_length - len,
cursor->strm);
if (cursor->status > 0)
cursor->cursor.limit += cursor->status;
}
/* Return success if there's anything in the buffer */
return cursor->cursor.limit == cursor->cursor.ptr ? cursor->status : 1;
}
/* Deinit a read cursor */
void
pl_top_cursor_dnit(
pl_top_cursor_t *cursor /* cursor to operate on */
)
{
return;
}
|