#! /usr/bin/gawk -f # Last edited on 2010-11-20 18:32:05 by stolfilocal # THIS FILE IS IN ISO-Latin-1 # A library of {gawk} functions for parsing spreadsheets in CVS format. # To be included with "-f" in {gawk} programs. # Assumes that the spreadsheet is in ISO Latin-1 encoding. function fvd(k, x,r) { # Makes sure that field {k} is empty (with or without quotes). # Returns the empty string. x = $(k); if ((x != "") && (x != "\"\"")) { field_error(k, ("esperada cadeia vazia, encontrado {" x "}")); } return ""; } function fno(k,d,lo,hi, x,r) { # Returns field {$(k)} checking for number format in range {lo..hi} # Converts to number. # If field is empty, substitutes the default {d} unless "!" x = $(k); r = check_number(x,k,d,lo,hi); return r; } function rm_quotes(x) { # If {x} is quoted, returns {x} minus the quotes. # Otherwise returns {x} unchanged. # Note that the string may contain embedded quotes. if (x ~ /^["]/) { if (x !~ /^["][^;]+["]$/) { field_error(k, ("cadeia invalida {" x "}")); } # Remove outer quotes: x = substr(x, 2, length(x)-2); } return x; } function ftx(k,d, x) { # Returns field {$(k)} which is an arbitrary string, possibly quoted. # Converts to bare text (without quotes). # If field is empty, substitutes the default {d} unless "!" x = $(k); if ((x == "") || (x == "\"\"")) { if (d == "!") { field_error(k, ("campo vazio")); } else { return d; } } # Remove outer quotes, if any: x = rm_quotes(x); return x; } function check_number(x,k,d,lo,hi, r) { # Returns {x} checking for number format in range {lo..hi} # Removes quotes if necessary. # Converts to number. # If field is empty, substitutes the default {d} unless "!" # If error, complains using {k} as field index. if ((x == "") || (x == "\"\"")) { if (d == "!") { field_error(k, ("campo vazio")); } else { return d; } } x = rm_quotes(x); if (x !~ /^[0-9]+$/) { field_error(k, ("numero invalido {" x "}")); } r = x + 0; if ((r < lo) || (r > hi)) { field_error(k, ("numero {" x "} fora do intervalo " lo ".." hi)); } return r; } function check_date(x) { # Checks {x} for date format "{dd}/{mm}/{yyyy}" or "{dd}-{mmm}-{yy}". # Converts to "{yyyy}-{mm}-{dd}". # If error returns "!". x = rm_quotes(x); if (x ~ /^[0-9][0-9][\/][0-9][0-9][\/][0-9][0-9][0-9][0-9]$/) { return (substr(x,7,4) "-" substr(x,4,2) "-" substr(x,1,2)); } else if (x ~ /^[0-9][0-9][-][A-Z][A-Z][A-Z][-][0-9][0-9]$/) { return ("20" substr(x,8,2) "-" mnum[substr(x,4,3)] "-" substr(x,1,2)); } else { return "!"; } } function check_time(x) { # Checks {x} for time format "{HH}:{MM}:{SS}". # If error returns "!". x = rm_quotes(x); if (x ~ /^[0-9][0-9][:][0-9][0-9][:][0-9][0-9]$/) { return x; } else { return "!"; } } function fdt(k,d, x,r) { # Returns field {$(k)} checking for date format "{dd}/{mm}/{yyyy}". # Converts to "{yyyy}-{mm}-{dd}". # If field is empty, substitutes the default {d} unless "!". x = $(k); if ((x == "") || (x == "\"\"")) { if (d == "!") { field_error(k, ("campo vazio")); } else { return d; } } else { r = check_date(x); if (r == "!") { field_error(k, ("data invalida {" x "}")); } else { return r; } } } function fhr(k,d, x,r) { # Returns field {$(k)} checking for time format "{HH}:{MM}:{SS}". # If field is empty, substitutes the default {d} unless "!". x = $(k); if ((x == "") || (x == "\"\"")) { if (d == "!") { field_error(k, ("campo vazio")); } else { return d; } } else { r = check_time(x); if (r == "!") { field_error(k, ("horario invalido {" x "}")); } else { return r; } } } function fdh(k,d, x,rd,rh) { # Returns field {$(k)} checking for date and optionally time # separated by " " or " - ". # Converts to "{yyyy}-{mm}-{dd} {HH}:{MM}:{SS}". # If field is empty, substitutes the default {d} unless "!". x = $(k); if ((x == "") || (x == "\"\"")) { if (d == "!") { field_error(k, ("campo vazio")); } else { return d; } } else if (match(x, /[ ][- ]*/)) { rd = check_date(substr(x,1,RSTART-1)); rh = check_time(substr(x,RSTART+RLENGTH)); } else { rd = check_date(x); rh = hora_default; } if ((rd == "!") || (rh == "!")) { field_error(k, ("data-hora invalida {" x "}")); } else { return (rd " " rh); } } function field_error(k,msg, i) { printf " campos do registro:\n", i, $(i) > "/dev/stderr"; for (i = 1; i <= NF;i++) { printf " %2d = {%s}\n", i, $(i) > "/dev/stderr"; } data_error(("campo " k ": " msg)); }