os.odin 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package os
  2. import "core:mem"
  3. import "core:strconv"
  4. import "core:unicode/utf8"
  5. write_string :: proc(fd: Handle, str: string) -> (int, Errno) {
  6. return write(fd, cast([]byte)str);
  7. }
  8. write_byte :: proc(fd: Handle, b: byte) -> (int, Errno) {
  9. return write(fd, []byte{b});
  10. }
  11. write_rune :: proc(fd: Handle, r: rune) -> (int, Errno) {
  12. if r < utf8.RUNE_SELF {
  13. return write_byte(fd, byte(r));
  14. }
  15. b, n := utf8.encode_rune(r);
  16. return write(fd, b[:n]);
  17. }
  18. write_encoded_rune :: proc(fd: Handle, r: rune) {
  19. write_byte(fd, '\'');
  20. switch r {
  21. case '\a': write_string(fd, "\\a");
  22. case '\b': write_string(fd, "\\b");
  23. case '\e': write_string(fd, "\\e");
  24. case '\f': write_string(fd, "\\f");
  25. case '\n': write_string(fd, "\\n");
  26. case '\r': write_string(fd, "\\r");
  27. case '\t': write_string(fd, "\\t");
  28. case '\v': write_string(fd, "\\v");
  29. case:
  30. if r < 32 {
  31. write_string(fd, "\\x");
  32. b: [2]byte;
  33. s := strconv.append_bits(b[:], u64(r), 16, true, 64, strconv.digits, nil);
  34. switch len(s) {
  35. case 0: write_string(fd, "00");
  36. case 1: write_rune(fd, '0');
  37. case 2: write_string(fd, s);
  38. }
  39. } else {
  40. write_rune(fd, r);
  41. }
  42. }
  43. write_byte(fd, '\'');
  44. }
  45. file_size_from_path :: proc(path: string) -> i64 {
  46. fd, err := open(path, O_RDONLY, 0);
  47. if err != 0 {
  48. return -1;
  49. }
  50. defer close(fd);
  51. length: i64;
  52. if length, err = file_size(fd); err != 0 {
  53. return -1;
  54. }
  55. return length;
  56. }
  57. read_entire_file :: proc(name: string) -> (data: []byte, success: bool) {
  58. fd, err := open(name, O_RDONLY, 0);
  59. if err != 0 {
  60. return nil, false;
  61. }
  62. defer close(fd);
  63. length: i64;
  64. if length, err = file_size(fd); err != 0 {
  65. return nil, false;
  66. }
  67. if length <= 0 {
  68. return nil, true;
  69. }
  70. data = make([]byte, int(length));
  71. if data == nil {
  72. return nil, false;
  73. }
  74. bytes_read, read_err := read(fd, data);
  75. if read_err != 0 {
  76. delete(data);
  77. return nil, false;
  78. }
  79. return data[0:bytes_read], true;
  80. }
  81. write_entire_file :: proc(name: string, data: []byte, truncate := true) -> (success: bool) {
  82. flags: int = O_WRONLY|O_CREATE;
  83. if truncate {
  84. flags |= O_TRUNC;
  85. }
  86. fd, err := open(name, flags, 0);
  87. if err != 0 {
  88. return false;
  89. }
  90. defer close(fd);
  91. _, write_err := write(fd, data);
  92. return write_err == 0;
  93. }
  94. write_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (int, Errno) {
  95. s := transmute([]byte)mem.Raw_Slice{data, len};
  96. return write(fd, s);
  97. }
  98. read_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (int, Errno) {
  99. s := transmute([]byte)mem.Raw_Slice{data, len};
  100. return read(fd, s);
  101. }
  102. heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
  103. size, alignment: int,
  104. old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
  105. switch mode {
  106. case .Alloc:
  107. return heap_alloc(size);
  108. case .Free:
  109. heap_free(old_memory);
  110. return nil;
  111. case .Free_All:
  112. // NOTE(bill): Does nothing
  113. case .Resize:
  114. if old_memory == nil {
  115. return heap_alloc(size);
  116. }
  117. ptr := heap_resize(old_memory, size);
  118. assert(ptr != nil);
  119. return ptr;
  120. }
  121. return nil;
  122. }
  123. heap_allocator :: proc() -> mem.Allocator {
  124. return mem.Allocator{
  125. procedure = heap_allocator_proc,
  126. data = nil,
  127. };
  128. }