detect_enc() retourne faux si l'encoding n'a pas pu être détecté

This commit is contained in:
Jephte CLAIN 2014-10-12 22:15:33 +04:00
parent db51a75dbe
commit 43bdecc08b
1 changed files with 11 additions and 12 deletions

23
fconv
View File

@ -41,18 +41,19 @@ COMMANDES
}
function detect_enc() {
local enc
if [ -f "$1" ]; then
"$scriptdir/lib/pywrapper" uencdetect.py -f "$1"
return $?
setx enc="$scriptdir/lib/pywrapper" uencdetect.py -f "$1"
elif [ -d "$1" ]; then
local -a files
setx -a files=evalp find "$1" -type f // head -n1
if [ ${#files[*]} -gt 0 ]; then
"$scriptdir/lib/pywrapper" uencdetect.py -f "${files[0]}"
return $?
setx enc="$scriptdir/lib/pywrapper" uencdetect.py -f "${files[0]}"
fi
fi
echo Unknown
[ -n "$enc" ] || enc=Unknown
echo "$enc"
[ "$enc" != Unknown ]
}
function before_parse_cmd() {
@ -64,8 +65,7 @@ function before_parse_cmd() {
local from="${3:-detect}"
if [ "$from" == "detect" ]; then
setx from=detect_enc "$FILE"
if [ "$from" == "Unknown" ]; then
if ! setx from=detect_enc "$FILE"; then
echo "$FILE: Impossible de détecter l'encoding"
return 1
fi
@ -83,11 +83,7 @@ function before_parse_cmd() {
function iconv_detect() {
local to="$1"; shift
local from
setx from=detect_enc "$FILE"
if [ "$from" == "Unknown" ]; then
cat
return 1
else
if setx from=detect_enc "$FILE"; then
if [ -n "$REVERSE_CONV" ]; then
local tmp="$to"
to="$from"
@ -95,6 +91,9 @@ function iconv_detect() {
fi
iconv -f "$from" -t "$to" "$@"
return 0
else
cat
return 1
fi
}