shithub: pdffs

ref: f8f7ffe655e3a6439adf0614d0232e7520757566
dir: /object.c/

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

Object *
pdfobject(char *p, char **e, int len)
{
	Object *o;
	int sz;

	o = nil;
	for(; len > 0 && isws(*p); p++, len--);
	if(len < 2){
		werrstr("too short");
		goto err;
	}

	if(*p < 1){
		werrstr("unexpected non-ascii char");
		goto err;
	}

	switch(*p){
	case '<': /* dictionary or a string */
		if(len < 2){
			werrstr("too short");
			goto err;
		}
		if(p[1] == '<'){ /* dictionary */
			o = pdfdict(p, e, len);
			break;
		}
		/* fall through */

	case '(': /* string */
		if((sz = pdfstring(p, e, len)) < 0)
			goto err;
		if((o = malloc(sizeof(*o)+sz+1)) != nil){
			o->type = Ostr;
			o->str = (char*)(o+1);
			strcpy(o->str, p);
		}
		break;

	case '/':
		if((sz = pdfname(p, e, len)) < 0)
			goto err;
		if((o = malloc(sizeof(*o)+sz+1)) != nil){
			o->type = Oname;
			o->str = (char*)(o+1);
			strcpy(o->str, p);
		}
		break;

	default:
		if(isdigit(*p)){
			o->type = Onum;
			o->num = strtod(p, e);
			break;
		}
		werrstr("unexpected char %c", *p);
		goto err;
	}

	if(o != nil)
		return o;
err:
	werrstr("object: %r");
	freeobject(o);
	return nil;
}

void
freeobject(Object *o)
{
	if(o == nil)
		return;
	free(o);
}