Created
June 23, 2011 13:28
-
-
Save metasim/1042527 to your computer and use it in GitHub Desktop.
Poor man's resource compiler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Poor man's resource compiler | |
# Converts given file into compilable byte stream via | |
# C code header and implementation files. | |
# Based on original by http://www.linuxjournal.com/users/mitch-frazier | |
# | |
if [[ $# -ne 1 ]]; then | |
echo "Usage: $0 FILENAME" | |
exit 1 | |
fi | |
file=$1 | |
if [[ ! -f "$file" ]]; then | |
echo "File not found: $file" | |
exit 1 | |
fi | |
CNAME=${file} | |
CNAME=${CNAME//-/_} | |
CNAME=${CNAME//./_} | |
VNAME=$(basename ${CNAME}) | |
MNAME=$(echo ${VNAME} | tr "[:lower:]" "[:upper:]") | |
DTS=$(date) | |
CMD=$(basename $0) | |
COMMENT=$(cat << EOF | |
/* | |
* Automatically generated with $CMD | |
* from $file by $USER on $DTS | |
*/ | |
EOF) | |
cat > ${VNAME}.h << EOF | |
$COMMENT | |
#ifndef __${MNAME}_H | |
#define __${MNAME}_H | |
extern unsigned char ${VNAME}[]; | |
extern unsigned int ${VNAME}_size; | |
#endif // __${MNAME}_H | |
EOF | |
cat > ${VNAME}.c << EOF | |
$COMMENT | |
#include "${VNAME}.h" | |
unsigned char ${VNAME}[] = { | |
EOF | |
hexdump -v -e '16/1 "0x%02x," "\n"' $file | \ | |
sed -e '$s/0x ,//g' >> ${VNAME}.c | |
SIZE=$(cat $file | wc -c) | |
cat >> ${VNAME}.c << EOF | |
}; | |
unsigned int ${VNAME}_size = $SIZE; | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Corresponding CMake script to generate source: