os.odin 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package os
  2. import "base:runtime"
  3. import "core:mem"
  4. import "core:strconv"
  5. import "core:unicode/utf8"
  6. OS :: ODIN_OS
  7. ARCH :: ODIN_ARCH
  8. ENDIAN :: ODIN_ENDIAN
  9. SEEK_SET :: 0
  10. SEEK_CUR :: 1
  11. SEEK_END :: 2
  12. write_string :: proc(fd: Handle, str: string) -> (int, Errno) {
  13. return write(fd, transmute([]byte)str)
  14. }
  15. write_byte :: proc(fd: Handle, b: byte) -> (int, Errno) {
  16. return write(fd, []byte{b})
  17. }
  18. write_rune :: proc(fd: Handle, r: rune) -> (int, Errno) {
  19. if r < utf8.RUNE_SELF {
  20. return write_byte(fd, byte(r))
  21. }
  22. b, n := utf8.encode_rune(r)
  23. return write(fd, b[:n])
  24. }
  25. write_encoded_rune :: proc(fd: Handle, r: rune) {
  26. write_byte(fd, '\'')
  27. switch r {
  28. case '\a': write_string(fd, "\\a")
  29. case '\b': write_string(fd, "\\b")
  30. case '\e': write_string(fd, "\\e")
  31. case '\f': write_string(fd, "\\f")
  32. case '\n': write_string(fd, "\\n")
  33. case '\r': write_string(fd, "\\r")
  34. case '\t': write_string(fd, "\\t")
  35. case '\v': write_string(fd, "\\v")
  36. case:
  37. if r < 32 {
  38. write_string(fd, "\\x")
  39. b: [2]byte
  40. s := strconv.append_bits(b[:], u64(r), 16, true, 64, strconv.digits, nil)
  41. switch len(s) {
  42. case 0: write_string(fd, "00")
  43. case 1: write_rune(fd, '0')
  44. case 2: write_string(fd, s)
  45. }
  46. } else {
  47. write_rune(fd, r)
  48. }
  49. }
  50. write_byte(fd, '\'')
  51. }
  52. read_at_least :: proc(fd: Handle, buf: []byte, min: int) -> (n: int, err: Errno) {
  53. if len(buf) < min {
  54. return 0, -1
  55. }
  56. nn := max(int)
  57. for nn > 0 && n < min && err == 0 {
  58. nn, err = read(fd, buf[n:])
  59. n += nn
  60. }
  61. if n >= min {
  62. err = 0
  63. }
  64. return
  65. }
  66. read_full :: proc(fd: Handle, buf: []byte) -> (n: int, err: Errno) {
  67. return read_at_least(fd, buf, len(buf))
  68. }
  69. file_size_from_path :: proc(path: string) -> i64 {
  70. fd, err := open(path, O_RDONLY, 0)
  71. if err != 0 {
  72. return -1
  73. }
  74. defer close(fd)
  75. length: i64
  76. if length, err = file_size(fd); err != 0 {
  77. return -1
  78. }
  79. return length
  80. }
  81. read_entire_file_from_filename :: proc(name: string, allocator := context.allocator, loc := #caller_location) -> (data: []byte, success: bool) {
  82. context.allocator = allocator
  83. fd, err := open(name, O_RDONLY, 0)
  84. if err != 0 {
  85. return nil, false
  86. }
  87. defer close(fd)
  88. return read_entire_file_from_handle(fd, allocator, loc)
  89. }
  90. read_entire_file_from_handle :: proc(fd: Handle, allocator := context.allocator, loc := #caller_location) -> (data: []byte, success: bool) {
  91. context.allocator = allocator
  92. length: i64
  93. err: Errno
  94. if length, err = file_size(fd); err != 0 {
  95. return nil, false
  96. }
  97. if length <= 0 {
  98. return nil, true
  99. }
  100. data = make([]byte, int(length), allocator, loc)
  101. if data == nil {
  102. return nil, false
  103. }
  104. bytes_read, read_err := read_full(fd, data)
  105. if read_err != ERROR_NONE {
  106. delete(data)
  107. return nil, false
  108. }
  109. return data[:bytes_read], true
  110. }
  111. read_entire_file :: proc {
  112. read_entire_file_from_filename,
  113. read_entire_file_from_handle,
  114. }
  115. write_entire_file :: proc(name: string, data: []byte, truncate := true) -> (success: bool) {
  116. flags: int = O_WRONLY|O_CREATE
  117. if truncate {
  118. flags |= O_TRUNC
  119. }
  120. mode: int = 0
  121. when OS == .Linux || OS == .Darwin {
  122. // NOTE(justasd): 644 (owner read, write; group read; others read)
  123. mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
  124. }
  125. fd, err := open(name, flags, mode)
  126. if err != 0 {
  127. return false
  128. }
  129. defer close(fd)
  130. _, write_err := write(fd, data)
  131. return write_err == 0
  132. }
  133. write_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (int, Errno) {
  134. s := transmute([]byte)mem.Raw_Slice{data, len}
  135. return write(fd, s)
  136. }
  137. read_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (int, Errno) {
  138. s := transmute([]byte)mem.Raw_Slice{data, len}
  139. return read(fd, s)
  140. }
  141. heap_allocator_proc :: runtime.heap_allocator_proc
  142. heap_allocator :: runtime.heap_allocator
  143. heap_alloc :: runtime.heap_alloc
  144. heap_resize :: runtime.heap_resize
  145. heap_free :: runtime.heap_free
  146. processor_core_count :: proc() -> int {
  147. return _processor_core_count()
  148. }