# `filenc' library - convert input file encodings # Features: # - Accept file::encoding patterns as file arguments # - Compile a list of supported encodings in a FILENCNAMES array # - Attempt to fix punctuation of encoding names function filencvalid( encoding, k ) { encoding = toupper(encoding) if (encoding in FILENCNAMES) { return encoding } else { for (k in FILENCNAMES) { if (FILENCNAMES[k] == _filencbase(encoding)) { return k } } } return "" } function _filenc( k, filenc, file, command, encoding, rc) { # compile a list of supported encodings command = "iconv -l" while ((command | getline encoding) > 0) { split(encoding, filenc, " ") for (k=1; k in filenc; k++) { FILENCNAMES[filenc[k]] = _filencbase(filenc[k]) } } close(command) # process file::encoding arguments for (k = 1; k < ARGC; k++) { if (split(ARGV[k], filenc, /\:\:/) == 2) { # file::encoding encoding = filencvalid(filenc[2]) if (encoding) { # convert to temporary file file = "/tmp/_" filenc[1] command = sprintf("iconv -f %s %s > %s", encoding, filenc[1], file) if (rc=system( command )) { # fail to convert printf ("filenc: conversion of '%s' failed with error %d\n", ARGV[k], rc) > "/dev/stderr" } else { # success _filenc_arg[file] = filenc[1] ARGV[k] = file } } else { # unsupported encoding printf ("filenc: unsupported encoding '%s'\n", filenc[2]) > "/dev/stderr" } } } } # remove punctuation from a name function _filencbase( encoding ) { gsub(/[^[:alnum:]]/, "", encoding) return toupper(encoding) } BEGIN { _filenc() } BEGINFILE { if (FILENAME in _filenc_arg) { FILENAME = _filenc_arg[FILENAME] } }