summaryrefslogtreecommitdiff
path: root/source/base/base_os.h
diff options
context:
space:
mode:
Diffstat (limited to 'source/base/base_os.h')
-rw-r--r--source/base/base_os.h68
1 files changed, 68 insertions, 0 deletions
diff --git a/source/base/base_os.h b/source/base/base_os.h
new file mode 100644
index 0000000..4f84d93
--- /dev/null
+++ b/source/base/base_os.h
@@ -0,0 +1,68 @@
1#ifndef BASE_OS_H
2#define BASE_OS_H
3
4internal string8
5load_file(mem_arena *arena, const char *path)
6{
7 string8 result = {0};
8 struct stat sbuf = {0};
9
10 // TODO(nasr): abstract this to a platform layer
11 s32 file = open(path, O_RDONLY);
12 if(file == -1)
13 {
14 warn("fialed to open file. path could be invalid");
15 return (string8){0};
16 }
17
18 if(fstat(file, &sbuf) == -1)
19 {
20 warn("error: fstat failed");
21 close(file);
22 return (string8){0};
23 }
24
25
26 result = PushString(arena, sbuf.st_size);
27
28 result.size = (u64)sbuf.st_size;
29 if(result.size != 0)
30 {
31 result.data = (u8 *)mmap(0, result.size, PROT_READ, MAP_PRIVATE, file, 0);
32 }
33
34 close(file);
35 return result;
36}
37
38internal string8
39write_file(const char *path, string8 data)
40{
41
42 string8 result = {0};
43 s32 file = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
44 if(file == -1)
45 {
46 warn("failed to open file for writing. path could be invalid");
47 return (string8){0};
48 }
49
50 u64 written = 0;
51 while(written < data.size)
52 {
53 s64 err = write(file, data.data + written, data.size - written);
54 if(err == -1)
55 {
56 warn("write syscall failed");
57 close(file);
58 return (string8){0};
59 }
60 written += err;
61 }
62
63 close(file);
64 result = data;
65 return result;
66}
67
68#endif /* BASE_OS_H */