-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathf2c.sh
More file actions
39 lines (35 loc) · 1.02 KB
/
Copy pathf2c.sh
File metadata and controls
39 lines (35 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/sh
# file: f2c.sh
# vim:fileencoding=utf-8:ft=sh
# Convert a file to an array for inclusion in a C program.
#
# Copyright © 2025 R.F. Smith <rsmith@xs4all.nl>
# SPDX-License-Identifier: MIT
# Created: 2025-08-19T10:17:13+0200
# Last modified: 2025-08-19T11:46:40+0200
set -e
# Check for arguments
ID=$(basename $0)
if [ $# -lt 2 ]; then
echo "Usage: $ID infile outfile.c"
echo ""
echo "Convert “infile” to an array of unsigned characters in “outfile.c”"
echo "that can be included in to a program written in C."
echo "The name of the array is derived from the name of the input file."
echo ""
exit 0
fi
INF=$1
OUTF=$2
shift 2
# Determine the size of the input in bytes.
SIZE=`cat $INF| wc -c|tr -d ' \n'`
# Name the asset
NAME=`echo $INF|tr '.,-' '_'`
# Create initial file
echo "// Contents of “$INF”:" >$OUTF
echo "unsigned char $NAME[$SIZE] = {" >>$OUTF
hexdump -v -e '" " 16/1 "0x%02X," "\n"' $INF >>$OUTF
echo "};" >>$OUTF
# Remove trailing nonsense
sed -i '' -e 's/,0x .*$//' $OUTF