<commit_before>/*
 * Copyright 2004,2006 The Poderosa Project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * $Id: SerialSettings.cs,v 1.6 2012/03/15 15:19:18 kzmi Exp $
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Diagnostics;

using Poderosa.Util;
using Poderosa.Serializing;
using Poderosa.ConnectionParam;
using Poderosa.Protocols;
using Poderosa.Terminal;
using Poderosa.View;
using Poderosa.MacroEngine;

namespace Poderosa.SerialPort {
    internal class SerialTerminalParam : ITerminalParameter, IAutoExecMacroParameter {
        private string _portName;
        private string _terminalType;
        private string _autoExecMacro;

        [MacroConnectionParameter]
        public string PortName {
            get {
                return _portName;
            }
            set {
                _portName = value;
            }
        }

        public SerialTerminalParam() {
            _portName = "COM1";
        }

        //VAł͕E͊֒mB̒l͍ŏGLine̒ɂȂ̂łOɂ͂łȂ
        public int InitialWidth {
            get {
                return 80;
            }
        }

        public int InitialHeight {
            get {
                return 25;
            }
        }

        public string TerminalType {
            get {
                return _terminalType;
            }
        }

        public void SetTerminalName(string terminalType) {
            _terminalType = terminalType;
        }
        public void SetTerminalSize(int width, int height) {
            //do nothing
        }

        public bool UIEquals(ITerminalParameter param) {
            SerialTerminalParam tp = param as SerialTerminalParam;
            return tp != null && _portName == tp.PortName;
        }

        public IAdaptable GetAdapter(Type adapter) {
            return SerialPortPlugin.Instance.PoderosaWorld.AdapterManager.GetAdapter(this, adapter);
        }

        public object Clone() {
            SerialTerminalParam tp = new SerialTerminalParam();
            tp._portName = _portName;
            tp._terminalType = _terminalType;
            return tp;
        }

        #region IAutoExecMacroParameter

        public string AutoExecMacroPath {
            get {
                return _autoExecMacro;
            }
            set {
                _autoExecMacro = value;
            }
        }

        #endregion
    }

    internal class SerialTerminalSettings : TerminalSettings {

        private int _baudRate;
        private byte _byteSize;  //7,8̂ǂ炩
        private Parity _parity; //Win32NX̒萔̂ꂩ
        private StopBits _stopBits; //Win32NX̒萔̂ꂩ
        private FlowControl _flowControl;
        private int _transmitDelayPerChar;
        private int _transmitDelayPerLine;

        /// <summary>
        /// <ja>ftHgݒŏ܂B</ja>
        /// <en>Initializes with default values.</en>
        /// <seealso cref="Poderosa.Macro.ConnectionList.Open"/>
        /// </summary>
        /// <remarks>
        /// <ja>p[^͎̂悤ɏ܂B</ja>
        /// <en>The parameters are set as following:</en>
        /// <list type="table">
        ///   <item><term><ja>GR[fBO</ja><en>Encoding</en></term><description><ja>EUC-JP</ja><en>iso-8859-1</en></description></item>@
        ///   <item><term><ja>O</ja><en>Log</en></term><description><ja>擾Ȃ</ja><en>None</en></description></item>@@@@@@@
        ///   <item><term><ja>[JGR[</ja><en>Local echo</en></term><description><ja>Ȃ</ja><en>Don't</en></description></item>@@
        ///   <item><term><ja>Ms</ja><en>New line</en></term><description>CR</description></item>@@@@
        ///   <item><term><ja>{[[g</ja><en>Baud Rate</en></term><description>9600</description></item>
        ///   <item><term><ja>f[^</ja><en>Data Bits</en></term><description><ja>8rbg</ja><en>8 bits</en></description></item>
        ///   <item><term><ja>peB</ja><en>Parity</en></term><description><ja>Ȃ</ja><en>None</en></description></item>
        ///   <item><term><ja>Xgbvrbg</ja><en>Stop Bits</en></term><description><ja>Prbg</ja><en>1 bit</en></description></item>
        ///   <item><term><ja>t[Rg[</ja><en>Flow Control</en></term><description><ja>Ȃ</ja><en>None</en></description></item>
        /// </list>
        /// <ja>ڑJɂ́A<see cref="Poderosa.Macro.ConnectionList.Open"/>\bḧƂSerialTerminalParamIuWFNgn܂B</ja>
        /// <en>To open a new connection, pass the SerialTerminalParam object to the <see cref="Poderosa.Macro.ConnectionList.Open"/> method.</en>
        /// </remarks>
        public SerialTerminalSettings() {
            _baudRate = 9600;
            _byteSize = 8;
            _parity = Parity.NOPARITY;
            _stopBits = StopBits.ONESTOPBIT;
            _flowControl = FlowControl.None;
        }
        public override ITerminalSettings Clone() {
            SerialTerminalSettings p = new SerialTerminalSettings();
            p.Import(this);
            return p;
        }

        public void BaseImport(ITerminalSettings ts) {
            base.Import(ts);
            //ACR͕ێ
            this.BeginUpdate();
            this.Icon = SerialPortPlugin.Instance.LoadIcon();
            this.EndUpdate();
        }

        public override void Import(ITerminalSettings src) {
            base.Import(src);
            SerialTerminalSettings p = src as SerialTerminalSettings;
            Debug.Assert(p != null);

            _baudRate = p._baudRate;
            _byteSize = p._byteSize;
            _parity = p._parity;
            _stopBits = p._stopBits;
            _flowControl = p._flowControl;
            _transmitDelayPerChar = p._transmitDelayPerChar;
            _transmitDelayPerLine = p._transmitDelayPerLine;
        }



        //TODO ȉłEnsureUpdatełȂĂ̂H

        /// <summary>
        /// <ja>{[[głB</ja>
        /// <en>Gets or sets the baud rate.</en>
        /// </summary>
        [MacroConnectionParameter]
        public int BaudRate {
            get {
                return _baudRate;
            }
            set {
                _baudRate = value;
            }
        }
        /// <summary>
        /// <ja>f[^̃rbgłB</ja>
        /// <en>Gets or sets the bit count of the data.</en>
        /// </summary>
        /// <remarks>
        /// <ja>VWłȂƂ܂B</ja>
        /// <en>The value must be 7 or 8.</en>
        /// </remarks>
        [MacroConnectionParameter]
        public byte ByteSize {
            get {
                return _byteSize;
            }
            set {
                _byteSize = value;
            }
        }
        /// <summary>
        /// <ja>peBłB</ja>
        /// <en>Gets or sets the parity.</en>
        /// </summary>
        [MacroConnectionParameter]
        public Parity Parity {
            get {
                return _parity;
            }
            set {
                _parity = value;
            }
        }
        /// <summary>
        /// <ja>XgbvrbgłB</ja>
        /// <en>Gets or sets the stop bit.</en>
        /// </summary>
        [MacroConnectionParameter]
        public StopBits StopBits {
            get {
                return _stopBits;
            }
            set {
                _stopBits = value;
            }
        }
        /// <summary>
        /// <ja>t[Rg[łB</ja>
        /// <en>Gets or sets the flow control.</en>
        /// </summary>
        [MacroConnectionParameter]
        public FlowControl FlowControl {
            get {
                return _flowControl;
            }
            set {
                _flowControl = value;
            }
        }

        /// <summary>
        /// <ja>̃fBC(~bP)łB</ja>
        /// <en>Gets or sets the delay time per a character in milliseconds.</en>
        /// </summary>
        public int TransmitDelayPerChar {
            get {
                return _transmitDelayPerChar;
            }
            set {
                _transmitDelayPerChar = value;
            }
        }
        /// <summary>
        /// <ja>s̃fBC(~bP)łB</ja>
        /// <en>Gets or sets the delay time per a line in milliseconds.</en>
        /// </summary>
        public int TransmitDelayPerLine {
            get {
                return _transmitDelayPerLine;
            }
            set {
                _transmitDelayPerLine = value;
            }
        }

    }

    //Serializers
    internal class SerialTerminalParamSerializer : ISerializeServiceElement {
        public Type ConcreteType {
            get {
                return typeof(SerialTerminalParam);
            }
        }


        public StructuredText Serialize(object obj) {
            SerialTerminalParam tp = obj as SerialTerminalParam;
            Debug.Assert(tp != null);

            StructuredText node = new StructuredText(this.ConcreteType.FullName);
            node.Set("Port", tp.PortName);
            if (tp.TerminalType != "vt100")
                node.Set("TerminalType", tp.TerminalType);
            if (tp.AutoExecMacroPath != null)
                node.Set("autoexec-macro", tp.AutoExecMacroPath);
            return node;
        }

        public object Deserialize(StructuredText node) {
            SerialTerminalParam tp = new SerialTerminalParam();
            tp.PortName = node.Get("PortName", "COM1");
            tp.SetTerminalName(node.Get("TerminalType", "vt100"));
            tp.AutoExecMacroPath = node.Get("autoexec-macro", null);
            return tp;
        }
    }
    internal class SerialTerminalSettingsSerializer : ISerializeServiceElement {
        public Type ConcreteType {
            get {
                return typeof(SerialTerminalSettings);
            }
        }


        public StructuredText Serialize(object obj) {
            SerialTerminalSettings ts = obj as SerialTerminalSettings;
            Debug.Assert(ts != null);

            StructuredText node = new StructuredText(this.ConcreteType.FullName);
            node.AddChild(SerialPortPlugin.Instance.SerializeService.Serialize(typeof(TerminalSettings), ts));

            node.Set("baud-rate", ts.BaudRate.ToString());
            if (ts.ByteSize != 8)
                node.Set("byte-size", ts.ByteSize.ToString());
            if (ts.Parity != Parity.NOPARITY)
                node.Set("parity", ts.Parity.ToString());
            if (ts.StopBits != StopBits.ONESTOPBIT)
                node.Set("stop-bits", ts.StopBits.ToString());
            if (ts.FlowControl != FlowControl.None)
                node.Set("flow-control", ts.FlowControl.ToString());
            if (ts.TransmitDelayPerChar != 0)
                node.Set("delay-per-char", ts.TransmitDelayPerChar.ToString());
            if (ts.TransmitDelayPerLine != 0)
                node.Set("delay-per-line", ts.TransmitDelayPerLine.ToString());

            return node;
        }

        public object Deserialize(StructuredText node) {
            SerialTerminalSettings ts = SerialPortUtil.CreateDefaultSerialTerminalSettings("COM1");

            //TODO Deserialize̕ʃo[Wimportׂ낤BService̎ςBvfɂ͋RXgN^΂
            StructuredText basenode = node.FindChild(typeof(TerminalSettings).FullName);
            if (basenode != null)
                ts.BaseImport((ITerminalSettings)SerialPortPlugin.Instance.SerializeService.Deserialize(basenode));

            ts.BaudRate = ParseUtil.ParseInt(node.Get("baud-rate"), 9600);
            ts.ByteSize = (byte)ParseUtil.ParseInt(node.Get("byte-size"), 8);
            ts.Parity = ParseUtil.ParseEnum<Parity>(node.Get("parity"), Parity.NOPARITY);
            ts.StopBits = ParseUtil.ParseEnum<StopBits>(node.Get("stop-bits"), StopBits.ONESTOPBIT);
            ts.FlowControl = ParseUtil.ParseEnum<FlowControl>(node.Get("flow-control"), FlowControl.None);
            ts.TransmitDelayPerChar = ParseUtil.ParseInt(node.Get("delay-per-char"), 0);
            ts.TransmitDelayPerLine = ParseUtil.ParseInt(node.Get("delay-per-line"), 0);

            return ts;
        }
    }
}
<commit_msg>Serial port name is saved in .conf file with "PortName=" line. Old "Port=n" line is still accepted.<commit_after>/*
 * Copyright 2004,2006 The Poderosa Project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * $Id: SerialSettings.cs,v 1.6 2012/03/15 15:19:18 kzmi Exp $
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Diagnostics;

using Poderosa.Util;
using Poderosa.Serializing;
using Poderosa.ConnectionParam;
using Poderosa.Protocols;
using Poderosa.Terminal;
using Poderosa.View;
using Poderosa.MacroEngine;

namespace Poderosa.SerialPort {
    internal class SerialTerminalParam : ITerminalParameter, IAutoExecMacroParameter {
        private string _portName;
        private string _terminalType;
        private string _autoExecMacro;

        [MacroConnectionParameter]
        public string PortName {
            get {
                return _portName;
            }
            set {
                _portName = value;
            }
        }

        public SerialTerminalParam() {
            _portName = "COM1";
        }

        //VAł͕E͊֒mB̒l͍ŏGLine̒ɂȂ̂łOɂ͂łȂ
        public int InitialWidth {
            get {
                return 80;
            }
        }

        public int InitialHeight {
            get {
                return 25;
            }
        }

        public string TerminalType {
            get {
                return _terminalType;
            }
        }

        public void SetTerminalName(string terminalType) {
            _terminalType = terminalType;
        }
        public void SetTerminalSize(int width, int height) {
            //do nothing
        }

        public bool UIEquals(ITerminalParameter param) {
            SerialTerminalParam tp = param as SerialTerminalParam;
            return tp != null && _portName == tp.PortName;
        }

        public IAdaptable GetAdapter(Type adapter) {
            return SerialPortPlugin.Instance.PoderosaWorld.AdapterManager.GetAdapter(this, adapter);
        }

        public object Clone() {
            SerialTerminalParam tp = new SerialTerminalParam();
            tp._portName = _portName;
            tp._terminalType = _terminalType;
            return tp;
        }

        #region IAutoExecMacroParameter

        public string AutoExecMacroPath {
            get {
                return _autoExecMacro;
            }
            set {
                _autoExecMacro = value;
            }
        }

        #endregion
    }

    internal class SerialTerminalSettings : TerminalSettings {

        private int _baudRate;
        private byte _byteSize;  //7,8̂ǂ炩
        private Parity _parity; //Win32NX̒萔̂ꂩ
        private StopBits _stopBits; //Win32NX̒萔̂ꂩ
        private FlowControl _flowControl;
        private int _transmitDelayPerChar;
        private int _transmitDelayPerLine;

        /// <summary>
        /// <ja>ftHgݒŏ܂B</ja>
        /// <en>Initializes with default values.</en>
        /// <seealso cref="Poderosa.Macro.ConnectionList.Open"/>
        /// </summary>
        /// <remarks>
        /// <ja>p[^͎̂悤ɏ܂B</ja>
        /// <en>The parameters are set as following:</en>
        /// <list type="table">
        ///   <item><term><ja>GR[fBO</ja><en>Encoding</en></term><description><ja>EUC-JP</ja><en>iso-8859-1</en></description></item>@
        ///   <item><term><ja>O</ja><en>Log</en></term><description><ja>擾Ȃ</ja><en>None</en></description></item>@@@@@@@
        ///   <item><term><ja>[JGR[</ja><en>Local echo</en></term><description><ja>Ȃ</ja><en>Don't</en></description></item>@@
        ///   <item><term><ja>Ms</ja><en>New line</en></term><description>CR</description></item>@@@@
        ///   <item><term><ja>{[[g</ja><en>Baud Rate</en></term><description>9600</description></item>
        ///   <item><term><ja>f[^</ja><en>Data Bits</en></term><description><ja>8rbg</ja><en>8 bits</en></description></item>
        ///   <item><term><ja>peB</ja><en>Parity</en></term><description><ja>Ȃ</ja><en>None</en></description></item>
        ///   <item><term><ja>Xgbvrbg</ja><en>Stop Bits</en></term><description><ja>Prbg</ja><en>1 bit</en></description></item>
        ///   <item><term><ja>t[Rg[</ja><en>Flow Control</en></term><description><ja>Ȃ</ja><en>None</en></description></item>
        /// </list>
        /// <ja>ڑJɂ́A<see cref="Poderosa.Macro.ConnectionList.Open"/>\bḧƂSerialTerminalParamIuWFNgn܂B</ja>
        /// <en>To open a new connection, pass the SerialTerminalParam object to the <see cref="Poderosa.Macro.ConnectionList.Open"/> method.</en>
        /// </remarks>
        public SerialTerminalSettings() {
            _baudRate = 9600;
            _byteSize = 8;
            _parity = Parity.NOPARITY;
            _stopBits = StopBits.ONESTOPBIT;
            _flowControl = FlowControl.None;
        }
        public override ITerminalSettings Clone() {
            SerialTerminalSettings p = new SerialTerminalSettings();
            p.Import(this);
            return p;
        }

        public void BaseImport(ITerminalSettings ts) {
            base.Import(ts);
            //ACR͕ێ
            this.BeginUpdate();
            this.Icon = SerialPortPlugin.Instance.LoadIcon();
            this.EndUpdate();
        }

        public override void Import(ITerminalSettings src) {
            base.Import(src);
            SerialTerminalSettings p = src as SerialTerminalSettings;
            Debug.Assert(p != null);

            _baudRate = p._baudRate;
            _byteSize = p._byteSize;
            _parity = p._parity;
            _stopBits = p._stopBits;
            _flowControl = p._flowControl;
            _transmitDelayPerChar = p._transmitDelayPerChar;
            _transmitDelayPerLine = p._transmitDelayPerLine;
        }



        //TODO ȉłEnsureUpdatełȂĂ̂H

        /// <summary>
        /// <ja>{[[głB</ja>
        /// <en>Gets or sets the baud rate.</en>
        /// </summary>
        [MacroConnectionParameter]
        public int BaudRate {
            get {
                return _baudRate;
            }
            set {
                _baudRate = value;
            }
        }
        /// <summary>
        /// <ja>f[^̃rbgłB</ja>
        /// <en>Gets or sets the bit count of the data.</en>
        /// </summary>
        /// <remarks>
        /// <ja>VWłȂƂ܂B</ja>
        /// <en>The value must be 7 or 8.</en>
        /// </remarks>
        [MacroConnectionParameter]
        public byte ByteSize {
            get {
                return _byteSize;
            }
            set {
                _byteSize = value;
            }
        }
        /// <summary>
        /// <ja>peBłB</ja>
        /// <en>Gets or sets the parity.</en>
        /// </summary>
        [MacroConnectionParameter]
        public Parity Parity {
            get {
                return _parity;
            }
            set {
                _parity = value;
            }
        }
        /// <summary>
        /// <ja>XgbvrbgłB</ja>
        /// <en>Gets or sets the stop bit.</en>
        /// </summary>
        [MacroConnectionParameter]
        public StopBits StopBits {
            get {
                return _stopBits;
            }
            set {
                _stopBits = value;
            }
        }
        /// <summary>
        /// <ja>t[Rg[łB</ja>
        /// <en>Gets or sets the flow control.</en>
        /// </summary>
        [MacroConnectionParameter]
        public FlowControl FlowControl {
            get {
                return _flowControl;
            }
            set {
                _flowControl = value;
            }
        }

        /// <summary>
        /// <ja>̃fBC(~bP)łB</ja>
        /// <en>Gets or sets the delay time per a character in milliseconds.</en>
        /// </summary>
        public int TransmitDelayPerChar {
            get {
                return _transmitDelayPerChar;
            }
            set {
                _transmitDelayPerChar = value;
            }
        }
        /// <summary>
        /// <ja>s̃fBC(~bP)łB</ja>
        /// <en>Gets or sets the delay time per a line in milliseconds.</en>
        /// </summary>
        public int TransmitDelayPerLine {
            get {
                return _transmitDelayPerLine;
            }
            set {
                _transmitDelayPerLine = value;
            }
        }

    }

    //Serializers
    internal class SerialTerminalParamSerializer : ISerializeServiceElement {
        public Type ConcreteType {
            get {
                return typeof(SerialTerminalParam);
            }
        }


        public StructuredText Serialize(object obj) {
            SerialTerminalParam tp = obj as SerialTerminalParam;
            Debug.Assert(tp != null);

            StructuredText node = new StructuredText(this.ConcreteType.FullName);
            node.Set("PortName", tp.PortName);
            if (tp.TerminalType != "vt100")
                node.Set("TerminalType", tp.TerminalType);
            if (tp.AutoExecMacroPath != null)
                node.Set("autoexec-macro", tp.AutoExecMacroPath);
            return node;
        }

        public object Deserialize(StructuredText node) {
            SerialTerminalParam tp = new SerialTerminalParam();
            if (node.Get("Port") != null) {
                // accept old parameter.
                // "PortName" setting overwrites this setting.
                tp.PortName = "COM" + node.Get("Port");
            }
            tp.PortName = node.Get("PortName", tp.PortName);
            tp.SetTerminalName(node.Get("TerminalType", "vt100"));
            tp.AutoExecMacroPath = node.Get("autoexec-macro", null);
            return tp;
        }
    }
    internal class SerialTerminalSettingsSerializer : ISerializeServiceElement {
        public Type ConcreteType {
            get {
                return typeof(SerialTerminalSettings);
            }
        }


        public StructuredText Serialize(object obj) {
            SerialTerminalSettings ts = obj as SerialTerminalSettings;
            Debug.Assert(ts != null);

            StructuredText node = new StructuredText(this.ConcreteType.FullName);
            node.AddChild(SerialPortPlugin.Instance.SerializeService.Serialize(typeof(TerminalSettings), ts));

            node.Set("baud-rate", ts.BaudRate.ToString());
            if (ts.ByteSize != 8)
                node.Set("byte-size", ts.ByteSize.ToString());
            if (ts.Parity != Parity.NOPARITY)
                node.Set("parity", ts.Parity.ToString());
            if (ts.StopBits != StopBits.ONESTOPBIT)
                node.Set("stop-bits", ts.StopBits.ToString());
            if (ts.FlowControl != FlowControl.None)
                node.Set("flow-control", ts.FlowControl.ToString());
            if (ts.TransmitDelayPerChar != 0)
                node.Set("delay-per-char", ts.TransmitDelayPerChar.ToString());
            if (ts.TransmitDelayPerLine != 0)
                node.Set("delay-per-line", ts.TransmitDelayPerLine.ToString());

            return node;
        }

        public object Deserialize(StructuredText node) {
            SerialTerminalSettings ts = SerialPortUtil.CreateDefaultSerialTerminalSettings("COM1");

            //TODO Deserialize̕ʃo[Wimportׂ낤BService̎ςBvfɂ͋RXgN^΂
            StructuredText basenode = node.FindChild(typeof(TerminalSettings).FullName);
            if (basenode != null)
                ts.BaseImport((ITerminalSettings)SerialPortPlugin.Instance.SerializeService.Deserialize(basenode));

            ts.BaudRate = ParseUtil.ParseInt(node.Get("baud-rate"), 9600);
            ts.ByteSize = (byte)ParseUtil.ParseInt(node.Get("byte-size"), 8);
            ts.Parity = ParseUtil.ParseEnum<Parity>(node.Get("parity"), Parity.NOPARITY);
            ts.StopBits = ParseUtil.ParseEnum<StopBits>(node.Get("stop-bits"), StopBits.ONESTOPBIT);
            ts.FlowControl = ParseUtil.ParseEnum<FlowControl>(node.Get("flow-control"), FlowControl.None);
            ts.TransmitDelayPerChar = ParseUtil.ParseInt(node.Get("delay-per-char"), 0);
            ts.TransmitDelayPerLine = ParseUtil.ParseInt(node.Get("delay-per-line"), 0);

            return ts;
        }
    }
}

{'fraction_non_alphanumeric': 0.12362335920672979, 'fraction_numerical': 0.0059739098630471, 'mean_word_length': 1.6861696321362296, 'pattern_counts': {'":': 0, '<': 383, '<?xml version=': 0, '>': 383, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2733227', 'n_tokens_mistral': 7280, 'n_tokens_neox': 7028, 'n_words': 1274}