<commit_before>
# Misc utilities to enable simple integration of some low level stdlib
# stuff that doesn't map nicely to nim at the moment.

# NOTE: This is (unfortunately) going to be a dumping ground for the
# time being, but my hope is to break things into manageable pieces in
# the future


# These are currently not supported by the 'standalone' implementation
# of Nim.  That said, we need them anyway, so pilfer them until we can
# get this taken care of
type
  CFile {.importc: "FILE", header: "<stdio.h>",
         final, incompletestruct.} = object
  File* = ptr CFile ## The type representing a file handle.

var
  stdin* {.importc: "stdin", header: "<stdio.h>".}: File
  ## The standard input stream.
  stdout* {.importc: "stdout", header: "<stdio.h>".}: File
  ## The standard output stream.
  stderr* {.importc: "stderr", header: "<stdio.h>".}: File
  ## The standard error stream.

const
  IONBF* = 2


proc printf*(formatstr: cstring) {.importc: "printf", varargs,
                                  header: "<stdio.h>".}

# Enabling unbuffered IO will shoot every generated character directly
# to the _write method (typically defined in newlib.c).  Typically
# data written to stdin/stdout is line or block buffered, but this
# requires memory allocations that might not be suitable for deeply
# embedded hosts.  For more info, see the man page for setvbuf
proc setvbuf(stream: File, buf: cstring, buftype: int, size: int32): int {.importc: "setvbuf", header:"<stdio.h>".}
proc enable_unbuffered_io*(): void =
  discard setvbuf(stdin, nil, IONBF, 0)
  discard setvbuf(stdout, nil, IONBF, 0)
  discard setvbuf(stderr, nil, IONBF, 0)
<commit_msg>Make use of 'for x in [...]' syntax in stdio module<commit_after>
# Misc utilities to enable simple integration of some low level stdlib
# stuff that doesn't map nicely to nim at the moment.

# NOTE: This is (unfortunately) going to be a dumping ground for the
# time being, but my hope is to break things into manageable pieces in
# the future


# These are currently not supported by the 'standalone' implementation
# of Nim.  That said, we need them anyway, so pilfer them until we can
# get this taken care of
type
  CFile {.importc: "FILE", header: "<stdio.h>",
         final, incompletestruct.} = object
  File* = ptr CFile ## The type representing a file handle.

var
  stdin* {.importc: "stdin", header: "<stdio.h>".}: File
  ## The standard input stream.
  stdout* {.importc: "stdout", header: "<stdio.h>".}: File
  ## The standard output stream.
  stderr* {.importc: "stderr", header: "<stdio.h>".}: File
  ## The standard error stream.

const
  IONBF* = 2

proc printf*(formatstr: cstring) {.importc: "printf", varargs,
                                  header: "<stdio.h>".}

# Enabling unbuffered IO will shoot every generated character directly
# to the _write method (typically defined in newlib.c).  Typically
# data written to stdin/stdout is line or block buffered, but this
# requires memory allocations that might not be suitable for deeply
# embedded hosts.  For more info, see the man page for setvbuf
proc setvbuf(stream: File, buf: cstring, buftype: int, size: int32): int {.importc: "setvbuf", header:"<stdio.h>".}
proc enable_unbuffered_io*(): void =
  for stream in [stdin, stdout, stderr]:
      discard setvbuf(stream, nil, IONBF, 0)

{'fraction_non_alphanumeric': 0.10820559062218214, 'fraction_numerical': 0.003005710850616171, 'mean_word_length': 3.7475035663338088, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '92844', 'n_tokens_mistral': 1037, 'n_tokens_neox': 991, 'n_words': 441}