diff --git a/FileHelpers.Examples/Examples/18.Converters/70.PaddingConverter.cs b/FileHelpers.Examples/Examples/18.Converters/70.PaddingConverter.cs new file mode 100644 index 000000000..4f8b8c657 --- /dev/null +++ b/FileHelpers.Examples/Examples/18.Converters/70.PaddingConverter.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using FileHelpers; + +namespace ExamplesFx +{ + //-> Name:Write Delimited File + //-> Description:Example of how to write a Delimited File + //-> AutoRun:true + + public class PaddingConverterExample + : ExampleBase + { + //-> To write an output file (separated by a "|"): + + //-> FileOut: Output.txt + + // -> You use the same Record Mapping Class as you would to read it: + //-> File:RecordClass.cs + /// + /// Layout for a file delimited by | + /// + [DelimitedRecord("|")] + public class Orders + { + public int OrderID; + + [FieldConverter(ConverterKind.Padding, 8, PaddingMode.Left, '0')] + public string CustomerID; + + [FieldConverter(ConverterKind.Date, "ddMMyyyy")] + public DateTime OrderDate; + + [FieldConverter(ConverterKind.Decimal, ".")] // The decimal separator is . + public decimal Freight; + } + + //-> /File + + //-> Instantiate a FileHelperEngine and write the file: + + public override void Run() + { + //-> File:Example.cs + var engine = new FileHelperEngine(); + + var orders = new List(); + + orders.Add(new Orders() + { + OrderID = 1, + CustomerID = "9001", + Freight = 82.43M, + OrderDate = new DateTime(2009, 05, 01) + }); + + orders.Add(new Orders() + { + OrderID = 2, + CustomerID = "9002", + Freight = 12.22M, + OrderDate = new DateTime(2009, 05, 02) + }); + + //engine now supports custom EOL for writing + engine.NewLineForWrite = "\n"; + + engine.WriteFile("Output.Txt", orders); + + //-> /File + + Console.WriteLine(engine.WriteString(orders)); + } + + //-> The classes you use could come from anywhere: LINQ to Entities, SQL database reads, or in this case, classes created within an application. + } +} diff --git a/FileHelpers.Examples/FileHelpers.Examples.csproj b/FileHelpers.Examples/FileHelpers.Examples.csproj index cdbdb47fc..c77fca4ad 100644 --- a/FileHelpers.Examples/FileHelpers.Examples.csproj +++ b/FileHelpers.Examples/FileHelpers.Examples.csproj @@ -84,6 +84,7 @@ Code + diff --git a/FileHelpers/Attributes/FieldConverterAttribute.cs b/FileHelpers/Attributes/FieldConverterAttribute.cs index 439670d94..0981970e8 100644 --- a/FileHelpers/Attributes/FieldConverterAttribute.cs +++ b/FileHelpers/Attributes/FieldConverterAttribute.cs @@ -37,83 +37,32 @@ public FieldConverterAttribute(ConverterKind converter, string arg1, string arg2 public FieldConverterAttribute(ConverterKind converter, string arg1, string arg2, string arg3) : this(converter, new string[] {arg1, arg2, arg3}) {} - /// /// Indicates the used for read/write operations. /// /// The used for the transformations. /// An array of parameters passed directly to the Converter - private FieldConverterAttribute(ConverterKind converter, params string[] args) - { + public FieldConverterAttribute(ConverterKind converter, params object[] args) + { Kind = converter; - Type convType; - - switch (converter) { - case ConverterKind.Date: - convType = typeof (ConvertHelpers.DateTimeConverter); - break; - - case ConverterKind.DateMultiFormat: - convType = typeof (ConvertHelpers.DateTimeMultiFormatConverter); - break; - - case ConverterKind.Byte: - convType = typeof (ConvertHelpers.ByteConverter); - break; - - case ConverterKind.SByte: - convType = typeof (ConvertHelpers.SByteConverter); - break; + Type convType = GetConverter(converter); + //mType = type; - case ConverterKind.Int16: - convType = typeof (ConvertHelpers.Int16Converter); - break; - case ConverterKind.Int32: - convType = typeof (ConvertHelpers.Int32Converter); - break; - case ConverterKind.Int64: - convType = typeof (ConvertHelpers.Int64Converter); - break; + CreateConverter(convType, args); + } - case ConverterKind.UInt16: - convType = typeof (ConvertHelpers.UInt16Converter); - break; - case ConverterKind.UInt32: - convType = typeof (ConvertHelpers.UInt32Converter); - break; - case ConverterKind.UInt64: - convType = typeof (ConvertHelpers.UInt64Converter); - break; + + /// + /// Indicates the used for read/write operations. + /// + /// The used for the transformations. + /// An array of parameters passed directly to the Converter + private FieldConverterAttribute(ConverterKind converter, params string[] args) + { + Kind = converter; - case ConverterKind.Decimal: - convType = typeof (ConvertHelpers.DecimalConverter); - break; - case ConverterKind.Double: - convType = typeof (ConvertHelpers.DoubleConverter); - break; - // Added by Shreyas Narasimhan 17 March 2010 - case ConverterKind.PercentDouble: - convType = typeof (ConvertHelpers.PercentDoubleConverter); - break; - case ConverterKind.Single: - convType = typeof (ConvertHelpers.SingleConverter); - break; - case ConverterKind.Boolean: - convType = typeof (ConvertHelpers.BooleanConverter); - break; - // Added by Alexander Obolonkov 2007.11.08 - case ConverterKind.Char: - convType = typeof (ConvertHelpers.CharConverter); - break; - // Added by Alexander Obolonkov 2007.11.08 - case ConverterKind.Guid: - convType = typeof (ConvertHelpers.GuidConverter); - break; - default: - throw new BadUsageException("Converter '" + converter.ToString() + - "' not found, you must specify a valid converter."); - } + Type convType = GetConverter(converter); //mType = type; CreateConverter(convType, args); @@ -207,12 +156,90 @@ private void CreateConverter(Type convType, object[] args) else throw new BadUsageException("The custom converter must inherit from ConverterBase"); - } - + } + #endregion + + #region " Get ConverterType " + private Type GetConverter(ConverterKind converter) + { + Type convType; - #region " ArgsToTypes " + switch (converter) + { + case ConverterKind.Date: + convType = typeof(ConvertHelpers.DateTimeConverter); + break; + + case ConverterKind.DateMultiFormat: + convType = typeof(ConvertHelpers.DateTimeMultiFormatConverter); + break; + + case ConverterKind.Byte: + convType = typeof(ConvertHelpers.ByteConverter); + break; + + case ConverterKind.SByte: + convType = typeof(ConvertHelpers.SByteConverter); + break; + + case ConverterKind.Int16: + convType = typeof(ConvertHelpers.Int16Converter); + break; + case ConverterKind.Int32: + convType = typeof(ConvertHelpers.Int32Converter); + break; + case ConverterKind.Int64: + convType = typeof(ConvertHelpers.Int64Converter); + break; + case ConverterKind.UInt16: + convType = typeof(ConvertHelpers.UInt16Converter); + break; + case ConverterKind.UInt32: + convType = typeof(ConvertHelpers.UInt32Converter); + break; + case ConverterKind.UInt64: + convType = typeof(ConvertHelpers.UInt64Converter); + break; + + case ConverterKind.Decimal: + convType = typeof(ConvertHelpers.DecimalConverter); + break; + case ConverterKind.Double: + convType = typeof(ConvertHelpers.DoubleConverter); + break; + // Added by Shreyas Narasimhan 17 March 2010 + case ConverterKind.PercentDouble: + convType = typeof(ConvertHelpers.PercentDoubleConverter); + break; + case ConverterKind.Single: + convType = typeof(ConvertHelpers.SingleConverter); + break; + case ConverterKind.Boolean: + convType = typeof(ConvertHelpers.BooleanConverter); + break; + // Added by Alexander Obolonkov 2007.11.08 + case ConverterKind.Char: + convType = typeof(ConvertHelpers.CharConverter); + break; + // Added by Alexander Obolonkov 2007.11.08 + case ConverterKind.Guid: + convType = typeof(ConvertHelpers.GuidConverter); + break; + case ConverterKind.Padding: + convType = typeof(ConvertHelpers.PaddingConverter); + break; + default: + throw new BadUsageException("Converter '" + converter.ToString() + + "' not found, you must specify a valid converter."); + } + return convType; + } + #endregion + + #region " ArgsToTypes " + private static Type[] ArgsToTypes(object[] args) { if (args == null) { @@ -264,7 +291,8 @@ internal void ValidateTypes(FieldInfo fi) fieldType = fieldType.GetGenericArguments()[0]; switch (Kind) { - case ConverterKind.None: + case ConverterKind.None: + case ConverterKind.Padding: valid = true; break; diff --git a/FileHelpers/Converters/ConvertHelpers.cs b/FileHelpers/Converters/ConvertHelpers.cs index 0c2b4cc9c..1765f80dc 100644 --- a/FileHelpers/Converters/ConvertHelpers.cs +++ b/FileHelpers/Converters/ConvertHelpers.cs @@ -958,7 +958,7 @@ public override string FieldToString(object from) #endregion - #region " GUID, Char, String Converters " + #region " GUID, Char, String, Padding Converters " #region " Convert Classes " @@ -1214,6 +1214,98 @@ public override string FieldToString(object from) // } //} + /// + /// Convert a numeric value with separators into a value + /// + internal sealed class PaddingConverter + : ConverterBase + { + /// + /// Total length of the field when written + /// + private readonly int totalLength; + + /// + /// Padding to left or right + /// + private readonly PaddingMode paddingMode; + + /// + /// Character to pad with + /// + private readonly Char paddingChar; + + /// + /// Padding Converter with Length and option Padding Mode and Padding character + /// + /// Total length of the field when written + /// Whether to pad left or right + /// /// Character to pad with + public PaddingConverter(Int32 TotalLength, PaddingMode PaddingMode = PaddingMode.Left, char PaddingCharacter = ' ') + { + totalLength = TotalLength; + paddingMode = PaddingMode; + paddingChar = PaddingCharacter; + } + + /// + /// Convert a string to object for the record object + /// + /// String representation of the object + /// object or empty + public override object StringToField(string from) + { + if (String.IsNullOrEmpty(from)) + return string.Empty; + + try + { + return from; + } + catch + { + throw new ConvertException(from, typeof(string)); + } + } + + /// + /// Convert a object to padded string + /// + /// Object to convert + /// String converted to + public override string FieldToString(object from) + { + string paddedString = ""; + try + { + paddedString = from.ToString(); + + if (paddedString.Length < totalLength) + { + int paddingLength = totalLength - paddedString.Length; + string strPadding = new string(paddingChar, paddingLength); + if (paddingMode == PaddingMode.Right) + { + return string.Concat(paddedString, strPadding); + } + else + { + return string.Concat(strPadding, paddedString); + } + } + + return paddedString; + } + catch + { + + throw new ConvertException(paddedString, from.GetType(), "Padding failed"); + } + + } + + } + #endregion #endregion diff --git a/FileHelpers/Enums/AlignMode.cs b/FileHelpers/Enums/AlignMode.cs index 20ec417df..e955abada 100644 --- a/FileHelpers/Enums/AlignMode.cs +++ b/FileHelpers/Enums/AlignMode.cs @@ -12,4 +12,16 @@ public enum AlignMode /// Aligns the field to the right. Right } + + /// + /// Indicates padding mode when field writes the record. + /// + public enum PaddingMode + { + /// Padding field to the left. + Left, + + /// Padding field to the right. + Right + } } \ No newline at end of file diff --git a/FileHelpers/Enums/ConverterKind.cs b/FileHelpers/Enums/ConverterKind.cs index fd7516a70..b7ee3c530 100644 --- a/FileHelpers/Enums/ConverterKind.cs +++ b/FileHelpers/Enums/ConverterKind.cs @@ -117,6 +117,14 @@ public enum ConverterKind /// "B" -> {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} /// "P" -> (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) /// - Guid + Guid, + // Added by Jimmy Mathai 2018.08.15 + /// + /// Add fixed Padding to Any types + /// Params: arg1 is an integer. This is the total length for writing. + /// Params: arg2 is a Align weather to padd left or right. Default is left + /// Params: arg3 is a Char. The character to padd with. Default is space. + /// + Padding } } \ No newline at end of file