Exemple #1
0
        /// <summary>
        /// Creates a new RDY file object.
        /// </summary>
        public static RdyFile Create(char tableType, int tableId, int recordSize, int keyLength = 0, int keyOffset = 0, bool sorted = false, bool packed = false, char tableVersion = 'A')
        {
            if (recordSize < 0 || recordSize > 99)
                throw new ArgumentOutOfRangeException("recordSize", "Invalid record size for RDY file.");

            var header = RdyHeader.Create(tableType, tableId, recordSize, keyLength, keyOffset, sorted, packed, tableVersion);
            var rdy = new RdyFile {Header = header, _records = new List<RdyRecord>()};
            return rdy;
        }
Exemple #2
0
        public SystemParameters(RdyFile rdyFile)
        {
            if (rdyFile.Header.TableType != 'p' || rdyFile.Header.TableId != 1)
                throw new ArgumentException("The file should be for table p001 only.");

            if (rdyFile.Header.RecordCount != 1)
                throw new ArgumentException("The file should have exactly one record.");

            if (rdyFile.Header.RecordSize != rdyFile.Records[0].Data.Length)
                throw new ArgumentException("The record length does not match the value specified in the header.");

            _parameters = Parse(rdyFile.Records[0].Data);
        }
Exemple #3
0
        public SystemParameters(RdyFile rdyFile)
        {
            if (rdyFile.Header.TableType != 'p' || rdyFile.Header.TableId != 1)
            {
                throw new ArgumentException("The file should be for table p001 only.");
            }

            if (rdyFile.Header.RecordCount != 1)
            {
                throw new ArgumentException("The file should have exactly one record.");
            }

            if (rdyFile.Header.RecordSize != rdyFile.Records[0].Data.Length)
            {
                throw new ArgumentException("The record length does not match the value specified in the header.");
            }

            _parameters = Parse(rdyFile.Records[0].Data);
        }
Exemple #4
0
        /// <summary>
        /// Returns an awaitable task that reads an RDY file from an input stream.
        /// </summary>
        internal static async Task <RdyFile> ReadAsync(Stream stream, bool force = false)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream", "The input stream cannot be null.");
            }

            if (!stream.CanRead)
            {
                throw new InvalidOperationException("Could not read from the input stream.");
            }

            var rdy = new RdyFile();

            using (var reader = new StreamReader(stream))
            {
                // Read and parse the header
                var header = await rdy.ReadHeaderAsync(reader);

                rdy.Header = RdyHeader.Parse(header, force);

                // See if we are working with a directory file
                if (rdy.Header.TableType == 'z')
                {
                    rdy.IsDirectoryFile = true;

                    // Put data back in the buffer
                    rdy._buffer.Insert(0, header.Substring(9));

                    // Read the body as directory data
                    await rdy.ReadDirBodyAsync(reader);
                }
                else
                {
                    // Read and load the records in the body
                    await rdy.ReadBodyAsync(reader, force);
                }
            }

            return(rdy);
        }
Exemple #5
0
        /// <summary>
        /// Returns an awaitable task that reads an RDY file from an input stream.
        /// </summary>
        internal static async Task<RdyFile> ReadAsync(Stream stream, bool force = false)
        {
            if (stream == null)
                throw new ArgumentNullException("stream", "The input stream cannot be null.");

            if (!stream.CanRead)
                throw new InvalidOperationException("Could not read from the input stream.");

            var rdy = new RdyFile();

            using (var reader = new StreamReader(stream))
            {
                // Read and parse the header
                var header = await rdy.ReadHeaderAsync(reader);
                rdy.Header = RdyHeader.Parse(header, force);

                // See if we are working with a directory file
                if (rdy.Header.TableType == 'z')
                {
                    rdy.IsDirectoryFile = true;

                    // Put data back in the buffer
                    rdy._buffer.Insert(0, header.Substring(9));

                    // Read the body as directory data
                    await rdy.ReadDirBodyAsync(reader);
                }
                else
                {
                    // Read and load the records in the body
                    await rdy.ReadBodyAsync(reader, force);
                }
            }

            return rdy;
        }
        /// <summary>
        /// Returns an awaitable task that uploads an RDY file object to the terminal.
        /// </summary>
        /// <param name="rdy">The RDY file object.</param>
        /// <param name="replace">True to replace any existing table.  False to throw an exception if the table exists already. (True by default.)</param>
        /// <param name="force">True to upload the file even if it fails validation. (False by default.)</param>
        public async Task UploadTableFromRdyAsync(RdyFile rdy, bool replace = true, bool force = false)
        {
            // make sure we're not uploading a directory file
            if (rdy.IsDirectoryFile)
                throw new InvalidOperationException("Cannot upload a directory file to the terminal.");

            // calculate how many blocks we'll need to send
            var totalBlocks = (int)Math.Ceiling(rdy.Header.TotalCharacters / (double)MaxBlockSize);
            var blockNumber = 1;

            // create a buffer and append the header
            var buffer = new StringBuilder(MaxBlockSize * 2);
            buffer.Append(rdy.Header);

            // get the table information from the header
            var tableType = rdy.Header.TableType;
            var tableId = rdy.Header.TableId;

            // reset the progress info
            OnProgressChanged(new UploadProgressChangedEventArgs(0, totalBlocks, rdy.Filename));

            foreach (var record in rdy.Records)
            {
                // append the record to the buffer
                buffer.Append(record.Data.PadRight(rdy.Header.RecordSize));

                // don't send the buffer until we have a full block
                if (buffer.Length < MaxBlockSize)
                    continue;

                // cut a block from the buffer and send it
                var block = buffer.Cut(0, MaxBlockSize);
                await SendBlockAsync(tableType, tableId, blockNumber, totalBlocks, block, replace, rdy.Filename);
                blockNumber++;
            }

            // send a partial block if there is any data remaining in the buffer
            if (buffer.Length > 0)
            {
                var block = buffer.ToString();
                await SendBlockAsync(tableType, tableId, blockNumber, totalBlocks, block, replace, rdy.Filename);
            }
        }