#!/bin/bash # Horizontal text mirroring # $Id: unix1-mirror.sh 191 2006-03-29 11:07:00Z cactus $ # # (C) 2005 Dr. ERDI Gergo # # See http://cactus.rulez.org/elte/2005-1-unix/#1 for a description of what it does # # Licensed under the GNU General Public License, version 2 function help () { self=`basename $0` cat << EOF Usage: $self [-c] [-s] [FILE] Horizontally mirrors the lines of FILE (or the standard input) Options: -c Replace certain graphical characters with their mirrored counterparts -s Strip leading spaces from result -help Display this help message (C) 2005 Dr. ERDI Gergo Version: \$Id: unix1-mirror.sh 191 2006-03-29 11:07:00Z cactus $ EOF exit 0 } function error () { echo ERROR: $@! >&2 exit 1 } function options () { [ -z "$1" ] && return case "$1" in -help) help ;; -c) do_mirror_chars=1 shift options "$@" ;; -s) do_truncate_beginning=1 shift options "$@" ;; *) [ $# -gt 1 ] && error "Too many arguments" [ -f "$1" -a -r "$1" ] || error "$1: Unable to open file" exec <"$1" ;; esac } # Mivel a feladat lenyegebol kovetkezik, hogy ismernunk kell a leghosszabb # sort, nem tudjuk "azonnal" feldolgozni, hanem ideiglenesen el kell eloszor # tarolnunk az osszes sort. # Ez biztositja, hogy programunk lassu es nagy memoriaigenyu is lesz. # # Eredetileg pure Bash-ben irtam meg, de az borzaszto lassu lett (2 # perces teszt-futasidok, ugyhogy Awk lett belole. function mirror () { AWKPROG=' BEGIN { width = 0 } { lines[size++] = $0; if (length () > width) width = length (); } END { for (i = 0; i != size; i++) { for (j = length (lines[i]); j < width; j++) printf " "; for (j = length (lines[i]); j != 0; j--) printf "%s", substr (lines[i], j, 1) print ""; } }' awk "$AWKPROG" 2>/dev/null } function mirror_chars () { [ -z $do_mirror_chars ] && (cat; return) # Hogy en mit szenvedtem, mire megfelelo " es ' vedelmeket # talaltam ezekhez... local class1='()<>\\/'"\`'"'\]\[}{' local class2=')(>/dev/null } function truncate_beginning () { [ -z $do_truncate_beginning ] && (cat; return) sed -e 's/^ *//' 2>/dev/null } options "$@" mirror | sed -e 's/ *$//' | truncate_beginning | mirror_chars