fmt_os.odin 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //+build !freestanding
  2. //+build !js
  3. package fmt
  4. import "base: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, newline := false) -> 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, newline=newline)
  34. }
  35. // fprintfln formats according to the specified format string and writes to fd, followed by a newline.
  36. fprintfln :: proc(fd: os.Handle, fmt: string, args: ..any, flush := true) -> int {
  37. return fprintf(fd, fmt, ..args, flush=flush, newline=true)
  38. }
  39. fprint_type :: proc(fd: os.Handle, info: ^runtime.Type_Info, flush := true) -> (n: int, err: io.Error) {
  40. buf: [1024]byte
  41. b: bufio.Writer
  42. defer bufio.writer_flush(&b)
  43. bufio.writer_init_with_buf(&b, os.stream_from_handle(fd), buf[:])
  44. w := bufio.writer_to_writer(&b)
  45. return wprint_type(w, info, flush=flush)
  46. }
  47. fprint_typeid :: proc(fd: os.Handle, id: typeid, flush := true) -> (n: int, err: io.Error) {
  48. buf: [1024]byte
  49. b: bufio.Writer
  50. defer bufio.writer_flush(&b)
  51. bufio.writer_init_with_buf(&b, os.stream_from_handle(fd), buf[:])
  52. w := bufio.writer_to_writer(&b)
  53. return wprint_typeid(w, id, flush=flush)
  54. }
  55. // print formats using the default print settings and writes to os.stdout
  56. print :: proc(args: ..any, sep := " ", flush := true) -> int { return fprint(os.stdout, ..args, sep=sep, flush=flush) }
  57. // println formats using the default print settings and writes to os.stdout
  58. println :: proc(args: ..any, sep := " ", flush := true) -> int { return fprintln(os.stdout, ..args, sep=sep, flush=flush) }
  59. // printf formats according to the specified format string and writes to os.stdout
  60. printf :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(os.stdout, fmt, ..args, flush=flush) }
  61. // printfln formats according to the specified format string and writes to os.stdout, followed by a newline.
  62. printfln :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(os.stdout, fmt, ..args, flush=flush, newline=true) }
  63. // eprint formats using the default print settings and writes to os.stderr
  64. eprint :: proc(args: ..any, sep := " ", flush := true) -> int { return fprint(os.stderr, ..args, sep=sep, flush=flush) }
  65. // eprintln formats using the default print settings and writes to os.stderr
  66. eprintln :: proc(args: ..any, sep := " ", flush := true) -> int { return fprintln(os.stderr, ..args, sep=sep, flush=flush) }
  67. // eprintf formats according to the specified format string and writes to os.stderr
  68. eprintf :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(os.stderr, fmt, ..args, flush=flush) }
  69. // eprintfln formats according to the specified format string and writes to os.stderr, followed by a newline.
  70. eprintfln :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(os.stderr, fmt, ..args, flush=flush, newline=true) }