From f2c827ff6b024d3af88ab68a052eb26d03aa6c0e Mon Sep 17 00:00:00 2001 From: _ <_> Date: Sun, 1 Apr 2018 23:45:30 -0700 Subject: [PATCH] Split main.c into separate files and set up directory structure --- Makefile | 14 +++++++++++ devil1geo.h => include/devil1geo.h | 0 devil1pld.h => include/devil1pld.h | 6 +++++ src/devil1pld.c | 37 ++++++++++++++++++++++++++++++ main.c => test/main.c | 30 ------------------------ 5 files changed, 57 insertions(+), 30 deletions(-) create mode 100644 Makefile rename devil1geo.h => include/devil1geo.h (100%) rename devil1pld.h => include/devil1pld.h (58%) create mode 100644 src/devil1pld.c rename main.c => test/main.c (76%) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bec6c8c --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +EX=devil1test +CC=gcc +CFLAGS= -I"include" + +all: main + +main: devil1pld.o + $(CC) $^ test/main.c $(CFLAGS) -o $(EX) + +devil1pld.o: src/devil1pld.c + $(CC) -c $^ $(CFLAGS) + +clean: + rm *.o $(EX) diff --git a/devil1geo.h b/include/devil1geo.h similarity index 100% rename from devil1geo.h rename to include/devil1geo.h diff --git a/devil1pld.h b/include/devil1pld.h similarity index 58% rename from devil1pld.h rename to include/devil1pld.h index 2f92cc8..2082afc 100644 --- a/devil1pld.h +++ b/include/devil1pld.h @@ -9,4 +9,10 @@ struct PldHeader { uint32_t *offsets; // }; +// input: a pld header struct. +void showpldh(struct PldHeader *); + +// input: contents of the .pld file. +struct PldHeader *getpldh(const char*); + #endif \ No newline at end of file diff --git a/src/devil1pld.c b/src/devil1pld.c new file mode 100644 index 0000000..5169a17 --- /dev/null +++ b/src/devil1pld.c @@ -0,0 +1,37 @@ +#include "devil1pld.h" +#include "stdio.h" +#include "stdlib.h" +#include "string.h" + +void printpldh(struct PldHeader *ph) { + if (ph == NULL) { + return; + } + printf("number of offsets = %i\n", ph -> numOffset); + unsigned int i; + for (i = 0; i < ph -> numOffset; i++) { + printf("offset %i = %x\n", i, ph -> offsets[i]); + } +} + +struct PldHeader *getpldh(const char *buffer) { + if (buffer == NULL) { + return NULL; + } + int32_t *n = (int32_t*)buffer; + struct PldHeader *ph = (struct PldHeader*)malloc( + sizeof(struct PldHeader) + ); + uint32_t size_offsets = sizeof(uint32_t) * n[0]; + ph -> offsets = (uint32_t*)malloc(size_offsets); + if (ph -> offsets == NULL) { + perror("Error 4: "); + free(ph); + free(ph -> offsets); + return NULL; + } + // set data of struct. + ph -> numOffset = n[0]; + memcpy(ph -> offsets, buffer + sizeof(int32_t), size_offsets); + return ph; +} diff --git a/main.c b/test/main.c similarity index 76% rename from main.c rename to test/main.c index ca27635..42020b5 100644 --- a/main.c +++ b/test/main.c @@ -44,36 +44,6 @@ void splitpld(const char *buffer, free(wbuffer); } -void showpldh(struct PldHeader *x) { - printf("number of offsets = %i\n", x -> numOffset); - unsigned int i; - for (i = 0; i < x -> numOffset; i++) { - printf("offset %i = %x\n", i, x -> offsets[i]); - } -} - -struct PldHeader *getpldh(const char *buffer) { - if (buffer == NULL) { - return NULL; - } - int32_t *n = (int32_t*)buffer; - struct PldHeader *ph = (struct PldHeader*)malloc( - sizeof(struct PldHeader) - ); - uint32_t size_offsets = sizeof(uint32_t) * n[0]; - ph -> offsets = (uint32_t*)malloc(size_offsets); - if (ph -> offsets == NULL) { - perror("Error 4: "); - free(ph); - free(ph -> offsets); - return NULL; - } - // set data of struct. - ph -> numOffset = n[0]; - memcpy(ph -> offsets, buffer + sizeof(int32_t), size_offsets); - return ph; -} - char *loadfile(const char *fname, unsigned int *s) { FILE *f = fopen(fname, "rb"); unsigned int size = 0; // number of elements to buffer;