bump version to 0.9.3
[rofl0r-agsutils.git] / File.c
blob082d014224b6d22a92564c37b81b8d7bab1369cf
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 long long AF_read_longlong(AF* f) {
19 return ByteArray_readUnsignedLongLong(f->b);
22 int AF_read_int(AF* f) {
23 return ByteArray_readInt(f->b);
26 unsigned AF_read_uint(AF* f) {
27 return ByteArray_readUnsignedInt(f->b);
30 short AF_read_short(AF* f) {
31 return ByteArray_readShort(f->b);
34 unsigned short AF_read_ushort(AF* f) {
35 return ByteArray_readUnsignedShort(f->b);
38 off_t AF_get_pos(AF* f) {
39 return ByteArray_get_position(f->b);
42 int AF_set_pos(AF* f, off_t x) {
43 return ByteArray_set_position(f->b, x);
46 int AF_dump_chunk_stream(AF* a, size_t start, size_t len, FILE* out) {
47 char buf[4096];
48 ByteArray_set_position(a->b, start);
49 while(len) {
50 size_t togo = len > sizeof(buf) ? sizeof(buf) : len;
51 if(togo != (size_t) ByteArray_readMultiByte(a->b, buf, togo)) {
52 return 0;
54 len -= togo;
55 char *p = buf;
56 while (togo) {
57 size_t n = fwrite(p, 1, togo, out);
58 if(!n) return 0;
59 p += n;
60 togo -= n;
63 return 1;
66 int AF_dump_chunk(AF* a, size_t start, size_t len, char* fn) {
67 FILE *out = fopen(fn, "w");
68 if(!out) return 0;
69 int ret = AF_dump_chunk_stream(a, start, len, out);
70 fclose(out);
71 return ret;
74 int AF_read_junk(AF* a, size_t l) {
75 /*char buf[512];
76 while(l) {
77 size_t togo = l > sizeof(buf) ? sizeof(buf) : l;
78 if(togo != (size_t) ByteArray_readMultiByte(a->b, buf, togo)) return 0;
79 l -= togo;
81 return 1;*/
82 return ByteArray_set_position(a->b, ByteArray_get_position(a->b) + l);
85 int AF_read_string(AF* a, char* buf, size_t max) {
86 size_t l = 0;
87 while(l < max) {
88 if(ByteArray_readMultiByte(a->b, buf + l, 1) != 1)
89 return 0;
90 if(!buf[l]) return 1;
91 l++;
93 return 0;