Example #1
0
        /// <summary>
        /// Create a new instance of <see cref="DataPackage"/> with headerSpaceSize from list of columns definitions and list of values
        /// </summary>
        /// <param name="headerSpaceSize">Header size (default 512 bytes)</param>
        /// <param name="columns">list of columns definitions strings  in format ColimnName[.ColumnType][.ColumnWidth]</param>
        /// <param name="values">list of values</param>
        public DataPackage(int headerSpaceSize, string[] columns, object[] values) : base(null)
        {
            _headerSpaceSize = headerSpaceSize <= 0 ? header_space_size_default : headerSpaceSize;
            _b  = new BinaryDataBuffer(_headerSpaceSize * 2);
            _bw = new BinaryDataWriter(_b);
            _br = new BinaryDataReader(_b);

            _colCount = columns.Length;
            bool hasValues = values != null && values.Length == _colCount;

            _headers = new MapTable <string, object>(StringComparer.Ordinal);

            //Create col info
            _indexes = new string[_colCount];
            _colInfo = new ColumnInfo[_colCount];
            for (int i = 0; i < _colCount; i++)
            {
                ColumnInfo info;
                string     name;
                if (columns[i].IndexOf('.') > 0)
                {
                    info = ColumnInfo.FromName(columns[i]);
                    name = columns[i].GetToken(0, ".");
                }
                else if (hasValues)
                {
                    name = columns[i];
                    info = ColumnInfo.FromValue(values[i]);
                }
                else
                {
                    name = columns[i];
                    info = ColumnInfo.FromType(typeof(string));
                }
                _indexes[i] = name;
                _colInfo[i] = info;
            }

            WritePackageHeader();

            if (hasValues)
            {
                AddNew();
                SetValues(values);
                Update();
            }
            else if (values != null)
            {
                throw new ArgumentException("The length of the data array must be equal to the length of the array of field names");
            }
        }