os.odin 5.2 KB

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