ref: a65b2474e954e41d6cc3b0d4542bc9da6bf108c1
dir: /array.c/
#include <u.h> #include <libc.h> #include "pdf.h" /* 7.3.6 Array Objects */ Object * pdfarray(Pdf *pdf, Stream *s) { Object *o, *m; int c, noel; o = arraynew(pdf); if(o == nil) return nil; Sgetc(s); /* throw away '[' */ for(noel = 0;;){ if((c = Sgetc(s)) < 0 || c == ']') break; if(noel){ werrstr("no ']'"); goto err; } Sungetc(s); if((m = pdfobj(pdf, s)) == nil){ noel = 1; continue; } if(!arrayadd(o, m)){ pdfobjfree(m); goto err; } } if(c != ']'){ werrstr("no ']'"); goto err; } return o; err: werrstr("array: %r"); pdfobjfree(o); return nil; } Object * arraynew(Pdf *pdf) { Object *o; o = calloc(1, sizeof(*o)); if(o == nil) return nil; o->pdf = pdf; o->type = Oarray; return o; } int arrayadd(Object *arr, Object *o) { Object **a; a = realloc(arr->array.e, (arr->array.ne+1)*sizeof(Object*)); if(a == nil) return 0; arr->array.e = a; a[arr->array.ne] = o; arr->array.ne += 1; return 1; } int arraylen(Object *o) { if(o == nil || o == &null) return 0; return (o->type == Oarray) ? o->array.ne : 1; } Object * arrayget(Object *o, int i) { if(arraylen(o) <= i) sysfatal("array: indexing out of range"); o = o->type == Oarray ? o->array.e[i] : o; return pdfeval(&o); } int arrayint(Object *o, int i) { return (o = arrayget(o, i))->type == Onum ? o->num.i : 0; }