55 lines
903 B
C
55 lines
903 B
C
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include "context.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
char* context_load(const char* path) {
|
|
if (!path) {
|
|
return NULL;
|
|
}
|
|
|
|
FILE* fp = fopen(path, "rb");
|
|
if (!fp) {
|
|
return NULL;
|
|
}
|
|
|
|
if (fseek(fp, 0, SEEK_END) != 0) {
|
|
fclose(fp);
|
|
return NULL;
|
|
}
|
|
|
|
long len = ftell(fp);
|
|
if (len < 0) {
|
|
fclose(fp);
|
|
return NULL;
|
|
}
|
|
|
|
if (fseek(fp, 0, SEEK_SET) != 0) {
|
|
fclose(fp);
|
|
return NULL;
|
|
}
|
|
|
|
char* buffer = (char*)malloc((size_t)len + 1U);
|
|
if (!buffer) {
|
|
fclose(fp);
|
|
return NULL;
|
|
}
|
|
|
|
size_t read_len = fread(buffer, 1, (size_t)len, fp);
|
|
fclose(fp);
|
|
|
|
if (read_len != (size_t)len) {
|
|
free(buffer);
|
|
return NULL;
|
|
}
|
|
|
|
buffer[len] = '\0';
|
|
return buffer;
|
|
}
|
|
|
|
void context_free(char* context) {
|
|
free(context);
|
|
}
|