134f9b3eeSRoland Mainz /***********************************************************************
234f9b3eeSRoland Mainz * *
334f9b3eeSRoland Mainz * This software is part of the ast package *
4*3e14f97fSRoger A. Faulkner * Copyright (c) 1985-2010 AT&T Intellectual Property *
534f9b3eeSRoland Mainz * and is licensed under the *
634f9b3eeSRoland Mainz * Common Public License, Version 1.0 *
734f9b3eeSRoland Mainz * by AT&T Intellectual Property *
834f9b3eeSRoland Mainz * *
934f9b3eeSRoland Mainz * A copy of the License is available at *
1034f9b3eeSRoland Mainz * http://www.opensource.org/licenses/cpl1.0.txt *
1134f9b3eeSRoland Mainz * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1234f9b3eeSRoland Mainz * *
1334f9b3eeSRoland Mainz * Information and Software Systems Research *
1434f9b3eeSRoland Mainz * AT&T Research *
1534f9b3eeSRoland Mainz * Florham Park NJ *
1634f9b3eeSRoland Mainz * *
1734f9b3eeSRoland Mainz * Glenn Fowler <gsf@research.att.com> *
1834f9b3eeSRoland Mainz * David Korn <dgk@research.att.com> *
1934f9b3eeSRoland Mainz * Phong Vo <kpv@research.att.com> *
2034f9b3eeSRoland Mainz * *
2134f9b3eeSRoland Mainz ***********************************************************************/
2234f9b3eeSRoland Mainz #pragma prototyped
2334f9b3eeSRoland Mainz
2434f9b3eeSRoland Mainz #include "stdhdr.h"
2534f9b3eeSRoland Mainz
2634f9b3eeSRoland Mainz ssize_t
getdelim(char ** sp,size_t * np,int delim,Sfio_t * f)2734f9b3eeSRoland Mainz getdelim(char** sp, size_t* np, int delim, Sfio_t* f)
2834f9b3eeSRoland Mainz {
2934f9b3eeSRoland Mainz ssize_t m;
3034f9b3eeSRoland Mainz ssize_t n;
3134f9b3eeSRoland Mainz ssize_t k;
3234f9b3eeSRoland Mainz ssize_t p;
3334f9b3eeSRoland Mainz uchar* s;
3434f9b3eeSRoland Mainz uchar* ps;
3534f9b3eeSRoland Mainz SFMTXDECL(f);
3634f9b3eeSRoland Mainz
3734f9b3eeSRoland Mainz STDIO_INT(f, "getdelim", ssize_t, (char**, size_t*, int, Sfio_t*), (sp, np, delim, f))
3834f9b3eeSRoland Mainz
3934f9b3eeSRoland Mainz SFMTXENTER(f, -1);
4034f9b3eeSRoland Mainz
4134f9b3eeSRoland Mainz if(delim < 0 || delim > 255 || !sp || !np) /* bad parameters */
4234f9b3eeSRoland Mainz SFMTXRETURN(f, -1);
4334f9b3eeSRoland Mainz
4434f9b3eeSRoland Mainz if(f->mode != SF_READ && _sfmode(f,SF_READ,0) < 0)
4534f9b3eeSRoland Mainz SFMTXRETURN(f, -1);
4634f9b3eeSRoland Mainz
4734f9b3eeSRoland Mainz SFLOCK(f,0);
4834f9b3eeSRoland Mainz
4934f9b3eeSRoland Mainz if(!(s = (uchar*)(*sp)) || (n = *np) < 0)
5034f9b3eeSRoland Mainz { s = NIL(uchar*); n = 0; }
5134f9b3eeSRoland Mainz for(m = 0;; )
5234f9b3eeSRoland Mainz { /* read new data */
5334f9b3eeSRoland Mainz if((p = f->endb - (ps = f->next)) <= 0 )
5434f9b3eeSRoland Mainz { f->getr = delim;
5534f9b3eeSRoland Mainz f->mode |= SF_RC;
5634f9b3eeSRoland Mainz if(SFRPEEK(f,ps,p) <= 0)
5734f9b3eeSRoland Mainz { m = -1;
5834f9b3eeSRoland Mainz break;
5934f9b3eeSRoland Mainz }
6034f9b3eeSRoland Mainz }
6134f9b3eeSRoland Mainz
6234f9b3eeSRoland Mainz for(k = 0; k < p; ++k) /* find the delimiter */
6334f9b3eeSRoland Mainz { if(ps[k] == delim)
6434f9b3eeSRoland Mainz { k += 1; /* include delim in copying */
6534f9b3eeSRoland Mainz break;
6634f9b3eeSRoland Mainz }
6734f9b3eeSRoland Mainz }
6834f9b3eeSRoland Mainz
6934f9b3eeSRoland Mainz if((m+k+1) >= n ) /* make sure there is space */
7034f9b3eeSRoland Mainz { n = ((m+k+15)/8)*8;
7134f9b3eeSRoland Mainz if(!(s = (uchar*)realloc(s, n)) )
7234f9b3eeSRoland Mainz { *sp = 0; *np = 0;
7334f9b3eeSRoland Mainz m = -1;
7434f9b3eeSRoland Mainz break;
7534f9b3eeSRoland Mainz }
7634f9b3eeSRoland Mainz *sp = (char*)s; *np = n;
7734f9b3eeSRoland Mainz }
7834f9b3eeSRoland Mainz
7934f9b3eeSRoland Mainz memcpy(s+m, ps, k); m += k;
8034f9b3eeSRoland Mainz f->next = ps+k; /* skip copied data in buffer */
8134f9b3eeSRoland Mainz
8234f9b3eeSRoland Mainz if(s[m-1] == delim)
8334f9b3eeSRoland Mainz { s[m] = 0; /* 0-terminated */
8434f9b3eeSRoland Mainz break;
8534f9b3eeSRoland Mainz }
8634f9b3eeSRoland Mainz }
8734f9b3eeSRoland Mainz
8834f9b3eeSRoland Mainz SFOPEN(f,0);
8934f9b3eeSRoland Mainz SFMTXRETURN(f,m);
9034f9b3eeSRoland Mainz }
9134f9b3eeSRoland Mainz
9234f9b3eeSRoland Mainz ssize_t
__getdelim(char ** sp,size_t * np,int delim,Sfio_t * f)9334f9b3eeSRoland Mainz __getdelim(char** sp, size_t* np, int delim, Sfio_t* f)
9434f9b3eeSRoland Mainz {
9534f9b3eeSRoland Mainz return getdelim(sp, np, delim, f);
9634f9b3eeSRoland Mainz }
97