ifile.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "stdinc.h"
  2. #include "dat.h"
  3. #include "fns.h"
  4. int
  5. readIFile(IFile *f, char *name)
  6. {
  7. ZBlock *b;
  8. b = readFile(name);
  9. if(b == nil)
  10. return 0;
  11. f->name = name;
  12. f->b = b;
  13. f->pos = 0;
  14. return 1;
  15. }
  16. void
  17. freeIFile(IFile *f)
  18. {
  19. freeZBlock(f->b);
  20. f->b = nil;
  21. f->pos = 0;
  22. }
  23. int
  24. partIFile(IFile *f, Part *part, u64int start, u32int size)
  25. {
  26. ZBlock *b;
  27. b = allocZBlock(size, 0);
  28. if(b == nil)
  29. return 0;
  30. if(!readPart(part, start, b->data, size)){
  31. setErr(EAdmin, "can't read %s: %R", part->name);
  32. freeZBlock(b);
  33. return 0;
  34. }
  35. f->name = part->name;
  36. f->b = b;
  37. f->pos = 0;
  38. return 1;
  39. }
  40. /*
  41. * return the next non-blank input line,
  42. * stripped of leading white space and with # comments eliminated
  43. */
  44. char*
  45. ifileLine(IFile *f)
  46. {
  47. char *s, *e, *t;
  48. int c;
  49. for(;;){
  50. s = (char*)&f->b->data[f->pos];
  51. e = memchr(s, '\n', f->b->len - f->pos);
  52. if(e == nil)
  53. return nil;
  54. *e++ = '\0';
  55. f->pos = e - (char*)f->b->data;
  56. t = strchr(s, '#');
  57. if(t != nil)
  58. *t = '\0';
  59. for(; c = *s; s++)
  60. if(c != ' ' && c != '\t' && c != '\r')
  61. return s;
  62. }
  63. }
  64. int
  65. ifileName(IFile *f, char *dst)
  66. {
  67. char *s;
  68. s = ifileLine(f);
  69. if(s == nil || strlen(s) >= ANameSize)
  70. return 0;
  71. nameCp(dst, s);
  72. return 1;
  73. }
  74. int
  75. ifileU32Int(IFile *f, u32int *r)
  76. {
  77. char *s;
  78. s = ifileLine(f);
  79. if(s == nil)
  80. return 0;
  81. return strU32Int(s, r);
  82. }