1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
#pragma once
#include <al/str.h>
#include <al/lib.h>
#ifdef NAUNET_NEEDS_STDIO_ASSIST
// It seems meson adds _FILE_OFFSET_BITS=64 on all "linuxlike" compilers even MinGW.
// https://github.com/mesonbuild/meson/commit/853634a48da025c59eef70161dba0d150833f60d
// https://github.com/mesonbuild/meson/issues/12931
// I don't think we necessarily want this on 32-bit Windows.
#include <stdio.h>
#ifdef NAUNET_ON_WINDOWS
#if _FILE_OFFSET_BITS == 64
#define fseek _fseeki64
#define ftell _ftelli64
#endif
#else
#define fseek fseeko
#define ftell ftello
#endif
#else
#ifdef NAUNET_ON_WINDOWS
#else
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#endif
#endif
enum {
NNWT_FILE_READONLY = 1,
NNWT_FILE_CREATE = 1 << 1,
NNWT_FILE_LOCK = 1 << 2
};
enum {
NNWT_ENTRY_FILE = 0,
NNWT_ENTRY_DIR,
NNWT_ENTRY_OTHER
};
struct nn_file {
s32 flags;
#ifdef NAUNET_NEEDS_STDIO_ASSIST
FILE *file;
#else
#ifdef NAUNET_ON_WINDOWS
#else
s32 fd;
#endif
#endif
str path;
size_t size;
};
struct nn_dir {
#ifdef NAUNET_NEEDS_STDIO_ASSIST
#else
#ifdef NAUNET_ON_WINDOWS
#else
DIR *dir;
#endif
#endif
str path;
};
struct nn_dir_entry {
u8 type;
#ifdef NAUNET_NEEDS_STDIO_ASSIST
#else
#ifdef NAUNET_ON_WINDOWS
#else
struct dirent *entry;
#endif
#endif
};
bool nn_file_open(struct nn_file *file, str *path, s32 flags);
bool nn_file_exists(str *path);
bool nn_file_create(str *path);
bool nn_file_truncate(struct nn_file *file, off_t size);
bool nn_file_flush(struct nn_file *file);
off_t nn_file_seek(struct nn_file *file, off_t offset, s32 whence);
bool nn_file_write(struct nn_file *file, void *buf, size_t size);
bool nn_file_read(struct nn_file *file, void *buf, size_t size);
s32 nn_file_read_as_str(struct nn_file *file, str *out);
s32 nn_file_read_as_c_str(struct nn_file *file, char **out);
s32 nn_file_replace(struct nn_file *file, str *buf);
void *nn_file_mmap(struct nn_file *file);
void *nn_file_mremap(struct nn_file *file, size_t size, void *old_map);
void nn_file_munmap(struct nn_file *file, void *map);
void nn_file_close(struct nn_file *file);
bool nn_dir_open(struct nn_dir *dir, str *path);
void nn_dir_close(struct nn_dir *dir);
bool nn_dir_exists(str *path);
bool nn_dir_create(str *path);
bool nn_dir_read(struct nn_dir *dir, struct nn_dir_entry *entry);
void nn_dir_entry_get_name(struct nn_dir_entry *entry, str *name);
const char *nn_dir_entry_get_os_name(struct nn_dir_entry *entry);
void nn_dir_entry_get_path(struct nn_dir_entry *entry, struct nn_dir *dir, str *path);
|