summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c12
-rw-r--r--os-posix.c (renamed from filesystem-posix.c)25
-rw-r--r--os-win.c (renamed from filesystem-win.c)22
-rw-r--r--os.h (renamed from filesystem.h)36
-rw-r--r--time.c92
-rw-r--r--util.c22
-rw-r--r--util.h4
7 files changed, 107 insertions, 106 deletions
diff --git a/main.c b/main.c
index 9ae5b68..72a9ff7 100644
--- a/main.c
+++ b/main.c
@@ -7,6 +7,7 @@
- check LSP process status (TEST: what happens if LSP server is not installed)
- make tags_dir the root folder
- check that tags still works
+- do we need higher than 1-second resolution in time_last_modified on windows?
- TESTING: make rust-analyzer-slow (waits 10s before sending response)
- TESTING: check all IDE features with different servers
- run everything through valgrind ideally with leak checking
@@ -89,22 +90,15 @@ no_warn_end
#include "util.c"
#if _WIN32
-#include "filesystem-win.c"
-#elif __unix__
-#include "filesystem-posix.c"
-#else
-#error "Unrecognized operating system."
-#endif
-
-#if _WIN32
+#include "os-win.c"
#include "process-win.c"
#elif __unix__
+#include "os-posix.c"
#include "process-posix.c"
#else
#error "Unrecognized operating system."
#endif
-#include "time.c"
#include "ted.h"
#include "gl.c"
#include "text.c"
diff --git a/filesystem-posix.c b/os-posix.c
index 5506278..68e1d4f 100644
--- a/filesystem-posix.c
+++ b/os-posix.c
@@ -1,10 +1,11 @@
-#include "filesystem.h"
+#include "os.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
+#include <time.h>
static FsType statbuf_path_type(const struct stat *statbuf) {
if (S_ISREG(statbuf->st_mode))
@@ -109,3 +110,25 @@ int fs_get_cwd(char *buf, size_t buflen) {
return -1;
}
}
+
+struct timespec time_last_modified(char const *filename) {
+ struct stat statbuf = {0};
+ stat(filename, &statbuf);
+ return statbuf.st_mtim;
+}
+
+struct timespec time_get(void) {
+ struct timespec ts = {0};
+ clock_gettime(CLOCK_REALTIME, &ts);
+ return ts;
+}
+
+void time_sleep_ns(u64 ns) {
+ struct timespec rem = {0}, req = {
+ (time_t)(ns / 1000000000),
+ (long)(ns % 1000000000)
+ };
+
+ while (nanosleep(&req, &rem) == EINTR) // sleep interrupted by signal
+ req = rem;
+}
diff --git a/filesystem-win.c b/os-win.c
index dc17eaf..df32cdf 100644
--- a/filesystem-win.c
+++ b/os-win.c
@@ -2,6 +2,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
+#include <sysinfoapi.h>
static FsType windows_file_attributes_to_type(DWORD attrs) {
if (attrs == INVALID_FILE_ATTRIBUTES)
@@ -104,3 +105,24 @@ int fs_get_cwd(char *buf, size_t buflen) {
return 0;
return 1;
}
+
+struct timespec time_last_modified(char const *filename) {
+ // windows' _stat does not have st_mtim
+ struct _stat statbuf = {0};
+ struct timespec ts = {0};
+ _stat(filename, &statbuf);
+ ts.tv_sec = statbuf.st_mtime;
+ return ts;
+}
+
+
+struct timespec time_get(void) {
+ struct timespec ts = {0};
+ timespec_get(&ts, TIME_UTC);
+ return ts;
+}
+
+void time_sleep_ns(u64 ns) {
+ // windows....
+ Sleep((DWORD)(ns / 1000000));
+}
diff --git a/filesystem.h b/os.h
index 03ae04a..ebf0317 100644
--- a/filesystem.h
+++ b/os.h
@@ -1,5 +1,5 @@
-#ifndef FILESYSTEM_H_
-#define FILESYSTEM_H_
+#ifndef OS_H_
+#define OS_H_
typedef enum {
FS_NON_EXISTENT,
@@ -40,12 +40,40 @@ int fs_mkdir(char const *path);
// 0 if buf is too short to hold the cwd
// -1 if we can't get the cwd for whatever reason.
int fs_get_cwd(char *buf, size_t buflen);
-// free the entries generated by fs_list_directory.s
+struct timespec time_last_modified(char const *filename);
+struct timespec time_get(void);
+// sleep for a certain number of nanoseconds
+void time_sleep_ns(u64 ns);
+
+
+// free the entries generated by fs_list_directory.
static void fs_dir_entries_free(FsDirectoryEntry **entries) {
for (int i = 0; entries[i]; ++i)
free(entries[i]);
free(entries);
}
-#endif // FILESYSTEM_H_
+static double time_get_seconds(void) {
+ return timespec_to_seconds(time_get());
+}
+
+
+
+// sleep for microseconds
+static void time_sleep_us(u64 us) {
+ time_sleep_ns(us * 1000);
+}
+
+// sleep for milliseconds
+static void time_sleep_ms(u64 ms) {
+ time_sleep_ns(ms * 1000000);
+}
+
+// sleep for seconds
+static void time_sleep_s(u64 s) {
+ time_sleep_ns(s * 1000000000);
+}
+
+
+#endif // OS_H_
diff --git a/time.c b/time.c
deleted file mode 100644
index 7e82f0d..0000000
--- a/time.c
+++ /dev/null
@@ -1,92 +0,0 @@
-#include <time.h>
-#include <errno.h>
-#include <sys/stat.h>
-#if _WIN32
-#include <sysinfoapi.h>
-#endif
-
-static struct timespec time_last_modified(char const *filename) {
-#if __unix__
- struct stat statbuf = {0};
- stat(filename, &statbuf);
- return statbuf.st_mtim;
-#else
- // windows' _stat does not have st_mtim
- struct _stat statbuf = {0};
- struct timespec ts = {0};
- _stat(filename, &statbuf);
- ts.tv_sec = statbuf.st_mtime;
- return ts;
-#endif
-}
-
-static int timespec_cmp(struct timespec a, struct timespec b) {
- if (a.tv_sec > b.tv_sec) return 1;
- if (a.tv_sec < b.tv_sec) return -1;
- if (a.tv_nsec > b.tv_nsec) return 1;
- if (a.tv_nsec < b.tv_nsec) return -1;
- return 0;
-}
-
-static bool timespec_eq(struct timespec a, struct timespec b) {
- return timespec_cmp(a, b) == 0;
-}
-
-static struct timespec timespec_max(struct timespec a, struct timespec b) {
- return timespec_cmp(a, b) < 0 ? b : a;
-}
-
-static double timespec_to_seconds(struct timespec ts) {
- return (double)ts.tv_sec
- + (double)ts.tv_nsec * 1e-9;
-}
-
-static double timespec_sub(struct timespec a, struct timespec b) {
- return (double)(a.tv_sec - b.tv_sec)
- + (double)(a.tv_nsec - b.tv_nsec) * 1e-9;
-}
-
-static struct timespec time_get(void) {
- struct timespec ts = {0};
-#if _WIN32
- timespec_get(&ts, TIME_UTC);
-#else
- clock_gettime(CLOCK_REALTIME, &ts);
-#endif
- return ts;
-}
-
-static double time_get_seconds(void) {
- return timespec_to_seconds(time_get());
-}
-
-// sleep for a certain number of nanoseconds
-static void time_sleep_ns(u64 ns) {
-#if __unix__
- struct timespec rem = {0}, req = {
- (time_t)(ns / 1000000000),
- (long)(ns % 1000000000)
- };
-
- while (nanosleep(&req, &rem) == EINTR) // sleep interrupted by signal
- req = rem;
-#else
- // windows....
- Sleep((DWORD)(ns / 1000000));
-#endif
-}
-
-// sleep for microseconds
-static void time_sleep_us(u64 us) {
- time_sleep_ns(us * 1000);
-}
-
-// sleep for milliseconds
-static void time_sleep_ms(u64 ms) {
- time_sleep_ns(ms * 1000000);
-}
-
-// sleep for seconds
-static void time_sleep_s(u64 s) {
- time_sleep_ns(s * 1000000000);
-}
diff --git a/util.c b/util.c
index 39d1e39..f8bdab5 100644
--- a/util.c
+++ b/util.c
@@ -1228,3 +1228,25 @@ u32 color_interpolate(float x, u32 color1, u32 color2) {
c_out = color_hsva_to_rgba(c_out);
return rgba_v4_to_u32(c_out);
}
+
+
+int timespec_cmp(struct timespec a, struct timespec b) {
+ if (a.tv_sec > b.tv_sec) return 1;
+ if (a.tv_sec < b.tv_sec) return -1;
+ if (a.tv_nsec > b.tv_nsec) return 1;
+ if (a.tv_nsec < b.tv_nsec) return -1;
+ return 0;
+}
+
+bool timespec_eq(struct timespec a, struct timespec b) {
+ return timespec_cmp(a, b) == 0;
+}
+
+struct timespec timespec_max(struct timespec a, struct timespec b) {
+ return timespec_cmp(a, b) < 0 ? b : a;
+}
+
+double timespec_to_seconds(struct timespec ts) {
+ return (double)ts.tv_sec
+ + (double)ts.tv_nsec * 1e-9;
+}
diff --git a/util.h b/util.h
index cbde09c..5645341 100644
--- a/util.h
+++ b/util.h
@@ -249,5 +249,9 @@ Rect rect_grow(Rect r, float amount);
v4 color_rgba_to_hsva(v4 rgba);
v4 color_hsva_to_rgba(v4 hsva);
u32 color_interpolate(float x, u32 color1, u32 color2);
+int timespec_cmp(struct timespec a, struct timespec b);
+bool timespec_eq(struct timespec a, struct timespec b);
+struct timespec timespec_max(struct timespec a, struct timespec b);
+double timespec_to_seconds(struct timespec ts);
#endif // UTIL_H_