dir.odin 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package os2
  2. import "base:runtime"
  3. import "core:slice"
  4. read_dir :: read_directory
  5. @(require_results)
  6. read_directory :: proc(f: ^File, n: int, allocator: runtime.Allocator) -> (files: []File_Info, err: Error) {
  7. if f == nil {
  8. return nil, .Invalid_File
  9. }
  10. n := n
  11. size := n
  12. if n <= 0 {
  13. n = -1
  14. size = 100
  15. }
  16. TEMP_ALLOCATOR_GUARD()
  17. it := read_directory_iterator_create(f) or_return
  18. defer _read_directory_iterator_destroy(&it)
  19. dfi := make([dynamic]File_Info, 0, size, temp_allocator())
  20. defer if err != nil {
  21. for fi in dfi {
  22. file_info_delete(fi, allocator)
  23. }
  24. }
  25. for fi, index in read_directory_iterator(&it) {
  26. if n > 0 && index == n {
  27. break
  28. }
  29. append(&dfi, file_info_clone(fi, allocator) or_return)
  30. }
  31. return slice.clone(dfi[:], allocator)
  32. }
  33. @(require_results)
  34. read_all_directory :: proc(f: ^File, allocator: runtime.Allocator) -> (fi: []File_Info, err: Error) {
  35. return read_directory(f, -1, allocator)
  36. }
  37. @(require_results)
  38. read_directory_by_path :: proc(path: string, n: int, allocator: runtime.Allocator) -> (fi: []File_Info, err: Error) {
  39. f := open(path) or_return
  40. defer close(f)
  41. return read_directory(f, n, allocator)
  42. }
  43. @(require_results)
  44. read_all_directory_by_path :: proc(path: string, allocator: runtime.Allocator) -> (fi: []File_Info, err: Error) {
  45. return read_directory_by_path(path, -1, allocator)
  46. }
  47. Read_Directory_Iterator :: struct {
  48. f: ^File,
  49. impl: Read_Directory_Iterator_Impl,
  50. }
  51. @(require_results)
  52. read_directory_iterator_create :: proc(f: ^File) -> (Read_Directory_Iterator, Error) {
  53. return _read_directory_iterator_create(f)
  54. }
  55. read_directory_iterator_destroy :: proc(it: ^Read_Directory_Iterator) {
  56. _read_directory_iterator_destroy(it)
  57. }
  58. // NOTE(bill): `File_Info` does not need to deleted on each iteration. Any copies must be manually copied with `file_info_clone`
  59. @(require_results)
  60. read_directory_iterator :: proc(it: ^Read_Directory_Iterator) -> (fi: File_Info, index: int, ok: bool) {
  61. return _read_directory_iterator(it)
  62. }