Esempio n. 1
0
        public virtual int Input(out byte[] inData, int length)
        {
            try
            {
                IOmonitor.IdleForLock(_IOprocessing);
                //
                int    resultLength = -1;
                byte[] inBuff;

                int   parentMachineRequestCharCount, inputDeviceRequestByteCount;
                Int64 number;

                InputConvertings inputConverting = _InputConverting;

                inData = new byte[length];

                if (!InputFromBufferAllowed)
                {
                    switch (inputConverting)
                    {
                    case InputConvertings.None:
                        DirectInput(out inBuff, length);
                        resultLength = inBuff.Length <= length ? inBuff.Length : length;
                        Array.Copy(inBuff, inData, resultLength);
                        break;

                    case InputConvertings.String:
                        parentMachineRequestCharCount = ParentIDE.Machine.CharSet.GetMaxCharCount(length);
                        inputDeviceRequestByteCount   = _InputDevice.CharSet.GetMaxByteCount(parentMachineRequestCharCount);
                        DirectInput(out inBuff, inputDeviceRequestByteCount);
                        inBuff = Encoding.Convert(_InputDevice.CharSet, ParentIDE.Machine.CharSet
                                                  , inBuff);
                        resultLength = inBuff.Length <= length ? inBuff.Length : length;
                        Array.Copy(inBuff, inData, resultLength);
                        break;

                    case InputConvertings.ParsingNumber:
                        byte[] inBuff2;
                        parentMachineRequestCharCount = XBinary.MaxDigitCount(256, 10, length) + 1;
                        inputDeviceRequestByteCount   = _InputDevice.CharSet.GetMaxByteCount(parentMachineRequestCharCount);
                        DirectInput(out inBuff, inputDeviceRequestByteCount);
                        if (XBinary.TryParseTextNumberBytesToBinaryNumberBytes(inBuff
                                                                               , _InputDevice.CharSet, out inBuff2, length, ParentIDE.Machine.ByteOrder))
                        {
                            inData       = inBuff2;
                            resultLength = length;
                        }
                        else
                        {
                            resultLength = 0;
                        }
                        break;

                    default:
                        throw new Exception();
                    }
                }
                else
                {
                    // using input buffer:
                    switch (inputConverting)
                    {
                    case InputConvertings.None:
                        if (_InputBuffer.Length == 0)
                        {
                            DirectInput(out _InputBuffer, length);
                        }
                        //
                        if (_InputBuffer.Length == 0)
                        {
                            resultLength = 0;
                        }
                        else if (_InputBuffer.Length - _InBuffPtr <= length)
                        {
                            resultLength = _InputBuffer.Length - _InBuffPtr;
                            Array.Copy(_InputBuffer, _InBuffPtr, inData, 0, resultLength);
                            _InputBuffer = new byte[0];
                            _InBuffPtr   = 0;
                        }
                        else if (_InputBuffer.Length - _InBuffPtr > length)
                        {
                            resultLength = length;
                            Array.Copy(_InputBuffer, _InBuffPtr, inData, 0, resultLength);
                            _InBuffPtr += resultLength;
                        }
                        break;

                    case InputConvertings.String:
                        if (_InputBuffer.Length == 0)
                        {
                            parentMachineRequestCharCount = ParentIDE.Machine.CharSet.GetMaxCharCount(length);
                            inputDeviceRequestByteCount   = _InputDevice.CharSet.GetMaxByteCount(parentMachineRequestCharCount);
                            DirectInput(out inBuff, inputDeviceRequestByteCount);
                            _InputBuffer = Encoding.Convert(_InputDevice.CharSet, ParentIDE.Machine.CharSet
                                                            , inBuff);
                        }
                        //
                        if (_InputBuffer.Length == 0)
                        {
                            resultLength = 0;
                        }
                        else if (_InputBuffer.Length - _InBuffPtr <= length)
                        {
                            resultLength = _InputBuffer.Length - _InBuffPtr;
                            Array.Copy(_InputBuffer, _InBuffPtr, inData, 0, resultLength);
                            _InputBuffer = new byte[0];
                            _InBuffPtr   = 0;
                        }
                        else if (_InputBuffer.Length - _InBuffPtr > length)
                        {
                            resultLength = length;
                            Array.Copy(_InputBuffer, _InBuffPtr, inData, 0, resultLength);
                            _InBuffPtr += resultLength;
                        }
                        break;

                    case InputConvertings.ParsingNumber:
                        if (_InputWordBuffer.Length == 0)
                        {
                            parentMachineRequestCharCount = XBinary.MaxDigitCount(256, 10, length) + 1;
                            inputDeviceRequestByteCount   = _InputDevice.CharSet.GetMaxByteCount(parentMachineRequestCharCount);
                            DirectInput(out inBuff, inputDeviceRequestByteCount);
                            byte[] unicodeLineBytes = Encoding.Convert(_InputDevice.CharSet, Encoding.Unicode, inBuff);
                            char[] unicodeLineChars = new char[Encoding.Unicode.GetCharCount(unicodeLineBytes, 0, unicodeLineBytes.Length)];
                            Encoding.Unicode.GetChars(unicodeLineBytes, 0, unicodeLineBytes.Length, unicodeLineChars, 0);
                            string newLine = new string(unicodeLineChars);
                            _InputWordBuffer = newLine.Split(Separators, StringSplitOptions.RemoveEmptyEntries);
                        }
                        //
                        if (_InputWordBuffer.Length == 0)
                        {
                            resultLength = 0;
                        }
                        else if (_InputWordBuffer.Length - _WdBuffPtr > 0)
                        {
                            if (Int64.TryParse(_InputWordBuffer[_WdBuffPtr], out number))
                            {
                                inData = XBinary.FormatSignedNumberBytes(BitConverter.GetBytes(number), length
                                                                         , XBinary.HostMachineByteOrder, ParentIDE.Machine.ByteOrder);
                                resultLength = length;
                            }
                            else
                            {
                                resultLength = 0;
                            }
                            //
                            _WdBuffPtr++;
                            if (_InputWordBuffer.Length == _WdBuffPtr)
                            {
                                _InputWordBuffer = new string[0];
                                _WdBuffPtr       = 0;
                            }
                        }
                        break;

                    default:
                        throw new Exception();
                    }
                }

                return(resultLength);
            }
            finally
            {
                //
                IOmonitor.Unlock(_IOprocessing);
            }
        }
Esempio n. 2
0
        protected virtual void UpdateInputBuffers(InputConvertings upToNewInputConverting)
        {
            if (InputFromBufferAllowed)
            {
                // converting input buffer
                InputConvertings inputConverting    = _InputConverting;
                InputConvertings newInputConverting = upToNewInputConverting;

                switch (inputConverting)
                {
                case InputConvertings.None:
                    switch (newInputConverting)
                    {
                    case InputConvertings.None:
                        break;

                    case InputConvertings.String:
                        _InputBuffer = Encoding.Convert(_InputDevice.CharSet, ParentIDE.Machine.CharSet, _InputBuffer, _InBuffPtr, _InputBuffer.Length - _InBuffPtr);
                        _InBuffPtr   = 0;
                        break;

                    case InputConvertings.ParsingNumber:
                        byte[] unicodeLineBytes = Encoding.Convert(_InputDevice.CharSet, Encoding.Unicode, _InputBuffer, _InBuffPtr, _InputBuffer.Length - _InBuffPtr);
                        char[] unicodeLineChars = new char[Encoding.Unicode.GetCharCount(unicodeLineBytes, 0, unicodeLineBytes.Length)];
                        Encoding.Unicode.GetChars(unicodeLineBytes, 0, unicodeLineBytes.Length, unicodeLineChars, 0);
                        string newLine = new string(unicodeLineChars);
                        _InputWordBuffer = newLine.Split(Separators, StringSplitOptions.RemoveEmptyEntries);
                        _WdBuffPtr       = 0;
                        _InputBuffer     = new byte[0];
                        _InBuffPtr       = 0;
                        break;

                    default:
                        _InputBuffer     = new byte[0];
                        _InBuffPtr       = 0;
                        _InputWordBuffer = new string[0];
                        _WdBuffPtr       = 0;
                        break;
                    }
                    break;

                case InputConvertings.String:
                    switch (newInputConverting)
                    {
                    case InputConvertings.None:
                        _InputBuffer = Encoding.Convert(ParentIDE.Machine.CharSet, _InputDevice.CharSet, _InputBuffer, _InBuffPtr, _InputBuffer.Length - _InBuffPtr);
                        _InBuffPtr   = 0;
                        break;

                    case InputConvertings.String:
                        break;

                    case InputConvertings.ParsingNumber:
                        byte[] unicodeLineBytes = Encoding.Convert(ParentIDE.Machine.CharSet, Encoding.Unicode, _InputBuffer, _InBuffPtr, _InputBuffer.Length - _InBuffPtr);
                        char[] unicodeLineChars = new char[Encoding.Unicode.GetCharCount(unicodeLineBytes, 0, unicodeLineBytes.Length)];
                        Encoding.Unicode.GetChars(unicodeLineBytes, 0, unicodeLineBytes.Length, unicodeLineChars, 0);
                        string newLine = new string(unicodeLineChars);
                        _InputWordBuffer = newLine.Split(Separators, StringSplitOptions.RemoveEmptyEntries);
                        _WdBuffPtr       = 0;
                        _InputBuffer     = new byte[0];
                        _InBuffPtr       = 0;
                        break;

                    default:
                        _InputBuffer     = new byte[0];
                        _InBuffPtr       = 0;
                        _InputWordBuffer = new string[0];
                        _WdBuffPtr       = 0;
                        break;
                    }
                    break;

                case InputConvertings.ParsingNumber:
                    switch (newInputConverting)
                    {
                    case InputConvertings.None:
                        string lineBuffer             = String.Join(Separators[0] + "", _InputWordBuffer, _WdBuffPtr, _InputWordBuffer.Length - _WdBuffPtr);
                        byte[] unicodeLineBufferBytes = Encoding.Unicode.GetBytes(lineBuffer);
                        _InputBuffer     = Encoding.Convert(Encoding.Unicode, _InputDevice.CharSet, unicodeLineBufferBytes);
                        _InBuffPtr       = 0;
                        _InputWordBuffer = new string[0];
                        _WdBuffPtr       = 0;
                        break;

                    case InputConvertings.String:
                        lineBuffer             = String.Join(Separators[0] + "", _InputWordBuffer, _WdBuffPtr, _InputWordBuffer.Length - _WdBuffPtr);
                        unicodeLineBufferBytes = Encoding.Unicode.GetBytes(lineBuffer);
                        _InputBuffer           = Encoding.Convert(Encoding.Unicode, ParentIDE.Machine.CharSet, unicodeLineBufferBytes);
                        _InBuffPtr             = 0;
                        _InputWordBuffer       = new string[0];
                        _WdBuffPtr             = 0;
                        break;

                    case InputConvertings.ParsingNumber:
                        break;

                    default:
                        _InputBuffer     = new byte[0];
                        _InBuffPtr       = 0;
                        _InputWordBuffer = new string[0];
                        _WdBuffPtr       = 0;
                        break;
                    }
                    break;

                default:
                    _InputBuffer     = new byte[0];
                    _InBuffPtr       = 0;
                    _InputWordBuffer = new string[0];
                    _WdBuffPtr       = 0;
                    break;
                }
            }
            else
            {
                _InputBuffer     = new byte[0];
                _InBuffPtr       = 0;
                _InputWordBuffer = new string[0];
                _WdBuffPtr       = 0;
            }
        }