os.odin 3.7 KB

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