file_util.odin 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package os2
  2. import "core:mem"
  3. import "base:runtime"
  4. import "core:strconv"
  5. import "core:unicode/utf8"
  6. write_string :: proc(f: ^File, s: string) -> (n: int, err: Error) {
  7. return write(f, transmute([]byte)s)
  8. }
  9. write_byte :: proc(f: ^File, b: byte) -> (n: int, err: Error) {
  10. return write(f, []byte{b})
  11. }
  12. write_rune :: proc(f: ^File, r: rune) -> (n: int, err: Error) {
  13. if r < utf8.RUNE_SELF {
  14. return write_byte(f, byte(r))
  15. }
  16. b: [4]byte
  17. b, n = utf8.encode_rune(r)
  18. return write(f, b[:n])
  19. }
  20. write_encoded_rune :: proc(f: ^File, r: rune) -> (n: int, err: Error) {
  21. wrap :: proc(m: int, merr: Error, n: ^int, err: ^Error) -> bool {
  22. n^ += m
  23. if merr != nil {
  24. err^ = merr
  25. return true
  26. }
  27. return false
  28. }
  29. if wrap(write_byte(f, '\''), &n, &err) { return }
  30. switch r {
  31. case '\a': if wrap(write_string(f, "\\a"), &n, &err) { return }
  32. case '\b': if wrap(write_string(f, "\\b"), &n, &err) { return }
  33. case '\e': if wrap(write_string(f, "\\e"), &n, &err) { return }
  34. case '\f': if wrap(write_string(f, "\\f"), &n, &err) { return }
  35. case '\n': if wrap(write_string(f, "\\n"), &n, &err) { return }
  36. case '\r': if wrap(write_string(f, "\\r"), &n, &err) { return }
  37. case '\t': if wrap(write_string(f, "\\t"), &n, &err) { return }
  38. case '\v': if wrap(write_string(f, "\\v"), &n, &err) { return }
  39. case:
  40. if r < 32 {
  41. if wrap(write_string(f, "\\x"), &n, &err) { return }
  42. b: [2]byte
  43. s := strconv.append_bits(b[:], u64(r), 16, true, 64, strconv.digits, nil)
  44. switch len(s) {
  45. case 0: if wrap(write_string(f, "00"), &n, &err) { return }
  46. case 1: if wrap(write_rune(f, '0'), &n, &err) { return }
  47. case 2: if wrap(write_string(f, s), &n, &err) { return }
  48. }
  49. } else {
  50. if wrap(write_rune(f, r), &n, &err) { return }
  51. }
  52. }
  53. _ = wrap(write_byte(f, '\''), &n, &err)
  54. return
  55. }
  56. write_ptr :: proc(f: ^File, data: rawptr, len: int) -> (n: int, err: Error) {
  57. return write(f, ([^]byte)(data)[:len])
  58. }
  59. read_ptr :: proc(f: ^File, data: rawptr, len: int) -> (n: int, err: Error) {
  60. return read(f, ([^]byte)(data)[:len])
  61. }
  62. read_entire_file :: proc{
  63. read_entire_file_from_path,
  64. read_entire_file_from_file,
  65. }
  66. read_entire_file_from_path :: proc(name: string, allocator: runtime.Allocator) -> (data: []byte, err: Error) {
  67. f, ferr := open(name)
  68. if ferr != nil {
  69. return nil, ferr
  70. }
  71. defer close(f)
  72. return read_entire_file_from_file(f, allocator)
  73. }
  74. read_entire_file_from_file :: proc(f: ^File, allocator: runtime.Allocator) -> (data: []byte, err: Error) {
  75. size: int
  76. if size64, err := file_size(f); err == nil {
  77. if i64(int(size64)) != size64 {
  78. size = int(size64)
  79. }
  80. }
  81. size += 1 // for EOF
  82. // TODO(bill): Is this correct logic?
  83. total: int
  84. data = make([]byte, size, allocator) or_return
  85. for {
  86. n: int
  87. n, err = read(f, data[total:])
  88. total += n
  89. if err != nil {
  90. if err == .EOF {
  91. err = nil
  92. }
  93. data = data[:total]
  94. return
  95. }
  96. }
  97. }
  98. write_entire_file :: proc(name: string, data: []byte, perm: File_Mode, truncate := true) -> Error {
  99. flags := O_WRONLY|O_CREATE
  100. if truncate {
  101. flags |= O_TRUNC
  102. }
  103. f, err := open(name, flags, perm)
  104. if err != nil {
  105. return err
  106. }
  107. _, err = write(f, data)
  108. if cerr := close(f); cerr != nil && err == nil {
  109. err = cerr
  110. }
  111. return err
  112. }