Work on system_get_file_list.

master
Yuval Dolev 2019-12-26 03:16:34 +02:00
parent 2f9a4dbe3a
commit 1317692233
3 changed files with 120 additions and 3 deletions

View File

@ -38,8 +38,10 @@
#include "mac_objective_c_to_cpp_links.h" #include "mac_objective_c_to_cpp_links.h"
#include <stdlib.h> #include <unistd.h> // NOTE(yuval): Used for getcwd
#include <unistd.h> #include <dirent.h> // NOTE(yuval): Used for opendir, readdir
#include <stdlib.h> // NOTE(yuval): Used for free
//////////////////////////////// ////////////////////////////////
@ -89,6 +91,10 @@ mac_push_string_copy(Arena *arena, String_Const_u8 src){
external void external void
mac_init(){ mac_init(){
Arena test_arena = make_arena_malloc();
system_get_file_list(&test_arena,
string_u8_litexpr("/Users/yuvaldolev/Desktop"));
// NOTE(yuval): Context Setup // NOTE(yuval): Context Setup
Thread_Context _tctx = {}; Thread_Context _tctx = {};
thread_ctx_init(&_tctx, ThreadKind_Main, thread_ctx_init(&_tctx, ThreadKind_Main,

View File

@ -41,6 +41,27 @@
} }
@end @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 external String_Const_u8
mac_standardize_path(Arena* arena, String_Const_u8 path){ mac_standardize_path(Arena* arena, String_Const_u8 path){
NSString *path_ns_str = NSString *path_ns_str =

View File

@ -54,7 +54,97 @@ function
system_get_file_list_sig(){ system_get_file_list_sig(){
File_List result = {}; 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); return(result);
} }