fmt_os.odin 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //+build !freestanding
  2. //+build !js
  3. package fmt
  4. import "core:runtime"
  5. import "core:os"
  6. import "core:io"
  7. import "core:bufio"
  8. // fprint formats using the default print settings and writes to fd
  9. fprint :: proc(fd: os.Handle, args: ..any, sep := " ", flush := true) -> int {
  10. buf: [1024]byte
  11. b: bufio.Writer
  12. defer bufio.writer_flush(&b)
  13. bufio.writer_init_with_buf(&b, os.stream_from_handle(fd), buf[:])
  14. w := bufio.writer_to_writer(&b)
  15. return wprint(w, ..args, sep=sep, flush=flush)
  16. }
  17. // fprintln formats using the default print settings and writes to fd
  18. fprintln :: proc(fd: os.Handle, args: ..any, sep := " ", flush := true) -> int {
  19. buf: [1024]byte
  20. b: bufio.Writer
  21. defer bufio.writer_flush(&b)
  22. bufio.writer_init_with_buf(&b, os.stream_from_handle(fd), buf[:])
  23. w := bufio.writer_to_writer(&b)
  24. return wprintln(w, ..args, sep=sep, flush=flush)
  25. }
  26. // fprintf formats according to the specified format string and writes to fd
  27. fprintf :: proc(fd: os.Handle, fmt: string, args: ..any, flush := true) -> int {
  28. buf: [1024]byte
  29. b: bufio.Writer
  30. defer bufio.writer_flush(&b)
  31. bufio.writer_init_with_buf(&b, os.stream_from_handle(fd), buf[:])
  32. w := bufio.writer_to_writer(&b)
  33. return wprintf(w, fmt, ..args, flush=flush)
  34. }
  35. fprint_type :: proc(fd: os.Handle, info: ^runtime.Type_Info, flush := true) -> (n: int, err: io.Error) {
  36. buf: [1024]byte
  37. b: bufio.Writer
  38. defer bufio.writer_flush(&b)
  39. bufio.writer_init_with_buf(&b, os.stream_from_handle(fd), buf[:])
  40. w := bufio.writer_to_writer(&b)
  41. return wprint_type(w, info, flush=flush)
  42. }
  43. fprint_typeid :: proc(fd: os.Handle, id: typeid, flush := true) -> (n: int, err: io.Error) {
  44. buf: [1024]byte
  45. b: bufio.Writer
  46. defer bufio.writer_flush(&b)
  47. bufio.writer_init_with_buf(&b, os.stream_from_handle(fd), buf[:])
  48. w := bufio.writer_to_writer(&b)
  49. return wprint_typeid(w, id, flush=flush)
  50. }
  51. // print formats using the default print settings and writes to os.stdout
  52. print :: proc(args: ..any, sep := " ", flush := true) -> int { return fprint(os.stdout, ..args, sep=sep, flush=flush) }
  53. // println formats using the default print settings and writes to os.stdout
  54. println :: proc(args: ..any, sep := " ", flush := true) -> int { return fprintln(os.stdout, ..args, sep=sep, flush=flush) }
  55. // printf formats according to the specified format string and writes to os.stdout
  56. printf :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(os.stdout, fmt, ..args, flush=flush) }
  57. // eprint formats using the default print settings and writes to os.stderr
  58. eprint :: proc(args: ..any, sep := " ", flush := true) -> int { return fprint(os.stderr, ..args, sep=sep, flush=flush) }
  59. // eprintln formats using the default print settings and writes to os.stderr
  60. eprintln :: proc(args: ..any, sep := " ", flush := true) -> int { return fprintln(os.stderr, ..args, sep=sep, flush=flush) }
  61. // eprintf formats according to the specified format string and writes to os.stderr
  62. eprintf :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(os.stderr, fmt, ..args, flush=flush) }