News for hackers. Stuff that matters.
Re:Embedding files in C programs with koio by wahern on Wednesday May 30, 2018 @05:23PM (#17191958)
I imagine that many situations where you might want to embed assets into a binary involve embedded work. With embedded work you often want to be able to cross-compile. Requiring that the koio tool be built first on the host architecture (as opposed to the target architecture) gets messy, especially if you can't or don't want to depend on having it preinstalled.

The koio utility might better written in POSIX shell.

FWIW, here's a simple POSIX shell-compatible routine that will convert an 8-bit stream into a quoted C string

  cstring() {
    # use od to translate each byte to hexadecimal, sed to format as
    # proper C string
    od -An -tx1 -v | sed -ne '/./p' | sed -e '
      # prefix \x to each hexadecimal pair and remove trailing space
      s/\([0-9a-fA-F][0-9a-fA-F]\)[[:space:]]*/\\x\1/g;
  
      # quote escaped bytes
      s/^[[:space:]]*/"/;
      s/$/"/;
  
      # escape newline for all but the last line
      $!s/$/ \\/;
    '
  }

There are no comments on this comment yet.