summaryrefslogtreecommitdiff
path: root/source/kit/file.c
diff options
context:
space:
mode:
Diffstat (limited to 'source/kit/file.c')
-rw-r--r--source/kit/file.c104
1 files changed, 52 insertions, 52 deletions
diff --git a/source/kit/file.c b/source/kit/file.c
index fd24390..6a7f740 100644
--- a/source/kit/file.c
+++ b/source/kit/file.c
@@ -26,15 +26,16 @@ enum { PATH_BUF_SIZE = 4096 };
# define st_mtim st_mtimespec
#endif
-static int is_delim(char c) {
+static i32 is_delim(char c) {
return c == '/' || c == '\\';
}
-static kit_string_t kit_get_env_(char *name, kit_allocator_t *alloc) {
- char *val = getenv(name);
- ptrdiff_t size = val != NULL ? (ptrdiff_t) strlen(val) : 0;
+static kit_str_builder_t kit_get_env_(char *name,
+ kit_allocator_t *alloc) {
+ char *val = getenv(name);
+ i64 size = val != NULL ? (i64) strlen(val) : 0;
- string_t result;
+ str_builder_t result;
DA_INIT(result, size, alloc);
assert(result.size == size);
@@ -46,11 +47,12 @@ static kit_string_t kit_get_env_(char *name, kit_allocator_t *alloc) {
return result;
}
-kit_string_t kit_path_norm(kit_str_t path, kit_allocator_t *alloc) {
- str_t parent = SZ("..");
- ptrdiff_t i, i1, j;
+kit_str_builder_t kit_path_norm(kit_str_t path,
+ kit_allocator_t *alloc) {
+ str_t parent = SZ("..");
+ i64 i, i1, j;
- string_t norm;
+ str_builder_t norm;
DA_INIT(norm, path.size, alloc);
assert(norm.size == path.size);
@@ -65,8 +67,8 @@ kit_string_t kit_path_norm(kit_str_t path, kit_allocator_t *alloc) {
str_t s = { .size = i - i1 - 1, .values = path.values + i1 + 1 };
if (AR_EQUAL(s, parent)) {
- int have_parent = 0;
- ptrdiff_t i0 = 0;
+ i32 have_parent = 0;
+ i64 i0 = 0;
for (j = 0; j < i1; j++) {
if (norm.values[j] != '\0')
@@ -86,7 +88,7 @@ kit_string_t kit_path_norm(kit_str_t path, kit_allocator_t *alloc) {
i1 = i;
}
- ptrdiff_t size = 0;
+ i64 size = 0;
for (i = 0; i < norm.size; i++) {
if (norm.values[i] != '\0') {
@@ -102,11 +104,11 @@ kit_string_t kit_path_norm(kit_str_t path, kit_allocator_t *alloc) {
return norm;
}
-kit_string_t kit_path_join(kit_str_t left, kit_str_t right,
- kit_allocator_t *alloc) {
- ptrdiff_t left_size = left.size;
- ptrdiff_t right_size = right.size;
- char *right_values = right.values;
+kit_str_builder_t kit_path_join(kit_str_t left, kit_str_t right,
+ kit_allocator_t *alloc) {
+ i64 left_size = left.size;
+ i64 right_size = right.size;
+ char *right_values = right.values;
if (left_size > 0 && is_delim(left.values[left_size - 1]))
left_size--;
@@ -115,7 +117,7 @@ kit_string_t kit_path_join(kit_str_t left, kit_str_t right,
right_values++;
}
- kit_string_t joined;
+ kit_str_builder_t joined;
DA_INIT(joined, left_size + right_size + 1, alloc);
assert(joined.size == left_size + right_size + 1);
@@ -129,8 +131,8 @@ kit_string_t kit_path_join(kit_str_t left, kit_str_t right,
return joined;
}
-kit_string_t kit_path_user(kit_allocator_t *alloc) {
- kit_string_t user = kit_get_env_(KIT_ENV_HOME, alloc);
+kit_str_builder_t kit_path_user(kit_allocator_t *alloc) {
+ kit_str_builder_t user = kit_get_env_(KIT_ENV_HOME, alloc);
if (user.size == 0) {
DA_RESIZE(user, 1);
if (user.size == 1)
@@ -139,8 +141,8 @@ kit_string_t kit_path_user(kit_allocator_t *alloc) {
return user;
}
-kit_string_t kit_path_cache(kit_allocator_t *alloc) {
- kit_string_t cache, user;
+kit_str_builder_t kit_path_cache(kit_allocator_t *alloc) {
+ kit_str_builder_t cache, user;
#if defined(_WIN32) && !defined(__CYGWIN__)
cache = kit_get_env_("LOCALAPPDATA", alloc);
@@ -167,12 +169,12 @@ kit_string_t kit_path_cache(kit_allocator_t *alloc) {
return cache;
}
-kit_str_t kit_path_index(kit_str_t path, ptrdiff_t index) {
+kit_str_t kit_path_index(kit_str_t path, i64 index) {
str_t s = { .size = 0, .values = NULL };
- ptrdiff_t i0 = 0;
- ptrdiff_t i = 0;
- ptrdiff_t n = 0;
+ i64 i0 = 0;
+ i64 i = 0;
+ i64 n = 0;
for (; i < path.size; i++) {
if (!is_delim(path.values[i]))
@@ -197,12 +199,12 @@ kit_str_t kit_path_index(kit_str_t path, ptrdiff_t index) {
return s;
}
-kit_str_t kit_path_take(kit_str_t path, ptrdiff_t count) {
+kit_str_t kit_path_take(kit_str_t path, i64 count) {
str_t s = { .size = 0, .values = path.values };
- ptrdiff_t i0 = 0;
- ptrdiff_t i = 0;
- ptrdiff_t n = 0;
+ i64 i0 = 0;
+ i64 i = 0;
+ i64 n = 0;
for (; i < path.size; i++) {
if (!is_delim(path.values[i]))
@@ -235,7 +237,7 @@ static void win32_prepare_path_(WCHAR *buf, kit_str_t path) {
buf[2] = L'?';
buf[3] = L'\\';
if (path.size > 0 && path.size + 5 < PATH_BUF_SIZE)
- for (ptrdiff_t i = 0; i < path.size; i++) {
+ for (i64 i = 0; i < path.size; i++) {
if (path.values[i] == '/')
buf[4 + i] = L'\\';
else
@@ -270,11 +272,11 @@ kit_status_t kit_file_create_folder(kit_str_t path) {
}
kit_status_t kit_file_create_folder_recursive(kit_str_t path) {
- ptrdiff_t i;
+ i64 i;
for (i = 0;; i++) {
str_t part = kit_path_take(path, i);
- int type = kit_path_type(part);
+ i32 type = kit_path_type(part);
if (type == KIT_PATH_FILE)
return KIT_ERROR_FILE_ALREADY_EXISTS;
if (type == KIT_PATH_NONE) {
@@ -309,8 +311,8 @@ kit_status_t kit_file_remove_folder(kit_str_t path) {
kit_status_t kit_file_remove_recursive(kit_str_t path,
kit_allocator_t *alloc) {
- int type = kit_path_type(path);
- ptrdiff_t i;
+ i32 type = kit_path_type(path);
+ i64 i;
switch (type) {
case KIT_PATH_FILE: return kit_file_remove(path);
@@ -369,11 +371,10 @@ kit_file_info_t kit_file_info(kit_str_t path) {
if (f != INVALID_HANDLE_VALUE) {
FILETIME ft;
if (GetFileTime(f, NULL, NULL, &ft) != 0) {
- uint64_t nsec100 = (((uint64_t) ft.dwHighDateTime) << 32) |
- (uint64_t) ft.dwLowDateTime;
- result.time_modified_sec = (int64_t) (nsec100 / 10000000);
- result.time_modified_nsec = (int32_t) (100 *
- (nsec100 % 10000000));
+ ui64 nsec100 = (((ui64) ft.dwHighDateTime) << 32) |
+ (ui64) ft.dwLowDateTime;
+ result.time_modified_sec = (i64) (nsec100 / 10000000);
+ result.time_modified_nsec = (i32) (100 * (nsec100 % 10000000));
} else {
assert(0);
}
@@ -381,8 +382,7 @@ kit_file_info_t kit_file_info(kit_str_t path) {
DWORD high;
DWORD low = GetFileSize(f, &high);
- result.size = (int64_t) ((((uint64_t) high) << 32) |
- (uint64_t) low);
+ result.size = (i64) ((((ui64) high) << 32) | (ui64) low);
result.status = KIT_OK;
CloseHandle(f);
@@ -391,14 +391,14 @@ kit_file_info_t kit_file_info(kit_str_t path) {
#else
struct stat info;
if (stat(buf, &info) == 0 && S_ISREG(info.st_mode)) {
- result.size = (int64_t) info.st_size;
+ result.size = (i64) info.st_size;
# ifndef st_mtime
/* No support for nanosecond timestamps.
*/
- result.time_modified_sec = (int64_t) info.st_mtime;
+ result.time_modified_sec = (i64) info.st_mtime;
# else
- result.time_modified_sec = (int64_t) info.st_mtim.tv_sec;
- result.time_modified_nsec = (int32_t) info.st_mtim.tv_nsec;
+ result.time_modified_sec = (i64) info.st_mtim.tv_sec;
+ result.time_modified_nsec = (i32) info.st_mtim.tv_nsec;
# endif
result.status = KIT_OK;
return result;
@@ -432,14 +432,14 @@ kit_path_list_t kit_file_enum_folder(kit_str_t path,
return result;
do {
- ptrdiff_t n = result.files.size;
+ i64 n = result.files.size;
DA_RESIZE(result.files, n + 1);
if (result.files.size != n + 1) {
result.status = KIT_ERROR_BAD_ALLOC;
break;
}
- ptrdiff_t size = 0;
+ i64 size = 0;
while (size < MAX_PATH && data.cFileName[size] != L'\0') size++;
DA_INIT(result.files.values[n], size, alloc);
if (result.files.values[n].size != size) {
@@ -448,7 +448,7 @@ kit_path_list_t kit_file_enum_folder(kit_str_t path,
break;
}
- for (ptrdiff_t i = 0; i < size; i++)
+ for (i64 i = 0; i < size; i++)
result.files.values[n].values[i] = data.cFileName[i];
} while (FindNextFileW(find, &data) != 0);
@@ -468,14 +468,14 @@ kit_path_list_t kit_file_enum_folder(kit_str_t path,
if (entry->d_name[0] == '.')
continue;
- ptrdiff_t n = result.files.size;
+ i64 n = result.files.size;
DA_RESIZE(result.files, n + 1);
if (result.files.size != n + 1) {
result.status = KIT_ERROR_BAD_ALLOC;
break;
}
- ptrdiff_t size = (ptrdiff_t) strlen(entry->d_name);
+ i64 size = (i64) strlen(entry->d_name);
DA_INIT(result.files.values[n], size, alloc);
if (result.files.values[n].size != size) {
DA_RESIZE(result.files, n);
@@ -494,7 +494,7 @@ kit_path_list_t kit_file_enum_folder(kit_str_t path,
}
void kit_path_list_destroy(kit_path_list_t list) {
- ptrdiff_t i;
+ i64 i;
for (i = 0; i < list.files.size; i++)
DA_DESTROY(list.files.values[i]);
DA_DESTROY(list.files);