#include "file.h" void nn_path_append(str *path, str *part) { #ifdef NAUNET_ON_WINDOWS al_str_cat(path, &al_str_c("\\")); #else al_str_cat(path, &al_str_c("/")); #endif al_str_cat(path, part); } void nn_path_prepend(str *path, str *part) { #ifdef NAUNET_ON_WINDOWS al_str_prepend(path, &al_str_c("\\")); #else al_str_prepend(path, &al_str_c("/")); #endif al_str_prepend(path, part); } s32 nn_file_read_as_str(struct nn_file *file, str *out) { size_t size = file->size; al_str_sized(out, size); if (!nn_file_read(file, (void *)out->data, size)) { al_str_free(out); return -1; } out->length = size; return size; } s32 nn_file_read_as_c_str(struct nn_file *file, char **out) { size_t size = file->size; char *c_str = (char *)al_malloc(size + 1); if (!nn_file_read(file, c_str, size)) { al_free(*out); return -1; } c_str[size] = '\0'; *out = c_str; return size; } s32 nn_file_replace(struct nn_file *file, str *buf) { if ((nn_file_seek(file, 0, SEEK_SET) == (off_t)-1) || (!nn_file_write(file, buf->data, buf->length)) || (!nn_file_truncate(file, buf->length))) { return -1; } return buf->length; }