#! /usr/bin/gawk -f # Last edited on 2002-01-03 22:59:49 by stolfi BEGIN { abort = -1; usage = ( \ "elem-to-stroke-pair \\\n" \ " [ -v inField=NUM ] \\\n" \ " [ -v erase=BOOL ] \\\n" \ " [ -v outField=NUM ] \\\n" \ " < INFILE > OUTFILE" \ ); # Expects the `inField'th field of INFILE to be a # string of elements, each surrounded in braces, already capitalized as # in full EVA. Replaces each element by three pseudo-elements: # a left stroke code, a "X", and a right-stroke code, # each inside braces "{}". # # If `erase' is set, the input field is erased, otherwise it is # preserved. Then inserts the stroke string as the "outField"th # field. if (inField == "") inField = 1; if (erase == "") erase = 0; if (outField == "") outField = inField; } (abort >= 0) { exit abort; } /^#/ { print; next; } /./ { if (NF < inField) { data_error("not enough input fields"); } x = $(inField); y = convert_elems_to_stroke_pairs(x); printout(y, outField, inField, erase); next; } function convert_elems_to_stroke_pairs(x) { gsub(/{e}/, "", x); gsub(/{i}/, "", x); gsub(/{o}/, "", x); gsub(/{a}/, "", x); gsub(/{y}/, "", x); gsub(/{q}/, "", x); gsub(/{l}/, "", x); gsub(/{d}/, "", x); gsub(/{r}/, "", x); gsub(/{s}/, "", x); gsub(/{n}/, "", x); gsub(/{m}/, "", x); gsub(/{Ch}/, "", x); gsub(/{Sh}/, "", x); gsub(/{k}/, "

", x); gsub(/{t}/, "

", x); gsub(/{CKh}/, "", x); gsub(/{CTh}/, "", x); gsub(/{f}/, "", x); gsub(/{p}/, "", x); gsub(/{CFh}/, "", x); gsub(/{CPh}/, "", x); # Validity check: if (x ~ /[{}]/) { data_error(("bad element \"" x "\"")); } # Convert "<>" into "{}" in strokes: gsub(/[<]/, "{", x); gsub(/[>]/, "}", x); return x; } function printout(mw, ofn, ifn, del, i) { # prints $0 with "mw" inserted as field "$(ofn)" # if "del" is true, deletes field "$(ifn)" if (del) { if (NF < ifn) { data_error("not enough input fields\n"); } else { for(i=ifn; i> "/dev/stderr"; abort = 1; exit 1; } function arg_error(msg) { printf "%s\n", msg >> "/dev/stderr"; abort = 1; exit 1; }