fmt_os.odin 3.8 KB

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