示例#1
0
        public ParsedLine ParseAndAddSingleLine(LineTemplate lineTemplate, byte[] lineBytes, int flushThreshhold)
        {
            bool isSingleLine = true;

            this.ValidateInputParameters(lineTemplate, lineBytes, isSingleLine);
            ParsedLine parsedLine = new ParsedLine(lineTemplate, lineBytes);

            ParsedLine[] parsed = this.ParsedLines;
            if (parsed == null)
            {
                parsed = new ParsedLine[1];
            }
            else
            {
                if (parsed.Length == flushThreshhold)
                {
                    Array.Resize(ref parsed, 1);
                }
                else
                {
                    Array.Resize(ref parsed, (parsed == null ? 0 : parsed.Length) + 1);
                }
            }
            parsed[parsed.Length - 1] = parsedLine;
            this.ParsedLines          = parsed;
            return(parsedLine);
        }
示例#2
0
        private bool ValidateInputParameters(LineTemplate lineTemplate, byte[] allBytes, bool isSingleLine)
        {
            if (allBytes == null)
            {
                throw new ArgumentNullException(Messages.DataNotProvided);
            }
            if (lineTemplate == null)
            {
                throw new ArgumentNullException(Messages.LineTemplateNotProvided);
            }
            if (lineTemplate.FieldsCount == 0)
            {
                throw new Exception(Messages.LineTemplateHasNoFields);
            }
            if (allBytes.Length > 0 && allBytes.Length < lineTemplate.LineSize)
            {
                throw new Exception(Messages.DataShorterThanExpected);
            }
            if (isSingleLine && allBytes.Length != lineTemplate.LineSize)
            {
                throw new Exception(Messages.DataLengthDifferentThanExpected);
            }

            double expectedRows = (double)allBytes.Length / lineTemplate.LineSize;

            if (expectedRows % 1 != 0) //Expected number of rows is not a whole number
            {
                string errMsg = String.Format(Messages.ExpectedNumberOfRows, allBytes.Length, lineTemplate.LineSize, expectedRows);
                throw new Exception(errMsg);
            }

            return(true);
        }
示例#3
0
        /// <summary>
        /// Parses single line of binary data.
        /// </summary>
        /// <param name="lineTemplate">Template</param>
        /// <param name="lineBytes">Source bytes</param>
        /// <returns>Single parsed line</returns>
        public ParsedLine ParseSingleLine(LineTemplate lineTemplate, byte[] lineBytes)
        {
            bool isSingleLine = true;

            this.ValidateInputParameters(lineTemplate, lineBytes, isSingleLine);
            ParsedLine parsedLine = new ParsedLine(lineTemplate, lineBytes);

            return(parsedLine);
        }
示例#4
0
        private void ParseLine(byte[] lineBytes, LineTemplate lineTemplate)
        {
            this.ValidateInputParameters(lineBytes, lineTemplate);

            foreach (var fieldTemplate in lineTemplate.FieldTemplates)
            {
                var parsedField = new ParsedField(lineBytes, lineTemplate.FieldTemplates[fieldTemplate.Key]);
                this.ParsedFields.Add(fieldTemplate.Key, parsedField);
            }
        }
示例#5
0
 private void ValidateInputParameters(byte[] lineBytes, LineTemplate template)
 {
     if (lineBytes == null)
     {
         throw new ArgumentNullException(Messages.LineBytesRequired);
     }
     if (template == null)
     {
         throw new ArgumentNullException(Messages.LineTemplateRequired);
     }
     if (template.FieldsCount == 0)
     {
         throw new Exception(Messages.FieldTemplatesNotDefined);
     }
 }
示例#6
0
        /// <summary>
        /// Parses multiple lines of binary data.
        /// </summary>
        /// <param name="lineTemplate">Template</param>
        /// <param name="allBytes">Source file bytes</param>
        /// <returns>Array of parsed lines</returns>
        public ParsedLine[] ParseAllLines(LineTemplate lineTemplate, byte[] allBytes)
        {
            Console.WriteLine("{0}: Parsing...", DateTime.Now);
            this.ValidateInputParameters(lineTemplate, allBytes, false);

            double expectedRows = (double)allBytes.Length / lineTemplate.LineSize;

            if (expectedRows % 1 == 0)
            {
                Console.WriteLine("{1}: Line count est {0:#,###.00}", expectedRows, DateTime.Now);
            }

            byte[]       lineBytes  = new byte[lineTemplate.LineSize];
            ParsedLine[] linesList  = new ParsedLine[Convert.ToInt32(expectedRows)];
            ParsedLine   parsedLine = null;
            int          lineIndex  = 0;

            for (int i = 0; i < allBytes.Length; i += lineTemplate.LineSize)
            {
                try
                {
                    Array.Copy(allBytes, i, lineBytes, 0, lineTemplate.LineSize);

                    parsedLine = this.ParseSingleLine(lineTemplate, lineBytes);

                    if (parsedLine != null)
                    {
                        linesList[lineIndex] = parsedLine;
                    }

                    lineIndex++;

                    if (lineIndex % 1000 == 0)
                    {
                        Console.Write(lineIndex + "\r");
                    }
                }
                catch (Exception ex)
                {
                    //Used for dubugging
                    Console.WriteLine("Exception at line index {0}", lineIndex);
                    throw ex;
                }
            }
            Console.WriteLine("{1}: {0} line(s) have been parsed", linesList.Count(), DateTime.Now);
            return(linesList);
        }
示例#7
0
        public ParsedLine[] ParseAllLines(LineTemplate lineTemplate, byte[] allBytes)
        {
            Console.WriteLine("{0}: Parsing...", DateTime.Now);
            this.ValidateInputParameters(lineTemplate, allBytes, false);

            double expectedRows = (double)allBytes.Length / lineTemplate.LineSize;
            if (expectedRows % 1 == 0)
            {
                Console.WriteLine("{1}: Line count est {0:#,###.00}", expectedRows, DateTime.Now);
            }

            byte[] lineBytes = new byte[lineTemplate.LineSize];
            List<ParsedLine> linesList = new List<ParsedLine>();
            ParsedLine parsedLine = null;
            int lineIndex = 0;

            for (int i = 0; i < allBytes.Length; i += lineTemplate.LineSize)
            {
                try
                {
                    Array.Copy(allBytes, i, lineBytes, 0, lineTemplate.LineSize);

                    parsedLine = this.ParseSingleLine(lineTemplate, lineBytes);

                    if (parsedLine != null)
                    {
                        linesList.Add(parsedLine);
                    }

                    lineIndex++;

                    if (lineIndex % 1000 == 0)
                    {
                        Console.Write(lineIndex + "\r");
                    }
                }
                catch (Exception ex)
                {
                    //Used for dubugging
                    Console.WriteLine("Exception at line index {0}", lineIndex);
                    throw ex;
                }
            }
            Console.WriteLine("{1}: {0} line(s) have been parsed", linesList.Count(), DateTime.Now);
            return linesList.ToArray();
        }
示例#8
0
 //Constructor
 public ParsedLine(LineTemplate template, byte[] lineBytes)
 {
     this.Template = template;
     this.ParseLine(lineBytes, template);
 }
示例#9
0
 private void ValidateInputParameters(byte[] lineBytes, LineTemplate template)
 {
     if (lineBytes == null)
     {
         throw new ArgumentNullException("Line bytes required");
     }
     if (lineBytes.Length < template.LineSize)
     {
         //TODO:Do something maybe??
         Console.WriteLine(String.Format("Bytes provided: {0}, line size: {1}", lineBytes.Length, template.LineSize));
     }
     if (template == null)
     {
         throw new ArgumentNullException("line template is required");
     }
     if (template.FieldsCount == 0)
     {
         throw new Exception("Field templates have not been defined in the line template");
     }
 }
示例#10
0
 public ParsedLine[] ParseAllLines(LineTemplate lineTemplate, string sourceFilePath)
 {
     Console.WriteLine("{1}: Reading {2}...", sourceFilePath, DateTime.Now);
     return this.ParseAllLines(lineTemplate, File.ReadAllBytes(sourceFilePath));
 }
示例#11
0
 //Constractor 3
 public EbcdicParser(byte[] allBytes, LineTemplate lineTemplate)
 {
     this.ParsedLines = this.ParseAllLines(lineTemplate, allBytes);
 }
示例#12
0
 //Constractor 2
 public EbcdicParser(string sourceFilePath, LineTemplate lineTemplate)
     : this(File.ReadAllBytes(sourceFilePath), lineTemplate)
 {
     //Read all file bytes and call 3rd constructor
 }
示例#13
0
        private bool ValidateInputParameters(LineTemplate lineTemplate, byte[] allBytes, bool isSingleLine)
        {
            if (allBytes == null)
            {
                throw new ArgumentNullException("Ebcdic data is not provided");
            }
            if (lineTemplate == null)
            {
                throw new ArgumentNullException("Line template is not provided");
            }
            if (lineTemplate.FieldsCount == 0)
            {
                throw new Exception("Line template must contain at least one field template");
            }
            if (allBytes.Length > 0 && allBytes.Length < lineTemplate.LineSize)
            {
                throw new Exception("Data length is shorter than the line size");
            }
            if (isSingleLine && allBytes.Length != lineTemplate.LineSize)
            {
                throw new Exception("Bytes count doesn't equal to line size");
            }

            double expectedRows = (double)allBytes.Length / lineTemplate.LineSize;

            if (expectedRows % 1 != 0) //Expected number of rows is not a whole number
            {
                string errMsg = String.Format("Data bytes = {0}; line size = {1}; line count check = {2:#,###.00}.\r\nExpected number of rows is not a whole number. Check line template.", allBytes.Length, lineTemplate.LineSize, expectedRows);
                throw new Exception(errMsg);
            }

            return true;
        }
示例#14
0
 //Constractor 2
 public EbcdicParser(string sourceFilePath, LineTemplate lineTemplate)
     : this(File.ReadAllBytes(sourceFilePath), lineTemplate)
 {
     //Read all file bytes and call 3rd constructor
 }
示例#15
0
 public ParsedLine ParseSingleLine(LineTemplate lineTemplate, byte[] lineBytes)
 {
     bool isSingleLine = true;
     this.ValidateInputParameters(lineTemplate, lineBytes, isSingleLine);
     ParsedLine parsedLine = new ParsedLine(lineTemplate, lineBytes);
     return parsedLine;
 }
示例#16
0
 //Constractor 3
 public EbcdicParser(byte[] allBytes, LineTemplate lineTemplate)
 {
     this.ParsedLines = this.ParseAllLines(lineTemplate, allBytes);
 }
示例#17
0
        private void ParseLine(byte[] lineBytes, LineTemplate lineTemplate)
        {
            this.ValidateInputParameters(lineBytes, lineTemplate);

            foreach (var fieldTemplate in lineTemplate.FieldTemplates)
            {
                ParsedField parsedField = new ParsedField(lineBytes, lineTemplate.FieldTemplates[fieldTemplate.Key]);
                this.ParsedFields.Add(fieldTemplate.Key, parsedField);
            }
        }
示例#18
0
 /// <summary>
 /// Parses multiple lines of binary data.
 /// </summary>
 /// <param name="lineTemplate">Template</param>
 /// <param name="sourceFilePath">Source file path</param>
 /// <returns>Array of parsed lines</returns>
 public ParsedLine[] ParseAllLines(LineTemplate lineTemplate, string sourceFilePath)
 {
     Console.WriteLine("{1}: Reading {2}...", sourceFilePath, DateTime.Now);
     return(this.ParseAllLines(lineTemplate, File.ReadAllBytes(sourceFilePath)));
 }
示例#19
0
 //Constructor
 public ParsedLine(LineTemplate template, byte[] lineBytes)
 {
     this.ParsedFields = new Dictionary <string, ParsedField>(template.FieldsCount);
     this.Template     = template;
     this.ParseLine(lineBytes, template);
 }