/// <summary> /// Default constructor. /// </summary> /// <param name="columnName">Column name.</param> /// <param name="dataType">Column data type.</param> /// <param name="columnSize">Specifies column data size. This is available for String datatype only.</param> public LDB_DataColumn(string columnName, LDB_DataType dataType, int columnSize) { m_ColumnName = columnName; m_DataType = dataType; // TODO: check that column name won't exceed 50 bytes if (dataType == LDB_DataType.Bool) { m_ColumSize = 1; } else if (dataType == LDB_DataType.DateTime) { m_ColumSize = 13; } else if (dataType == LDB_DataType.Int) { m_ColumSize = 4; } else if (dataType == LDB_DataType.Long) { m_ColumSize = 8; } else { m_ColumSize = columnSize; } }
/// <summary> /// Converts internal data to .NET data type. /// </summary> /// <param name="dataType">Data type.</param> /// <param name="val">Data buffer.</param> /// <param name="offset">Offset in data buffer where to start reading data.</param> /// <param name="length">Lenght of data to read from data buffer.</param> /// <returns></returns> public static object ConvertFromInternalData(LDB_DataType dataType, byte[] val, int offset, int length) { if (dataType == LDB_DataType.Bool) { return(Convert.ToBoolean(val[offset + 0])); } else if (dataType == LDB_DataType.DateTime) { /* Data structure * 1 byte day * 1 byte month * 4 byte year (int) * 1 byte hour * 1 byte minute * 1 byte second */ // day int day = val[offset + 0]; // month int month = val[offset + 1]; // year int year = ldb_Utils.ByteToInt(val, offset + 2); // hour int hour = val[offset + 6]; // minute int minute = val[offset + 7]; // second int second = val[offset + 8]; return(new DateTime(year, month, day, hour, minute, second)); } else if (dataType == LDB_DataType.Long) { return(ldb_Utils.ByteToLong(val, offset + 0)); } else if (dataType == LDB_DataType.Int) { return(ldb_Utils.ByteToInt(val, offset + 0)); } else if (dataType == LDB_DataType.String) { return(Encoding.UTF8.GetString(val, offset, length)); } else { throw new Exception("Invalid column data type, never must reach here !"); } }
/// <summary> /// Parses column from byte[] data. /// </summary> /// <param name="columnData">Column data.</param> internal void Parse(byte[] columnData) { if (columnData.Length != 102) { throw new Exception("Invalid column data length '" + columnData.Length + "' !"); } //-- total 102 bytes // 1 byte - column type // 4 bytes - column size // 45 bytes - reserved // 50 bytes - column name // 2 bytes - CRLF // column type m_DataType = (LDB_DataType)columnData[0]; // column size m_ColumSize = (columnData[1] << 24) | (columnData[2] << 16) | (columnData[3] << 8) | (columnData[4] << 0); // reserved // 45 bytes // column name byte[] columnName = new byte[50]; Array.Copy(columnData, 50, columnName, 0, columnName.Length); m_ColumnName = GetChar0TerminatedString(Encoding.UTF8.GetString(columnName)); if (m_DataType == LDB_DataType.Bool) { m_ColumSize = 1; } else if (m_DataType == LDB_DataType.DateTime) { m_ColumSize = 13; } else if (m_DataType == LDB_DataType.Int) { m_ColumSize = 4; } else if (m_DataType == LDB_DataType.Long) { m_ColumSize = 8; } }
/// <summary> /// Default constructor. /// </summary> /// <param name="columnName">Column name.</param> /// <param name="dataType">Column data type.</param> public LDB_DataColumn(string columnName, LDB_DataType dataType) : this(columnName, dataType, -1) {}
/// <summary> /// Parses column from byte[] data. /// </summary> /// <param name="columnData">Column data.</param> internal void Parse(byte[] columnData) { if (columnData.Length != 102) { throw new Exception("Invalid column data length '" + columnData.Length + "' !"); } //-- total 102 bytes // 1 byte - column type // 4 bytes - column size // 45 bytes - reserved // 50 bytes - column name // 2 bytes - CRLF // column type m_DataType = (LDB_DataType) columnData[0]; // column size m_ColumSize = (columnData[1] << 24) | (columnData[2] << 16) | (columnData[3] << 8) | (columnData[4] << 0); // reserved // 45 bytes // column name byte[] columnName = new byte[50]; Array.Copy(columnData, 50, columnName, 0, columnName.Length); m_ColumnName = GetChar0TerminatedString(Encoding.UTF8.GetString(columnName)); if (m_DataType == LDB_DataType.Bool) { m_ColumSize = 1; } else if (m_DataType == LDB_DataType.DateTime) { m_ColumSize = 13; } else if (m_DataType == LDB_DataType.Int) { m_ColumSize = 4; } else if (m_DataType == LDB_DataType.Long) { m_ColumSize = 8; } }
/// <summary> /// Default constructor. /// </summary> /// <param name="columnName">Column name.</param> /// <param name="dataType">Column data type.</param> public LDB_DataColumn(string columnName, LDB_DataType dataType) : this(columnName, dataType, -1) { }
/// <summary> /// Converts internal data to .NET data type. /// </summary> /// <param name="dataType">Data type.</param> /// <param name="val">Data buffer.</param> /// <param name="offset">Offset in data buffer where to start reading data.</param> /// <param name="length">Lenght of data to read from data buffer.</param> /// <returns></returns> public static object ConvertFromInternalData(LDB_DataType dataType,byte[] val,int offset,int length) { if(dataType == LDB_DataType.Bool){ return Convert.ToBoolean(val[offset + 0]); } else if(dataType == LDB_DataType.DateTime){ /* Data structure 1 byte day 1 byte month 4 byte year (int) 1 byte hour 1 byte minute 1 byte second */ // day int day = val[offset + 0]; // month int month = val[offset + 1]; // year int year = ldb_Utils.ByteToInt(val,offset + 2); // hour int hour = val[offset + 6]; // minute int minute = val[offset + 7]; // second int second = val[offset + 8]; return new DateTime(year,month,day,hour,minute,second); } else if(dataType == LDB_DataType.Long){ return ldb_Utils.ByteToLong(val,offset + 0); } else if(dataType == LDB_DataType.Int){ return ldb_Utils.ByteToInt(val,offset + 0); } else if(dataType == LDB_DataType.String){ return System.Text.Encoding.UTF8.GetString(val,offset,length); } else{ throw new Exception("Invalid column data type, never must reach here !"); } }