fmt_os.odin 2.7 KB

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