[3] | 1 | # |
---|
| 2 | # Copyright (c) 2004-2007 - Consultas, PKG.fr |
---|
| 3 | # |
---|
| 4 | # This file is part of A2P. |
---|
| 5 | # |
---|
| 6 | # A2P is free software; you can redistribute it and/or modify |
---|
| 7 | # it under the terms of the GNU General Public License as published by |
---|
| 8 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 9 | # (at your option) any later version. |
---|
| 10 | # |
---|
| 11 | # A2P is distributed in the hope that it will be useful, |
---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | # GNU General Public License for more details. |
---|
| 15 | # |
---|
| 16 | # You should have received a copy of the GNU General Public License |
---|
| 17 | # along with A2P; if not, write to the Free Software |
---|
| 18 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 19 | # |
---|
| 20 | # $Id: Common.pm 3 2007-10-18 16:20:19Z guillaume $ |
---|
| 21 | # |
---|
| 22 | # Some common subfunctions useful to A2P and Convertion |
---|
| 23 | # |
---|
| 24 | |
---|
| 25 | package A2P::Common; |
---|
| 26 | |
---|
| 27 | use strict; |
---|
| 28 | use A2P::Globals; |
---|
| 29 | use A2P::Syslog; |
---|
| 30 | |
---|
| 31 | BEGIN { |
---|
| 32 | use Exporter (); |
---|
| 33 | our ( $VERSION , @ISA , @EXPORT_OK ); |
---|
| 34 | $VERSION = sprintf "%s", q$Rev: 415 $ =~ /(\d[0-9.]+)\s+/ ; |
---|
| 35 | @ISA = qw(Exporter); |
---|
| 36 | @EXPORT_OK = qw(&validate &debug &initdebug &closedebug); |
---|
| 37 | } |
---|
| 38 | our $VERSION ; |
---|
| 39 | |
---|
| 40 | # Private Global Variables |
---|
| 41 | my @CHARTABLE = (); |
---|
| 42 | |
---|
| 43 | # Sub only used for compatibility with AFP2PRINT V1 |
---|
| 44 | sub debug ; |
---|
| 45 | *debug = \&Debug ; |
---|
| 46 | |
---|
| 47 | sub initdebug { |
---|
| 48 | $LOGFILENAME = shift ; |
---|
| 49 | $LOGFILENAME .= '.log' ; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | sub closedebug {} |
---|
| 53 | |
---|
| 54 | sub validate (\$ $) { |
---|
| 55 | my $buffer = shift ; |
---|
| 56 | my $nospacestrip = shift ; |
---|
| 57 | |
---|
| 58 | my $space = chr(0x12); # Space mapping for overlays |
---|
| 59 | $$buffer =~ s/$space/ /g ; |
---|
| 60 | # Strip ending spaces |
---|
| 61 | $$buffer =~ s/\s+$// if (!defined($nospacestrip)); |
---|
| 62 | |
---|
| 63 | return if ( length($$buffer) == 0 ); |
---|
| 64 | |
---|
| 65 | my $index = 0 ; |
---|
| 66 | my $SPCcount = 0 ; |
---|
| 67 | |
---|
| 68 | # Pre-computing for TeX and AFP texts |
---|
| 69 | if ( $$buffer =~ /[\\\';\-]/ ) { |
---|
| 70 | # Start to replace backslash as backslashs will be inserted |
---|
| 71 | $$buffer =~ s/\\/\\textbackslash{}/g ; |
---|
| 72 | # Substitutions made in AFP text |
---|
| 73 | $$buffer =~ s/\'\'/\'/g ; #' |
---|
| 74 | $$buffer =~ s/;;/;/g ; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | # Return in case string is only with good chars |
---|
| 78 | return if ( $$buffer =~ /^[a-zA-Z0-1\\;\-:=?\*\+\,]*$/ ); |
---|
| 79 | |
---|
| 80 | # Otherwise split the string and compute it again by converting needed chars from CHARTABLE |
---|
| 81 | &Debug("Have string '$$buffer' to validate"); |
---|
| 82 | |
---|
| 83 | # Split also on J and 7 that could be TeX redefined by FM12 font |
---|
| 84 | my @temp = split( /(?![a-zA-IK-Z0-689])/ , $$buffer ); |
---|
| 85 | |
---|
| 86 | #&Debug("Buffer splitted in " . @temp . " parts, beginning with chars " . |
---|
| 87 | # join(", ",map { sprintf("0x%2X",ord($_)) } @temp )); |
---|
| 88 | $$buffer = "" ; |
---|
| 89 | foreach my $string ( @temp ) { |
---|
| 90 | my $chr = ord( $string ); |
---|
| 91 | if (defined($CHARTABLE[$chr])) { |
---|
| 92 | #&Debug("Char " . sprintf("0x%2X",$chr) . " defined to " . $CHARTABLE[$chr]); |
---|
| 93 | if ( $CHARTABLE[$chr] eq '\SPC' ) { |
---|
| 94 | $SPCcount ++ ; |
---|
| 95 | next if (length($string) == 1 ); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | $$buffer .= ($SPCcount > 1 ?"\\LSP\{" . $SPCcount . "\}":'\SPC{}') , $SPCcount = 0 if $SPCcount ; |
---|
| 99 | |
---|
| 100 | $$buffer .= $CHARTABLE[$chr] if ( $CHARTABLE[$chr] ne '\SPC' ); |
---|
| 101 | |
---|
| 102 | # We must protect any TeX token as necessary |
---|
| 103 | $$buffer .= "{}" if ( $CHARTABLE[$chr] =~ /^\\.+/ and $CHARTABLE[$chr] ne '\SPC' ); |
---|
| 104 | |
---|
| 105 | $$buffer .= substr( $string , 1 ) if (length($string) > 1 ); |
---|
| 106 | |
---|
| 107 | } else { |
---|
| 108 | #&Debug("Char " . sprintf("0x%2X",$chr) . " not defined in table"); |
---|
| 109 | $$buffer .= ($SPCcount > 1 ?"\\LSP\{" . $SPCcount . "\}":'\SPC{}') , $SPCcount = 0 if $SPCcount ; |
---|
| 110 | $$buffer .= $string ; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | $$buffer .= ($SPCcount > 1 ?"\\LSP\{" . $SPCcount . "\}":'\SPC{}') , $SPCcount = 0 if $SPCcount ; |
---|
| 114 | $$buffer =~ s/;;/;/g ; # For some overlay validation |
---|
| 115 | &Debug("Validated to '$$buffer'"); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | #======================================================================= |
---|
| 119 | # initialisation code - stuff the DATA into the CHARTABLE hash |
---|
| 120 | #======================================================================= |
---|
| 121 | { |
---|
| 122 | my ( $code , $texcode , $comment ); |
---|
| 123 | |
---|
| 124 | &Debug("Loading CHARTABLE..."); |
---|
| 125 | while (<DATA>) |
---|
| 126 | { |
---|
| 127 | next unless /\S/; |
---|
| 128 | chop; |
---|
| 129 | s/^\s+//; |
---|
| 130 | ( $code, $texcode , $comment ) = split(/\s+/, $_ , 3); |
---|
| 131 | next if ( $code =~ /^skip/ ); |
---|
| 132 | $code = eval($code) if ( $code =~ /^0x/ ); |
---|
| 133 | $CHARTABLE[$code] = $texcode; |
---|
| 134 | #&Debug("Char " . sprintf("0x%2X",$code) . " defined to '" . $CHARTABLE[$code] . "'"); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | close(DATA); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | &Debug("Module " . __PACKAGE__ . " v$VERSION loaded"); |
---|
| 141 | |
---|
| 142 | 1; |
---|
| 143 | |
---|
| 144 | __DATA__ |
---|
| 145 | 0x00 \SPC |
---|
| 146 | 0x01 \SPC |
---|
| 147 | 0x02 \SPC |
---|
| 148 | 0x03 \SPC |
---|
| 149 | 0x04 \SPC |
---|
| 150 | 0x05 \SPC |
---|
| 151 | 0x06 \SPC |
---|
| 152 | 0x07 \SPC |
---|
| 153 | 0x08 \SPC |
---|
| 154 | 0x09 \SPC |
---|
| 155 | 0x0a \SPC |
---|
| 156 | 0x0b \SPC |
---|
| 157 | 0x0c \SPC |
---|
| 158 | 0x0d \SPC |
---|
| 159 | 0x0e \SPC |
---|
| 160 | |
---|
| 161 | 0x0f \char"27 Used to valid string value from AFP overlays |
---|
| 162 | |
---|
| 163 | 0x10 \textbackslash Used to valid string value from AFP overlays |
---|
| 164 | 0x11 \char"2A Used to valid string value from AFP overlays |
---|
| 165 | skip 0x12 \SPC (Was used to valid string value from AFP overlays) |
---|
| 166 | 0x13 \char"28 Used to valid string value from AFP overlays |
---|
| 167 | 0x14 \char"29 Used to valid string value from AFP overlays |
---|
| 168 | 0x15 \char"3F Used to valid string value from AFP overlays |
---|
| 169 | 0x16 \char"2E Used to valid string value from AFP overlays |
---|
| 170 | 0x17 \char"2B Used to valid string value from AFP overlays |
---|
| 171 | 0x18 \textasciicircum Used to valid string value from AFP overlays |
---|
| 172 | 0x19 \$ Used to valid string value from AFP overlays |
---|
| 173 | 0x1a \char"5B Used to valid string value from AFP overlays |
---|
| 174 | 0x1b \char"5D Used to valid string value from AFP overlays |
---|
| 175 | 0x1c \{ Used to valid string value from AFP overlays |
---|
| 176 | 0x1d \} Used to valid string value from AFP overlays |
---|
| 177 | 0x1e \char"2D Used to valid string value from AFP overlays |
---|
| 178 | 0x1f \char"7C Used to valid string value from AFP overlays |
---|
| 179 | |
---|
| 180 | 0x20 \SPC space |
---|
| 181 | 0x21 \char"21 ! |
---|
| 182 | 0x22 \textquotedbl " |
---|
| 183 | 0x23 \# |
---|
| 184 | 0x24 \$ |
---|
| 185 | 0x25 \% |
---|
| 186 | 0x26 \& |
---|
| 187 | 0x27 \char"27 ' |
---|
| 188 | skip 0x28 ( |
---|
| 189 | skip 0x29 ) |
---|
| 190 | skip 0x2a * |
---|
| 191 | skip 0x2b + |
---|
| 192 | skip 0x2c , |
---|
| 193 | 0x2d \char"2D - |
---|
| 194 | skip 0x2e . |
---|
| 195 | 0x2f \ourslash / |
---|
| 196 | |
---|
| 197 | 0x37 \cSev 7 |
---|
| 198 | |
---|
| 199 | skip 0x3a : |
---|
| 200 | skip 0x3b ; |
---|
| 201 | 0x3c \char"3C < |
---|
| 202 | skip 0x3d $=$ = |
---|
| 203 | 0x3e \char"3E > |
---|
| 204 | skip 0x3f ? |
---|
| 205 | |
---|
| 206 | skip 0x40 @ |
---|
| 207 | |
---|
| 208 | 0x4A \cJ J |
---|
| 209 | |
---|
| 210 | 0x5b ffl [ |
---|
| 211 | 0x5c \textbackslash \ |
---|
| 212 | 0x5d \textquotedblleft ] |
---|
| 213 | 0x5e \textasciicircum ^ Does not exist in IBM chars |
---|
| 214 | 0x5f \_ |
---|
| 215 | |
---|
| 216 | skip 0x60 ` |
---|
| 217 | 0x7b \{ |
---|
| 218 | 0x7c ! IBM<!>=4F -> Latin1=7C and not \textbar | |
---|
| 219 | 0x7d \} |
---|
| 220 | 0x7e \textasciitilde ~ |
---|
| 221 | 0x7f \SPC |
---|
| 222 | |
---|
| 223 | 0x80 \SPC |
---|
| 224 | 0x81 \SPC |
---|
| 225 | 0x82 \SPC |
---|
| 226 | 0x83 \SPC |
---|
| 227 | 0x84 \textquotedblright |
---|
| 228 | 0x85 \SPC |
---|
| 229 | 0x86 \char"86 or \dag |
---|
| 230 | 0x87 \char"87 or \ddag |
---|
| 231 | 0x88 \SPC |
---|
| 232 | 0x89 \SPC |
---|
| 233 | 0x8a \SPC |
---|
| 234 | 0x8b \SPC |
---|
| 235 | 0x8c \SPC |
---|
| 236 | 0x8d \SPC |
---|
| 237 | 0x8e \SPC |
---|
| 238 | 0x8f \SPC |
---|
| 239 | |
---|
| 240 | 0x90 \SPC |
---|
| 241 | 0x91 \SPC |
---|
| 242 | 0x92 \SPC |
---|
| 243 | 0x93 \SPC |
---|
| 244 | 0x94 \SPC |
---|
| 245 | 0x95 \SPC |
---|
| 246 | 0x96 \SPC |
---|
| 247 | 0x97 \SPC |
---|
| 248 | 0x98 \SPC |
---|
| 249 | 0x99 \SPC |
---|
| 250 | 0x9a \SPC |
---|
| 251 | 0x9b \SPC |
---|
| 252 | 0x9c \SPC |
---|
| 253 | 0x9d \SPC |
---|
| 254 | 0x9e \SPC |
---|
| 255 | 0x9f \SPC IBM chars not used |
---|
| 256 | |
---|
| 257 | 0xA0 \SPC |
---|
| 258 | 0xA1 \textexclamdown |
---|
| 259 | 0xA2 \char"5B or $\lbrack$ IBM<[>=4A -> Latin1=A2 and not <?> |
---|
| 260 | 0xA3 \char"A3 |
---|
| 261 | 0xA4 \ourbullet Euro symbol in ASCII |
---|
| 262 | 0xA5 \SPC |
---|
| 263 | 0xA6 \textbar |
---|
| 264 | 0xA7 \char"A7 |
---|
| 265 | 0xA8 \textquoteleft |
---|
| 266 | 0xA9 \char"A9 |
---|
| 267 | 0xAA \char"96\char"96\setbox4\hbox{\char"96}\kern0.5\wd4 Used to replace double zero for numbers decimal part |
---|
| 268 | 0xAB \guillemotleft |
---|
| 269 | 0xAC \char"A2 or $\not\subset$ |
---|
| 270 | 0xAD \ourtop or $top$ |
---|
| 271 | 0xAE \char"AE |
---|
| 272 | 0xAF \textquoteleft |
---|
| 273 | |
---|
| 274 | 0xB0 \char"B0 |
---|
| 275 | 0xB1 $)$ |
---|
| 276 | 0xB2 $\dashv$ |
---|
| 277 | 0xB3 \ourtextbar or $\mid$ |
---|
| 278 | 0xB4 \textquoteright |
---|
| 279 | 0xB5 \char"B5 or $\mu$ |
---|
| 280 | skip 0xB6 |
---|
| 281 | skip 0xB7 |
---|
| 282 | 0xB8 \SPC |
---|
| 283 | 0xB9 \ourbot or $\bot$ |
---|
| 284 | skip 0xBA |
---|
| 285 | 0xBB \guillemotright |
---|
| 286 | 0xBC \char"BC |
---|
| 287 | 0xBD \char"BD |
---|
| 288 | 0xBE \char"BE |
---|
| 289 | 0xBF \char"BF |
---|
| 290 | |
---|
| 291 | skip 0xC0 |
---|
| 292 | skip 0xC1 |
---|
| 293 | skip 0xC2 |
---|
| 294 | skip 0xC3 |
---|
| 295 | skip 0xC4 |
---|
| 296 | skip 0xC5 |
---|
| 297 | skip 0xC6 |
---|
| 298 | skip 0xC7 |
---|
| 299 | skip 0xC8 |
---|
| 300 | skip 0xC9 |
---|
| 301 | skip 0xCA |
---|
| 302 | skip 0xCB |
---|
| 303 | skip 0xCC |
---|
| 304 | skip 0xCD |
---|
| 305 | skip 0xCE |
---|
| 306 | skip 0xCF |
---|
| 307 | |
---|
| 308 | 0xD0 \oe |
---|
| 309 | skip 0xD1 |
---|
| 310 | skip 0xD2 |
---|
| 311 | skip 0xD3 |
---|
| 312 | skip 0xD4 |
---|
| 313 | skip 0xD5 |
---|
| 314 | skip 0xD6 |
---|
| 315 | 0xD7 \ourline Used in IBM to make lines |
---|
| 316 | 0xD8 \ourOslash Used in E-Service |
---|
| 317 | skip 0xD9 |
---|
| 318 | skip 0xDA |
---|
| 319 | skip 0xDB |
---|
| 320 | skip 0xDC |
---|
| 321 | 0xDD \char"20 |
---|
| 322 | 0xDE \ddag |
---|
| 323 | skip 0xDF |
---|
| 324 | |
---|
| 325 | skip 0xE0 |
---|
| 326 | skip 0xE1 |
---|
| 327 | skip 0xE2 |
---|
| 328 | skip 0xE3 |
---|
| 329 | skip 0xE4 |
---|
| 330 | skip 0xE5 |
---|
| 331 | skip 0xE6 |
---|
| 332 | skip 0xE7 |
---|
| 333 | skip 0xE8 |
---|
| 334 | skip 0xE9 |
---|
| 335 | skip 0xEA |
---|
| 336 | skip 0xEB |
---|
| 337 | skip 0xEC |
---|
| 338 | skip 0xED |
---|
| 339 | skip 0xEE |
---|
| 340 | skip 0xEF |
---|
| 341 | |
---|
| 342 | 0xF0 \dh |
---|
| 343 | skip 0xF1 |
---|
| 344 | skip 0xF2 |
---|
| 345 | skip 0xF3 |
---|
| 346 | skip 0xF4 |
---|
| 347 | skip 0xF5 |
---|
| 348 | skip 0xF6 |
---|
| 349 | 0xF7 \char"F7 or $\div$ |
---|
| 350 | skip 0xF8 |
---|
| 351 | skip 0xF9 |
---|
| 352 | skip 0xFA |
---|
| 353 | skip 0xFB |
---|
| 354 | skip 0xFC |
---|
| 355 | skip 0xFD |
---|
| 356 | skip 0xFE |
---|
| 357 | 0xFF \char"FF |
---|