makeres.sh 865 B

123456789101112131415161718192021222324252627282930313233343536
  1. #! /bin/sh
  2. echo "/* Generated by $0 */"
  3. needcomma=0
  4. types=
  5. while read -r line; do
  6. [ -z "${line%%#*}" ] && continue # skip comments
  7. if [ -n "${line}" -a -z "${line%%:*}" ]; then
  8. [ ${needcomma} -eq 1 ] && printf ",\n};\n\n"
  9. type="${line#:}"
  10. [ "${type}" = "end" ] && break
  11. printf "static const char* res_%s[] = {\n" "${type}"
  12. needcomma=0
  13. types="${types}${types:+ }${type}"
  14. else
  15. [ ${needcomma} -eq 1 ] && printf ",\n"
  16. printf "\t\"%s\"" "${line}"
  17. needcomma=1
  18. fi
  19. done
  20. echo "typedef struct {"
  21. echo " const char* name;"
  22. echo " const char** res;"
  23. echo " const size_t size;"
  24. echo "} res_t;"
  25. echo
  26. echo "static const res_t res[] = {"
  27. needcomma=0
  28. for type in ${types}; do
  29. [ ${needcomma} -eq 1 ] && printf ",\n"
  30. printf "\t{\"%s\",\tres_%s,\tsizeof(res_%s)/sizeof(res_%s[0])}" "${type}" "${type}" "${type}" "${type}"
  31. needcomma=1
  32. done
  33. echo
  34. echo "};"