#! /bin/ksh

#
# Name        : ${HOME}/bin/MakeTable2.ksh
#
# Version     : 1.0
#
# Author      : Gerard van Wageningen
#
# Description : Create 1-byte to multi-byte translation table
#
# 1.0   First Version
#

typeset i=''
typeset j=''

\whence set_debug 1>/dev/null 2>&1 && eval $( set_debug ${0##*/} )

\cd "${HOME}/src"

print "#include <stdio.h>\n\nstatic char CP1252[][256]= {" > CP1252ToUTF-8.c 

for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f
do
  for j in 0 1 2 3 4 5 6 7 8 9 a b c d e f
  do
    print -- "${i}${j}0a" | dx
  done
done \
| iconv -f CP1252 -t UTF-8 \
| xxd \
| cut -c10-48 \
| sed -e '$s/$/z/' \
| tr -d '\n ' \
| tr 'z' '\n' \
| sed -e 's/\(..\)/0x\1 /g; s/0x0a /y/g; s/yy/y0x0a/g' \
| tr 'y' '\n' \
| sed -e '/^$/d; s/ 0x/, 0x/g; s/^/  { /; s/$/},/' >> CP1252ToUTF-8.c

print "};\n\nint main()\n{\n  int c;\n\n  while( ( c = getchar() ) != EOF ) {\n    printf( \"%s\", CP1252[c] );\n  }\n\n  return 0;\n}" >> CP1252ToUTF-8.c
ccc CP1252ToUTF-8.c

exit $?

################# EOF #################
