public DbfField GetField(string column) { DbfField field = null; this.TryGetField(column, out field); return(field); }
public override byte[] Encode(DbfField field, object data) { if (field.Length != 8) { throw new ArgumentException("DateTime fields must always be 8 bytes in length."); } // Null values result in zeroes. if (data == null) { return(new byte[field.Length]); } // The date gets encoded as a Julian Day. DateTime dt = (DateTime)data; ulong date = DateToJulian(dt); // The time gets encoded as number of milliseconds since midnight. ulong time = (ulong)(dt.Hour * 3600000 + dt.Minute * 60000 + dt.Second * 1000); // Time is shifted to be the high double-word. for (int i = 0; i < 32; i++) { time *= 2; } // Date and time are encoded as two uint32 double words. return(BitConverter.GetBytes(date + time)); }
public static string MakeFieldKey(DbfField field) { if (field == null) { return(null); } return(MakeFieldKey(field.GetColumnName())); }
private static void AddDbfField(DbfRecord record, string columnName, object val) { DbfField field = CreateDbfField(columnName, val); if (field != null) { record.AddField(field); } }
public override byte[] Encode(DbfField field, object data) { // Convert data to string. NULL is the empty string. string text = data == null ? "" : (string)data; // Pad string with spaces. // Convert string to byte array. return(Encoding.GetBytes(text.PadRight(field.Length))); }
public bool TryGetField(string column, out DbfField field) { field = null; if (column == null) { return(false); } return(this.m_fieldMap.TryGetValue(column, out field)); }
public byte[] Encode(DbfField field, object data) { string text = Convert.ToString(data).PadLeft(field.Length, ' '); if (text.Length > field.Length) { text.Substring(0, field.Length); } return(Encoding.ASCII.GetBytes(text)); }
public byte[] Encode(DbfField field, object data) { string text = Convert.ToString(data, System.Globalization.CultureInfo.InvariantCulture).PadLeft(field.Length, ' '); if (text.Length > field.Length) { text.Substring(0, field.Length); } return(Encoding.ASCII.GetBytes(text)); }
public byte[] Encode(DbfField field, object data) { float value = 0; if (data != null) { value = float.Parse(data.ToString()); } return(BitConverter.GetBytes(value)); }
/// <inheritdoc /> public byte[] Encode(DbfField field, object data, Encoding encoding) { float value = 0; if (data != null) { value = (float)data; } return(BitConverter.GetBytes(value)); }
protected void CheckField(DbfField field, string name, DbfType type, int length, int?decimals = null) { Assert.AreEqual(name, field.Name); Assert.AreEqual(type, field.DbfType); Assert.AreEqual(length, field.FieldLength); if (decimals != null) { Assert.AreEqual(decimals, field.Decimals); } }
public byte[] Encode(DbfField field, object data) { int value = 0; if (data != null) { value = (int)data; } return(BitConverter.GetBytes(value)); }
public override byte[] Encode(DbfField field, object data) { if (data == null) { return(null); } System.Diagnostics.Debug.Assert(field.Length == 8); return(BitConverter.GetBytes((long)data)); }
/// <inheritdoc /> public byte[] Encode(DbfField field, object data, Encoding encoding) { string text = Convert.ToString(data, CultureInfo.InvariantCulture).PadLeft(field.Length, ' '); if (text.Length > field.Length) { text.Substring(0, field.Length); } return(encoding.GetBytes(text)); }
public void DemoCode() { var dbf = new Dbf(DbfVersion.FoxPro2WithMemo); DbfField testField = dbf.AddNewField("TEST", DbfFieldType.Character, 12); DbfRecord record = dbf.CreateRecord(); record[testField] = "HELLO"; dbf.SaveTo("test.dbf"); Assert.AreEqual(record.Data[0], record[testField]); }
public byte[] Encode(DbfField field, object data) { string text = new string(' ', field.Length); if (data != null) { DateTime dt = DateTime.Parse(data.ToString()); text = String.Format("{0:d4}{1:d2}{2:d2}", dt.Year, dt.Month, dt.Day).PadLeft(field.Length, ' '); } return(Encoding.ASCII.GetBytes(text)); }
private static DbfField CreateDbfField(string columnName, object val) { if (val == null) { return(null); } DbfField field = new DbfField(); field.SetColumnName(columnName); field.SetValue(val); return(field); }
public override byte[] Encode(DbfField field, object data) { string text = new string(' ', field.Length); if (data != null) { DateTime dt = (DateTime)data; text = string.Format("{0:d4}{1:d2}{2:d2}", dt.Year, dt.Month, dt.Day).PadLeft(field.Length, ' '); } return(Encoding.GetBytes(text)); }
public override byte[] Encode(DbfField field, object data) { string format = $"{{0,{field.Length}:0.{new string('0', field.Precision)}}}"; string text = string.Format(CultureInfo.InvariantCulture, format, data); if (text.Length > field.Length) { text.Substring(0, field.Length); } return(this.Encoding.GetBytes(text)); }
public void WriteOneField() { dbf = new Dbf(); DbfField field = new DbfField("TEST", DbfFieldType.Character, 12); dbf.Fields.Add(field); dbf.Write("test.dbf", DbfVersion.VisualFoxPro); dbf = new Dbf(); dbf.Read("test.dbf"); Assert.AreEqual("TEST", dbf.Fields[0].Name, "Field name should be TEST."); }
public override byte[] Encode(DbfField field, object data) { var entry = (DbfMemoEntry)data; if (field.Length > 4) { return(data == null ? Enumerable.Repeat((byte)' ', field.Length).ToArray() : Encoding.GetBytes(entry.Index.ToString().PadLeft(field.Length))); } return(data == null ? (new byte[4]) : BitConverter.GetBytes(entry.Index)); }
public byte[] Encode(DbfField field, object data) { // Convert data to string. NULL is the empty string. string text = data == null ? "" : (string)data; // Pad string with spaces. while (text.Length < field.Length) { text = text + " "; } // Convert string to byte array. return(Encoding.ASCII.GetBytes((string)text)); }
/// <inheritdoc /> public byte[] Encode(DbfField field, object data, Encoding encoding) { string text; if (data is DateTime dt) { text = dt.ToString(format).PadLeft(field.Length, ' '); } else { text = field.DefaultValue; } return(encoding.GetBytes(text)); }
public byte[] Encode(DbfField field, object data) { // Convert boolean value to string. string text = "?"; if (data != null) { text = bool.Parse(data.ToString()) == true ? "Y" : "N"; } // Grow string to fill field length. text = text.PadLeft(field.Length, ' '); // Convert string to byte array. return(Encoding.ASCII.GetBytes(text)); }
/// <inheritdoc /> public byte[] Encode(DbfField field, object data, Encoding encoding) { // Convert boolean value to string. string text = "?"; if (data != null) { text = (bool)data == true ? "Y" : "N"; } // Grow string to fill field length. text = text.PadLeft(field.Length, ' '); // Convert string to byte array. return(encoding.GetBytes(text)); }
public void NumericField() { dbf = new Dbf(); DbfField field = new DbfField("TEST", DbfFieldType.Numeric, 12, 2); dbf.Fields.Add(field); DbfRecord record = dbf.CreateRecord(); record.Data[0] = 3.14; dbf.Write("test.dbf", DbfVersion.VisualFoxPro); dbf = new Dbf(); dbf.Read("test.dbf"); Assert.AreEqual(3.14, dbf.Records[0][0], "Record content should be 3.14."); }
public void WriteFieldAndRecord() { dbf = new Dbf(); DbfField field = new DbfField("TEST", DbfFieldType.Character, 12); dbf.Fields.Add(field); DbfRecord record = dbf.CreateRecord(); record.Data[0] = "HELLO"; dbf.Write("test.dbf", DbfVersion.VisualFoxPro); dbf = new Dbf(); dbf.Read("test.dbf"); Assert.AreEqual("HELLO", dbf.Records[0][0], "Record content should be HELLO."); }
public void CurrencyField() { dbf = new Dbf(); DbfField field = new DbfField("TEST", DbfFieldType.Currency, 4); dbf.Fields.Add(field); DbfRecord record = dbf.CreateRecord(); record.Data[0] = 4.34F; dbf.Write("test.dbf", DbfVersion.VisualFoxPro); dbf = new Dbf(); dbf.Read("test.dbf"); Assert.AreEqual((float)4.34, dbf.Records[0][0], "Record content should be 4.34."); }
public void DateTimeField() { dbf = new Dbf(); DbfField field = new DbfField("TEST", DbfFieldType.DateTime, 8); dbf.Fields.Add(field); DbfRecord record = dbf.CreateRecord(); record.Data[0] = new DateTime(2018, 8, 7, 20, 15, 8); dbf.Write("test.dbf", DbfVersion.VisualFoxPro); dbf = new Dbf(); dbf.Read("test.dbf"); Assert.AreEqual(new DateTime(2018, 8, 7, 20, 15, 8), dbf.Records[0][0], "Record content should be 2018-08-07 20:15:08."); }
public void LogicalField() { dbf = new Dbf(); DbfField field = new DbfField("TEST", DbfFieldType.Logical, 12); dbf.Fields.Add(field); DbfRecord record = dbf.CreateRecord(); record.Data[0] = true; dbf.Write("test.dbf", DbfVersion.VisualFoxPro); dbf = new Dbf(); dbf.Read("test.dbf"); Assert.AreEqual(true, dbf.Records[0][0], "Record content should be TRUE."); }