shithub: pdffs

ref: b4e6b3b89646f4dd5973b770b36aba511843624e
dir: /array.c/

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

/* 7.3.6 Array Objects */

Object *
pdfarray(Pdf *pdf, Biobuf *b)
{
	Object *o, *m;
	Object **a;
	int c, noel;

	o = calloc(1, sizeof(*o));
	o->pdf = pdf;
	o->type = Oarray;
	Bgetc(b); /* throw away '[' */

	for(noel = 0;;){
		if((c = Bgetc(b)) < 0 || c == ']')
			break;
		if(noel){
			werrstr("no ']'");
			goto err;
		}

		Bungetc(b);
		if((m = pdfobj(pdf, b)) == nil){
			noel = 1;
			continue;
		}

		if((a = realloc(o->array.e, (o->array.ne+1)*sizeof(Object*))) == nil){
			pdfobjfree(m);
			goto err;
		}

		o->array.e = a;
		a[o->array.ne++] = m;
	}

	if(c != ']'){
		werrstr("no ']'");
		goto err;
	}

	return o;
err:
	werrstr("array: %r");
	pdfobjfree(o);
	return nil;
}