io.odin 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. package io
  2. import "core:intrinsics"
  3. import "core:runtime"
  4. import "core:unicode/utf8"
  5. Seek_From :: enum {
  6. Start = 0, // seek relative to the origin of the file
  7. Current = 1, // seek relative to the current offset
  8. End = 2, // seek relative to the end
  9. }
  10. Error :: enum i32 {
  11. // No Error
  12. None = 0,
  13. // EOF is the error returned by `read` when no more input is available
  14. EOF,
  15. // Unexpected_EOF means that EOF was encountered in the middle of reading a fixed-sized block of data
  16. Unexpected_EOF,
  17. // Short_Write means that a write accepted fewer bytes than requested but failed to return an explicit error
  18. Short_Write,
  19. // Invalid_Write means that a write returned an impossible count
  20. Invalid_Write,
  21. // Short_Buffer means that a read required a longer buffer than was provided
  22. Short_Buffer,
  23. // No_Progress is returned by some implementations of `io.Reader` when many calls
  24. // to `read` have failed to return any data or error.
  25. // This is usually a signed of a broken `io.Reader` implementation
  26. No_Progress,
  27. Invalid_Whence,
  28. Invalid_Offset,
  29. Invalid_Unread,
  30. Negative_Read,
  31. Negative_Write,
  32. Negative_Count,
  33. Buffer_Full,
  34. // Unknown means that an error has occurred but cannot be categorized
  35. Unknown,
  36. // Empty is returned when a procedure has not been implemented for an io.Stream
  37. Empty = -1,
  38. }
  39. Close_Proc :: proc(using s: Stream) -> Error;
  40. Flush_Proc :: proc(using s: Stream) -> Error;
  41. Seek_Proc :: proc(using s: Stream, offset: i64, whence: Seek_From) -> (n: i64, err: Error);
  42. Size_Proc :: proc(using s: Stream) -> i64;
  43. Read_Proc :: proc(using s: Stream, p: []byte) -> (n: int, err: Error);
  44. Read_At_Proc :: proc(using s: Stream, p: []byte, off: i64) -> (n: int, err: Error);
  45. Read_From_Proc :: proc(using s: Stream, r: Reader) -> (n: i64, err: Error);
  46. Read_Byte_Proc :: proc(using s: Stream) -> (byte, Error);
  47. Read_Rune_Proc :: proc(using s: Stream) -> (ch: rune, size: int, err: Error);
  48. Unread_Byte_Proc :: proc(using s: Stream) -> Error;
  49. Unread_Rune_Proc :: proc(using s: Stream) -> Error;
  50. Write_Proc :: proc(using s: Stream, p: []byte) -> (n: int, err: Error);
  51. Write_At_Proc :: proc(using s: Stream, p: []byte, off: i64) -> (n: int, err: Error);
  52. Write_To_Proc :: proc(using s: Stream, w: Writer) -> (n: i64, err: Error);
  53. Write_Byte_Proc :: proc(using s: Stream, c: byte) -> Error;
  54. Write_Rune_Proc :: proc(using s: Stream, r: rune) -> (size: int, err: Error);
  55. Destroy_Proc :: proc(using s: Stream) -> Error;
  56. Stream :: struct {
  57. using stream_vtable: ^Stream_VTable,
  58. stream_data: rawptr,
  59. }
  60. Stream_VTable :: struct {
  61. impl_close: Close_Proc,
  62. impl_flush: Flush_Proc,
  63. impl_seek: Seek_Proc,
  64. impl_size: Size_Proc,
  65. impl_read: Read_Proc,
  66. impl_read_at: Read_At_Proc,
  67. impl_read_byte: Read_Byte_Proc,
  68. impl_read_rune: Read_Rune_Proc,
  69. impl_write_to: Write_To_Proc,
  70. impl_write: Write_Proc,
  71. impl_write_at: Write_At_Proc,
  72. impl_write_byte: Write_Byte_Proc,
  73. impl_write_rune: Write_Rune_Proc,
  74. impl_read_from: Read_From_Proc,
  75. impl_unread_byte: Unread_Byte_Proc,
  76. impl_unread_rune: Unread_Rune_Proc,
  77. impl_destroy: Destroy_Proc,
  78. }
  79. Reader :: struct {using stream: Stream};
  80. Writer :: struct {using stream: Stream};
  81. Closer :: struct {using stream: Stream};
  82. Flusher :: struct {using stream: Stream};
  83. Seeker :: struct {using stream: Stream};
  84. Read_Writer :: struct {using stream: Stream};
  85. Read_Closer :: struct {using stream: Stream};
  86. Read_Write_Closer :: struct {using stream: Stream};
  87. Read_Write_Seeker :: struct {using stream: Stream};
  88. Write_Closer :: struct {using stream: Stream};
  89. Write_Seeker :: struct {using stream: Stream};
  90. Write_Flusher :: struct {using stream: Stream};
  91. Write_Flush_Closer :: struct {using stream: Stream};
  92. Reader_At :: struct {using stream: Stream};
  93. Writer_At :: struct {using stream: Stream};
  94. Reader_From :: struct {using stream: Stream};
  95. Writer_To :: struct {using stream: Stream};
  96. Byte_Reader :: struct {using stream: Stream};
  97. Byte_Scanner :: struct {using stream: Stream};
  98. Byte_Writer :: struct {using stream: Stream};
  99. Rune_Reader :: struct {using stream: Stream};
  100. Rune_Scanner :: struct {using stream: Stream};
  101. destroy :: proc(s: Stream) -> Error {
  102. close_err := close({s});
  103. if s.stream_vtable != nil && s.impl_destroy != nil {
  104. return s->impl_destroy();
  105. }
  106. if close_err != .None {
  107. return close_err;
  108. }
  109. return .Empty;
  110. }
  111. read :: proc(s: Reader, p: []byte) -> (n: int, err: Error) {
  112. if s.stream_vtable != nil && s.impl_read != nil {
  113. return s->impl_read(p);
  114. }
  115. return 0, .Empty;
  116. }
  117. write :: proc(s: Writer, p: []byte) -> (n: int, err: Error) {
  118. if s.stream_vtable != nil && s.impl_write != nil {
  119. return s->impl_write(p);
  120. }
  121. return 0, .Empty;
  122. }
  123. seek :: proc(s: Seeker, offset: i64, whence: Seek_From) -> (n: i64, err: Error) {
  124. if s.stream_vtable != nil && s.impl_seek != nil {
  125. return s->impl_seek(offset, whence);
  126. }
  127. return 0, .Empty;
  128. }
  129. close :: proc(s: Closer) -> Error {
  130. if s.stream_vtable != nil && s.impl_close != nil {
  131. return s->impl_close();
  132. }
  133. // Instead of .Empty, .None is fine in this case
  134. return .None;
  135. }
  136. flush :: proc(s: Flusher) -> Error {
  137. if s.stream_vtable != nil && s.impl_flush != nil {
  138. return s->impl_flush();
  139. }
  140. // Instead of .Empty, .None is fine in this case
  141. return .None;
  142. }
  143. size :: proc(s: Stream) -> i64 {
  144. if s.stream_vtable == nil {
  145. return 0;
  146. }
  147. if s.impl_size != nil {
  148. return s->impl_size();
  149. }
  150. if s.impl_seek == nil {
  151. return 0;
  152. }
  153. curr, end: i64;
  154. err: Error;
  155. if curr, err = s->impl_seek(0, .Current); err != nil {
  156. return 0;
  157. }
  158. if end, err = s->impl_seek(0, .End); err != nil {
  159. return 0;
  160. }
  161. if _, err = s->impl_seek(curr, .Start); err != nil {
  162. return 0;
  163. }
  164. return end;
  165. }
  166. read_at :: proc(r: Reader_At, p: []byte, offset: i64) -> (n: int, err: Error) {
  167. if r.stream_vtable == nil {
  168. return 0, .Empty;
  169. }
  170. if r.impl_read_at != nil {
  171. return r->impl_read_at(p, offset);
  172. }
  173. if r.impl_seek == nil || r.impl_read == nil {
  174. return 0, .Empty;
  175. }
  176. curr_offset: i64;
  177. curr_offset, err = r->impl_seek(offset, .Current);
  178. if err != nil {
  179. return 0, err;
  180. }
  181. n, err = r->impl_read(p);
  182. _, err1 := r->impl_seek(curr_offset, .Start);
  183. if err1 != nil && err == nil {
  184. err = err1;
  185. }
  186. return;
  187. }
  188. write_at :: proc(w: Writer_At, p: []byte, offset: i64) -> (n: int, err: Error) {
  189. if w.stream_vtable == nil {
  190. return 0, .Empty;
  191. }
  192. if w.impl_write_at != nil {
  193. return w->impl_write_at(p, offset);
  194. }
  195. if w.impl_seek == nil || w.impl_write == nil {
  196. return 0, .Empty;
  197. }
  198. curr_offset: i64;
  199. curr_offset, err = w->impl_seek(offset, .Current);
  200. if err != nil {
  201. return 0, err;
  202. }
  203. n, err = w->impl_write(p);
  204. _, err1 := w->impl_seek(curr_offset, .Start);
  205. if err1 != nil && err == nil {
  206. err = err1;
  207. }
  208. return;
  209. }
  210. write_to :: proc(r: Writer_To, w: Writer) -> (n: i64, err: Error) {
  211. if r.stream_vtable == nil || w.stream_vtable == nil {
  212. return 0, .Empty;
  213. }
  214. if r.impl_write_to != nil {
  215. return r->impl_write_to(w);
  216. }
  217. return 0, .Empty;
  218. }
  219. read_from :: proc(w: Reader_From, r: Reader) -> (n: i64, err: Error) {
  220. if r.stream_vtable == nil || w.stream_vtable == nil {
  221. return 0, .Empty;
  222. }
  223. if r.impl_read_from != nil {
  224. return w->impl_read_from(r);
  225. }
  226. return 0, .Empty;
  227. }
  228. read_byte :: proc(r: Byte_Reader) -> (byte, Error) {
  229. if r.stream_vtable == nil {
  230. return 0, .Empty;
  231. }
  232. if r.impl_read_byte != nil {
  233. return r->impl_read_byte();
  234. }
  235. if r.impl_read == nil {
  236. return 0, .Empty;
  237. }
  238. b: [1]byte;
  239. _, err := r->impl_read(b[:]);
  240. return b[0], err;
  241. }
  242. write_byte :: proc{
  243. write_byte_to_byte_writer,
  244. write_byte_to_writer,
  245. };
  246. write_byte_to_byte_writer :: proc(w: Byte_Writer, c: byte) -> Error {
  247. return _write_byte(w, c);
  248. }
  249. write_byte_to_writer :: proc(w: Writer, c: byte) -> Error {
  250. return _write_byte(auto_cast w, c);
  251. }
  252. @(private)
  253. _write_byte :: proc(w: Byte_Writer, c: byte) -> Error {
  254. if w.stream_vtable == nil {
  255. return .Empty;
  256. }
  257. if w.impl_write_byte != nil {
  258. return w->impl_write_byte(c);
  259. }
  260. if w.impl_write == nil {
  261. return .Empty;
  262. }
  263. b := [1]byte{c};
  264. _, err := w->impl_write(b[:]);
  265. return err;
  266. }
  267. read_rune :: proc(br: Rune_Reader) -> (ch: rune, size: int, err: Error) {
  268. if br.stream_vtable == nil {
  269. return 0, 0, .Empty;
  270. }
  271. if br.impl_read_rune != nil {
  272. return br->impl_read_rune();
  273. }
  274. if br.impl_read == nil {
  275. return 0, 0, .Empty;
  276. }
  277. b: [utf8.UTF_MAX]byte;
  278. _, err = br->impl_read(b[:1]);
  279. s0 := b[0];
  280. ch = rune(s0);
  281. size = 1;
  282. if err != nil {
  283. return;
  284. }
  285. if ch < utf8.RUNE_SELF {
  286. return;
  287. }
  288. x := utf8.accept_sizes[s0];
  289. if x >= 0xf0 {
  290. mask := rune(x) << 31 >> 31;
  291. ch = ch &~ mask | utf8.RUNE_ERROR&mask;
  292. return;
  293. }
  294. sz := int(x&7);
  295. n: int;
  296. n, err = br->impl_read(b[1:sz]);
  297. if err != nil || n+1 < sz {
  298. ch = utf8.RUNE_ERROR;
  299. return;
  300. }
  301. ch, size = utf8.decode_rune(b[:sz]);
  302. return;
  303. }
  304. unread_byte :: proc(s: Byte_Scanner) -> Error {
  305. if s.stream_vtable != nil && s.impl_unread_byte != nil {
  306. return s->impl_unread_byte();
  307. }
  308. return .Empty;
  309. }
  310. unread_rune :: proc(s: Rune_Scanner) -> Error {
  311. if s.stream_vtable != nil && s.impl_unread_rune != nil {
  312. return s->impl_unread_rune();
  313. }
  314. return .Empty;
  315. }
  316. write_string :: proc(s: Writer, str: string) -> (n: int, err: Error) {
  317. return write(s, transmute([]byte)str);
  318. }
  319. write_rune :: proc(s: Writer, r: rune) -> (size: int, err: Error) {
  320. if s.stream_vtable != nil && s.impl_write_rune != nil {
  321. return s->impl_write_rune(r);
  322. }
  323. if r < utf8.RUNE_SELF {
  324. err = write_byte(s, byte(r));
  325. if err == nil {
  326. size = 1;
  327. }
  328. return;
  329. }
  330. buf, w := utf8.encode_rune(r);
  331. return write(s, buf[:w]);
  332. }
  333. read_full :: proc(r: Reader, buf: []byte) -> (n: int, err: Error) {
  334. return read_at_least(r, buf, len(buf));
  335. }
  336. read_at_least :: proc(r: Reader, buf: []byte, min: int) -> (n: int, err: Error) {
  337. if len(buf) < min {
  338. return 0, .Short_Buffer;
  339. }
  340. for n < min && err == nil {
  341. nn: int;
  342. nn, err = read(r, buf[n:]);
  343. n += n;
  344. }
  345. if n >= min {
  346. err = nil;
  347. } else if n > 0 && err == .EOF {
  348. err = .Unexpected_EOF;
  349. }
  350. return;
  351. }
  352. // copy copies from src to dst till either EOF is reached on src or an error occurs
  353. // It returns the number of bytes copied and the first error that occurred whilst copying, if any.
  354. copy :: proc(dst: Writer, src: Reader) -> (written: i64, err: Error) {
  355. return _copy_buffer(dst, src, nil);
  356. }
  357. // copy_buffer is the same as copy except that it stages through the provided buffer (if one is required)
  358. // rather than allocating a temporary one on the stack through `intrinsics.alloca`
  359. // If buf is `nil`, it is allocate through `intrinsics.alloca`; otherwise if it has zero length, it will panic
  360. copy_buffer :: proc(dst: Writer, src: Reader, buf: []byte) -> (written: i64, err: Error) {
  361. if buf != nil && len(buf) == 0 {
  362. panic("empty buffer in io.copy_buffer");
  363. }
  364. return _copy_buffer(dst, src, buf);
  365. }
  366. // copy_n copies n bytes (or till an error) from src to dst.
  367. // It returns the number of bytes copied and the first error that occurred whilst copying, if any.
  368. // On return, written == n IFF err == nil
  369. copy_n :: proc(dst: Writer, src: Reader, n: i64) -> (written: i64, err: Error) {
  370. nsrc := limited_reader_init(&Limited_Reader{}, src, n);
  371. written, err = copy(dst, nsrc);
  372. if written == n {
  373. return n, nil;
  374. }
  375. if written < n && err == nil {
  376. // src stopped early and must have been an EOF
  377. err = .EOF;
  378. }
  379. return;
  380. }
  381. @(private)
  382. _copy_buffer :: proc(dst: Writer, src: Reader, buf: []byte) -> (written: i64, err: Error) {
  383. if dst.stream_vtable == nil || src.stream_vtable == nil {
  384. return 0, .Empty;
  385. }
  386. if src.impl_write_to != nil {
  387. return src->impl_write_to(dst);
  388. }
  389. if src.impl_read_from != nil {
  390. return dst->impl_read_from(src);
  391. }
  392. buf := buf;
  393. if buf == nil {
  394. DEFAULT_SIZE :: 4 * 1024;
  395. size := DEFAULT_SIZE;
  396. if src.stream_vtable == _limited_reader_vtable {
  397. l := (^Limited_Reader)(src.stream_data);
  398. if i64(size) > l.n {
  399. if l.n < 1 {
  400. size = 1;
  401. } else {
  402. size = int(l.n);
  403. }
  404. }
  405. }
  406. // NOTE(bill): alloca is fine here
  407. buf = transmute([]byte)runtime.Raw_Slice{intrinsics.alloca(size, 2*align_of(rawptr)), size};
  408. }
  409. for {
  410. nr, er := read(src, buf);
  411. if nr > 0 {
  412. nw, ew := write(dst, buf[0:nr]);
  413. if nw > 0 {
  414. written += i64(nw);
  415. }
  416. if ew != nil {
  417. err = ew;
  418. break;
  419. }
  420. if nr != nw {
  421. err = .Short_Write;
  422. break;
  423. }
  424. }
  425. if er != nil {
  426. if er != .EOF {
  427. err = er;
  428. }
  429. break;
  430. }
  431. }
  432. return;
  433. }