init.lisp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. (import lua/io (open))
  2. (defun read-all-mode! (path binary) :hidden
  3. (with ((handle err) (open path (.. "r" (if binary "b" ""))))
  4. (if handle
  5. (with (data (self handle :read "*a"))
  6. (self handle :close)
  7. data)
  8. (values-list handle err))))
  9. (defun read-all! (path)
  10. "Reads the data from the file at PATH and returns it as a string.
  11. Returns nil if it failed.
  12. ### Example
  13. ```cl
  14. > (read-all! \"tests/data/hello.txt\")
  15. out = \"Hello, world!\"
  16. > (read-all! \"tests/data/non-existent.txt\")
  17. out = nil
  18. ```"
  19. (read-all-mode! path false))
  20. (defun read-lines! (path)
  21. "Reads the lines from the file at PATH and returns it as a list of strings.
  22. Returns nil if it failed.
  23. ### Example
  24. ```cl
  25. > (read-lines! \"tests/data/lines.txt\")
  26. out = (\"This is the first line.\" \"This is the second.\")
  27. > (read-lines! \"tests/data/non-existent.txt\")
  28. out = nil
  29. ```"
  30. (with ((data err) (read-all-mode! path true))
  31. (if data
  32. (string/split data "\n")
  33. (values-list nil err))))
  34. (defun read-bytes! (path)
  35. "Reads the data from the file at PATH and returns it as a list of bytes
  36. (numbers). Returns nil if it failed.
  37. ### Example
  38. ```cl
  39. > (read-bytes! \"tests/data/abc.txt\")
  40. out = (97 98 99)
  41. > (read-bytes! \"tests/data/non-existent.txt\")
  42. out = nil
  43. ```"
  44. (with ((data err) (read-all-mode! path true))
  45. (if data
  46. (string/string->bytes data)
  47. (values-list data err))))
  48. (defun write-all-mode! (path append binary data) :hidden
  49. (with (handle (open path (.. (if append "a" "w") (if binary "b" ""))))
  50. (cond
  51. [handle
  52. (self handle :write data)
  53. (self handle :close)
  54. true]
  55. [else false])))
  56. (defun write-all-bytes-mode! (path append data) :hidden
  57. (with (handle (open path (if append "ab" "wb")))
  58. (cond
  59. [handle
  60. ;; Appending each byte at a time ends up being faster than generating
  61. ;; a string and appending it.
  62. (with (write (.> handle :write))
  63. (for-each byte data
  64. (write handle (string/char byte))))
  65. (self handle :close)
  66. true]
  67. [else false])))
  68. (defun write-all! (path data)
  69. "Writes the string DATA to the file at PATH.
  70. Creates a new file if it doesn't exist and overwrite the file if it
  71. does. Returns true if it succeeded or false if it failed.
  72. ### Example
  73. ```cl
  74. > (write-all! \"tests/data/hello_.txt\" \"Hello, world!\")
  75. out = true
  76. ```"
  77. (write-all-mode! path false false data))
  78. (defun write-lines! (path data)
  79. "Writes the lines (list of strings) DATA to the file at PATH.
  80. Creates a new file if it doesn't exist and overwrite the file if it
  81. does. Returns true if it succeeded or false if it failed.
  82. ### Example
  83. ```cl
  84. > (write-lines! \"tests/data/lines_.txt\" `(\"This is the first line.\" \"This is the second.\"))
  85. out = true
  86. ```"
  87. (write-all! path (concat data "\n")))
  88. (defun write-bytes! (path data)
  89. "Writes the bytes (list of numbers) DATA to the file at PATH.
  90. Creates a new file if it doesn't exist and overwrite the file if it
  91. does. Returns true if it succeeded or false if it failed.
  92. ### Example
  93. ```cl
  94. > (write-bytes! \"tests/data/abc_.txt\" `(97 98 99))
  95. out = true
  96. ```"
  97. (write-all-bytes-mode! path false data))
  98. (defun append-all! (path data)
  99. "Appends the string DATA to the file at PATH.
  100. Creates a new file if it doesn't exist.
  101. Returns true if it succeeded or false if it failed.
  102. ### Example
  103. ```cl
  104. > (append-all! \"tests/data/hello_.txt\" \" Some appended text.\")
  105. out = true
  106. ```"
  107. (write-all-mode! path true false data))
  108. (defun append-lines! (path data)
  109. "Appends the lines (list of strings) DATA to the file at PATH.
  110. Creates a new file if it doesn't exist. Returns true if it succeeded
  111. or false if it failed.
  112. ### Example
  113. ```cl
  114. > (append-lines! \"tests/data/lines_.txt\" `(\" Here's another line:\" \"Another line.\"))
  115. out = true
  116. ```"
  117. (append-all! path (concat data "\n")))
  118. (defun append-bytes! (path data)
  119. "Appends the bytes (list of numbers) DATA to the file at PATH.
  120. Creates a new file if it doesn't exist. Returns true if it succeeded
  121. or false if it failed.
  122. ### Example
  123. ```cl
  124. > (append-bytes! \"tests/data/abc_.txt\" `(100 101 102))
  125. out = true
  126. ```"
  127. (write-all-bytes-mode! path true data))