From 131769223346d8471470580ac4471aa1bbe64fc8 Mon Sep 17 00:00:00 2001 From: Yuval Dolev Date: Thu, 26 Dec 2019 03:16:34 +0200 Subject: [PATCH] Work on system_get_file_list. --- platform_mac/mac_4ed.cpp | 10 +++- platform_mac/mac_4ed.mm | 21 +++++++ platform_mac/mac_4ed_functions.cpp | 92 +++++++++++++++++++++++++++++- 3 files changed, 120 insertions(+), 3 deletions(-) diff --git a/platform_mac/mac_4ed.cpp b/platform_mac/mac_4ed.cpp index 7b66f04d..f9f57fb6 100644 --- a/platform_mac/mac_4ed.cpp +++ b/platform_mac/mac_4ed.cpp @@ -38,8 +38,10 @@ #include "mac_objective_c_to_cpp_links.h" -#include -#include +#include // NOTE(yuval): Used for getcwd +#include // NOTE(yuval): Used for opendir, readdir + +#include // NOTE(yuval): Used for free //////////////////////////////// @@ -89,6 +91,10 @@ mac_push_string_copy(Arena *arena, String_Const_u8 src){ external void mac_init(){ + Arena test_arena = make_arena_malloc(); + system_get_file_list(&test_arena, + string_u8_litexpr("/Users/yuvaldolev/Desktop")); + // NOTE(yuval): Context Setup Thread_Context _tctx = {}; thread_ctx_init(&_tctx, ThreadKind_Main, diff --git a/platform_mac/mac_4ed.mm b/platform_mac/mac_4ed.mm index a39fe56e..ca147bb1 100644 --- a/platform_mac/mac_4ed.mm +++ b/platform_mac/mac_4ed.mm @@ -41,6 +41,27 @@ } @end +#if 0 +external File_List +mac_get_file_list(Arena* arena, String_Const_u8 directory){ + File_List result = {}; + + NSString *directory_ns_string = + [[NSString alloc] + initWithBytes:directory.data length:directory.size encoding:NSUTF8StringEncoding]; + + NSFileManager *file_manager = [NSFileManager defaultManager]; + NSDirectoryEnumerator *dirEnum = [file_manager enumeratorAtPath:directory_ns_string]; + + NSString *filename; + while ((filename = [dirEnum nextObject])){ + NSLog(filename); + } + + [directory_ns_string release]; +} +#endif + external String_Const_u8 mac_standardize_path(Arena* arena, String_Const_u8 path){ NSString *path_ns_str = diff --git a/platform_mac/mac_4ed_functions.cpp b/platform_mac/mac_4ed_functions.cpp index bb385e2e..1790e857 100644 --- a/platform_mac/mac_4ed_functions.cpp +++ b/platform_mac/mac_4ed_functions.cpp @@ -54,7 +54,97 @@ function system_get_file_list_sig(){ File_List result = {}; - NotImplemented; + u8* c_directory = push_array(arena, u8, directory.size + 1); + block_copy(c_directory, directory.str, directory.size); + c_directory[directory.size] = 0; + + DIR *dir = opendir((char*)c_directory); + if (dir){ + File_Info* first = 0; + File_Info* last = 0; + i32 count = 0; + + for (struct dirent *entry = readdir(dir); + entry; + entry = readdir(dir)){ + char *c_file_name = entry->d_name; + String_Const_u8 file_name = SCu8(c_file_name); + if (string_match(file_name, string_u8_litexpr(".")) || string_match(file_name, string_u8_litexpr(".."))){ + continue; + } + + File_Info* info = push_array(arena, File_Info, 1); + sll_queue_push(first, last, info); + count += 1; + + info->file_name = push_string_copy(arena, file_name); + //info->attributes.size = + + /*++file_count; + i32 size = 0; + for (; fname[size]; ++size); + character_count += size + 1;*/ + } + +#if 0 + i32 required_size = character_count + file_count * sizeof(File_Info); + if (file_list->block_size < required_size){ + system_memory_free(file_list->block, file_list->block_size); + file_list->block = system_memory_allocate(required_size); + file_list->block_size = required_size; + } + + file_list->infos = (File_Info*)file_list->block; + char *cursor = (char*)(file_list->infos + file_count); + + if (file_list->block != 0){ + rewinddir(d); + File_Info *info_ptr = file_list->infos; + for (struct dirent *entry = readdir(d); + entry != 0; + entry = readdir(d)){ + char *fname = entry->d_name; + if (match(fname, ".") || match(fname, "..")){ + continue; + } + char *cursor_start = cursor; + i32 length = copy_fast_unsafe_cc(cursor_start, fname); + cursor += length; + + if (entry->d_type == DT_LNK){ + struct stat st; + if (stat(entry->d_name, &st) != -1){ + info_ptr->folder = S_ISDIR(st.st_mode); + } + else{ + info_ptr->folder = false; + } + } + else{ + info_ptr->folder = (entry->d_type == DT_DIR); + } + + info_ptr->filename = cursor_start; + info_ptr->filename_len = length; + *cursor++ = 0; + ++info_ptr; + } + } + + file_list->count = file_count; + +#endif + + closedir(dir); + } + else{ + + /*system_memory_free(file_list->block, file_list->block_size); + file_list->block = 0; + file_list->block_size = 0; + file_list->infos = 0; + file_list->count = 0;*/ + } return(result); }