# This just parses a few of the canonical fields # John Leibacher: 21 November, 1999 { package TX ; # #-------- Read a World Wide Soaring Turnpoint Exchange File -------------------- # # returned to the main: # the definitions of the columns in the hash "col" # the file definitions in the hash "file_definitions" # the waypoint data in the array "cells" # # "altitude_unit", if defined # # if $TX::quiet is defined, messages turned off [ not recommended!! ] # if $TX::debug is defined, debugging messages turned on # local ( $filename ) ; # #------------------------------------------------------------------------------- sub read { # local ( $line ) ; local ( $return_value ) ; REREAD: if( $_ = ) { s/\cM//g ; chop ; $line .= $_ ; # Note, concatenation performed before comments are stripped, # so multiple line comments are allowed if( $line =~ s/\\\s*$// ) { goto REREAD ; } # Concatanate lines # # Strip comment $line =~ s/\s*\*.*$// ; # Skip blank line if( $line =~ /^\s*$/ ) { undef $line ; goto REREAD ; } # if( $line =~ s/^\s*\$\s*// ) { &file_definitions ; $return_value = 2 ; } elsif( ! defined $column_definitions_read ) { $column_definitions_read = 'y' ; &column_definitions ; $return_value = 1 ; } else { if( $sep eq '\,' ) { local ( $commas_found ) ; # Translate , to ^G while( $line =~ s/,\s*"([^"]*?),([^"]*?)"\s*,/,"$1$2",/g ) { $commas_found = 'y' ; } $line =~ s/,\s*"(.*?)"\s*,/,$1,/g ; # Strip quotes @main::cells = split( /,/, $line ) ; # Translate ^G to , if( defined $commas_found ) { foreach ( @main::cells ) { s//,/g ; } } } else { @main::cells = split( /$sep/, $line ) ; } foreach ( @main::cells ) { s/^\s*(.*?)\s*$/$1/ ; } $return_value = 3 ; } } $return_value ; } #-------- Start the TX package for a new file ---------------------------------- sub start { # # if $TX::quiet is defined, messages turned off [ not recommended!! ] # if $TX::debug is defined, debugging messages turned on # # Is the filehandle already in use? if( -r IN ) { close IN || die "Could not close $filename\n" ; unless( defined $quiet ) { print STDERR "Closed $filename\n" ; } } open( IN, $_[0] ) || die "Could not open $_[0]\n" ; $filename = $_[0] ; undef $column_definitions_read ; undef %main::col ; undef $altitude_unit ; $sep = "\t" ; # reset default - for spreadsheets } #------------------------------------------------------------------------------- sub column_definitions { local( @cells ) = split( /$sep/, $line ) ; local( $cell_new ) ; local( $col_number ) = 0 ; foreach $cell ( @cells ) { if( $cell_new = &parse_column_definitions( $cell ) ) { if( defined $debug && defined $main::col{$cell_new} ) { print STDERR "Overwriting column definition ", "of $cell_new in column $main::col{$cell_new}\n", " with that in column $col_number [ $cell ]\n" ; } if( defined $debug ) { print STDERR "$cell_new = $col_number\n" ; } $main::col{$cell_new} = $col_number ; } else { $main::unused_cells{$cell} = $col_number ; } ++$col_number ; } # Needs check for redundant lat/lon definitions # e.g. lat_d and lat_r } #------------------------------------------------------------------------------- sub parse_column_definitions { local( $cell ) = $_[0] ; $cell =~ s/^\s*// ; $cell =~ s/\s*$// ; if ( $cell =~ /^nam/i ) { $cell = 'NAM' ;} # # More complicated than need be, to skip over redundant decimal data in # existing "Turnpoint Exchange" .tab files # elsif( $cell =~ /^lon.*deg/i && $cell !~ /decimal/ ) { $cell = 'LON_D' ; } elsif( $cell =~ /^lon.*min/i && $cell !~ /decimal/ ) { $cell = 'LON_M' ; } elsif( $cell =~ /^lon.*sec/i ) { $cell = 'LON_S' ; } elsif( $cell =~ /^lon.*rad/i ) { $cell = 'LON_R' ; } # elsif( $cell =~ /^lat.*deg/i && $cell !~ /decimal/ ) { $cell = 'LAT_D' ; } elsif( $cell =~ /^lat.*min/i && $cell !~ /decimal/ ) { $cell = 'LAT_M' ; } elsif( $cell =~ /^lat.*sec/i ) { $cell = 'LAT_S' ; } elsif( $cell =~ /^lat.*rad/i ) { $cell = 'LAT_R' ; } # elsif( $cell =~ /^cod/i ) { $cell = 'COD' ; } elsif( $cell =~ /^num/i ) { $cell = 'NUM' ; } elsif( $cell =~ /^tur/i ) { $cell = 'TUR' ; } # elsif( $cell =~ /^id.*?(\d+)*/i ) { elsif( $cell =~ s/^id//i ) { $cell =~ s/[\[\(\)\]]//g ; if( $cell =~ /(\d+)/ ) { $cell = 'ID_' . $1 ; } else { $cell = 'ID' ; } } elsif( $cell =~ /^elev/i ) { if( $cell =~ /feet/i ) { $main::altitude_unit = 'meters' ; } elsif( $cell =~ /meter/i ) { $main::altitude_unit = 'feet'; } $cell = 'ALT' ; } elsif( $cell =~ /^n(orth)*.*s(outh)*/i ) { $cell = "NS" ; } elsif( $cell =~ /^e(ast)*.*w(est)*/i ) { $cell = "EW" ; } elsif( $cell =~ /^icao/i ) { $cell = "ICAO" ; } elsif( $cell =~ /^CAI/i ) { $cell = "\U$cell" ; } #------------------------------------------------------------------------------- # Put in any other cells that the application wants to use here #------------------------------------------------------------------------------- else { unless( defined $quiet ) { print STDERR "Column definition \"$cell\" not used\n" ; } undef $cell ; } # $cell ; } #------------------------------------------------------------------------------- sub file_definitions { # if( defined $debug ) { print STDERR "File definition: $line\n" ; } $line =~ /^(\S.*?)$sep\s*(\S.*?)($sep|$)/ ; local( $key ) = $1 ; local( $value ) = $2 ; if( $key =~ /^sep/i ) { if( ! defined $quiet ) { print STDERR "SEPARATOR: $value\n" ; } # prepend \ in case the cell separator character is a meta-character $sep = '\\' . $value ; } else {$main::file_definitions{"\U$key"} = $value ; } # Render the key upper case } #------------------------------------------------------------------------------- } # End of the TX package 1 ;