beginning drafting of tcp server

This commit is contained in:
2025-08-18 20:34:25 -05:00
parent 8b5fac0f3f
commit a9efa2dd52
9 changed files with 192 additions and 25 deletions

View File

@@ -1,9 +1,33 @@
#include <cstdio>
#include <assert.h>
#include <cstring>
#include <stdlib.h>
#include "util.hpp"
template<class T> void append(array<T>& a, T el) {
if (a.len == a.cap) {
resize(a, a.cap*2);
}
a[a.len] = el;
a.len++;
}
template<class T> T pop(array<T>& a) {
assert(a.len >= 1);
a.len--;
return a.data[a.len+1];
}
template<class T> void resize(array<T> &a, size_t new_cap) {
T* new_data = (T*)malloc(new_cap);
memcpy(new_data, a.data, min(a.len, new_cap));
free(a.data);
a.len = min(a.len, new_cap);
a.cap = new_cap;
}
bool read_file(array<char>* out, const char* filepath) {
FILE* fp = NULL;
if (fopen_s(&fp, filepath, "rb") != 0) {
@@ -15,7 +39,7 @@ bool read_file(array<char>* out, const char* filepath) {
size_t sz = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *data = (char*)malloc(sizeof(char)*sz);
char* data = (char*)malloc(sizeof(char) * sz);
fread(data, sizeof(char), sz, fp);
fclose(fp);
out->data = data;