Exemplo n.º 1
0
        private void processSingleLineString(char c)
        {
            _isString = true;
            switch (c)
            {
            case '"':
            case '\'':
                if (_stack.Peek() == c)
                {
                    _stack.Pop();
                    _mode = (ReadMode)_stack.Pop();
                    map[(int)_mode](' ');
                }
                else
                {
                    _buf.Append(c);
                }
                return;

            case '\r':
            case '\n':
                throw new BxlException("new line in regular string", _info.Clone());

            case '\\':
                _stack.Push((char)_mode);
                _mode = ReadMode.EscapingBackSlash;
                return;

            default:
                _buf.Append(c);
                return;
            }
        }
Exemplo n.º 2
0
        private void processWaitingForNL(char c)
        {
            switch (c)
            {
            case ' ':
            case ',':
            case '\t':
                return;

            case '\n':
            case '\r':
                _mode = ReadMode.NewLine;
                return;

            case '#':
                map[(int)_mode]('\n');
                _stack.Push((char)_mode);
                _mode = ReadMode.Commentary;
                return;

            case '/':
                if (_next == '*')
                {
                    _stack.Push((char)_mode);
                    _mode    = ReadMode.MultilineCommentary;
                    mlcdepth = 1;
                    _skip    = 1;
                    return;
                }
                goto default;

            default:
                throw new BxlException("unexpected symbol " + c, _info.Clone());
            }
        }
Exemplo n.º 3
0
        void ReadMultiWordsContent(ReadMode rm)
        {
            string fileName = "";

            switch (rm)
            {
            case ReadMode.Predlogenie:
                fileName = "Предложения.txt";
                ReadMultiWordsContent(fileName);
                break;

            case ReadMode.Schitalki:
                ReadMultiLineContent(MultiLineContentType.Schitalki);
                break;

            case ReadMode.Stishki:
                ReadMultiLineContent(MultiLineContentType.Stishki);
                break;

            case ReadMode.Skorogovorki:
                ReadMultiLineContent(MultiLineContentType.Skorogovorki);
                break;

            case ReadMode.Songs:
                ReadMultiLineContent(MultiLineContentType.Songs);
                break;

            case ReadMode.Rasskazy:
                ReadMultiLineContent(MultiLineContentType.Rasskazy);
                break;
            }
        }
Exemplo n.º 4
0
        private void Test(ReadMode readMode, string path, TestType testType, int attempts)
        {
            enabled = false;
            coroutineHost.StartCoroutine(ErrorCatchingCoroutine(TestHarness(readMode, path, testType, attempts,
                                                                            (duration, bytes, memory, maxMemory, names) =>
            {
                enabled = true;
                LogWorkProgress("Retries: " + attempts);
                LogWorkProgress("Avg duration: " + duration);
                LogWorkProgress("Avg bytes read: " + bytes);
                LogWorkProgress("Avg memory peak: " + memory);
                LogWorkProgress("Max memory peak: " + maxMemory);

                if (names != null)
                {
                    LogWorkProgress("Asset names:");
                    foreach (var name in names)
                    {
                        LogWorkProgress("    " + name);
                    }
                }
            }),
                                                                ex =>
            {
                enabled = true;
                LogWorkProgress(ex.ToString());
            }
                                                                ));
        }
Exemplo n.º 5
0
 public CsvReader(string filename)
 {
     string[] lines = System.IO.File.ReadAllLines(filename);
     for (int i = 0; i < lines.Length; i++)
     {
         string line = lines[i].Trim();
         if (line.StartsWith("#") || line.StartsWith("//"))
         {
             continue;
         }
         string lineLower = line.ToLower();
         if (lineLower.StartsWith("abs"))
         {
             readMode = ReadMode.Absolute;
         }
         if (lineLower.StartsWith("relative"))
         {
             readMode = ReadMode.Relative;
         }
         if (lineLower.StartsWith("inc"))
         {
             readMode = ReadMode.Relative;
         }
         if (line.IndexOf(',') > 0)
         {
             ReadPoint(line);
         }
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// Gets the user value in the versioned item.
        /// </summary>
        /// <param name="metadataTable">The metadata table</param>
        /// <param name="valueSerializer">The value serializer.</param>
        /// <param name="isValueAReferenceType">Is reference type</param>
        /// <param name="readMode">The read mode.</param>
        /// <param name="valueCounter">Loaded value counter.</param>
        /// <param name="cancellationToken">Token used to signal cancellation.</param>
        /// <param name="traceType">trace Id</param>
        /// <param name="duringRecovery">Called during recovery.</param>
        /// <returns>The value.</returns>
        /// <remarks>
        /// Reference Type Life Cycle
        /// At Recovery:    [InUse = false, CanBeSweepedToDisk = true, Value = null]
        /// Read:           [InUse = readMode == CacheResult] [Value = CacheResult?] [CanBeSweepedToDisk = value != null]
        ///
        /// Value Type Life Cycle
        /// At Recovery:    [InUse = false, CanBeSweepedToDisk = true, Value = default(TValue)]
        /// First Read:     [Value = CacheResult?] [InUse = true] [CanBeSweepedToDisk = false]
        /// </remarks>
        public virtual async Task <TValue> GetValueAsync(
            MetadataTable metadataTable,
            IStateSerializer <TValue> valueSerializer,
            bool isValueAReferenceType,
            ReadMode readMode,
            LoadValueCounter valueCounter,
            CancellationToken cancellationToken,
            string traceType,
            bool duringRecovery = false)
        {
            TValue value = this.Value;

            if (this.ShouldValueBeLoadedFromDisk(isValueAReferenceType, value, traceType) == false || readMode == ReadMode.Off)
            {
                return(value);
            }

            Diagnostics.Assert(duringRecovery == true || readMode != ReadMode.CacheResult || isValueAReferenceType == false || this.InUse == true, traceType, "If CacheResult, InUse must have been set.");
            value = await MetadataManager.ReadValueAsync <TValue>(metadataTable.Table, this, valueSerializer, cancellationToken, traceType).ConfigureAwait(false);

            // Value must be set before updating the flags.
            // Example problem: After recovery two reads on the same key,
            // T1 sees InUse (IsLoaded) == false, reads the value, updates InUse, *Context Switch*
            // T2 sees InUse (IsLoaded) == true, reads this.Value which has not been set by T1.
            if (this.ShouldCacheValue(readMode, isValueAReferenceType))
            {
                this.Value = value;
                valueCounter.IncrementCounter();
            }

            // Flags must always be updated. For value type, inUse (IsLoadedFromDiskAfterRecovery) must be set.
            this.UpdateFlagsFollowingLoadValue(isValueAReferenceType, value, readMode, traceType);

            return(value);
        }
Exemplo n.º 7
0
        private void processColon(char c)
        {
            switch (c)
            {
            case ':':
                if (_value.Length == 0)
                {
                    throw new BxlException("wrong namespace", _info.Clone());
                }
                _prefix = _value;
                _value  = "";
                _mode   = (ReadMode)_stack.Pop();
                return;

            default:
                _mode = (ReadMode)_stack.Pop();
                if (_mode == ReadMode.ElementName)
                {
                    addNode();
                }
                else if (_mode == ReadMode.AttributeName)
                {
                    addAnonAttribute();
                }
                _mode = ReadMode.TextContent;
                map[(int)_mode](c);
                return;
            }
        }
Exemplo n.º 8
0
            public void SetReadMode(ReadMode Mode)
            {
                lock (Client)
                {
                    if (Mode == this.Mode)
                    {
                        return;
                    }

                    Log(4, "Switching to read mode: " + Mode.ToString());

                    if (Mode == ReadMode.XML)
                    {
                        XMLStream.ClearStart();
                        Log(4, "XMLStream Cleared and started");
                    }

                    this.Mode = Mode;

                    if (this.Mode == ReadMode.Text)
                    {
                        XMLStream.Stop();
                    }
                }
            }
Exemplo n.º 9
0
        private void processUnquoting(char c)
        {
            switch (c)
            {
            case '"':
                _symbolCount++;
                if (_symbolCount == 3)
                {
                    _mode        = (ReadMode)_stack.Pop();
                    _symbolCount = 0;
                    map[(int)_mode](' ');
                }
                return;

            case '\\':
                _buf.Append('"', _symbolCount);
                _stack.Push((char)ReadMode.MultiLineString);
                _mode = ReadMode.EscapingBackSlash;
                return;

            default:
                _buf.Append('"', _symbolCount);
                _buf.Append(c);
                _mode = ReadMode.MultiLineString;
                return;
            }
        }
Exemplo n.º 10
0
 /// <summary> 读取从开始字符到终止字符之间的数据,如果下一个有效字符不是开始字符,将抛出异常
 /// </summary>
 /// <param name="start">开始字符</param>
 /// <param name="stop">终止字符</param>
 /// <param name="mode">读取模式</param>
 public string ReadStartToStop(char start, char stop, ReadMode mode = ReadMode.SkipAll | ReadMode.RemoveAll)
 {
     _inner.StartChar = start;
     _inner.StopChar  = stop;
     _inner.Mode      = 0;
     return(ReadStartToStop(_inner.Start, mode));
 }
Exemplo n.º 11
0
        internal Message GetReadModeCommand(bool isMasterOnly)
        {
            var serverEndpoint = Bridge.ServerEndPoint;

            if (serverEndpoint.RequiresReadMode)
            {
                ReadMode requiredReadMode = isMasterOnly ? ReadMode.ReadWrite : ReadMode.ReadOnly;
                if (requiredReadMode != currentReadMode)
                {
                    currentReadMode = requiredReadMode;
                    switch (requiredReadMode)
                    {
                    case ReadMode.ReadOnly: return(ReusableReadOnlyCommand);

                    case ReadMode.ReadWrite: return(ReusableReadWriteCommand);
                    }
                }
            }
            else if (currentReadMode == ReadMode.ReadOnly)
            { // we don't need it (because we're not a cluster, or not a slave),
                // but we are in read-only mode; switch to read-write
                currentReadMode = ReadMode.ReadWrite;
                return(ReusableReadWriteCommand);
            }
            return(null);
        }
Exemplo n.º 12
0
 /// <summary> 读取从开始字符到终止字符之间的数据,如果下一个有效字符不在字符集中,将抛出异常
 /// </summary>
 /// <param name="start">开始字符集</param>
 /// <param name="getStopChar">根据匹配的开始字符,返回结束字符的委托</param>
 /// <param name="mode">读取模式</param>
 public string ReadStartToStop(char[] start, Func <char, char> getStopChar, ReadMode mode = ReadMode.SkipAll | ReadMode.RemoveAll)
 {
     _inner.CharArray   = start;
     _inner.Mode        = 2;
     _inner.GetStopChar = getStopChar;
     return(ReadStartToStop(_inner.Start, mode));
 }
Exemplo n.º 13
0
        private string SetMode(string line, ReadMode mode)
        {
            string name = string.Empty;

            switch (mode)
            {
            case ReadMode.None:
                if (Regex.IsMatch(line, " *Partial Public Class .*DataTable$"))
                {
                    this._ReadMode = ReadMode.Table;
                    Match m = Regex.Match(line, "(?<=Partial Public Class )(.*)(?=DataTable)");
                    break;
                }
                if (Regex.IsMatch(line, " *Partial Public Class .*Row$"))
                {
                    this._ReadMode = ReadMode.Row;
                    break;
                }
                break;

            case ReadMode.Table:
            case ReadMode.Row:
                if (Regex.IsMatch(line, " *End Sub$"))
                {
                    this._ReadMode = ReadMode.None;
                    break;
                }

                break;
            }
        }
Exemplo n.º 14
0
 private void processCommentary(char c)
 {
     if (c == '\n' || c == '\r')
     {
         _mode = (ReadMode)_stack.Pop();
     }
 }
Exemplo n.º 15
0
        public bool SetRead(int person_id, int message_id, ReadMode mode)
        {
            var read_status = db.Message_GetReadStatus(message_id, person_id) ?? new ReadStatus()
            {
                PersonId = person_id, MessageId = message_id
            };

            switch (mode)
            {
            case ReadMode.APP:
                read_status.App = true;
                break;

            case ReadMode.EMAIL:
                read_status.Email = true;
                break;

            case ReadMode.TELEGRAM:
                read_status.Telegram = true;
                break;

            case ReadMode.WEB:
                read_status.Web = true;
                break;

            case ReadMode.WHATSAPP:
                read_status.Whatsapp = true;
                break;

            default:
                return(false);
            }
            return(db.SaveObj(read_status));
        }
Exemplo n.º 16
0
        private void processQuoting1(char c)
        {
            _isString = true;
            switch (c)
            {
            case '"':
                _mode = ReadMode.Quoting2;
                return;

            case '\n':
            case '\r':
                throw new BxlException("new line in regular string", _info.Clone());

            case '\\':
                _stack.Push('"');
                _stack.Push((char)ReadMode.SingleLineString);
                _mode = ReadMode.EscapingBackSlash;
                return;

            default:
                _buf.Append(c);
                _stack.Push('"');
                _mode = ReadMode.SingleLineString;
                return;
            }
        }
Exemplo n.º 17
0
        List <RecItem> ParseItems(string items)
        {
            var      list   = new List <RecItem>();
            string   name   = String.Empty;
            string   stack  = String.Empty;
            string   prefix = String.Empty;
            ReadMode pos    = ReadMode.Stack;

            foreach (char ch in items)
            {
                switch (ch)
                {
                case ',':
                    name = name.Replace("(s)", String.Empty);
                    list.Add(new RecItem(
                                 name.Trim(),
                                 Int32.Parse(stack),
                                 prefix == "" ? 0 : TShock.Utils.GetPrefixByName(prefix.Trim()).First()));
                    break;

                case '[':
                    pos = ReadMode.Prefix;
                    break;

                case ']':
                    pos = ReadMode.Name;
                    break;

                default:
                    if (Char.IsWhiteSpace(ch) && pos != ReadMode.Name)
                    {
                        pos = ReadMode.Name;
                        break;
                    }
                    switch (pos)
                    {
                    case ReadMode.Stack:
                        stack += ch;
                        break;

                    case ReadMode.Prefix:
                        prefix += ch;
                        break;

                    case ReadMode.Name:
                        name += ch;
                        break;
                    }
                    break;
                }
            }
            // Additional one for the last item
            name = name.Replace("(s)", String.Empty);
            list.Add(new RecItem(
                         name.Trim(),
                         Int32.Parse(stack.Trim()),
                         prefix == "" ? 0 : TShock.Utils.GetPrefixByName(prefix.Trim()).First()));
            return(list);
        }
Exemplo n.º 18
0
    public KuduScanEnumerator(
        ILogger logger,
        KuduClient client,
        KuduTable table,
        List <ColumnSchemaPB> projectedColumnsPb,
        KuduSchema projectionSchema,
        OrderMode orderMode,
        ReadMode readMode,
        ReplicaSelection replicaSelection,
        bool isFaultTolerant,
        Dictionary <string, KuduPredicate> predicates,
        long limit,
        bool cacheBlocks,
        byte[] startPrimaryKey,
        byte[] endPrimaryKey,
        long startTimestamp,
        long htTimestamp,
        int batchSizeBytes,
        PartitionPruner partitionPruner,
        CancellationToken cancellationToken)
    {
        _logger            = logger;
        _client            = client;
        _table             = table;
        _partitionPruner   = partitionPruner;
        _orderMode         = orderMode;
        _readMode          = readMode;
        _columns           = projectedColumnsPb;
        _schema            = projectionSchema;
        _predicates        = predicates;
        _replicaSelection  = replicaSelection;
        _isFaultTolerant   = isFaultTolerant;
        _limit             = limit;
        _cacheBlocks       = cacheBlocks;
        _startPrimaryKey   = startPrimaryKey ?? Array.Empty <byte>();
        _endPrimaryKey     = endPrimaryKey ?? Array.Empty <byte>();
        _startTimestamp    = startTimestamp;
        SnapshotTimestamp  = htTimestamp;
        _batchSizeBytes    = batchSizeBytes;
        _scannerId         = ByteString.Empty;
        _lastPrimaryKey    = ByteString.Empty;
        _cancellationToken = cancellationToken;
        ResourceMetrics    = new ResourceMetrics();

        // If the partition pruner has pruned all partitions, then the scan can be
        // short circuited without contacting any tablet servers.
        if (!_partitionPruner.HasMorePartitionKeyRanges)
        {
            _closed = true;
        }

        // For READ_YOUR_WRITES scan mode, get the latest observed timestamp
        // and store it. Always use this one as the propagated timestamp for
        // the duration of the scan to avoid unnecessary wait.
        if (readMode == ReadMode.ReadYourWrites)
        {
            _lowerBoundPropagationTimestamp = client.LastPropagatedTimestamp;
        }
    }
Exemplo n.º 19
0
 public NamedPipeStatus(ushort value)
 {
     ICount        = (byte)(value & 0xFF);
     ReadMode      = (ReadMode)((value & 0x0300) >> 8);
     NamedPipeType = (NamedPipeType)((value & 0x0C00) >> 10);
     Endpoint      = (Endpoint)((value & 0x4000) >> 14);
     NonBlocking   = (NonBlocking)((value & 0x80) >> 15);
 }
Exemplo n.º 20
0
 public NamedPipeStatus(byte[] buffer, int offset)
 {
     ICount        = buffer[offset + 0];
     ReadMode      = (ReadMode)(buffer[offset + 1] & 0x03);
     NamedPipeType = (NamedPipeType)((buffer[offset + 1] & 0x0C) >> 2);
     Endpoint      = (Endpoint)((buffer[offset + 1] & 0x40) >> 6);
     NonBlocking   = (NonBlocking)((buffer[offset + 1] & 0x80) >> 7);
 }
Exemplo n.º 21
0
 public void AddToOptionList(OptionList options)
 {
     options.Clear();
     options["ReadMode"]     = ReadMode.ToString();
     options["BufferLength"] = BufferLength.ToString();
     options["BinStopCode"]  = BinStopCode.ToString();
     options["StopEnding"]   = StopEnding;
 }
 /// <summary>
 /// Sets the start timestamp and end timestamp for a diff scan.
 /// The timestamps should be encoded HT timestamps.
 /// Additionally sets any other scan properties required by diff scans.
 /// </summary>
 /// <param name="startTimestamp">A long representing a HybridTime-encoded start timestamp.</param>
 /// <param name="endTimestamp">A long representing a HybridTime-encoded end timestamp.</param>
 public TBuilder DiffScan(long startTimestamp, long endTimestamp)
 {
     StartTimestamp  = startTimestamp;
     HtTimestamp     = endTimestamp;
     IsFaultTolerant = true;
     ReadMode        = ReadMode.ReadAtSnapshot;
     return((TBuilder)this);
 }
Exemplo n.º 23
0
 /// <summary>
 /// Configura o modo de leitura do dispositivo
 /// </summary>
 /// <param name="mode">Modo de leitura</param>
 public void ConfigureReadMode(ReadMode mode)
 {
     byte[] package = new byte[] { 0x10, 0xFF };
     if (mode == ReadMode.Continuous)
     {
         package = new byte[] { 0x11, 0xFF };
     }
     serialPort.Write(package, 0, 2);
 }
 /// <summary>
 /// <para>
 /// Make scans resumable at another tablet server if current server fails if
 /// isFaultTolerant is true.
 /// </para>
 ///
 /// <para>
 /// Scans are by default non fault-tolerant, and scans will fail
 /// if scanning an individual tablet fails (for example, if a tablet server
 /// crashes in the middle of a tablet scan). If isFaultTolerant is set to true,
 /// scans will be resumed at another tablet server in the case of failure.
 /// </para>
 ///
 /// <para>
 /// Fault-tolerant scans typically have lower throughput than non
 /// fault-tolerant scans. Fault tolerant scans use READ_AT_SNAPSHOT read mode.
 /// If no snapshot timestamp is provided, the server will pick one.
 /// </para>
 /// </summary>
 /// <param name="isFaultTolerant">Indicates if scan is fault-tolerant.</param>
 public TBuilder SetFaultTolerant(bool isFaultTolerant)
 {
     IsFaultTolerant = isFaultTolerant;
     if (isFaultTolerant)
     {
         ReadMode = ReadMode.ReadAtSnapshot;
     }
     return((TBuilder)this);
 }
 public CircularBuffer(int initialCapacity, bool allowExtension, ReadMode readMode, int readTimeout)
 {
     Buffer           = new T[initialCapacity];
     InternalCapacity = initialCapacity;
     InternalSize     = 0;
     AllowExtension   = allowExtension;
     ReadMode         = readMode;
     ReadTimeout      = readTimeout;
     Head             = 0;
     Tail             = 0;
 }
        public ReadHandle Read(ReadCommand *readCmds, uint cmdCount, ReadMode mode = ReadMode.Async)
        {
            var handle = AsyncReadManager.Read(FilePath, readCmds, cmdCount);

            if (mode == ReadMode.Blocking)
            {
                handle.JobHandle.Complete();
            }

            return(handle);
        }
 public SerialPortConfigParameters(SerialPortState deviceType, int baudRate, Parity parity, StopBits stopBits, int dataBits, Handshake handshake, NewLine newLine, ReadMode readMode)
 {
     DeviceType      = deviceType;
     BaudRate        = baudRate;
     Parity          = parity;
     StopBits        = stopBits;
     DataBits        = dataBits;
     Handshake       = handshake;
     NewLine         = newLine;
     ReadMode        = readMode;
     SerialPortState = SerialPortState.Undiscovered;
 }
Exemplo n.º 28
0
 public void ReadFromXml(XmlTextReader xml)
 {
     Drive              = (Drive)Enum.Parse(typeof(Drive), xml.GetAttribute("Drive"));
     DataRate           = (DataRate)Enum.Parse(typeof(DataRate), xml.GetAttribute("DataRate"));
     Side               = (DiskSide)Enum.Parse(typeof(DiskSide), xml.GetAttribute("Side"));
     ReadMode           = (ReadMode)Enum.Parse(typeof(ReadMode), xml.GetAttribute("ReadMode"));
     SectorReadAttempts = Math.Max(0, int.Parse(xml.GetAttribute("SectorReadAttempts")));
     FirstTrack         = Math.Max(0, int.Parse(xml.GetAttribute("FirstTrack")));
     FirstTrack         = Math.Min(171, FirstTrack);
     LastTrack          = Math.Min(172, int.Parse(xml.GetAttribute("LastTrack")));
     LastTrack          = Math.Max(0, LastTrack);
 }
        public void Open(string serialPortNumber, int baudRate, Parity parity, StopBits stopBits, int dataBits, Handshake handshake, NewLine newLine, ReadMode readMode)
        {
            _logger.Information($"Opening serial port with these params:");
            _logger.Information($" -- serialPortNumber {serialPortNumber}");
            _logger.Information($" -- baudRate {baudRate}");
            _logger.Information($" -- parity {parity}");
            _logger.Information($" -- stopBits {stopBits}");
            _logger.Information($" -- dataBits {dataBits}");
            _logger.Information($" -- handshake {handshake}");
            _logger.Information($" -- newLine {newLine}");
            _logger.Information($" -- readMode {readMode}");



            try
            {
                _mySerialPort = new SerialPort(serialPortNumber);

                _mySerialPort.BaudRate  = baudRate;
                _mySerialPort.Parity    = parity;
                _mySerialPort.StopBits  = stopBits;
                _mySerialPort.DataBits  = dataBits;
                _mySerialPort.Handshake = handshake;
                _mySerialPort.NewLine   = NewLineHelper.ToString(newLine);
                _readMode = readMode;



                switch (_readMode)
                {
                case ReadMode.ReadChunksTillNoneMore:
                    _mySerialPort.DataReceived += DataReceivedHandlerReadLine;
                    break;

                case ReadMode.ReadTillSlashRSlashN:
                    _mySerialPort.DataReceived += DataReceivedHandlerSlashRSlashN;
                    break;

                case ReadMode.ReadLine:
                    _mySerialPort.DataReceived += DataReceivedHandlerReadLine;
                    break;
                }

                _mySerialPort.Open();
                _logger.Debug($"Opened Serial port {serialPortNumber}");
            }
            catch (Exception e)
            {
                _logger.Error($"Exception setting up COMM port {serialPortNumber}:  {e.Message}");
                _logger.Error($"{e}");
                throw;
            }
        }
Exemplo n.º 30
0
    /// <summary>
    /// removes line and block comments from a string.
    /// preset scripts support both traditional C line '//' and
    /// /* block comments */
    /// </summary>
    private static string RemoveComments(string str)
    {
        string result = "";

        for (int v = 0; v < str.Length; v++)
        {
            switch (m_ReadMode)
            {
            // in Normal mode, we usually copy text but go into
            // BlockComment mode upon /* and into LineComment mode upon //
            case ReadMode.Normal:
                if (str[v] == '/' && str[v + 1] == '*')
                {
                    m_ReadMode = ReadMode.BlockComment;
                    v++;
                    break;
                }
                else if (str[v] == '/' && str[v + 1] == '/')
                {
                    m_ReadMode = ReadMode.LineComment;
                    v++;
                    break;
                }

                // copy non-comment text
                result += str[v];

                break;

            // in LineComment mode, we go into Normal mode upon newline
            case ReadMode.LineComment:
                if (v == str.Length - 1)
                {
                    m_ReadMode = ReadMode.Normal;
                    break;
                }
                break;

            // in BlockComment mode, we go into normal mode upon */
            case ReadMode.BlockComment:
                if (str[v] == '*' && str[v + 1] == '/')
                {
                    m_ReadMode = ReadMode.Normal;
                    v++;
                    break;
                }
                break;
            }
        }

        return(result);
    }
Exemplo n.º 31
0
        public void Load(string fileName, ReadMode mode)
        {
            Stream s = null;

            if (fileName == null) {
                throw new ArgumentNullException("fileName");
            }

            try {
                s = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                Load(s, mode);
            } finally {
                if (s != null) s.Close();
            }
        }
Exemplo n.º 32
0
        /*
         * TagHeader:
         *
         * ID3			Tag identifier
         * xx		 	Major version byte
         * xx			Minor version byte; always zero
         * xx			Flag byte
         * xx xx xx xx	Tag size as unsync int, excluding tag header and footer but including ext. header.
         * Tag data
         *
         */
        internal static ID3v2Tag DecodeTag(Stream s, ReadMode mode)
        {
            ID3v2Tag tag;
            byte[] tagHeader = new byte[TagHeaderSize];
            byte flag;
            int tagSize, wholeTagSize;

            try {
                Util.ReadFully(s, tagHeader);
                if (!HasID3v2Tag(tagHeader)) {
                    return null;
                }
                flag = tagHeader[5];
                tagSize = NumberConverter.ReadInt32(tagHeader, 6, 4, true);
                wholeTagSize = GetID3v2TagSize(tagHeader);

                switch (tagHeader[3]) {
                    case 0x02:
                        tag = DecodeID3v2_2Tag(s, flag, tagSize, mode);
                        break;

                    case 0x03:
                        tag = DecodeID3v2_3Tag(s, flag, tagSize, mode);
                        break;

                    case 0x04:
                        tag = DecodeID3v2_4Tag(s, flag, tagSize, mode);
                        break;

                    default:
                        throw new ID3ParseException("Invalid ID3v2 tag version.");
                }

                tag.PaddingMode = PaddingMode.PadToSize;
                tag.PaddingSize = wholeTagSize;

                return tag;

            } catch (ID3TagException) {
                throw;
            } catch (IOException e) {
                throw new ID3TagException("IO Exception occured while parsing the tag.", e);
            } catch (Exception e) {
                throw new ID3TagException("Exception occured while parsing the tag.", e);
            }
        }
Exemplo n.º 33
0
        /// <summary>
        /// 指定されたフォルダを走査して、含まれるファイル情報を取得する
        /// </summary>
        /// <param name="dirName">フォルダ名</param>
        /// <param name="mode">読み込みモード</param>
        /// <returns>DEMファイル情報</returns>
        public List<DemSet> ScanDir(string dirName, ReadMode mode = ReadMode.HeaderOnly)
        {
            List<DemSet> maplist = new List<DemSet>();
            if (System.IO.Directory.Exists(dirName) == false) return maplist;
            string[] xmlFiles = System.IO.Directory.GetFiles(dirName, "*.xml", System.IO.SearchOption.TopDirectoryOnly); // 拡張子がxmlのファイルを全て取得する
            // 並列処理でデータを取得
            maplist = this.ReadHeader(xmlFiles);

            // データ本体も取得が必要ならばこちらも実行する
            if (mode == ReadMode.HeaderAndValue)
            {
                Parallel.For(0, maplist.Count, i =>                             // すべてのファイルについて処理する(可能ならマルチスレッドで)
                {
                    maplist[i].Load();
                });
            }
            return maplist;
        }
Exemplo n.º 34
0
        public void Load(Stream s, ReadMode mode)
        {
            if (s == null) {
                throw new ArgumentNullException("s");
            }
            if (!s.CanRead) {
                throw new ArgumentException("Cannot read from Stream.", "s");
            }
            if (!Enum.IsDefined(typeof(ReadMode), mode)) {
                throw new InvalidEnumArgumentException("mode", (int)mode, typeof(ReadMode));
            }

            try {
                id3v2Tag = ID3v2TagDecoder.DecodeTag(s, mode);
            } catch {
                if (mode == ReadMode.ThrowOnError) {
                    throw;
                }
                id3v2Tag = null;
            }

            if (s.CanSeek && s.Length >= ID3v1Tag.TagLength) {
                s.Seek(-ID3v1Tag.TagLength, SeekOrigin.End);
                try {
                    id3v1Tag = ID3v1TagDecoder.DecodeTag(s);
                } catch {
                    if (mode == ReadMode.ThrowOnError) {
                        throw;
                    }
                    id3v1Tag = null;
                }
            } else {
                id3v1Tag = null;
            }
        }
Exemplo n.º 35
0
        public void Load(FileInfo fileInfo, ReadMode mode)
        {
            Stream s = null;

            if (fileInfo == null) {
                throw new ArgumentNullException("fileInfo");
            }

            try {
                s = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
                Load(s, mode);
            } finally {
                if (s != null) s.Close();
            }
        }
Exemplo n.º 36
0
        private int ReadStartCommon(ReadMode mode, string flacFilePath, long skipFrames, long wantFrames,
            out PcmDataLib.PcmData pcmData_return, out List<FlacCuesheetTrackInfo> cueSheet_return)
        {
            pcmData_return = new PcmDataLib.PcmData();
            cueSheet_return = new List<FlacCuesheetTrackInfo>();

            StartChildProcess();

            switch (mode) {
            case ReadMode.Header:
                SendString("H");
                SendBase64(flacFilePath);
                break;
            case ReadMode.HeadereAndData:
                SendString("A");
                SendBase64(flacFilePath);
                SendString(skipFrames.ToString(CultureInfo.InvariantCulture));
                SendString(wantFrames.ToString(CultureInfo.InvariantCulture));
                break;
            default:
                System.Diagnostics.Debug.Assert(false);
                break;
            }

            int rv = mBinaryReader.ReadInt32();
            if (rv != 0) {
                return rv;
            }

            int nChannels     = mBinaryReader.ReadInt32();
            int bitsPerSample = mBinaryReader.ReadInt32();
            int sampleRate    = mBinaryReader.ReadInt32();

            mNumFrames         = mBinaryReader.ReadInt64();
            int numFramesPerBlock = mBinaryReader.ReadInt32();

            string titleStr = mBinaryReader.ReadString();
            string albumStr = mBinaryReader.ReadString();
            string artistStr = mBinaryReader.ReadString();

            byte md5Available = mBinaryReader.ReadByte();
            md5MetaAvailable = md5Available != 0;

            mMD5SumInMetadata = mBinaryReader.ReadBytes(MD5_BYTES);

            mPictureBytes = mBinaryReader.ReadInt32();
            mPictureData = new byte[0];
            if (0 < mPictureBytes) {
                mPictureData = mBinaryReader.ReadBytes(mPictureBytes);
            }

            {
                int numCuesheetTracks = mBinaryReader.ReadInt32();
                for (int trackId=0; trackId < numCuesheetTracks; ++trackId) {
                    var cti = new FlacCuesheetTrackInfo();
                    cti.trackNr = mBinaryReader.ReadInt32();
                    cti.offsetSamples = mBinaryReader.ReadInt64();

                    int numCuesheetTrackIndices = mBinaryReader.ReadInt32();
                    for (int indexId=0; indexId < numCuesheetTrackIndices; ++indexId) {
                        var indexInfo = new FlacCuesheetTrackIndexInfo();
                        indexInfo.indexNr = mBinaryReader.ReadInt32();
                        indexInfo.offsetSamples = mBinaryReader.ReadInt64();
                        cti.indices.Add(indexInfo);
                    }
                    cueSheet_return.Add(cti);
                }
            }

            pcmData_return.SetFormat(
                nChannels,
                bitsPerSample,
                bitsPerSample,
                sampleRate,
                PcmDataLib.PcmData.ValueRepresentationType.SInt,
                mNumFrames);

            pcmData_return.DisplayName = titleStr;
            pcmData_return.AlbumTitle = albumStr;
            pcmData_return.ArtistName = artistStr;

            pcmData_return.SetPicture(mPictureBytes, mPictureData);
            return 0;
        }
Exemplo n.º 37
0
    ///////////////////////////////////////////////////////////
    // sends every string in the 'lines' list to the parser
    ///////////////////////////////////////////////////////////
    private static void ParseLines(List<string> lines)
    {
        // reset comment mode, i.e. for cases when we were in
        // block comment mode when the previous file ended.
        m_ReadMode = ReadMode.Normal;

        // reset line number
        m_LineNumber = 0;

        // feed all lines to parser
        foreach (string s in lines)
        {

            m_LineNumber++;

            // ignore line- and block comments
            string line = RemoveComments(s);

            // if line is empty here, cancel
            if (string.IsNullOrEmpty(line))
                continue;

            // done, try executing the line
            if (!Parse(line))
                return;

        }

        // reset line number again. it should always be zero
        // outside of the above loop
        m_LineNumber = 0;
    }
Exemplo n.º 38
0
        private bool Read(BinaryReader br, ReadMode mode, long startFrame, long endFrame)
        {
            ErrorReason = string.Empty;

            bool riffChunkExist = false;
            bool fmtChunkExist = false;
            bool ds64ChunkExist = false;

            long offset = 0;
            try {
                do {
                    var fourcc = br.ReadBytes(4);
                    if (fourcc.Length < 4) {
                        // ファイルの終わりに達した。
                        break;
                    }
                    offset += 4;

                    long advance = 0;

                    if (!riffChunkExist) {
                        if (!PcmDataLib.Util.FourCCHeaderIs(fourcc, 0, "RIFF") &&
                            !PcmDataLib.Util.FourCCHeaderIs(fourcc, 0, "RF64")) {
                            // ファイルの先頭がRF64でもRIFFもない。WAVではない。
                            ErrorReason = "File does not start with RIFF nor RF64.";
                            return false;
                        }

                        advance = ReadRiffChunk(br, fourcc);
                        riffChunkExist = true;
                    } else if (PcmDataLib.Util.FourCCHeaderIs(fourcc, 0, "fmt ")) {
                        advance = ReadFmtChunk(br, fourcc);
                        fmtChunkExist = true;
                    } else if (PcmDataLib.Util.FourCCHeaderIs(fourcc, 0, "LIST")) {
                        advance = ReadListChunk(br);
                    } else if (PcmDataLib.Util.FourCCHeaderIs(fourcc, 0, "id3 ")) {
                        advance = ReadId3Chunk(br);
                    } else if (PcmDataLib.Util.FourCCHeaderIs(fourcc, 0, "ds64")) {
                        advance = ReadDS64Chunk(br);
                        ds64ChunkExist = true;
                    } else if (PcmDataLib.Util.FourCCHeaderIs(fourcc, 0, "data")) {
                        if (!fmtChunkExist) {
                            // fmtチャンクがないと量子化ビット数がわからず処理が継続できない。
                            ErrorReason = "fmt subchunk is missing.";
                            return false;
                        }
                        if (ds64ChunkExist && 0 < mDscList.Count) {
                            ErrorReason = "multiple data chunk in RF64. not supported format.";
                            return false;
                        }

                        int frameBytes = BitsPerSample / 8 * NumChannels;

                        var dsc = new DataSubChunk();

                        advance = dsc.ReadDataChunkHeader(br, offset, fourcc, NumChannels, BitsPerSample);

                        if (ds64ChunkExist) {
                            // ds64チャンクが存在する場合(RF64形式)
                            // dsc.ChunkSizeは正しくないので、そこから算出するdsc.NumFramesも正しくない。
                            // dsc.NumFrameをds64の値で上書きする。
                            dsc.NumFrames = Ds64DataSize / frameBytes;
                        } else {
                            const long MASK = UInt32.MaxValue;
                            if (MASK < (br.BaseStream.Length - 8) && RiffChunkSize != MASK) {
                                // RIFF chunkSizeが0xffffffffではないのにファイルサイズが4GB+8(8==RIFFチャンクのヘッダサイズ)以上ある。
                                // このファイルはdsc.ChunkSize情報の信憑性が薄い。
                                // dsc.ChunkSizeの上位ビットが桁あふれによって消失している可能性があるので、
                                // dsc.ChunkSizeの上位ビットをファイルサイズから類推して付加し、
                                // dsc.NumFrameを更新する。

                                long remainBytes = br.BaseStream.Length - (offset + advance);
                                long maskedRemainBytes =  remainBytes & 0xffffffffL;
                                if (maskedRemainBytes <= dsc.ChunkSize && RiffChunkSize - maskedRemainBytes < 4096) {
                                    long realChunkSize = dsc.ChunkSize;
                                    while (realChunkSize + 0x100000000L <= remainBytes) {
                                        realChunkSize += 0x100000000L;
                                    }
                                    dsc.NumFrames = realChunkSize / frameBytes;
                                }
                            }
                        }

                        // マルチデータチャンク形式の場合、data chunkの後にさらにdata chunkが続いたりするので、
                        // 読み込みを続行する。
                        long skipBytes = (dsc.NumFrames * frameBytes + 1) & (~1L);
                        PcmDataLib.Util.BinaryReaderSkip(br, skipBytes);

                        if (br.BaseStream.Length < (offset + advance) + skipBytes) {
                            // ファイルが途中で切れている。
                            dsc.NumFrames = (br.BaseStream.Length - (offset + advance)) / frameBytes;
                        }

                        if (0 < dsc.NumFrames) {
                            mDscList.Add(dsc);
                        } else {
                            // ファイルがDSCヘッダ部分を最後に切れていて、サンプルデータが1フレーム分すらも無いとき。
                        }

                        advance += skipBytes;
                    } else {
                        advance = SkipUnknownChunk(br, fourcc);
                    }

                    if (0 == advance) {
                        // 行儀が悪いWAVファイル。ファイルの最後に0がいくつか書き込まれている。
                        return riffChunkExist && fmtChunkExist && mDscList.Count != 0;
                    }
                    offset += advance;
                } while (true);
            } catch (Exception ex) {
                ErrorReason = string.Format("E: WavRWLib2.WavData.ReadRiffChunk() {0}", ex);
            }

            if (mode == ReadMode.HeaderAndPcmData) {
                if (!ReadPcmDataInternal(br, startFrame, endFrame)) {
                    return false;
                }
                return true;
            }

            return riffChunkExist && fmtChunkExist && mDscList.Count != 0;
        }
Exemplo n.º 39
0
        /*
         * Tag header
         * {Frames}
         * [padding]
         *
         * Note: unsync is applied on tag level(after header); no ext. header or compression
         */
        private static ID3v2Tag DecodeID3v2_2Tag(Stream s, byte tagFlag, int tagSize, ReadMode mode)
        {
            byte[] tagContent;
            ID3v2Tag tag;

            tag = new ID3v2Tag();
            tag.Version = ID3v2Version.ID3v2_2;
            tag.ExtendedHeader = null;
            tag.Flags = ID3v2TagFlags.None;

            tagContent = new byte[tagSize];
            Util.ReadFully(s, tagContent);

            /* handle tag flags */
            if ((tagFlag & 0x80) != 0) {
                tag.Flags |= ID3v2TagFlags.Unsync;
                tagContent = Util.RemoveUnsynchronization(tagContent);
            }
            if ((tagFlag & 0x40) != 0) {
                throw new ID3ParseException("Compression bit set although no compression schema defined in 2.2.");
            }
            if ((tagFlag & 0x3F) != 0) {
                /* some other undefined flags set */
                throw new ID3ParseException("Undefined tag flag(s) set.");
            }

            ReadFrames(tagContent, 0, tag, mode);

            return tag;
        }
	/// <summary>
	/// removes line and block comments from a string.
	/// preset scripts support both traditional C line '//' and
	/// /* block comments */
	/// </summary>
	private static string RemoveComments(string str)
	{

		string result = "";

		for (int v = 0; v < str.Length; v++)
		{
			switch (m_ReadMode)
			{

				// in Normal mode, we usually copy text but go into
				// BlockComment mode upon /* and into LineComment mode upon //
				case ReadMode.Normal:
					if (str[v] == '/' && str[v + 1] == '*')
					{
						m_ReadMode = ReadMode.BlockComment;
						v++;
						break;
					}
					else if (str[v] == '/' && str[v + 1] == '/')
					{
						m_ReadMode = ReadMode.LineComment;
						v++;
						break;
					}

					// copy non-comment text
					result += str[v];

					break;

				// in LineComment mode, we go into Normal mode upon newline
				case ReadMode.LineComment:
					if (v == str.Length - 1)
					{
						m_ReadMode = ReadMode.Normal;
						break;
					}
					break;

				// in BlockComment mode, we go into normal mode upon */
				case ReadMode.BlockComment:
					if (str[v] == '*' && str[v + 1] == '/')
					{
						m_ReadMode = ReadMode.Normal;
						v++;
						break;
					}
					break;

			}
		}

		return result;

	}
Exemplo n.º 41
0
 public ReadInstruction(ReadMode mode, int readBytes, int fullBytes)
 {
     Mode = mode;
     ReadBytes = readBytes;
     FullBytes = fullBytes;
 }
 internal Message GetReadModeCommand(bool isMasterOnly)
 {
     var serverEndpoint = bridge.ServerEndPoint;
     if (serverEndpoint.RequiresReadMode)
     {
         ReadMode requiredReadMode = isMasterOnly ? ReadMode.ReadWrite : ReadMode.ReadOnly;
         if (requiredReadMode != currentReadMode)
         {
             currentReadMode = requiredReadMode;
             switch (requiredReadMode)
             {
                 case ReadMode.ReadOnly: return ReusableReadOnlyCommand;
                 case ReadMode.ReadWrite: return ReusableReadWriteCommand;
             }
         }
     }
     else if (currentReadMode == ReadMode.ReadOnly)
     { // we don't need it (because we're not a cluster, or not a slave),
         // but we are in read-only mode; switch to read-write
         currentReadMode = ReadMode.ReadWrite;
         return ReusableReadWriteCommand;
     }
     return null;
 }
Exemplo n.º 43
0
        /*
         * Tagheader
         * [Extended header]
         * {Frames}
         * [Padding]	included in tag size
         *
         * Note: Unsync is applied on tag level; footer is not allowed.
         *
         */
        private static ID3v2Tag DecodeID3v2_3Tag(Stream s, byte flag, int tagSize, ReadMode mode)
        {
            byte[] tagContent;
            int offset = 0;
            ID3v2Tag tag;

            tag = new ID3v2Tag();
            tag.Version = ID3v2Version.ID3v2_3;
            tag.ExtendedHeader = null;
            tag.Flags = ID3v2TagFlags.None;

            tagContent = new byte[tagSize];
            Util.ReadFully(s, tagContent);

            /* handle tag flags */
            if ((flag & 0x80) != 0) {
                tag.Flags |= ID3v2TagFlags.Unsync;
                tagContent = Util.RemoveUnsynchronization(tagContent);
            }
            if ((flag & 0x40) != 0) {
                tag.ExtendedHeader = new ExtendedHeader();
                offset = DecodeID3v2_3ExtendedHeader(tagContent, tag.ExtendedHeader);
            }
            if ((flag & 0x20) != 0) {
                tag.Flags |= ID3v2TagFlags.Experimental;
            }
            if ((flag & 0x1F) != 0) {
                /* undefined flags set */
                throw new ID3TagException("Undefined flags set.");
            }

            ReadFrames(tagContent, offset, tag, mode);

            return tag;
        }
Exemplo n.º 44
0
        /*
         * Tagheader
         * [Extended header]
         * {Frames}
         * [Padding]
         * [Footer]
         *
         * Note footer is not included in size; unsync is done on frame level
         */
        private static ID3v2Tag DecodeID3v2_4Tag(Stream s, byte flag, int tagSize, ReadMode mode)
        {
            byte[] tagContent;
            int offset = 0;
            ID3v2Tag tag;

            tag = new ID3v2Tag();
            tag.Version = ID3v2Version.ID3v2_4;
            tag.ExtendedHeader = null;
            tag.Flags = ID3v2TagFlags.None;

            tagContent = new byte[tagSize];
            Util.ReadFully(s, tagContent);

            if ((flag & 0x80) != 0) {
                /* all frames are really unsychronized */
                tag.Flags = ID3v2TagFlags.Unsync;
            }
            if ((flag & 0x40) != 0) {
                tag.ExtendedHeader = new ExtendedHeader();
                offset = DecodeID3v2_4ExtendedHeader(tagContent, tag.ExtendedHeader);
            }
            if ((flag & 0x20) != 0) {
                tag.Flags |= ID3v2TagFlags.Experimental;
            }
            if ((flag & 0x10) != 0) {
                tag.Flags |= ID3v2TagFlags.Footer;

                const int footerSize = 10;
                byte[] footer = new byte[footerSize];

                Util.ReadFully(s, footer);
                if (footer[0] != '3' || footer[1] != 'D' || footer[2] != 'I' || footer[3] != 0x04 ||
                    footer[4] != 0x00 || footer[5] != flag || (int)NumberConverter.ReadInt32(footer, 6, 4, true) != tagSize) {

                    throw new ID3ParseException("Error while parsing footer.");
                }
            }
            if ((flag & 0x0F) != 0) {
                throw new ID3ParseException("Undefined flag(s) set");
            }

            ReadFrames(tagContent, offset, tag, mode);

            return tag;
        }
Exemplo n.º 45
0
        private static void ReadFrames(byte[] tagContent, int offset, ID3v2Tag tag, ReadMode mode)
        {
            MemoryStream s = new MemoryStream(tagContent, offset, tagContent.Length - offset, false);

            while (offset + s.Position < tagContent.Length && tagContent[offset + s.Position] != 0x00) {
                try {
                    tag.Frames.Add(FrameDecoder.DecodeFrame(s, tag));
                } catch {
                    if (mode == ReadMode.ThrowOnError) {
                        throw;
                    }
                }
            }

            s.Close();
        }
Exemplo n.º 46
0
        /// <summary>
        /// フォルダの指定にダイアログを用いて、DEMを読み込む
        /// </summary>
        /// <param name="mode">読み込みモード</param>
        /// <param name="rootFolder">探索開始とするルートディレクトリ</param>
        /// <returns>List形式のDEM情報及び選択したディレクトリへのパスをひとくくりにしたタプル<para>格納順は「DEM情報・パス」です。</para></returns>
        public Tuple<List<DemSet>, string> ScanDirWithDialog(ReadMode mode = ReadMode.HeaderOnly, Environment.SpecialFolder rootFolder = Environment.SpecialFolder.Desktop)
        {
            List<DemSet> maplist = null;
            FolderBrowserDialog fbd = new FolderBrowserDialog();                // FolderBrowserDialogクラスのインスタンスを作成
            string dirPath = "";

            // DEM格納フォルダの指定
            fbd.Description = "DEMファイルを格納したフォルダを指定して下さい。";// 上部に表示する説明テキストを指定する
            fbd.RootFolder = rootFolder;                                        // ルートフォルダを指定する
            fbd.ShowNewFolderButton = false;                                    // ユーザーが新しいフォルダを作成できないようにする
            try
            {
                if (fbd.ShowDialog() == DialogResult.OK)                        // ダイアログを表示する
                {
                    //System.Diagnostics.Stopwatch sw2 = System.Diagnostics.Stopwatch.StartNew();

                    maplist = this.ScanDir(fbd.SelectedPath, mode);
                    dirPath = fbd.SelectedPath;
                    //sw2.Stop();                                                             // ストップウォッチを止める
                    //Console.WriteLine("ヘッダ読み込みにかけた処理時間: " + sw2.Elapsed);   // 結果を表示する
                }
            }
            catch
            {

            }
            return new Tuple<List<DemSet>, string>(maplist, dirPath);
        }