agssim: don't accept wrong input when register name expected
[rofl0r-agsutils.git] / File.c
blob7dbe89709159f1a1989028da426e209eff593032
1 #include "File.h"
3 int AF_open(AF *f, const char* fn) {
4 f->b = &f->b_b;
5 ByteArray_ctor(f->b);
6 ByteArray_set_endian(f->b, BAE_LITTLE);
7 return ByteArray_open_file(f->b, fn);
10 void AF_close(AF* f) {
11 ByteArray_close_file(f->b);
14 ssize_t AF_read(AF* f, void* buf, size_t len) {
15 return ByteArray_readMultiByte(f->b, buf, len);
18 int AF_read_int(AF* f) {
19 return ByteArray_readInt(f->b);
22 unsigned AF_read_uint(AF* f) {
23 return ByteArray_readUnsignedInt(f->b);
26 short AF_read_short(AF* f) {
27 return ByteArray_readShort(f->b);
30 unsigned short AF_read_ushort(AF* f) {
31 return ByteArray_readUnsignedShort(f->b);
34 off_t AF_get_pos(AF* f) {
35 return ByteArray_get_position(f->b);
38 int AF_set_pos(AF* f, off_t x) {
39 return ByteArray_set_position(f->b, x);
42 int AF_dump_chunk_stream(AF* a, size_t start, size_t len, FILE* out) {
43 char buf[4096];
44 ByteArray_set_position(a->b, start);
45 while(len) {
46 size_t togo = len > sizeof(buf) ? sizeof(buf) : len;
47 if(togo != (size_t) ByteArray_readMultiByte(a->b, buf, togo)) {
48 return 0;
50 len -= togo;
51 char *p = buf;
52 while (togo) {
53 size_t n = fwrite(p, 1, togo, out);
54 if(!n) return 0;
55 p += n;
56 togo -= n;
59 return 1;
62 int AF_dump_chunk(AF* a, size_t start, size_t len, char* fn) {
63 FILE *out = fopen(fn, "w");
64 if(!out) return 0;
65 int ret = AF_dump_chunk_stream(a, start, len, out);
66 fclose(out);
67 return ret;
70 int AF_read_junk(AF* a, size_t l) {
71 /*char buf[512];
72 while(l) {
73 size_t togo = l > sizeof(buf) ? sizeof(buf) : l;
74 if(togo != (size_t) ByteArray_readMultiByte(a->b, buf, togo)) return 0;
75 l -= togo;
77 return 1;*/
78 return ByteArray_set_position(a->b, ByteArray_get_position(a->b) + l);
81 int AF_read_string(AF* a, char* buf, size_t max) {
82 size_t l = 0;
83 while(l < max) {
84 if(ByteArray_readMultiByte(a->b, buf + l, 1) != 1)
85 return 0;
86 if(!buf[l]) return 1;
87 l++;
89 return 0;