ref: a080ae88c6c65503575da220cb131ddada107bf6
dir: /dict.c/
#include <u.h> #include <libc.h> #include <bio.h> #include "pdf.h" /* 7.3.7 Dictionary Objects */ Object * pdfdict(Biobuf *b) { Object *o, *k, *v; KeyValue *kv; int c, nokey; /* skip '<<' */ Bseek(b, 2, 1); k = v = nil; o = calloc(1, sizeof(*o)); o->type = Odict; for(nokey = 0;;){ if((c = Bgetc(b)) < 0) goto err; if(c == '>'){ if(Bgetc(b) == '>') break; werrstr("no '>>'"); goto err; } if(nokey){ werrstr("no '>>'"); goto err; } Bungetc(b); if((k = pdfobject(b)) == nil){ nokey = 1; continue; } if((v = pdfobject(b)) == nil) goto err; if(k->type != Oname){ werrstr("expected name as a key"); goto err; } if((kv = realloc(o->dict.kv, (o->dict.nkv+1)*sizeof(KeyValue))) == nil) goto err; o->dict.kv = kv; kv[o->dict.nkv].key = strdup(k->name); freeobject(k); kv[o->dict.nkv++].value = v; k = v = nil; } return o; err: freeobject(o); freeobject(k); freeobject(v); werrstr("dict: %r"); return nil; } Object * pdfdictget(Object *o, char *name) { int i; if(o == nil || o->type != Odict || name == nil) return nil; for(i = 0; i < o->dict.nkv && strcmp(name, o->dict.kv[i].key) != 0; i++); return i < o->dict.nkv ? o->dict.kv[i].value : nil; }