summaryrefslogtreecommitdiff
path: root/source/base/base_platform_linux.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/base/base_platform_linux.cpp')
-rw-r--r--source/base/base_platform_linux.cpp189
1 files changed, 189 insertions, 0 deletions
diff --git a/source/base/base_platform_linux.cpp b/source/base/base_platform_linux.cpp
new file mode 100644
index 0000000..8fb190a
--- /dev/null
+++ b/source/base/base_platform_linux.cpp
@@ -0,0 +1,189 @@
1
2internal String8 unix_get_env(const char *var) {
3
4 AssertLog(environ, "Environment not loaded");
5
6 String8 buffer = {0};
7 u64 index = 0;
8 char *current = environ[index];
9 while(environ[index] != NULL) {
10 if(CstringAnyMatch(environ[index], var)) {
11 String8 buffer = str8(environ[index]);
12
13 u64 equal_sign_pos = 0;
14 b32 found_equal_sign = str8_find(&buffer, 0, str8("="), &equal_sign_pos);
15
16 if(equal_sign_pos) {
17 buffer = str8_chop_left(buffer, equal_sign_pos);
18 }
19
20
21 } else {
22 Assert(0);
23 }
24 ++index;
25 }
26 return buffer;
27
28
29}
30
31
32
33//-
34internal void unix_load_shared_library(MemArena *arena, SharedLibrary *library) {
35
36 void *data = dlopen(library->name, RTLD_LAZY | RTLD_LOCAL);
37 AssertLog(data, "failed to load shared object %s", library->name); // Assert and print the error that get's reported to dlerror
38 library->mem = data;
39}
40
41internal void *unix_shared_library_get_proc_from_name(SharedLibrary *library, const char *name)
42{
43 void *symbol = dlsym(library->mem, name);
44 return symbol;
45}
46
47internal int unix_shared_library_close(SharedLibrary *library)
48{
49 int result = dlclose(library);
50 AssertLog(result, "failed to close the library");
51
52 return result;
53}
54
55//- implementations
56internal b32 unix_file_exists(String8 path) { return access((const char *)path.data, F_OK) == 0; }
57
58internal b32 unix_relative_path(MemArena *arena, String8 *path) {
59
60 String8 forward_slash = str8("./");
61 b32 insertion_result = str8_insert(arena, path, &forward_slash, 0);
62 if(!insertion_result) {
63 breakpoint();
64 }
65 return insertion_result;
66
67}
68
69internal String8 unix_file_load(MemArena *arena, String8 path)
70{
71 if (path.size <= 0) {
72 Log("Can't find the file we we're supposed to open. This is an issue because we we're supposed to break way earlier");
73 return {};
74 }
75
76 String8 result = {0};
77 struct stat sbuf = {0};
78
79 if (!path.data) {
80 Log("Invalid path file");
81 return str8_zero();
82 }
83
84
85 const char *null_terminated_path = null_terminate(arena, &path);
86
87 s32 file = open(null_terminated_path, O_RDONLY);
88 if (file == -1) {
89 Log("An error occurred. File was not loaded. error code: ", Str8Fmt , Str8Arg(path));
90 return str8_zero();
91 }
92
93 if (fstat(file, &sbuf) == -1) {
94 close(file);
95 return str8_zero();
96 }
97
98 result = PushString8(arena, (u64)sbuf.st_size);
99
100 u64 file_size = (u64)sbuf.st_size;
101 result.size = file_size;
102
103 u64 total_bytes_read = 0;
104
105 while(total_bytes_read < file_size) {
106
107 s64 bytes = read(file, result.data + total_bytes_read, file_size - total_bytes_read);
108 if (bytes == -1) {
109 Log("Failed, reading file was interrupted");
110 return str8_zero();
111 }
112
113 if(bytes == 0) break;
114 total_bytes_read += (u64)bytes;
115 }
116
117 result.size = total_bytes_read;
118
119 close(file);
120 return result;
121}
122
123// TODO: why was this returning a str8 before?
124internal b32 unix_file_write(String8 data, String8 path)
125{
126 s32 file = open((const char *)path.data, O_WRONLY | O_CREAT | O_TRUNC, 0644);
127 if (file == -1) {
128 return false;
129 }
130
131 u64 written = 0;
132 while (written < data.size) {
133 s64 err = write(file, data.data + written, data.size - written);
134 if (err == -1) {
135 close(file);
136 return false;
137 }
138 written += err;
139 }
140
141 close(file);
142 return true;
143}
144
145internal void unix_log_with_args(const char *format, ...)
146{
147 va_list arguments;
148 va_start(arguments, format);
149 vprintf(format, arguments);
150 fflush(stdout);
151 va_end(arguments);
152}
153
154internal void unix_os_exit() { _exit(0); }
155
156// TODO: rewrite this
157internal String8List *unix_open_dir(MemArena *arena, String8 *path)
158{
159 String8List *files_list = PushStructZero(arena, String8List);
160 files_list->first = &nil_str8_node;
161 files_list->last = &nil_str8_node;
162
163 cstring cpath = null_terminate(arena, path);
164 DIR *dir = opendir(cpath);
165 if (!dir) {
166 Log("opendir(%s) failed\n", cpath);
167 return files_list;
168 }
169
170 struct dirent *entry;
171 while ((entry = readdir(dir)) != NULL) {
172 if (entry->d_type != DT_REG) { // TODO: should symbolic files be handles aswell?
173 continue;
174 }
175
176 String8 name = str8(entry->d_name);
177 String8Node *file_node = PushStruct(arena, String8Node);
178 *file_node = ToString8Node(str8_clone(arena, &name));
179 str8_list_push_node(files_list, file_node);
180 }
181
182 FOR_STR8_NODE(files_list, current) {
183 Log("Logging files names");
184 LogStr8(current->value);
185 }
186
187 closedir(dir);
188 return files_list;
189} \ No newline at end of file