cltostr.sh 918 B

123456789101112131415161718192021222324252627282930313233
  1. #!/bin/sh
  2. # Convert standard input to a C char array, write to a file, then create an
  3. # MD5 sum of that file and append said MD5 sum as char array to the file.
  4. [ -n "$1" ] || exit 1
  5. # Filter out whitespace, empty lines, and comments.
  6. sanitize() {
  7. sed 's/^[[:space:]]*//; /^$/d; /^\/\//d'
  8. }
  9. # Convert stdin to a \0-terminated char array.
  10. dump() {
  11. echo "static const char $1[] = {"
  12. od -v -A n -t x1 | sed 's/[[:space:]]*\([[:alnum:]]\{2\}\)/0x\1, /g'
  13. echo '0x00 };'
  14. }
  15. # Print MD5 hash w/o newline character to not embed the character in the array.
  16. hash() {
  17. # md5sum is not standard, so try different platform-specific alternatives.
  18. { md5sum "$1" || md5 -q "$1" || digest -a md5 "$1"; } 2>/dev/null |
  19. cut -b -32 | tr -d '\n\r'
  20. }
  21. trap 'rm -f "$1.temp"' EXIT
  22. sanitize | tee "$1.temp" |
  23. dump 'x264_opencl_source' > "$1"
  24. hash "$1.temp" |
  25. dump 'x264_opencl_source_hash' >> "$1"