Ejemplo n.º 1
0
        private static void WriteField(FieldBase field, byte size, BinaryWriter bw)
        {
            byte baseType = (byte)(field.Type & Fit.BaseTypeNumMask);

            // The field could be blank, correctly formed or partially filled
            while (field.GetSize() < size)
            {
                if (baseType == Fit.String)
                {
                    // Figure out how much we have to pad
                    byte padAmount = (byte)(size - field.GetSize());
                    //Has to be a string.
                    try
                    {
                        // Get the Last Value of the field
                        byte[]      value = (byte[])field.GetValue(field.GetNumValues() - 1);
                        List <byte> temp  = new List <byte>();

                        if (value != null)
                        {
                            temp.AddRange(value);
                        }

                        for (byte i = 0; i < padAmount; i++)
                        {
                            temp.Add(
                                Convert.ToByte(
                                    Fit.BaseType[baseType].invalidValue));
                        }

                        field.SetValue(temp.ToArray());
                    }
                    catch (Exception)
                    {
                        throw new FitException(
                                  "Exception occurred while resizing field to match definition.");
                    }
                }
                else
                {
                    field.AddValue(Fit.BaseType[baseType].invalidValue);
                }
            }

            for (int i = 0; i < field.GetNumValues(); i++)
            {
                object value = field.GetRawValue(i);
                if (value == null)
                {
                    value = Fit.BaseType[baseType].invalidValue;
                }

                switch (baseType)
                {
                case Fit.Enum:
                case Fit.Byte:
                case Fit.UInt8:
                case Fit.UInt8z:
                    bw.Write((byte)value);
                    break;

                case Fit.SInt8:
                    bw.Write((sbyte)value);
                    break;

                case Fit.SInt16:
                    bw.Write((short)value);
                    break;

                case Fit.UInt16:
                case Fit.UInt16z:
                    bw.Write((ushort)value);
                    break;

                case Fit.SInt32:
                    bw.Write((int)value);
                    break;

                case Fit.UInt32:
                case Fit.UInt32z:
                    bw.Write((uint)value);
                    break;

                case Fit.SInt64:
                    bw.Write((long)value);
                    break;

                case Fit.UInt64:
                case Fit.UInt64z:
                    bw.Write((ulong)value);
                    break;

                case Fit.Float32:
                    bw.Write((float)value);
                    break;

                case Fit.Float64:
                    bw.Write((double)value);
                    break;

                case Fit.String:
                    bw.Write((byte[])value);
                    break;

                default:
                    break;
                }
            }
        }