Ejemplo n.º 1
0
ssize_t
reclen(Recfmt_t f, const void* b, size_t n)
{
	register unsigned char*	s = (unsigned char*)b;
	register unsigned char*	e;
	size_t			h;
	size_t			z;

	switch (RECTYPE(f))
	{
	case REC_delimited:
		if (e = (unsigned char*)memchr(s, REC_D_DELIMITER(f), n))
			return e - s + 1;
		return 0;
	case REC_fixed:
		return REC_F_SIZE(f);
	case REC_variable:
		h = REC_V_HEADER(f);
		if (n < h)
			return 0;
		z = 0;
		s += REC_V_OFFSET(f);
		e = s + REC_V_LENGTH(f);
		if (REC_V_LITTLE(f))
			while (e > s)
				z = (z<<8)|*--e;
		else
			while (s < e)
				z = (z<<8)|*s++;
		if (!REC_V_INCLUSIVE(f))
			z += h;
		else if (z < h)
			z = h;
		return z;
	case REC_method:
		return -1;
	}
	return -1;
}
Ejemplo n.º 2
0
char*
fmtrec(Recfmt_t f, int fs)
{
	char*	b;
	char*	e;
	char*	s;
	long	n;
	char	del[2];

	b = s = fmtbuf(n = 32);
	e = b + n;
	switch (RECTYPE(f))
	{
	case REC_delimited:
		*s++ = 'd';
		if ((del[0] = REC_D_DELIMITER(f)) != '\n')
		{
			del[1] = 0;
			if (fs)
				sfsprintf(s, e - s, "0x%02x", *(unsigned char*)del);
			else
				sfsprintf(s, e - s, "%s", fmtquote(del, NiL, NiL, 1, 0));
		}
		else
			*s = 0;
		break;
	case REC_fixed:
		if (!fs)
			*s++ = 'f';
		sfsprintf(s, e - s, "%lu", REC_F_SIZE(f));
		break;
	case REC_variable:
		*s++ = 'v';
		if (n = REC_V_SIZE(f))
			s += sfsprintf(s, e - s, "%lu", n);
		if (REC_V_HEADER(f) != 4)
			s += sfsprintf(s, e - s, "h%u", REC_V_HEADER(f));
		if (REC_V_OFFSET(f) != 0)
			s += sfsprintf(s, e - s, "o%u", REC_V_OFFSET(f));
		if (REC_V_LENGTH(f) != 2)
			s += sfsprintf(s, e - s, "z%u", REC_V_LENGTH(f));
		if (REC_V_LITTLE(f) != 0)
			*s++ = 'l';
		if (REC_V_INCLUSIVE(f) == 0)
			*s++ = 'n';
		*s = 0;
		break;
	case REC_method:
		*s++ = 'm';
		switch (n = REC_M_INDEX(f))
		{
		case REC_M_data:
			sfsprintf(s, e - s, "data");
			break;
		case REC_M_path:
			sfsprintf(s, e - s, "path");
			break;
		default:
			sfsprintf(s, e - s, "%lu", n);
			break;
		}
		break;
	case REC_none:
		*s++ = 'n';
		*s = 0;
		break;
	default:
		sfsprintf(s, e - s, "u%u.0x%07x", RECTYPE(f), REC_U_ATTRIBUTES(f));
		break;
	}
	return b;
}