diff options
Diffstat (limited to 'source/kit/file.c')
-rw-r--r-- | source/kit/file.c | 84 |
1 files changed, 38 insertions, 46 deletions
diff --git a/source/kit/file.c b/source/kit/file.c index f767851..2d52e92 100644 --- a/source/kit/file.c +++ b/source/kit/file.c @@ -22,14 +22,13 @@ enum { PATH_BUF_SIZE = 4096 }; # define st_mtim st_mtimespec #endif -static int is_delim(char const c) { +static int is_delim(char c) { return c == '/' || c == '\\'; } -static kit_string_t kit_get_env_(char *const name, - kit_allocator_t const alloc) { - char *const val = getenv(name); - ptrdiff_t const size = val != NULL ? (ptrdiff_t) strlen(val) : 0; +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; string_t result; DA_INIT(result, size, alloc); @@ -43,10 +42,9 @@ static kit_string_t kit_get_env_(char *const name, return result; } -kit_string_t kit_path_norm(kit_str_t const path, - kit_allocator_t const alloc) { - str_t const parent = SZ(".."); - ptrdiff_t i, i1, j; +kit_string_t kit_path_norm(kit_str_t path, kit_allocator_t alloc) { + str_t parent = SZ(".."); + ptrdiff_t i, i1, j; string_t norm; DA_INIT(norm, path.size, alloc); @@ -61,8 +59,7 @@ kit_string_t kit_path_norm(kit_str_t const path, if (!is_delim(path.values[i])) continue; - str_t const s = { .size = i - i1 - 1, - .values = path.values + i1 + 1 }; + 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; @@ -101,12 +98,11 @@ kit_string_t kit_path_norm(kit_str_t const path, return norm; } -kit_string_t kit_path_join(kit_str_t const left, - kit_str_t const right, - kit_allocator_t const alloc) { - ptrdiff_t left_size = left.size; - ptrdiff_t right_size = right.size; - char const *right_values = right.values; +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; if (left_size > 0 && is_delim(left.values[left_size - 1])) left_size--; @@ -129,7 +125,7 @@ kit_string_t kit_path_join(kit_str_t const left, return joined; } -kit_string_t kit_path_user(kit_allocator_t const alloc) { +kit_string_t kit_path_user(kit_allocator_t alloc) { kit_string_t user = kit_get_env_(KIT_ENV_HOME, alloc); if (user.size == 0) { DA_RESIZE(user, 1); @@ -167,8 +163,7 @@ kit_string_t kit_path_cache(kit_allocator_t alloc) { return cache; } -kit_str_t kit_path_index(kit_str_t const path, - ptrdiff_t const index) { +kit_str_t kit_path_index(kit_str_t path, ptrdiff_t index) { str_t s = { .size = 0, .values = NULL }; ptrdiff_t i0 = 0; @@ -198,7 +193,7 @@ kit_str_t kit_path_index(kit_str_t const path, return s; } -kit_str_t kit_path_take(kit_str_t const path, ptrdiff_t const count) { +kit_str_t kit_path_take(kit_str_t path, ptrdiff_t count) { str_t s = { .size = 0, .values = path.values }; ptrdiff_t i0 = 0; @@ -226,8 +221,7 @@ kit_str_t kit_path_take(kit_str_t const path, ptrdiff_t const count) { } #if defined(_WIN32) && !defined(__CYGWIN__) -static void win32_prepare_path_(WCHAR *const buf, - kit_str_t const path) { +static void win32_prepare_path_(WCHAR *buf, kit_str_t path) { assert(path.size == 0 || path.values != NULL); assert(path.size + 5 < PATH_BUF_SIZE); @@ -248,8 +242,7 @@ static void win32_prepare_path_(WCHAR *const buf, WCHAR buf[PATH_BUF_SIZE]; \ win32_prepare_path_(buf, path) #else -static void unix_prepare_path_(char *const buf, - kit_str_t const path) { +static void unix_prepare_path_(char *buf, kit_str_t path) { assert(path.size == 0 || path.values != NULL); assert(path.size + 1 < PATH_BUF_SIZE); @@ -262,7 +255,7 @@ static void unix_prepare_path_(char *const buf, unix_prepare_path_(buf, path) #endif -kit_status_t kit_file_create_folder(kit_str_t const path) { +kit_status_t kit_file_create_folder(kit_str_t path) { PREPARE_PATH_BUF_; #if defined(_WIN32) && !defined(__CYGWIN__) return CreateDirectoryW(buf, NULL) ? KIT_OK @@ -272,16 +265,16 @@ kit_status_t kit_file_create_folder(kit_str_t const path) { #endif } -kit_status_t kit_file_create_folder_recursive(kit_str_t const path) { +kit_status_t kit_file_create_folder_recursive(kit_str_t path) { ptrdiff_t i; for (i = 0;; i++) { - str_t const part = kit_path_take(path, i); - int const type = kit_path_type(part); + str_t part = kit_path_take(path, i); + int type = kit_path_type(part); if (type == KIT_PATH_FILE) return KIT_ERROR_FILE_ALREADY_EXISTS; if (type == KIT_PATH_NONE) { - kit_status_t const s = kit_file_create_folder(part); + kit_status_t s = kit_file_create_folder(part); if (s != KIT_OK) return s; } @@ -292,7 +285,7 @@ kit_status_t kit_file_create_folder_recursive(kit_str_t const path) { return KIT_OK; } -kit_status_t kit_file_remove(kit_str_t const path) { +kit_status_t kit_file_remove(kit_str_t path) { PREPARE_PATH_BUF_; #if defined(_WIN32) && !defined(__CYGWIN__) return DeleteFileW(buf) ? KIT_OK : KIT_ERROR_UNLINK_FAILED; @@ -301,7 +294,7 @@ kit_status_t kit_file_remove(kit_str_t const path) { #endif } -kit_status_t kit_file_remove_folder(kit_str_t const path) { +kit_status_t kit_file_remove_folder(kit_str_t path) { PREPARE_PATH_BUF_; #if defined(_WIN32) && !defined(__CYGWIN__) return RemoveDirectoryW(buf) ? KIT_OK : KIT_ERROR_RMDIR_FAILED; @@ -310,8 +303,8 @@ kit_status_t kit_file_remove_folder(kit_str_t const path) { #endif } -kit_status_t kit_file_remove_recursive(kit_str_t const path, - kit_allocator_t const alloc) { +kit_status_t kit_file_remove_recursive(kit_str_t path, + kit_allocator_t alloc) { int type = kit_path_type(path); ptrdiff_t i; @@ -325,8 +318,8 @@ kit_status_t kit_file_remove_recursive(kit_str_t const path, return list.status; } for (i = 0; i < list.files.size; i++) { - str_t const s = { .size = list.files.values[i].size, - .values = list.files.values[i].values }; + str_t s = { .size = list.files.values[i].size, + .values = list.files.values[i].values }; kit_file_remove_recursive(s, alloc); } kit_path_list_destroy(list); @@ -339,7 +332,7 @@ kit_status_t kit_file_remove_recursive(kit_str_t const path, return KIT_ERROR_FILE_DO_NOT_EXIST; } -kit_path_type_t kit_path_type(kit_str_t const path) { +kit_path_type_t kit_path_type(kit_str_t path) { PREPARE_PATH_BUF_; #if defined(_WIN32) && !defined(__CYGWIN__) if (PathFileExistsW(buf)) { @@ -360,7 +353,7 @@ kit_path_type_t kit_path_type(kit_str_t const path) { return KIT_PATH_NONE; } -kit_file_info_t kit_file_info(kit_str_t const path) { +kit_file_info_t kit_file_info(kit_str_t path) { kit_file_info_t result; memset(&result, 0, sizeof result); @@ -372,9 +365,8 @@ kit_file_info_t kit_file_info(kit_str_t const path) { if (f != INVALID_HANDLE_VALUE) { FILETIME ft; if (GetFileTime(f, NULL, NULL, &ft) != 0) { - uint64_t const nsec100 = (((uint64_t) ft.dwHighDateTime) - << 32) | - (uint64_t) ft.dwLowDateTime; + 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)); @@ -413,8 +405,8 @@ kit_file_info_t kit_file_info(kit_str_t const path) { return result; } -kit_path_list_t kit_file_enum_folder(kit_str_t const path, - kit_allocator_t const alloc) { +kit_path_list_t kit_file_enum_folder(kit_str_t path, + kit_allocator_t alloc) { PREPARE_PATH_BUF_; kit_path_list_t result = { .status = KIT_OK }; @@ -436,7 +428,7 @@ kit_path_list_t kit_file_enum_folder(kit_str_t const path, return result; do { - ptrdiff_t const n = result.files.size; + ptrdiff_t n = result.files.size; DA_RESIZE(result.files, n + 1); if (result.files.size != n + 1) { result.status = KIT_ERROR_BAD_ALLOC; @@ -472,14 +464,14 @@ kit_path_list_t kit_file_enum_folder(kit_str_t const path, if (entry->d_name[0] == '.') continue; - ptrdiff_t const n = result.files.size; + ptrdiff_t 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 const size = (ptrdiff_t) strlen(entry->d_name); + ptrdiff_t size = (ptrdiff_t) strlen(entry->d_name); DA_INIT(result.files.values[n], size, alloc); if (result.files.values[n].size != size) { DA_RESIZE(result.files, n); |