diff options
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/file.c | 5 | ||||
-rw-r--r-- | src/utils/time.c | 32 | ||||
-rw-r--r-- | src/utils/time.h | 7 |
3 files changed, 42 insertions, 2 deletions
diff --git a/src/utils/file.c b/src/utils/file.c index f14334d..55a438c 100644 --- a/src/utils/file.c +++ b/src/utils/file.c @@ -58,7 +58,7 @@ char *readWholeFile(const char *filePath) { const size_t file_size = ftell(file); fseek(file, 0, SEEK_SET); - char *str = a404m_malloc((file_size + 1) * sizeof(char)); + char *str = a404m_malloc((file_size + 1) * sizeof(*str)); fread(str, file_size, 1, file); str[file_size] = '\0'; @@ -95,7 +95,8 @@ char *joinToPathOf(const char *original, const char *file) { result[result_size++] = '/'; for (size_t i = 0; file[i] != '\0'; ++i) { - if (result_size != 0 && result[result_size-1] == '/' && file[i + 1] == '.') { + if (result_size != 0 && result[result_size - 1] == '/' && + file[i + 1] == '.') { if (file[i + 2] == '/') { i += 2; continue; diff --git a/src/utils/time.c b/src/utils/time.c new file mode 100644 index 0000000..979a2c8 --- /dev/null +++ b/src/utils/time.c @@ -0,0 +1,32 @@ +#include "time.h" + +#include <stdio.h> + +#ifdef PRINT_STATISTICS +struct timespec time_diff(struct timespec end, struct timespec start) { + struct timespec temp; + if ((end.tv_nsec - start.tv_nsec) < 0) { + temp.tv_sec = end.tv_sec - start.tv_sec - 1; + temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec; + } else { + temp.tv_sec = end.tv_sec - start.tv_sec; + temp.tv_nsec = end.tv_nsec - start.tv_nsec; + } + return temp; +} + +struct timespec time_add(struct timespec left, struct timespec right) { + struct timespec result; + result.tv_nsec = left.tv_nsec + right.tv_nsec; + result.tv_sec = (left.tv_sec + right.tv_sec) + result.tv_nsec / 1000000000; + result.tv_nsec %= 1000000000; + return result; +} + +void time_print(struct timespec time) { + printf("%02ld:%02ld.%06ldus", time.tv_sec / 60, time.tv_sec % 60, + time.tv_nsec / 1000); +} +#endif + + diff --git a/src/utils/time.h b/src/utils/time.h new file mode 100644 index 0000000..6d5f2f6 --- /dev/null +++ b/src/utils/time.h @@ -0,0 +1,7 @@ +#pragma once + +#include <time.h> + +struct timespec time_diff(struct timespec end, struct timespec start); +struct timespec time_add(struct timespec left, struct timespec right); +void time_print(struct timespec time); |