bc0daa57e76433e7fde5e352df577d5120e3bcc1 markd Mon Jan 13 01:11:31 2025 -0800 Add an version of stringify that is in interperted langauges so it does not have to be built first and in the path. Useful if building just parts of the tree diff --git src/utils/stringify/stringifyEz src/utils/stringify/stringifyEz new file mode 100755 index 0000000..a737f00 --- /dev/null +++ src/utils/stringify/stringifyEz @@ -0,0 +1,68 @@ +#!/bin/bash -e +# implementation of stringify in bash + +set -beEu -o pipefail +staticPrefix="" +variableName="" +inputFile="" + +# Print usage +usage() { + echo "Usage:" >&2 + echo " stringify [options] in.txt" >&2 + echo "" >&2 + echo "Options:" >&2 + echo " -var=varname - Create a variable with the specified name containing the string." >&2 + echo " -static - Create the variable but put static in front of it." >&2 + exit 1 +} + +# Check if no arguments are passed +if [[ "$#" -lt 1 ]]; then + usage +fi + +# Parse command-line arguments +while [[ "$#" -gt 0 ]]; do + case "$1" in + -var=*) # Match -var=<varname> + variableName="${1#-var=}" # Extract the variable name + ;; + -static) # Match -static + staticPrefix="static " + ;; + -*) + echo "Unknown option: $1" + usage + ;; + *) + inputFile="$1" + ;; + esac + shift +done + +if [[ -z "$inputFile" ]]; then + echo "Error: Input file is required." >&2 + usage +fi + +if [[ -z "$variableName" && -n "$staticPrefix" ]]; then + echo "Error: -static requires -var=" >&2 + usage +fi + +# Output the C string +if [[ -n "$variableName" ]]; then + echo "${staticPrefix}char *${variableName} =" +fi + + +# escal \ and " +awk '{ + gsub(/\\/, "\\\\"); + gsub(/"/, "\\\""); + print "\"" $0 "\""; +}' ${inputFile} + +echo ';'