Name: Anonymous 2012-11-07 21:29
Why are old textbooks and manuals so boring? I'm not asking for w(p)gtr bullshite, but these books are written in such a matter-of-fact way it makes them more difficult to follow.
get FileName |c:[T:@4.utf8 L:@4.ul D:@L.y? @Xs] [[T D] @Xs,r]
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
typedef struct chunk {
uint8_t Tag[5];
uint32_t Len;
uint8_t *Data;
struct chunk *Next;
} chunk;
chunk *loadChunks(char *FileName) {
int I, L;
uint8_t *D, *P, *E;
chunk *C=0, *T, *N;
FILE *F = fopen(FileName, "r");
fseek(F, 0, SEEK_END);
L = ftell(F);
D = P = (uint8_t *)malloc(L);
E = P+L;
fseek(F, 0, SEEK_SET);
fread(D, 1, L, F);
fclose(F);
while (P < E) {
T = (chunk *)malloc(sizeof(chunk));
memcpy(T->Tag, P, 4);
T->Tag[4] = 0;
P += 4;
T->Len = *(uint32_t *)P;
P += 4;
T->Data = (uint8_t *)malloc(T->Len);
memcpy(T->Data, P, T->Len);
P += T->Len;
T->Next = C;
C = T;
}
for (T = 0; C; C = N) {
N = C->Next;
C->Next = T;
T = C;
}
free(D);
return T;
}