Split main.c into separate files and set up directory structure

This commit is contained in:
_ 2018-04-01 23:45:30 -07:00
parent 97527f549c
commit f2c827ff6b
5 changed files with 57 additions and 30 deletions

14
Makefile Normal file
View File

@ -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)

View File

@ -9,4 +9,10 @@ struct PldHeader {
uint32_t *offsets; // <format=hex> uint32_t *offsets; // <format=hex>
}; };
// input: a pld header struct.
void showpldh(struct PldHeader *);
// input: contents of the .pld file.
struct PldHeader *getpldh(const char*);
#endif #endif

37
src/devil1pld.c Normal file
View File

@ -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;
}

View File

@ -44,36 +44,6 @@ void splitpld(const char *buffer,
free(wbuffer); 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) { char *loadfile(const char *fname, unsigned int *s) {
FILE *f = fopen(fname, "rb"); FILE *f = fopen(fname, "rb");
unsigned int size = 0; // number of elements to buffer; unsigned int size = 0; // number of elements to buffer;