LINUX.ORG.RU

Yet another bin2c...


0

0

Может кому-нибудь пригодится:

#!/bin/sh

export LC_ALL=C

if [ $# -ne 2 ]; then
        echo "Invalid number of arguments!" >&2
        echo "Usage:"
        echo "    ${0} <input file> <C variable name>"
        echo "        If <input file> is -, read standard input."
        echo "Example:"
        echo "    ${0} ${0} bin2c"
        echo "        This command will create files bin2c.h and bin2c.c with"
        echo "        variables 'const unsigned char *const bin2c;' and"
        echo "        'const size_t bin2c_size;'."

        exit 1
fi

od --width=8 -A n -v -t x1 "${1}" | (
        echo "/* ${0} \"${1}\" \"${2}\" */"
        echo
        echo "#include \"${2}.h\""
        echo
        echo -n "const unsigned char *const ${2} = (unsigned char []) {"

        bytes=0
        l_end=""
        while read line; do
                echo "${l_end}"
                l_begin="    "
                for code in $line; do
                        echo -n "${l_begin}0x${code}"
                        l_begin=", "
                        bytes=$(( $bytes + 1 ))
                done
                l_end=","
        done

        echo "};"

        echo
        echo "const size_t ${2}_size = ${bytes};"
        echo
) > "${2}.c"

(
        echo "/* ${0} \"${1}\" \"${2}\" */"
        echo
        echo "#include <stddef.h>"
        echo
        echo "extern const unsigned char *const ${2};"
        echo "extern const size_t ${2}_size;"
        echo
) > "${2}.h"
Deleted

Ответ на: комментарий от anonymous

+1, нужно уметь использовать готовые велосипеды, дабы не изобретать новые :)

quarck
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.