lib3ddevil1/test/main.c

115 lines
3.3 KiB
C
Raw Normal View History

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "devil1pld.h"
#include "devil1tex.h"
2018-04-02 07:44:58 +05:30
bool writedata(const char *basename,
unsigned int id,
const char *data,
unsigned int dsize) {
bool status = false;
char *fn = (char*)malloc(sizeof(char) * strlen(basename) + 4);
sprintf(fn, "%s_%d", basename, id);
FILE *out = fopen(fn, "w");
if (out != NULL) {
fwrite(data, sizeof(char), dsize, out);
fclose(out);
status = true;
}
free(fn);
return status;
}
void splitpld(const char *filedata,
unsigned int filesize,
const struct PldHeader *ph,
const char *name) {
char *wbuffer = NULL; // write buffer that will change.
unsigned int wsize = 0; // size of wbuffer.
unsigned int i;
2018-04-02 07:44:58 +05:30
unsigned int start = 0; // ending offset for data
unsigned int end = 0; // ending offset for data
for (i = 0; (i + 1) < (ph -> numOffset) + 1; i++) {
start = ph -> offsets[i];
// the last offset still has some data until the end of file
end = (i == (ph -> numOffset) - 1) ?
filesize : ph -> offsets[i + 1];
// copy sector to write buffer
2018-04-02 07:44:58 +05:30
wsize = end - start;
wbuffer = (char*)malloc(sizeof(char) * wsize);
memcpy(wbuffer, filedata + start, wsize);
// create a file.
2018-04-02 07:44:58 +05:30
writedata(name, i, wbuffer, wsize);
free(wbuffer);
}
}
2018-04-02 11:54:14 +05:30
char *loadfile(const char *fname, unsigned int *s) {
FILE *f = fopen(fname, "rb");
unsigned int size = 0; // number of elements to buffer;
unsigned int rcnt = 0; // number of char's read by fread(...)
if (f == NULL) {
perror("Error 1: ");
return NULL;
}
// this method of determining file size doesn't work until 2 GB.
fseek(f, 0, SEEK_END);
size = ftell(f);
rewind(f);
char *buf = (char*)malloc(sizeof(char) * size);
2018-04-02 05:49:39 +05:30
if (buf == NULL) {
perror("Error 2: ");
2018-04-02 05:49:39 +05:30
free(buf);
return NULL;
}
2018-04-02 05:49:39 +05:30
rcnt = fread(buf, sizeof(char), size, f);
if (rcnt < size) {
perror("Error 3: ");
2018-04-02 05:49:39 +05:30
free(buf);
return NULL;
}
fclose(f);
2018-04-02 07:44:58 +05:30
*s = rcnt;
return buf;
}
2018-04-02 13:57:21 +05:30
bool unpackpld (const char *filedata,
unsigned int filesize,
const char *filename) {
2018-04-02 11:54:14 +05:30
bool status = false;
2018-04-02 13:57:21 +05:30
printf("fsize is %i\n", filesize);
if (filedata != NULL && filesize != 0) {
2018-04-02 11:54:14 +05:30
struct PldHeader *pldh = getpldh(filedata);
splitpld(filedata, filesize, pldh, filename);
2018-04-02 13:57:21 +05:30
destroypldh(pldh);
2018-04-02 11:54:14 +05:30
status = true;
}
2018-04-02 11:54:14 +05:30
return status;
}
2018-04-02 11:54:14 +05:30
int main(int argc, char ** argv) {
char *f = argv[1];
2018-04-02 13:57:21 +05:30
unsigned int bufsize = 0;
char *buffer = loadfile(f, &bufsize);
// unpackpld(buffer, bufsize, f);
struct TexturePack *tp = (struct TexturePack*)malloc
(sizeof(struct TexturePack));
printf("%d %d %d %d \n", buffer[0],
buffer[1],
buffer[2],
buffer[3]);
gettph(tp, buffer);
printf("%x %x \n", buffer + 0, tp -> id[0]);
printtph(tp);
free(tp);
2018-04-02 13:57:21 +05:30
free(buffer);
return 0;
}