|
楼主 |
发表于 2006-7-15 07:20
|
显示全部楼层
function _FindItem( $iAddrEntry, $iUnicodeCharToFind, &$iFlag, &$iAddrNext, &$iAddrSub, &$iLastAddr ) {
$iAddrWork = $iAddrEntry ;
while( $this->_GetItem( $iAddrWork, $iUnicodeChar, $iFlag, $iAddrNext, $iAddrSub ) ) {
if ( $iUnicodeCharToFind == $iUnicodeChar ) {
return $iAddrWork ;
}
$iLastAddr = $iAddrWork ;
$iAddrWork = $iAddrNext ;
}
return false ;
}
function _GetItem( $iDictContentPos, &$iUnicodeChar, &$iFlag, &$iAddrNext, &$iAddrSub ) {
if ( $iDictContentPos < ITEM_START_ADDR || ($iDictContentPos + ITEM_LENGTH) > strlen( $this->_sDictContent ) ) {
return false ;
}
$sBString = substr( $this->_sDictContent, $iDictContentPos, ITEM_LENGTH ) ;
$this->_BString2Item( $sBString, $iUnicodeChar, $iFlag, $iAddrNext, $iAddrSub ) ;
return $iDictContentPos ;
}
function _SetItem( $iDictContentPos, $iUnicodeChar, $iFlag, $iAddrNext, $iAddrSub ) {
$sBString = $this->_Item2BString( $iUnicodeChar, $iFlag, $iAddrNext, $iAddrSub ) ;
$this->_StringReplace( $this->_sDictContent, $sBString, $iDictContentPos ) ;
return true ;
}
function _AddFirstItem( $iIndex, $iUnicodeChar, $iFlag, $iAddrNext, $iAddrSub ) {
$iAddrNew = $this->_AddItem( $iUnicodeChar, $iFlag, $iAddrNext, $iAddrSub ) ;
if ( $this->_GetIndex( $iIndex, $iAddr, $iFlag ) ) {
$this->_SetIndex( $iIndex, $iAddrNew, $iFlag ) ;
}
else {
return false ;
}
return $iAddrNew ;
}
function _AddSubItem( $iDictContentPos, $iUnicodeChar, $iFlag, $iAddrNext, $iAddrSub ) {
$iAddrNew = $this->_AddItem( $iUnicodeChar, $iFlag, $iAddrNext, $iAddrSub ) ;
if ( $this->_GetItem( $iDictContentPos, $iUnicodeChar, $iFlag, $iAddrNext, $iAddrSub ) ) {
$this->_SetItem( $iDictContentPos, $iUnicodeChar, $iFlag, $iAddrNext, $iAddrNew ) ;
}
else {
return false ;
}
return $iAddrNew ;
}
function _AddItem( $iUnicodeChar, $iFlag, $iAddrNext, $iAddrSub ) {
$sBString = $this->_Item2BString( $iUnicodeChar, $iFlag, $iAddrNext, $iAddrSub ) ;
$iAddrNew = strlen( $this->_sDictContent ) ;
$this->_sDictContent .= $sBString ;
return $iAddrNew ;
}
function _AddNextItem( $iDictContentPos, $iUnicodeChar, $iFlag, $iAddrNext, $iAddrSub ) {
$iAddrNew = $this->_AddItem( $iUnicodeChar, $iFlag, $iAddrNext, $iAddrSub ) ;
if ( $this->_GetItem( $iDictContentPos, $iUnicodeChar, $iFlag, $iAddrNext, $iAddrSub ) ) {
$this->_SetItem( $iDictContentPos, $iUnicodeChar, $iFlag, $iAddrNew, $iAddrSub ) ;
}
else {
return false ;
}
return $iAddrNew ;
}
function _GetTotalCount( &$iTotalCount ) {
$sBString = substr( $this->_sDictContent, TOTAL_COUNT_START_ADDR, TOTAL_COUNT_LENGTH ) ;
$this->_BString2TotalCount( $sBString, $iTotalCount ) ;
return true ;
}
function _SetTotalCount( $iTotalCount ) {
$sBString = $this->_TotalCount2BString( $iTotalCount ) ;
$this->_StringReplace( $this->_sDictContent, $sBString, TOTAL_COUNT_START_ADDR ) ;
return true ;
}
function _GetIndex( $iIndex, &$iAddr, &$iFlag ) {
$iIndexAddr = (INDEX_LENGTH * $iIndex + TOTAL_COUNT_LENGTH) ;
$sBString = substr( $this->_sDictContent, $iIndexAddr, INDEX_LENGTH ) ;
$this->_BString2Index( $sBString, $iAddr, $iFlag ) ;
return $iIndexAddr ;
}
function _SetIndex( $iIndex, $iAddr, $iFlag ) {
$sBString = $this->_Index2BString( $iAddr, $iFlag ) ;
$this->_StringReplace( $this->_sDictContent, $sBString, INDEX_LENGTH * $iIndex + TOTAL_COUNT_LENGTH ) ;
return true ;
}
function _StringReplace ( &$sContent, $sReplace, $iOffset ) {
$iReplaceLen = strlen( $sReplace ) ;
if ( $iOffset < 0 ) {
die( 'error while iOffset=' . $iOffset );
}
if ( ( $iOffset + $iReplaceLen ) > strlen( $sContent ) ) {
die( 'error while $iReplaceLen too big ' . $iReplaceLen );
}
for( $i = $iReplaceLen - 1; $i >= 0; $i -- ) {
$sContent{ $iOffset + $i } = $sReplace{ $i } ;
}
}
function _PickUnicodeChar( $sUnicodeWord, $iIndex ) {
return substr( $sUnicodeWord, $iIndex << 1, 2 ) ;
}
function _Item2BString( $iUnicodeChar, $iFlag, $iAddrNext, $iAddrSub ) {
return pack( 'nCNN', $iUnicodeChar, $iFlag, $iAddrNext, $iAddrSub ) ;
}
function _BString2Item( $sBString, &$iUnicodeChar, &$iFlag, &$iAddrNext, &$iAddrSub ) {
$a = unpack( 'n1n/C1C/N1N1/N1N2', $sBString ) ;
$iUnicodeChar = $a['n'] ;
$iFlag = $a['C'] ;
$iAddrNext = $a['N1'] ;
$iAddrSub = $a['N2'] ;
}
function _TotalCount2BString( $iTotalCount ) {
return pack( 'N', $iTotalCount ) ;
}
function _BString2TotalCount( $sBString, &$iTotalCount ) {
$a = unpack( 'N1N', $sBString ) ;
$iTotalCount = $a[ 'N' ] ;
}
function _Index2BString( $iAddr, $iFlag ) {
return pack( 'NC', $iAddr, $iFlag ) ;
}
function _BString2Index( $sBString, &$iAddr, &$iFlag ) {
$a = unpack( 'N1N/C1C', $sBString ) ;
$iAddr = $a[ 'N' ] ;
$iFlag = $a[ 'C' ] ;
}
function _UnicodeChar2Int( $sUnicodeChar ) {
$iCode = 0 ;
$iByte1 = ord( $sUnicodeChar{ 0 } ) ;
$iByte2 = ord( $sUnicodeChar{ 1 } ) ;
if ( $iByte1 == 0 && $iByte2 > 64 && $iByte2 < 91 ) {
$iByte2 += 32 ;
}
$iCode = ( $iByte1 << 8 ) + $iByte2 ;
return $iCode ;
}
function _Int2UnicodeChar( $iUnicodeInt ) {
$iByte2 = $iUnicodeInt & 511 ;
$iByte1 = $iUnicodeInt >> 8 ;
return chr( $iByte1 ) . chr( $iByte2 ) ;
}
function Output( $bEchoItem = false, $bEchoTotalCount = true ) {
$sOutput = '' ;
$iTotalCount = 0 ;
for( $i = 0; $i<INDEX_COUNT; $i++ ) {
$this->_GetIndex( $i, $iAddr, $iFlag ) ;
if ( $iAddr >= ITEM_START_ADDR ) {
$sOutput .= $this->_OutputWordsFromIndexAddr( $i, $iAddr, $iFlag, $bEchoItem, $iTotalCount ) ;
}
}
if ( $bEchoTotalCount ) {
$this->_GetTotalCount( $iRecordTotalCount ) ;
$sOutput .= "The total count recorded is " . $iRecordTotalCount . " and the actual total count is " . $iTotalCount . ".\r\n" ;
}
return $sOutput ;
}
function _OutputWordsFromIndexAddr( $iIndex, $iAddr, $iFlag, $bEchoItem, &$iTotalCount ) {
$sOutput = '' ;
$sUnicodeChar = $this->_Int2UnicodeChar( $iIndex ) ;
if ( $iFlag == WORD_LAST ) {
if ( $bEchoItem ) {
$sOutput .= $this->_OutputKeyword( $sUnicodeChar ) ;
}
$iTotalCount ++ ;
}
$sOutput .= $this->_OutputWordsRegu( $sUnicodeChar, $iAddr, $bEchoItem, $iTotalCount ) ;
return $sOutput ;
}
function _OutputWordsRegu( $sBaseChars, $iAddr, $bEchoItem, &$iTotalCount ) {
$sOutput = '' ;
while( $this->_GetItem( $iAddr, $iUnicodeChar, $iFlag, $iAddrNext, $iAddrSub ) ) {
$sUnicodeChar = $this->_Int2UnicodeChar( $iUnicodeChar ) ;
if ( $iFlag == WORD_LAST ) {
if ( $bEchoItem ) {
$sOutput .= $this->_OutputKeyword( $sBaseChars . $sUnicodeChar ) ;
}
$iTotalCount ++ ;
}
$sOutput .= $this->_OutputWordsRegu( $sBaseChars . $sUnicodeChar, $iAddrSub, $bEchoItem, $iTotalCount ) ;
$iAddr = $iAddrNext ;
}
return $sOutput ;
}
function _OutputKeyword( $sKeyword ) {
$sKeyword = iconv( 'UNICODEBIG', 'gbk', $sKeyword ) ;
return $sKeyword ;
}
}
function file_put_contents( $filename, $content )
{
$filehandle = fopen( $filename, 'w' ) ;
fwrite( $filehandle, $content ) ;
fclose( $filehandle ) ;
}
?> |
|