Blob
1 #include "stdinc.h"2 #include "dat.h"3 #include "fns.h"5 int6 readifile(IFile *f, char *name)7 {8 ZBlock *b;10 b = readfile(name);11 if(b == nil)12 return -1;13 f->name = name;14 f->b = b;15 f->pos = 0;16 return 0;17 }19 void20 freeifile(IFile *f)21 {22 freezblock(f->b);23 f->b = nil;24 f->pos = 0;25 }27 int28 partifile(IFile *f, Part *part, u64int start, u32int size)29 {30 ZBlock *b;32 b = alloczblock(size, 0);33 if(b == nil)34 return -1;35 if(readpart(part, start, b->data, size) < 0){36 seterr(EAdmin, "can't read %s: %r", part->name);37 freezblock(b);38 return -1;39 }40 f->name = part->name;41 f->b = b;42 f->pos = 0;43 return 0;44 }46 /*47 * return the next non-blank input line,48 * stripped of leading white space and with # comments eliminated49 */50 char*51 ifileline(IFile *f)52 {53 char *s, *e, *t;54 int c;56 for(;;){57 s = (char*)&f->b->data[f->pos];58 e = memchr(s, '\n', f->b->len - f->pos);59 if(e == nil)60 return nil;61 *e++ = '\0';62 f->pos = e - (char*)f->b->data;63 t = strchr(s, '#');64 if(t != nil)65 *t = '\0';66 for(; c = *s; s++)67 if(c != ' ' && c != '\t' && c != '\r')68 return s;69 }70 }72 int73 ifilename(IFile *f, char *dst)74 {75 char *s;77 s = ifileline(f);78 if(s == nil || strlen(s) >= ANameSize)79 return -1;80 namecp(dst, s);81 return 0;82 }84 int85 ifileu32int(IFile *f, u32int *r)86 {87 char *s;89 s = ifileline(f);90 if(s == nil)91 return -1;92 return stru32int(s, r);93 }