public MereFileField(MereColumn mereColumnForField, string delimiter, int index, string recordKey, string toStringFormat, string padChar, List <MereFileParsingOption> fileFieldParsingOptions, int fieldLength) { MereColumnForField = mereColumnForField; Delimiter = delimiter; Index = index; RecordKey = recordKey; ToStringFormat = toStringFormat; PadChar = padChar ?? " "; FileFieldParsingOptions = fileFieldParsingOptions; FieldLength = fieldLength; }
public static MereFileField GetFileField(MereColumn mereColumn, IMereFileFieldAttribute fileFieldAttr) { return(new MereFileField(mereColumn, fileFieldAttr.Delimiter, fileFieldAttr.Index, fileFieldAttr.RecordKey, fileFieldAttr.ToStringFormat, fileFieldAttr.PadChar, fileFieldAttr.FileFieldParsingOptions, fileFieldAttr.FieldLength)); }
public static string ParseFieldForWrite(MereColumn mereColumn, string toStringFormat, List <MereFileParsingOption> options, int fieldLength, string padChar, object value, bool fixedWidth) { string v; if (!string.IsNullOrWhiteSpace(toStringFormat)) { if (mereColumn.PropertyDescriptor.PropertyType == typeof(DateTime)) { var d = (DateTime)value; // field.Get(value); v = d.ToString(toStringFormat); } else if (mereColumn.PropertyDescriptor.PropertyType == typeof(DateTime?)) { var o = value; // field.Get(value); if (o != null) { var d = (DateTime)o; v = d.ToString(toStringFormat); } else { v = ""; } } else if (mereColumn.PropertyDescriptor.PropertyType == typeof(int)) { var d = (int)value; v = d.ToString(toStringFormat); } else if (mereColumn.PropertyDescriptor.PropertyType == typeof(int?)) { var o = value; // field.Get(value); if (o != null) { var d = (int)o; v = d.ToString(toStringFormat); } else { v = ""; } } else if (mereColumn.PropertyDescriptor.PropertyType == typeof(decimal)) { var d = (decimal)value; v = d.ToString(toStringFormat); } else if (mereColumn.PropertyDescriptor.PropertyType == typeof(decimal?)) { var o = value; //field.Get(value); if (o != null) { var d = (decimal)o; v = d.ToString(toStringFormat); } else { v = ""; } } else { //var o = field.Get(value); v = value == null ? "" : value.ToString(); } } else { v = value == null ? "" : value.ToString(); } if (options != null && options.Any()) { //decimal precision if (options.Contains(MereFileParsingOption.DecimalPrecision1)) { v = decimal.Parse(string.IsNullOrEmpty(v.Trim()) ? "0" : v).ToString(".0"); } if (options.Contains(MereFileParsingOption.DecimalPrecision2)) { v = decimal.Parse(string.IsNullOrEmpty(v.Trim()) ? "0" : v).ToString(".00"); } if (options.Contains(MereFileParsingOption.DecimalPrecision3)) { v = decimal.Parse(string.IsNullOrEmpty(v.Trim()) ? "0" : v).ToString(".000"); } if (options.Contains(MereFileParsingOption.DecimalPrecision4)) { v = decimal.Parse(string.IsNullOrEmpty(v.Trim()) ? "0" : v).ToString(".0000"); } if (options.Contains(MereFileParsingOption.DecimalPrecision5)) { v = decimal.Parse(string.IsNullOrEmpty(v.Trim()) ? "0" : v).ToString(".00000"); } if (options.Contains(MereFileParsingOption.DecimalPrecision6)) { v = decimal.Parse(string.IsNullOrEmpty(v.Trim()) ? "0" : v).ToString(".000000"); } //end decimal precision //removed decimal if (options.Contains(MereFileParsingOption.RemovedDecimal1)) { v = decimal.Parse(string.IsNullOrEmpty(v.Trim()) ? "0" : v).ToString(".0").Replace(".", ""); } if (options.Contains(MereFileParsingOption.RemovedDecimal2)) { v = decimal.Parse(string.IsNullOrEmpty(v.Trim()) ? "0" : v).ToString(".00").Replace(".", ""); } if (options.Contains(MereFileParsingOption.RemovedDecimal3)) { v = decimal.Parse(string.IsNullOrEmpty(v.Trim()) ? "0" : v).ToString(".000").Replace(".", ""); } if (options.Contains(MereFileParsingOption.RemovedDecimal4)) { v = decimal.Parse(string.IsNullOrEmpty(v.Trim()) ? "0" : v).ToString(".0000").Replace(".", ""); } if (options.Contains(MereFileParsingOption.RemovedDecimal5)) { v = decimal.Parse(string.IsNullOrEmpty(v.Trim()) ? "0" : v).ToString(".00000").Replace(".", ""); } if (options.Contains(MereFileParsingOption.RemovedDecimal6)) { v = decimal.Parse(string.IsNullOrEmpty(v.Trim()) ? "0" : v).ToString(".000000").Replace(".", ""); } //end removed decimal //check Ebcidic if (options.Contains(MereFileParsingOption.Ebcidic)) { var pos = new[] { '{', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', ' ' }; var neg = new[] { '}', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', ' ' }; v = v.Trim(); var l = string.IsNullOrEmpty(v) ? 0 : int.Parse(v.Last().ToString()); l = l < 10 ? l : 10; var isNeg = v.StartsWith("-"); var c = isNeg ? neg[l] : pos[l]; v = string.Join("", v.Take(v.Length - 1)).TrimStart('-') + c; } //end check Ebcidic //check trailing neg if (options.Contains(MereFileParsingOption.TrailingNegative)) { v = v.Trim(); if (v.StartsWith("-")) { v = v.TrimStart('-') + "-"; } } //end check trailing neg if (options.Contains(MereFileParsingOption.TrimStartSpaces)) { v = v.TrimStart(); } if (options.Contains(MereFileParsingOption.TrimEndSpaces)) { v = v.TrimEnd(); } if (options.Contains(MereFileParsingOption.Trim)) { v = v.Trim(); } if (fixedWidth) { //pad to width if (options.Contains(MereFileParsingOption.AlignLeft)) { v = v.PadRight(fieldLength, padChar.First()); } else { v = v.PadLeft(fieldLength, padChar.First()); } //end pad to width } } else if (fixedWidth && !string.IsNullOrEmpty(padChar)) { v = v.PadLeft(fieldLength, padChar.First()); } if (options != null && options.Contains(MereFileParsingOption.WrapWithDoubleQuotes)) { v = "\"" + v + "\""; } if (fixedWidth) { //check length if (v.Length > fieldLength) { v = string.Join("", v.Take(fieldLength)); } //end check length } return(v); }