diff --git a/dhl.sh b/dhl.sh index 620a737..66c3335 100755 --- a/dhl.sh +++ b/dhl.sh @@ -1,5 +1,5 @@ #!/usr/bin/env sh -dependencies="sed test cp libreoffice" +dependencies="sed test cp libreoffice expr" assert_tools () { while test $# -gt 0; do which $1 >/dev/null 2>/dev/null || { @@ -11,6 +11,53 @@ assert_tools () { } assert_tools ${dependencies} +testcmp () { + # usage: testcmp "cmpresult" "cmdfunctionname" [ "argument1", "argument2", […] ] + if test "$#" -eq 0 ; then + echo "arguments missing."; exit 1 + fi + case $2 in + #whitelisted fn + chklength) + expect="$1"; shift + fn="$1"; shift + result=$("$fn" "$1" "$2") + if test "$result" = "$expect" ; then + echo "$fn passed for: $1 $2." + else + echo "$fn failed for: $1 $2." + fi + ;; + iso3countrycode) + expect="$1"; shift + fn="$1"; shift + for a in "$@"; do + result=$("$fn" "$a") + if test "$result" = "$expect" ; then + echo "$fn passed for: $a." + else + echo "$fn failed for: $a." + fi + done + ;; + *) + echo "no test performed, function or command not allowd." + esac +} + +chklength () { + test "$#" -eq "0" && { echo "-1"; } + expect="$1"; shift + result=$(expr length "$1") + test "$expect" -eq "$result" && { echo "true"; } || { echo "$result"; } +} + +testchklength () { + chklength "3" "123" + testcmp "true" "chklength" "3" "123" + testcmp "3" "chklength" "2" "1-2" +} + # target filename tn="dhl-cp1252.csv" @@ -23,11 +70,6 @@ cp -f "${fn}" "${fn}.bak" # replace special spaces and hyphens sed -i -e 's/[\u00A0\u202F[:space:]]+/ /g' -e 's/[\u2010\u2011\u2012\u2013\u002D]+/-/g' -e 's/[\u00AD\uFEFF]+//g' "$fn" -# todo: convert encoding from UTF-8 to CP1252 "Windows" - -# a possibility for conversion? seems not to be reliant -#~ iconv -o "$tn" -f UTF-8 -t CP1252 "$fn" - # todo: validate length for entries # todo: convert country to countrycode as in ISO3 @@ -49,32 +91,6 @@ iso3countrycode () { fi } -testcmp () { - # usage: testcmp "cmpresult" "cmdfunctionname" [ "argument1", "argument2", […] ] - if test "$#" -eq 0 ; then - echo "arguments missing."; exit 1 - fi - case $2 in - #whitelisted fn - iso3countrycode) - expected="$1" - shift - fn="$1" - shift - for a in "$@"; do - result=$("$fn" "$a") - if test "$result" = "$expected" ; then - echo "$fn passed for: $a." - else - echo "$fn failed for: $a." - fi - done - ;; - *) - echo "no test performed, function or command not allowd." - esac -} - testiso3 () { # covering data in https://doku.ccc.de/index.php?title=Attribut:Chaostreff-Country&limit=500&offset=0 testcmp "BEL" "iso3countrycode" "Belgien" @@ -89,10 +105,11 @@ testiso3 () { } #~ testiso3 +# convert encoding from UTF-8 to CP1252 "Windows" +# another possibility for conversion? seems not to be relyable: iconv -o "$of" -f UTF-8 -t CP1252 "$if" convcsv () { if test $# -eq 0 ; then echo "argument missing."; exit 1; fi if test -e $1 ; then echo "file already exists."; exit 2; fi -# charset windows codepage 1252 # doc for headless conversion options https://wiki.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options if="${1}" of=$(echo "${if}"|rev|cut -d. -f2-|rev)".ods"