#
# Copyright (c) 2004-2007 - Consultas, PKG.fr
#
# This file is part of A2P.
#
# A2P is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# A2P is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with A2P; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# $Id: Common.pm 3 2007-10-18 16:20:19Z guillaume $
#
# Some common subfunctions useful to A2P and Convertion
#

package A2P::Common;

use strict;
use A2P::Globals;
use A2P::Syslog;

BEGIN {
    use Exporter ();
    our ( $VERSION , @ISA , @EXPORT_OK );
    $VERSION = sprintf "%s", q$Rev: 415 $ =~ /(\d[0-9.]+)\s+/ ;
    @ISA = qw(Exporter);
    @EXPORT_OK = qw(&validate &debug &initdebug &closedebug);
}
our $VERSION ;

# Private Global Variables
my @CHARTABLE = ();

# Sub only used for compatibility with AFP2PRINT V1
sub debug ;
*debug = \&Debug ;

sub initdebug {
    $LOGFILENAME  = shift  ;
    $LOGFILENAME .= '.log' ;
}

sub closedebug {}

sub validate (\$ $) {
    my $buffer = shift ;
    my $nospacestrip = shift ;

    my $space = chr(0x12); # Space mapping for overlays
    $$buffer =~ s/$space/ /g ;
    # Strip ending spaces
    $$buffer =~ s/\s+$// if (!defined($nospacestrip));

    return if ( length($$buffer) == 0 );

    my $index = 0 ;
    my $SPCcount = 0 ;

    # Pre-computing for TeX and AFP texts
    if ( $$buffer =~ /[\\\';\-]/ ) {
        # Start to replace backslash as backslashs will be inserted
        $$buffer =~ s/\\/\\textbackslash{}/g ;
        # Substitutions made in AFP text
        $$buffer =~ s/\'\'/\'/g ; #'
        $$buffer =~ s/;;/;/g ;
    }

    # Return in case string is only with good chars
    return if ( $$buffer =~ /^[a-zA-Z0-1\\;\-:=?\*\+\,]*$/ );

    # Otherwise split the string and compute it again by converting needed chars from CHARTABLE
    &Debug("Have string  '$$buffer' to validate");

    # Split also on J and 7 that could be TeX redefined by FM12 font
    my @temp = split( /(?![a-zA-IK-Z0-689])/ , $$buffer );

    #&Debug("Buffer splitted in " . @temp . " parts, beginning with chars " .
    #  join(", ",map { sprintf("0x%2X",ord($_)) } @temp ));
    $$buffer = "" ;
    foreach my $string ( @temp ) {
        my $chr = ord( $string );
        if (defined($CHARTABLE[$chr])) {
            #&Debug("Char " . sprintf("0x%2X",$chr) . " defined to " . $CHARTABLE[$chr]);
            if ( $CHARTABLE[$chr] eq '\SPC' ) {
                $SPCcount ++ ;
                next if (length($string) == 1 );
            }

            $$buffer .= ($SPCcount > 1 ?"\\LSP\{" . $SPCcount . "\}":'\SPC{}') , $SPCcount = 0 if $SPCcount ;

            $$buffer .= $CHARTABLE[$chr] if ( $CHARTABLE[$chr] ne '\SPC' );

            # We must protect any TeX token as necessary
            $$buffer .= "{}" if ( $CHARTABLE[$chr] =~ /^\\.+/ and $CHARTABLE[$chr] ne '\SPC' );

            $$buffer .= substr( $string , 1 ) if (length($string) > 1 );

        } else {
            #&Debug("Char " . sprintf("0x%2X",$chr) . " not defined in table");
            $$buffer .= ($SPCcount > 1 ?"\\LSP\{" . $SPCcount . "\}":'\SPC{}') , $SPCcount = 0 if $SPCcount ;
            $$buffer .= $string ;
            }
        }
    $$buffer .= ($SPCcount > 1 ?"\\LSP\{" . $SPCcount . "\}":'\SPC{}') , $SPCcount = 0 if $SPCcount ;
    $$buffer =~ s/;;/;/g ; # For some overlay validation
    &Debug("Validated to '$$buffer'");
}

#=======================================================================
# initialisation code - stuff the DATA into the CHARTABLE hash
#=======================================================================
{
    my ( $code , $texcode , $comment );

    &Debug("Loading CHARTABLE...");
    while (<DATA>)
    {
        next unless /\S/;
        chop;
        s/^\s+//;
        ( $code, $texcode , $comment ) = split(/\s+/, $_ , 3);
        next if ( $code =~ /^skip/ );
        $code = eval($code) if ( $code =~ /^0x/ );
        $CHARTABLE[$code] = $texcode;
        #&Debug("Char " . sprintf("0x%2X",$code) . " defined to '" . $CHARTABLE[$code] . "'");
    }

    close(DATA);
}

&Debug("Module " . __PACKAGE__ . " v$VERSION loaded");

1;

__DATA__
0x00             \SPC
0x01             \SPC
0x02             \SPC
0x03             \SPC
0x04             \SPC
0x05             \SPC
0x06             \SPC
0x07             \SPC
0x08             \SPC
0x09             \SPC
0x0a             \SPC
0x0b             \SPC
0x0c             \SPC
0x0d             \SPC
0x0e             \SPC

0x0f             \char"27           Used to valid string value from AFP overlays

0x10             \textbackslash     Used to valid string value from AFP overlays
0x11             \char"2A           Used to valid string value from AFP overlays
skip 0x12        \SPC         (Was used to valid string value from AFP overlays)
0x13             \char"28           Used to valid string value from AFP overlays
0x14             \char"29           Used to valid string value from AFP overlays
0x15             \char"3F           Used to valid string value from AFP overlays
0x16             \char"2E           Used to valid string value from AFP overlays
0x17             \char"2B           Used to valid string value from AFP overlays
0x18             \textasciicircum   Used to valid string value from AFP overlays
0x19             \$                 Used to valid string value from AFP overlays
0x1a             \char"5B           Used to valid string value from AFP overlays
0x1b             \char"5D           Used to valid string value from AFP overlays
0x1c             \{                 Used to valid string value from AFP overlays
0x1d             \}                 Used to valid string value from AFP overlays
0x1e             \char"2D           Used to valid string value from AFP overlays
0x1f             \char"7C           Used to valid string value from AFP overlays

0x20            \SPC                space
0x21            \char"21            !
0x22            \textquotedbl       "
0x23            \#
0x24            \$
0x25            \%
0x26            \&
0x27            \char"27            '
skip 0x28                           (
skip 0x29                           )
skip 0x2a                           *
skip 0x2b                           +
skip 0x2c                           ,
0x2d            \char"2D            -
skip 0x2e                           .
0x2f            \ourslash           /

0x37           \cSev                7

skip 0x3a                           :
skip 0x3b                           ;
0x3c           \char"3C             <
skip 0x3d           $=$             =
0x3e           \char"3E             >
skip 0x3f                           ?

skip 0x40                           @

0x4A           \cJ                  J

0x5b           ffl                  [
0x5c           \textbackslash       \
0x5d           \textquotedblleft    ]
0x5e           \textasciicircum     ^                Does not exist in IBM chars
0x5f           \_

skip 0x60                           `
0x7b           \{
0x7c           !                    IBM<!>=4F -> Latin1=7C and not \textbar    |
0x7d           \}
0x7e           \textasciitilde      ~
0x7f           \SPC

0x80           \SPC
0x81           \SPC
0x82           \SPC
0x83           \SPC
0x84           \textquotedblright
0x85           \SPC
0x86           \char"86 or \dag
0x87           \char"87 or \ddag
0x88           \SPC
0x89           \SPC
0x8a           \SPC
0x8b           \SPC
0x8c           \SPC
0x8d           \SPC
0x8e           \SPC
0x8f           \SPC

0x90           \SPC
0x91           \SPC
0x92           \SPC
0x93           \SPC
0x94           \SPC
0x95           \SPC
0x96           \SPC
0x97           \SPC
0x98           \SPC
0x99           \SPC
0x9a           \SPC
0x9b           \SPC
0x9c           \SPC
0x9d           \SPC
0x9e           \SPC
0x9f           \SPC                         IBM chars not used

0xA0            \SPC
0xA1            \textexclamdown
0xA2            \char"5B or $\lbrack$       IBM<[>=4A -> Latin1=A2 and not <?>
0xA3            \char"A3
0xA4            \ourbullet                  Euro symbol in ASCII
0xA5            \SPC
0xA6            \textbar
0xA7            \char"A7
0xA8            \textquoteleft
0xA9            \char"A9
0xAA            \char"96\char"96\setbox4\hbox{\char"96}\kern0.5\wd4   Used to replace double zero for numbers decimal part
0xAB            \guillemotleft
0xAC            \char"A2 or $\not\subset$
0xAD            \ourtop or $top$
0xAE            \char"AE
0xAF            \textquoteleft

0xB0            \char"B0
0xB1            $)$
0xB2            $\dashv$
0xB3            \ourtextbar or  $\mid$
0xB4            \textquoteright
0xB5            \char"B5 or $\mu$
skip 0xB6
skip 0xB7
0xB8            \SPC
0xB9            \ourbot or $\bot$
skip 0xBA
0xBB            \guillemotright
0xBC            \char"BC
0xBD            \char"BD
0xBE            \char"BE
0xBF            \char"BF

skip 0xC0
skip 0xC1
skip 0xC2
skip 0xC3
skip 0xC4
skip 0xC5
skip 0xC6
skip 0xC7
skip 0xC8
skip 0xC9
skip 0xCA
skip 0xCB
skip 0xCC
skip 0xCD
skip 0xCE
skip 0xCF

0xD0            \oe
skip 0xD1
skip 0xD2
skip 0xD3
skip 0xD4
skip 0xD5
skip 0xD6
0xD7            \ourline                    Used in IBM to make lines
0xD8            \ourOslash                  Used in E-Service
skip 0xD9
skip 0xDA
skip 0xDB
skip 0xDC
0xDD            \char"20
0xDE            \ddag
skip 0xDF

skip 0xE0
skip 0xE1
skip 0xE2
skip 0xE3
skip 0xE4
skip 0xE5
skip 0xE6
skip 0xE7
skip 0xE8
skip 0xE9
skip 0xEA
skip 0xEB
skip 0xEC
skip 0xED
skip 0xEE
skip 0xEF

0xF0            \dh
skip 0xF1
skip 0xF2
skip 0xF3
skip 0xF4
skip 0xF5
skip 0xF6
0xF7            \char"F7 or $\div$
skip 0xF8
skip 0xF9
skip 0xFA
skip 0xFB
skip 0xFC
skip 0xFD
skip 0xFE
0xFF            \char"FF
