1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
18*4887Schin *                  David Korn <dgk@research.att.com>                   *
19*4887Schin *                   Phong Vo <kpv@research.att.com>                    *
20*4887Schin *                                                                      *
21*4887Schin ***********************************************************************/
22*4887Schin #pragma prototyped
23*4887Schin 
24*4887Schin /*
25*4887Schin  * return the length of the current record at b, size n, according to f
26*4887Schin  * -1 returned on error
27*4887Schin  *  0 returned if more data is required
28*4887Schin  */
29*4887Schin 
30*4887Schin #include <recfmt.h>
31*4887Schin #include <ctype.h>
32*4887Schin 
33*4887Schin ssize_t
34*4887Schin reclen(Recfmt_t f, const void* b, size_t n)
35*4887Schin {
36*4887Schin 	register unsigned char*	s = (unsigned char*)b;
37*4887Schin 	register unsigned char*	e;
38*4887Schin 	size_t			h;
39*4887Schin 	size_t			z;
40*4887Schin 
41*4887Schin 	switch (RECTYPE(f))
42*4887Schin 	{
43*4887Schin 	case REC_delimited:
44*4887Schin 		if (e = (unsigned char*)memchr(s, REC_D_DELIMITER(f), n))
45*4887Schin 			return e - s + 1;
46*4887Schin 		return 0;
47*4887Schin 	case REC_fixed:
48*4887Schin 		return REC_F_SIZE(f);
49*4887Schin 	case REC_variable:
50*4887Schin 		h = REC_V_HEADER(f);
51*4887Schin 		if (n < h)
52*4887Schin 			return 0;
53*4887Schin 		z = 0;
54*4887Schin 		s += REC_V_OFFSET(f);
55*4887Schin 		e = s + REC_V_LENGTH(f);
56*4887Schin 		if (REC_V_LITTLE(f))
57*4887Schin 			while (e > s)
58*4887Schin 				z = (z<<8)|*--e;
59*4887Schin 		else
60*4887Schin 			while (s < e)
61*4887Schin 				z = (z<<8)|*s++;
62*4887Schin 		if (!REC_V_INCLUSIVE(f))
63*4887Schin 			z += h;
64*4887Schin 		else if (z < h)
65*4887Schin 			z = h;
66*4887Schin 		return z;
67*4887Schin 	case REC_method:
68*4887Schin 		return -1;
69*4887Schin 	}
70*4887Schin 	return -1;
71*4887Schin }
72