|
| |
Пост N: 60
Зарегистрирован: 15.12.06
|
|
Отправлено: 24.04.07 10:59. Заголовок: Получилась полезная функция
После причёски (отбрасывания лишнего) получилось так: function get_var(file,varName) local b:=fopen(file),f_len,xVarValue,cID,cBuffer,cMemBuffer,nSize,nLen,cData local nHi,nLon,Value,lFlag,nB1,nB2,nB3,nB4,nTotal,nOutput if b<0 return nil endif f_len=fseek(b,0,2) fseek(b,0) do while fseek( b, 0, 1 ) + 1 < f_len .and. !FEOF( b ) cMemBuff := space( 18 ) fread( b, @cMemBuff, 18 ) cVarName := left( cMemBuff, at( chr( 0 ), cMemBuff ) - 1 ) cID := substr( cMemBuff, 12, 1 ) nSize := bin2w( right( cMemBuff, 2 ) ) nLen := if( cID $ "ГМ", 14 + nSize, 22 ) cBuffer := space( nLen ) fread( b, @cBuffer, nLen ) cData := substr( cBuffer, 15 ) if cVarName=upper(varName) //нашли нужную переменную if cID == chr( 195 ) // character Г xVarValue := cData elseif cID == chr( 204 ) // logic М xVarValue := asc( cData ) == 1 elseif cID == chr( 206 ) // Numeric О cBuffer := substr( cBuffer, 15 ) nHi := MODULUS( asc( substr( cBuffer, 8, 1 ) ), 128 ) * 16 nLo := int( asc( substr( cBuffer, 7, 1 ) ) / 16 ) nValue := nHi + nLo - 1023 lFlag := int( asc( substr( cBuffer, 8, 1 ) ) / 16 ) >= 8 nB1 := MODULUS( asc( substr( cBuffer, 7, 1 ) ), 16 ) / 16 nB2 := bin2w( substr( cBuffer, 5, 2 ) ) / ( 65536 * 16 ) nB3 := bin2w( substr( cBuffer, 3, 2 ) ) / ( 65536 * 65536 * 16 ) nB4 := bin2w( substr( cBuffer, 1, 2 ) ) / ( 65536 * 65536 * 65536 * 16 ) nTotal := nB1 + nB2 + nB3 + nB4 nOutput := if( lFlag, - ( 1 + nTotal ) * 2 ^ nValue, ( 1 + nTotal ) * 2 ^ nValue ) xVarValue := val( transform( nOutput, "@B" ) ) elseif cID == chr( 196 ) // Date cBuffer := substr( cBuffer, 15 ) nHi := MODULUS( asc( substr( cBuffer, 8, 1 ) ), 128 ) * 16 nLo := int( asc( substr( cBuffer, 7, 1 ) ) / 16 ) nValue := nHi + nLo - 1023 lFlag := int( asc( substr( cBuffer, 8, 1 ) ) / 16 ) >= 8 nB1 := MODULUS( asc( substr( cBuffer, 7, 1 ) ), 16 ) / 16 nB2 := bin2w( substr( cBuffer, 5, 2 ) ) / ( 65536 * 16 ) nB3 := bin2w( substr( cBuffer, 3, 2 ) ) / ( 65536 * 65536 * 16 ) nB4 := bin2w( substr( cBuffer, 1, 2 ) ) / ( 65536 * 65536 * 65536 * 16 ) nTotal := nB1 + nB2 + nB3 + nB4 nOutput := if( lFlag, - ( 1 + nTotal ) * 2 ^ nValue, ( 1 + nTotal ) * 2 ^ nValue ) xVarValue := dtoc( ctod( "01/01/0100" ) + nOutput - 1757585 ) endif exit endif //нашли нужную переменную enddo fclose( b ) return xVarValue function FEOF( nHandle ) local nCurrPos,nFileSize nCurrPos := fseek( nHandle, 0, 1 ) nFileSize := fseek( nHandle, 0, 2 ) fseek( nHandle, nCurrPos, 0 ) return nFileSize < nCurrPos function MODULUS( nParm1, nParm2 ) local x,nRetVal := 0 if valtype( nParm1 ) = "N" .and. valtype( nParm2 ) = "N" if nParm2 = 0 nRetVal := nParm1 else x := nParm1 % nParm2 nRetVal := if( x * nParm2 < 0, x + nParm2, x ) endif endif return nRetVal Хотя исходная читалка тоже полезная.
|