ref: bb10512127f96aeb40f9f1e8f32768032c6425fe
dir: /libvcard/vcard.c/
#include <u.h> #include <libc.h> #include "vcard.h" extern Vcard *vcparsecard; extern char *vcparsestr; extern int yyparse(void); static Vcard* parse(char *s) { memset(&vcstate, sizeof vcstate, 0); vcparsestr = s; vcparsecard = nil; yyparse(); return vcparsecard; } static void fold(char *str) { char *s; char *end; end = strchr(str, 0); while (str < end) { s = strchr(str, '\r'); if (!s) break; if (s[1] == 0) break; if (s[1] == '\n' && (s[2] == ' ' || s[2] == '\t')) { memmove(s, s+3, end-s-3); end -= 3; } str = s+1; } *end = 0; } #ifdef TEST void _vc_t_fold(char *s) { fold(s); } #endif Vcard* vcparse(char *s) { fold(s); return parse(s); } Vcard* vcparsefile(char *file) { int fd, step; char *s, *t; long n; Vcard *vc; fd = open(file, OREAD); if (fd < 0) return nil; step = 1; s = mallocz(8192, 1); t = s; while ((n = read(fd, t, 8191)) > 0) { t += n; step++; s = realloc(s, 8192 * step); memset(s + 8192*(step-1), 0, 8192); } close(fd); vc = vcparse(s); free(s); return vc; }