1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
21 ***********************************************************************/
24 /* Read a record delineated by a character.
25 ** The record length can be accessed via sfvalue(f).
27 ** Written by Kiem-Phong Vo
31 char* sfgetr(Sfio_t
*f
, int rc
, int type
)
33 char* sfgetr(f
,rc
,type
)
34 Sfio_t
* f
; /* stream to read from */
35 int rc
; /* record separator */
45 SFMTXENTER(f
, NIL(char*));
47 if(rc
< 0 || (f
->mode
!= SF_READ
&& _sfmode(f
,SF_READ
,0) < 0) )
48 SFMTXRETURN(f
, NIL(char*));
51 /* buffer to be returned */
52 rsrv
= NIL(Sfrsrv_t
*);
57 /* compatibility mode */
58 type
= type
< 0 ? SF_LASTR
: type
== 1 ? SF_STRING
: type
;
60 if(type
&SF_LASTR
) /* return the broken record */
61 { if((f
->flags
&SF_STRING
) && (un
= f
->endb
- f
->next
))
66 else if((rsrv
= f
->rsrv
) && (un
= -rsrv
->slen
) > 0)
74 { /* fill buffer if necessary */
75 if((n
= (ends
= f
->endb
) - (s
= f
->next
)) <= 0)
76 { /* for unseekable devices, peek-read 1 record */
80 /* fill buffer the conventional way */
81 if(SFRPEEK(f
,s
,n
) <= 0)
88 { s
= ends
[-1] == rc
? ends
-1 : ends
;
95 if(!(s
= (uchar
*)memchr((char*)s
,rc
,n
)))
103 if(s
< ends
) /* found separator */
104 { s
+= 1; /* include the separator */
108 (!(type
&SF_STRING
) || !(f
->flags
&SF_STRING
) ||
109 ((f
->flags
&SF_STRING
) && (f
->bits
&SF_BOTH
) ) ) )
110 { /* returning data in buffer */
118 /* amount to be read */
121 if(!found
&& (_Sfmaxr
> 0 && un
+n
+1 >= _Sfmaxr
|| (f
->flags
&SF_STRING
))) /* already exceed limit */
126 /* get internal buffer */
127 if(!rsrv
|| rsrv
->size
< un
+n
+1)
130 if((rsrv
= _sfrsrv(f
,un
+n
+1)) != NIL(Sfrsrv_t
*))
149 if(found
&& rc
!= 0 && (type
&SF_STRING
) )
151 if(us
>= f
->data
&& us
< f
->endb
)
157 /* prepare for a call to get the broken record */
159 rsrv
->slen
= found
? 0 : -un
;
163 if(us
&& (type
&SF_LOCKR
) )
164 { f
->mode
|= SF_PEEK
|SF_GETR
;
168 SFMTXRETURN(f
, (char*)us
);