Пример #1
0
        /// <summary>
        /// read a field
        /// </summary>
        /// <param name="fieldFormat"></param>
        /// <param name="readNumericValues"></param>
        /// <returns></returns>
        private object ReadField(FieldFormat fieldFormat, IDictionary <string, decimal> readNumericValues)
        {
            List <object> values = new List <object>();
            int           occurs;

            if (fieldFormat.HasDependencies())
            {
                if (readNumericValues.ContainsKey(fieldFormat.DependingOn))
                {
                    occurs = Decimal.ToInt32(readNumericValues[fieldFormat.DependingOn]);
                }
                else
                {
                    throw new System.Exception(
                              string.Format("Check your copybook :[{0}] is not present, but field format says it has dependencies ...",
                                            fieldFormat.DependingOn));
                }
            }
            else
            {
                occurs = fieldFormat.Occurs;
            }

            for (int i = 0; i < occurs; i++)
            {
                byte[] bytes = Read(fieldFormat.ByteSize, fieldFormat);
                object value = _decoder.Decode(bytes, fieldFormat);
                values.Add(value);
                if (value is decimal)
                {
                    readNumericValues[fieldFormat.Name] = (decimal)value;
                }
            }

            if (fieldFormat.HasDependencies() || occurs > 1)
            {
                // if occurs is variable or greater than one, return results in a list
                return(values);
            }
            else
            {
                // otherwise, just return the single item
                return(values[0]);
            }
        }
Пример #2
0
        /// <summary>
        /// Convert list of objects
        /// </summary>
        /// <param name="values"></param>
        /// <param name="format"></param>
        /// <param name="writtenNumbers"></param>
        /// <returns></returns>
        private List <object> ConvertList(List <object> values, FieldFormat format, IDictionary <string, decimal> writtenNumbers)
        {
            List <object> result = values.Select(element => Convert(element, format, writtenNumbers)).ToList();
            int           occurs;

            if (format.HasDependencies())
            {
                occurs = (int)writtenNumbers[format.DependingOn];
            }
            else
            {
                occurs = format.Occurs;
            }
            int toFill = occurs - values.Count;

            for (int i = 0; i < toFill; i++)
            {
                result.Add(GetFillValue(format));
            }
            return(result);
        }