protected static DateTime Read_DATE_YYYYMMDD_DateTime( IHasEncoding encoding, Byte[] srcBytes, int offset, int length //ref string stringValue ) { int?year = Read_NUMERICAL_NullableInt32(encoding, srcBytes, offset, 4); int?month = Read_NUMERICAL_NullableInt32(encoding, srcBytes, offset + 4, 2); int?day = Read_NUMERICAL_NullableInt32(encoding, srcBytes, offset + 6, 2); // if (year.HasValue && month.HasValue && day.HasValue) { try { return(new DateTime(year.Value, month.Value, day.Value)); } catch (Exception) { return(new DateTime()); } } else { return(DateTime.MinValue); } }
protected static Boolean?Read_LOGICAL_NullableBoolean( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { switch ((char)srcBytes[offset]) { case 'T': case 't': case 'Y': case 'y': return(true); case 'F': case 'f': case 'N': case 'n': return(false); default: return(null); //throw new Exception("Invalid Logical Value"); } }
protected static DateTime Read_DATE_YMD_DateTime( IHasEncoding encoding, Byte[] srcBytes, int offset, int length //ref string stringValue ) { int y, m, d; y = 1900 + srcBytes[offset]; m = srcBytes[offset + 1]; d = srcBytes[offset + 2]; if (d >= 1 && d <= 31 && m >= 1 && m <= 12) { try { return(new DateTime(y, m, d)); } catch (Exception) { // ignore } } return(new DateTime()); }
protected static void Write_String_CHARACTER(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, string stringValue) { if (stringValue == null) { stringValue = String.Empty; } var srcBytes = encoding.Encoding.GetBytes(stringValue); int srcLength = srcBytes.Length; if (srcLength > dstLength) { throw new FieldOverflowException(string.Format("Column #{0} cannot store more than {1} characters.", columnIndex + 1, dstLength)); } int src = 0; while (src < srcLength) { dstBytes[dst++] = srcBytes[src++]; } while (src < dstLength) { dstBytes[dst++] = 0; src++; } }
protected static byte Read_BYTE_Byte( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { return(srcBytes[offset]); }
protected static void Write_Byte_BYTE(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, Byte byteValue) { WriteSingleByte(dstBytes, dst, byteValue); }
protected static void Write_ByteArray_BYTES(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, Byte[] value) { Array.Copy(value, 0, dstBytes, dst, dstLength); }
protected static UInt16 Read_UINT16_UInt16( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { return((UInt16)(srcBytes[offset] | (srcBytes[offset + 1] << 8))); }
protected static void Write_String_MEMO(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, string value) { // Byte[] srcBytes = BitConverter.GetBytes(value); // WriteInBytes(dstBytes, dst, srcBytes); }
protected static void Write_UInt16_UINT16(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, UInt16 value) { WriteSingleByte(dstBytes, dst, (byte)(value & 0xFF)); WriteSingleByte(dstBytes, dst + 1, (byte)((value >> 8) & 0xFF)); }
protected static void Write_Boolean_DELETED_FLAG(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, Boolean value) { char val = (value ? '\x01' : '\x00'); WriteSingleByte(dstBytes, dst, (Byte)val); }
protected static void Write_Boolean_BOOL(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, Boolean value) { char val = (value ? 'T' : 'F'); WriteSingleByte(dstBytes, dst, (Byte)val); }
protected static void Write_NullableInt32_NUMERICAL(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, Int32?value) { string valueStr = value.ToString(); Write_String_CHARACTER(dstBytes, encoding, columnIndex, dst, dstLength, valueStr); }
protected static Byte[] Read_BYTES_ByteArray( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { Byte[] result = new Byte[length]; Array.Copy(srcBytes, offset, result, 0, length); return(result); }
protected static void Write_DateTime_DATE_YYYYMMDD(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, DateTime dateValue) { var strValue = dateValue.ToString("yyyyMMdd"); Write_String_CHARACTER(dstBytes, encoding, columnIndex, dst, dstLength, strValue); }
protected static DateTime Read_DATETIME_DateTime( IHasEncoding encoding, Byte[] srcBytes, int offset, int length //ref string stringValue ) { Int64 ticks = BitConverter.ToInt64(srcBytes, offset); return(new DateTime(ticks)); }
protected static void Write_UInt32_UINT32(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, UInt32 value) { WriteSingleByte(dstBytes, dst, (byte)(value & 0xFF)); WriteSingleByte(dstBytes, dst + 1, (byte)((value >> 8) & 0xFF)); WriteSingleByte(dstBytes, dst + 2, (byte)((value >> 16) & 0xFF)); WriteSingleByte(dstBytes, dst + 3, (byte)((value >> 24) & 0xFF)); }
protected static void Write_Char_CHAR(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, char charValue) { var stringValue = charValue.ToString(); var srcBytes = encoding.Encoding.GetBytes(stringValue); WriteInBytes(dstBytes, dst, srcBytes); }
protected static void Write_DateTime_DATETIME(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, DateTime dateValue) { Int64 ticks = dateValue.Ticks; Byte[] srcBytes = BitConverter.GetBytes(ticks); WriteInBytes(dstBytes, dst, srcBytes); }
protected static UInt32 Read_UINT32_UInt32( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { var result = (UInt32)(srcBytes[offset] | (srcBytes[offset + 1] << 8) | (srcBytes[offset + 2] << 16) | (srcBytes[offset + 3] << 24)); return(result); }
protected static double?Read_NUMERICAL_NullableDouble( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { string valueStr = Read_CHARACTER_String(encoding, srcBytes, offset, length).TrimEnd(); if (valueStr.Length == 0) { return(null); } return(double.Parse(valueStr)); }
protected static char Read_CHAR_Char( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { var resultString = encoding.Encoding.GetString(srcBytes, offset, length); if (resultString.Length == 0) { return('\0'); } return(resultString[0]); }
protected static void Write_DateTime_DATE_YMD(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, DateTime dateValue) { int y, m, d; y = dateValue.Year - 1900; m = dateValue.Month; d = dateValue.Day; WriteSingleByte(dstBytes, dst, (Byte)y); WriteSingleByte(dstBytes, dst + 1, (Byte)m); WriteSingleByte(dstBytes, dst + 2, (Byte)d); }
protected static string Read_CHARACTER_String( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { int realLength; for (realLength = 0; realLength < length && srcBytes[offset + realLength] != 0; realLength++) { ; } var result = encoding.Encoding.GetString(srcBytes, offset, realLength).TrimEnd(); return(result); }
protected static Int32?Read_NUMERICAL_NullableInt32( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { string valueStr = Read_CHARACTER_String(encoding, srcBytes, offset, length); if (valueStr.Length == 0) { return(null); } else { return(Int32.Parse(valueStr)); } }
protected static void Write_NullableBoolean_LOGICAL(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, Boolean?value) { char val; if (value.HasValue) { val = (value.Value ? 'T' : 'F'); } else { val = ' '; } WriteSingleByte(dstBytes, dst, (Byte)val); }
protected static string Read_MEMO_String( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { int?val = Read_NUMERICAL_NullableInt32(encoding, srcBytes, offset, length); // todo read the memo file... if (val.HasValue) { return(val.ToString()); // BitConverter.ToDouble(srcBytes, offset); } else { return(null); } }
protected static void Write_NullableDouble_NUMERICAL(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, double?value) { string valueStr; if (value.HasValue) { valueStr = value.ToString(); } else { valueStr = null; } Write_String_CHARACTER(dstBytes, encoding, columnIndex, dst, dstLength, valueStr); }
protected static Boolean Read_DELETED_FLAG_Boolean( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { switch ((char)srcBytes[offset]) { case '\x01': return(true); case '\x00': return(false); default: throw new Exception("Invalid Logical Value"); } }
protected static void Write_NullableDateTime_DATE_YYYYMMDD(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, DateTime?dateValue) { string strValue; if (dateValue.HasValue) { strValue = dateValue.Value.ToString("yyyyMMdd"); } else { strValue = null; } Write_String_CHARACTER(dstBytes, encoding, columnIndex, dst, dstLength, strValue); }
protected static char Read_CHAR_Char( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { var resultString = encoding.Encoding.GetString(srcBytes, offset, length); if (resultString.Length == 0) return '\0'; return resultString[0]; }
protected static void Write_String_CHARACTER(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, string stringValue) { if (stringValue == null) stringValue = String.Empty; var srcBytes = encoding.Encoding.GetBytes(stringValue); int srcLength = srcBytes.Length; if (srcLength > dstLength) { throw new FieldOverflowException(string.Format("Column #{0} cannot store more than {1} characters.", columnIndex + 1, dstLength)); } int src = 0; while (src < srcLength) { dstBytes[dst++] = srcBytes[src++]; } while (src < dstLength) { dstBytes[dst++] = 0; src++; } }
protected static void Write_NullableDouble_NUMERICAL(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, double? value) { string valueStr; if (value.HasValue) valueStr = value.ToString(); else valueStr = null; Write_String_CHARACTER(dstBytes, encoding, columnIndex, dst, dstLength, valueStr); }
protected static void Write_NullableBoolean_LOGICAL(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, Boolean? value) { char val; if (value.HasValue) val = (value.Value ? 'T' : 'F'); else val = ' '; WriteSingleByte(dstBytes, dst, (Byte)val); }
protected static UInt16 Read_UINT16_UInt16( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { return (UInt16)(srcBytes[offset] | (srcBytes[offset + 1] << 8)); }
protected static string Read_CHARACTER_String( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { int realLength; for (realLength = 0; realLength < length && srcBytes[offset + realLength] != 0; realLength++) ; var result = encoding.Encoding.GetString(srcBytes, offset, realLength).TrimEnd(); return result; }
protected static byte Read_BYTE_Byte( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { return srcBytes[offset]; }
protected static UInt32 Read_UINT32_UInt32( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { var result = (UInt32)(srcBytes[offset] | (srcBytes[offset + 1] << 8) | (srcBytes[offset + 2] << 16) | (srcBytes[offset + 3] << 24)); return result; }
protected static DateTime Read_DATETIME_DateTime( IHasEncoding encoding, Byte[] srcBytes, int offset, int length //ref string stringValue ) { Int64 ticks = BitConverter.ToInt64(srcBytes, offset); return new DateTime(ticks); }
protected static DateTime Read_DATE_YMD_DateTime( IHasEncoding encoding, Byte[] srcBytes, int offset, int length //ref string stringValue ) { int y, m, d; y = 1900 + srcBytes[offset]; m = srcBytes[offset + 1]; d = srcBytes[offset + 2]; if (d >= 1 && d <= 31 && m >= 1 && m <= 12) { try { return new DateTime(y, m, d); } catch (Exception) { // ignore } } return new DateTime(); }
protected static DateTime? Read_DATE_YYYYMMDD_NullableDateTime( IHasEncoding encoding, Byte[] srcBytes, int offset, int length //ref string stringValue ) { int? year = Read_NUMERICAL_NullableInt32(encoding, srcBytes, offset, 4); int? month = Read_NUMERICAL_NullableInt32(encoding, srcBytes, offset + 4, 2); int? day = Read_NUMERICAL_NullableInt32(encoding, srcBytes, offset + 6, 2); // if (year.HasValue && month.HasValue && day.HasValue) { return new DateTime(year.Value, month.Value, day.Value); } else return null; }
protected static Boolean Read_DELETED_FLAG_Boolean( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { switch ((char)srcBytes[offset]) { case '\x01': return true; case '\x00': return false; default: throw new Exception("Invalid Logical Value"); } }
protected static Boolean? Read_LOGICAL_NullableBoolean( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { switch ((char)srcBytes[offset]) { case 'T': case 't': case 'Y': case 'y': return true; case 'F': case 'f': case 'N': case 'n': return false; default: return null; //throw new Exception("Invalid Logical Value"); } }
protected static void Write_NullableDateTime_DATE_YYYYMMDD(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, DateTime? dateValue) { string strValue; if (dateValue.HasValue) strValue = dateValue.Value.ToString("yyyyMMdd"); else strValue = null; Write_String_CHARACTER(dstBytes, encoding, columnIndex, dst, dstLength, strValue); }
protected static string Read_MEMO_String( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { int? val = Read_NUMERICAL_NullableInt32(encoding, srcBytes, offset, length); // todo read the memo file... if (val.HasValue) { return val.ToString(); // BitConverter.ToDouble(srcBytes, offset); } else return null; }
protected static void Write_NullableInt32_NUMERICAL(Byte[] dstBytes, IHasEncoding encoding, int columnIndex, int dst, int dstLength, Int32? value) { string valueStr = value.ToString(); Write_String_CHARACTER(dstBytes, encoding, columnIndex, dst, dstLength, valueStr); }
protected static double? Read_NUMERICAL_NullableDouble( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { string valueStr = Read_CHARACTER_String(encoding, srcBytes, offset, length).TrimEnd(); if (valueStr.Length == 0) return null; return double.Parse(valueStr); }
protected static Int32? Read_NUMERICAL_NullableInt32( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { string valueStr = Read_CHARACTER_String(encoding, srcBytes, offset, length); if (valueStr.Length == 0) { return null; } else return Int32.Parse(valueStr); }
protected static Byte[] Read_BYTES_ByteArray( IHasEncoding encoding, Byte[] srcBytes, int offset, int length ) { Byte[] result = new Byte[length]; Array.Copy(srcBytes, offset, result, 0, length); return result; }