R/read_helpers.R

Defines functions read.c.string

#' @title Read a fixed-size, NUL-terminated C string from a binary connection.
#'
#' @description Reads a fixed-size string field as used by the Quake file formats (WAL, WAD, PAK, MDL, MD2). Such fields are NUL-padded C strings: the string is followed by zero bytes up to the fixed field size. Reading them with \code{readChar} triggers the "truncating string with embedded nuls" warning, so this helper reads the raw bytes and truncates at the first NUL byte instead (which is also the correct semantics for a C string).
#'
#' @param con a binary connection, opened for reading.
#'
#' @param n integer, the number of bytes to read (the fixed field size in the file).
#'
#' @return character string, the string up to (and excluding) the first NUL byte. An empty string if the field starts with (or entirely consists of) NUL bytes.
#'
#' @noRd
read.c.string <- function(con, n) {
  raw = readBin(con, "raw", n = n);
  nul_pos = which(raw == as.raw(0));
  if(length(nul_pos) > 0L) {
    raw = raw[seq_len(nul_pos[1] - 1L)];
  }
  return(rawToChar(raw));
}

Try the wal package in your browser

Any scripts or data that you put into this service are public.

wal documentation built on Aug. 22, 2026, 5:07 p.m.