os/textandloc/charconvfw/charconv_fw/tools/WRITER.PM
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/textandloc/charconvfw/charconv_fw/tools/WRITER.PM	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,186 @@
     1.4 +#
     1.5 +# Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +# All rights reserved.
     1.7 +# This component and the accompanying materials are made available
     1.8 +# under the terms of "Eclipse Public License v1.0"
     1.9 +# which accompanies this distribution, and is available
    1.10 +# at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +#
    1.12 +# Initial Contributors:
    1.13 +# Nokia Corporation - initial contribution.
    1.14 +#
    1.15 +# Contributors:
    1.16 +#
    1.17 +# Description: 
    1.18 +#
    1.19 +
    1.20 +use strict;
    1.21 +use integer;
    1.22 +use FindBin;
    1.23 +
    1.24 +package WRITER;
    1.25 +require Exporter;
    1.26 +@WRITER::ISA=qw(Exporter);
    1.27 +@WRITER::EXPORT=qw(write8 write16 write32 writePositiveIntegerCompacted15 writePositiveIntegerCompacted30 writeSignedIntegerCompacted29 writeString writeUids);
    1.28 +
    1.29 +sub write8
    1.30 +	{
    1.31 +	my $fileHandle=shift;
    1.32 +	my $integer=shift;
    1.33 +	if ($integer&0xffffff00)
    1.34 +		{
    1.35 +		die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 8 bits\n");
    1.36 +		}
    1.37 +	printf $fileHandle "%c", $integer;
    1.38 +	}
    1.39 +
    1.40 +sub write16 # little-endian
    1.41 +	{
    1.42 +	my $fileHandle=shift;
    1.43 +	my $integer=shift;
    1.44 +	if ($integer&0xffff0000)
    1.45 +		{
    1.46 +		die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 8 bits\n");
    1.47 +		}
    1.48 +	&write8($fileHandle, $integer&0x000000ff);
    1.49 +	&write8($fileHandle, ($integer>>8)&0x000000ff);
    1.50 +	}
    1.51 +
    1.52 +sub write32 # little-endian
    1.53 +	{
    1.54 +	my $fileHandle=shift;
    1.55 +	my $integer=shift;
    1.56 +	&write8($fileHandle, $integer&0x000000ff);
    1.57 +	&write8($fileHandle, ($integer>>8)&0x000000ff);
    1.58 +	&write8($fileHandle, ($integer>>16)&0x000000ff);
    1.59 +	&write8($fileHandle, ($integer>>24)&0x000000ff);
    1.60 +	}
    1.61 +
    1.62 +sub writePositiveIntegerCompacted15 # big-endian
    1.63 +	{
    1.64 +	my $fileHandle=shift;
    1.65 +	my $integer=shift;
    1.66 +	if ($integer<0)
    1.67 +		{
    1.68 +		die("Error: the integer ".sprintf("0x%08x", $integer)." is negative\n");
    1.69 +		}
    1.70 +	if ($integer<0x00000080)
    1.71 +		{
    1.72 +		&write8($fileHandle, $integer);
    1.73 +		}
    1.74 +	elsif ($integer<0x00008000)
    1.75 +		{
    1.76 +		&write8($fileHandle, 0x00000080|(($integer>>8)&0x0000007f));
    1.77 +		&write8($fileHandle, $integer&0x000000ff);
    1.78 +		}
    1.79 +	else
    1.80 +		{
    1.81 +		die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 15 bits\n");
    1.82 +		}
    1.83 +	}
    1.84 +
    1.85 +sub writePositiveIntegerCompacted30 # big-endian
    1.86 +	{
    1.87 +	my $fileHandle=shift;
    1.88 +	my $integer=shift;
    1.89 +	if ($integer<0)
    1.90 +		{
    1.91 +		die("Error: the integer ".sprintf("0x%08x", $integer)." is negative\n");
    1.92 +		}
    1.93 +	if ($integer<0x00000080)
    1.94 +		{
    1.95 +		&write8($fileHandle, $integer);
    1.96 +		}
    1.97 +	elsif ($integer<0x00004000)
    1.98 +		{
    1.99 +		&write8($fileHandle, 0x00000080|(($integer>>8)&0x0000003f));
   1.100 +		&write8($fileHandle, $integer&0x000000ff);
   1.101 +		}
   1.102 +	elsif ($integer<0x40000000)
   1.103 +		{
   1.104 +		&write8($fileHandle, 0x000000c0|(($integer>>24)&0x0000003f));
   1.105 +		&write8($fileHandle, ($integer>>16)&0x000000ff);
   1.106 +		&write8($fileHandle, ($integer>>8)&0x000000ff);
   1.107 +		&write8($fileHandle, $integer&0x000000ff);
   1.108 +		}
   1.109 +	else
   1.110 +		{
   1.111 +		die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 30 bits\n");
   1.112 +		}
   1.113 +	}
   1.114 +
   1.115 +sub writeSignedIntegerCompacted29 # big-endian
   1.116 +	{
   1.117 +	my $fileHandle=shift;
   1.118 +	my $integer=shift;
   1.119 +	my $negativeFlag=0x00000000;
   1.120 +	if ($integer<0)
   1.121 +		{
   1.122 +		$negativeFlag=0x00000080;
   1.123 +		if (($integer^-$integer)==0)
   1.124 +			{
   1.125 +			die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 29 bits\n");
   1.126 +			}
   1.127 +		$integer=-$integer;
   1.128 +		}
   1.129 +	if ($integer<0x00000040)
   1.130 +		{
   1.131 +		&write8($fileHandle, $negativeFlag|$integer);
   1.132 +		}
   1.133 +	elsif ($integer<0x00002000)
   1.134 +		{
   1.135 +		&write8($fileHandle, $negativeFlag|0x00000040|(($integer>>8)&0x0000001f));
   1.136 +		&write8($fileHandle, $integer&0x000000ff);
   1.137 +		}
   1.138 +	elsif ($integer<0x20000000)
   1.139 +		{
   1.140 +		&write8($fileHandle, $negativeFlag|0x00000060|(($integer>>24)&0x0000001f));
   1.141 +		&write8($fileHandle, ($integer>>16)&0x000000ff);
   1.142 +		&write8($fileHandle, ($integer>>8)&0x000000ff);
   1.143 +		&write8($fileHandle, $integer&0x000000ff);
   1.144 +		}
   1.145 +	else
   1.146 +		{
   1.147 +		die("Error: the integer ".sprintf("0x%08x", $integer)." is too large to write into 30 bits\n");
   1.148 +		}
   1.149 +	}
   1.150 +
   1.151 +sub writeString
   1.152 +	{
   1.153 +	my $fileHandle=shift;
   1.154 +	my $string=shift;
   1.155 +	my $lengthOfString=length($string);
   1.156 +	my $i;
   1.157 +	for ($i=0; $i<$lengthOfString; ++$i)
   1.158 +		{
   1.159 +		my $byteAsNumber;
   1.160 +		($byteAsNumber)=unpack("C", substr($string, $i, 1));
   1.161 +		&write8($fileHandle, $byteAsNumber);
   1.162 +		}
   1.163 +	}
   1.164 +
   1.165 +sub writeUids
   1.166 +	{
   1.167 +	my $fileHandle=shift;
   1.168 +	my $uid1=shift;
   1.169 +	my $uid2=shift;
   1.170 +	my $uid3=shift;
   1.171 +	use Cwd;
   1.172 +	my $uidsin = sprintf("0x%08x 0x%08x 0x%08x ", $uid1, $uid2, $uid3);
   1.173 +
   1.174 +	# Locate uidcrc on the same path as this module, and if not found then run through environment
   1.175 +
   1.176 +  	my $uidsout = "";
   1.177 +  	my $uidlocation = "$FindBin::Bin/uidcrc";
   1.178 +  	if(-e $uidlocation or -e $uidlocation.".exe") {
   1.179 +		$uidsout = `$uidlocation $uidsin` or die('Error: could not find UIDCRC at '.$uidlocation ); }
   1.180 +	else {
   1.181 +		$uidsout = `uidcrc $uidsin` or die('Error: could not find UIDCRC on PATH');
   1.182 +	}
   1.183 +
   1.184 +	die ('Did not understand output of UIDCRC')
   1.185 +		unless $uidsout =~ /^0x([0-9a-fA-F]{8}) 0x([0-9a-fA-F]{8}) 0x([0-9a-fA-F]{8}) 0x([0-9a-fA-F]{8})[ \n\r\t]*$/s;
   1.186 +	my $uidCrcBinaryBuffer = pack 'V4', hex($1), hex($2), hex($3), hex($4);
   1.187 +	&writeString($fileHandle, $uidCrcBinaryBuffer);
   1.188 +	}
   1.189 +