shithub: pdffs

ref: 474117ed563f8f84f11d3dcf90635c584be29ec1
dir: pdffs/misc.c

View raw version
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "pdf.h"

static char *otypes[] = {
	[Obool] = "bool",
	[Onum] = "num",
	[Ostr] = "str",
	[Oname] = "name",
	[Oarray] = "array",
	[Odict] = "dict",
	[Ostream] = "stream",
	[Onull] = "null",
	[Oindir] = "indir",
};

static char *xtypes[] = {
	[Xusual] = "usual",
	[Xuncompressed] = "uncompressed",
	[Xcompressed] = "compressed",
};

Object null = {
	.type = Onull,
};

int
Tfmt(Fmt *f)
{
	Object *o;

	o = va_arg(f->args, Object*);
	if(o == nil || o == &null)
		return fmtprint(f, "null");
	if(o->type < 0 || o->type >= nelem(otypes))
		return fmtprint(f, "????");
	return fmtprint(f, "%s", otypes[o->type]);
}

int
⊗fmt(Fmt *f)
{
	Xref x;

	x = va_arg(f->args, Xref);

	switch(x.type){
	case Xusual:
		return fmtprint(f, "<%s id=%d gen=%d off=%d>", xtypes[x.type], x.id, x.gen, x.off);
	case Xuncompressed:
		return fmtprint(f, "<%s gen=%d off=%d>", xtypes[x.type], x.gen, x.off);
	case Xcompressed:
		return fmtprint(f, "<%s id=%d objnum=%d>", xtypes[x.type], x.id, x.objnum);
	}
	return -1;
}

int
isws(int c)
{
	return /* \0 is missing on purpose */
		c == '\t' || c == '\n' || c == '\f' || c == '\r' ||
		c == ' ';
}

int
isdelim(int c)
{
	return
		c == '(' || c == ')' || c == '<' || c == '>' ||
		c == '[' || c == ']' || c == '{' || c == '}' ||
		c == '/' || c == '%';
}

int
Bgetint(Biobuf *b, int *i)
{
	double d;

	if(Bgetd(b, &d) != 1 || isNaN(d))
		return -1;
	*i = d;

	return 1;
}