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