implement write to file, start docs

This commit is contained in:
Evan Burkey 2024-03-12 13:33:01 -07:00
parent 1347c0741b
commit 63e79490c9
8 changed files with 88 additions and 17 deletions

4
.gitignore vendored
View File

@ -1,6 +1,2 @@
cmake-build*
.cache
build
compile_commands.json
test
.idea

View File

@ -6,3 +6,22 @@ test:
- cmake ..
- make
- ./test
docs:
image: polinux/mkdocs
timeout: 5 minutes
rules:
- if: $CI_COMMIT_BRANCH == 'master'
script:
- apk add openssh-client
- mkdir -p ~/.ssh
- ssh-keyscan -H fputs.com >> ~/.ssh/known_hosts
- echo "$ssh_key" >> ~/.ssh/id_rsa
- chmod -R 700 ~/.ssh
- eval "$(ssh-agent -s)"
- ssh-add ~/.ssh/id_rsa
- mkdocs build
- ssh debian@fputs.com rm -rf /var/www/fputs/docs/spitwad
- ssh debian@fputs.com mkdir -p /var/www/fputs.com/docs/spitwad
- ssh debian@fputs.com chmod 755 /var/www/fputs.com/docs/spitwad
- scp -r ./site/* debian@fputs.com:/var/www/fputs.com/docs/spitwad/

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.25)
project(spitwad C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD 99)
add_library(spitwad STATIC spitwad.c)
if(${CMAKE_PROJECT_NAME} STREQUAL spitwad)

15
docs/index.md Normal file
View File

@ -0,0 +1,15 @@
# spitwad
`spitwad` is a library for interacting with WAD files, popularized by ID games like DOOM and Quake. The "defining"
feature is for WADs to be transmitted as data over the network and transformed to/from JSON (still working on that part!)
Originally built as a joke between some friends after we laughed about how it would be easier to use WADs to send data
between applications then JSON, then the idea for a WAD library that could transform WADs to/from JSON was born.
## Requirements
Should work on any POSIX platform out of the box
## Usage
Docs are a work in progress!

6
mkdocs.yml Normal file
View File

@ -0,0 +1,6 @@
site_name: spitwad
site_url: https://fputs.com/docs/spitwad
theme:
name: readthedocs
nav:
- 'index.md'

View File

@ -19,7 +19,6 @@ static uint16_t getshort(const unsigned char* data, size_t offset) {
static char* getstring(const unsigned char* data, size_t offset) {
char* s = malloc(sizeof(char) * 9);
strncpy(s, data + offset, 8);
s[8] = '\0';
return s;
}
@ -62,7 +61,8 @@ int new_WAD(struct WAD* wad, const unsigned char* data, size_t data_sz) {
wad->directory[j].length = getlong(data, i + 4);
char *s = getstring(data, i + 8);
strcpy(wad->directory[j].name, s);
strncpy(wad->directory[j].name, s, 8);
wad->directory[j].name[8] = '\0';
free(s);
}
@ -107,9 +107,17 @@ void destroy_WAD(struct WAD* wad) {
free(wad->data);
free(wad->directory);
free(wad);
wad = NULL;
}
int write_to_file(struct WAD* wad, const char* path) {
FILE *fp = NULL;
fp = fopen(path, "wb");
if (fp == NULL) {
fprintf(stderr, "Failed to open %s\n", path);
return 1;
}
fwrite(wad->data, sizeof(unsigned char), wad->data_sz, fp);
fclose(fp);
return 0;
}

View File

@ -1,5 +1,5 @@
#ifndef SPITWAD_SPITWAD_H
#define SPITWAD_SPITWAD_H
#ifndef SPITWAD_H
#define SPITWAD_H
#include <stdint.h>
#include <stdlib.h>
@ -29,4 +29,4 @@ int new_WAD_from_file(struct WAD* wad, const char* path);
void destroy_WAD(struct WAD* wad);
int write_to_file(struct WAD* wad, const char* path);
#endif //SPITWAD_SPITWAD_H
#endif //SPITWAD_H

37
test.c
View File

@ -1,18 +1,45 @@
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "spitwad.h"
int main() {
struct WAD *wad = malloc(sizeof(struct WAD));
assert(new_WAD_from_file(wad, "DOOM1.WAD") == 0);
// Values from manually inspecting DOOM1.WAD with a third-party tool
// Values from manually inspecting DOOM1.WAD with a third-party tool
void assert_doom1_wad(struct WAD *wad) {
assert(wad->type == IWAD);
assert(wad->dir_sz == 1264);
assert(wad->dir_offset == 4175796);
assert(wad->directory[0].offset == 12);
assert(wad->directory[0].length == 10752);
assert(strcmp(wad->directory[0].name, "PLAYPAL") == 0);
}
void basic_test() {
struct WAD *wad = malloc(sizeof(struct WAD));
assert(new_WAD_from_file(wad, "DOOM1.WAD") == 0);
assert_doom1_wad(wad);
destroy_WAD(wad);
wad = NULL;
}
void read_write_test() {
struct WAD *orig = malloc(sizeof(struct WAD));
assert(new_WAD_from_file(orig, "DOOM1.WAD") == 0);
assert(write_to_file(orig, "TEST.WAD") == 0);
struct WAD *new = malloc(sizeof(struct WAD));
assert(new_WAD_from_file(new, "TEST.WAD") == 0);
assert_doom1_wad(new);
destroy_WAD(orig);
orig = NULL;
destroy_WAD(new);
new = NULL;
remove("TEST.WAD");
}
int main() {
basic_test();
read_write_test();
}