FixedLengthField.cs :  » Persistence-Frameworks » FileHelpers-Library » FileHelpers » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Persistence Frameworks » FileHelpers Library 
FileHelpers Library » FileHelpers » FixedLengthField.cs
#region "   Copyright 2005-07 to Marcos Meli - http://www.marcosmeli.com.ar" 

// Errors, suggestions, contributions, send a mail to: marcos@filehelpers.com.

#endregion

using System.Reflection;
using System;
using System.Text;

namespace FileHelpers{
  internal sealed class FixedLengthField : FieldBase
  {
    #region "  Properties  "

    internal int mFieldLength;
    internal FieldAlignAttribute mAlign = new FieldAlignAttribute(AlignMode.Left, ' ');

    internal FixedMode mFixedMode = FixedMode.ExactLength;


    #endregion

    #region "  Constructor  "

    internal FixedLengthField(FieldInfo fi, int length, FieldAlignAttribute align) : base(fi)
    {
      this.mFieldLength = length;

      if (align != null)
        this.mAlign = align;
      else
      {
        if (fi.FieldType == typeof(Int16) ||
          fi.FieldType == typeof(Int32) ||
          fi.FieldType == typeof(Int64) ||
          fi.FieldType == typeof(UInt16) ||
          fi.FieldType == typeof(UInt32) ||
          fi.FieldType == typeof(UInt64) ||
          fi.FieldType == typeof(byte) ||
          fi.FieldType == typeof(sbyte) ||
          fi.FieldType == typeof(decimal) ||
          fi.FieldType == typeof(float) ||
          fi.FieldType == typeof(double))

          mAlign = new FieldAlignAttribute(AlignMode.Right, ' ');
      }
    }

    #endregion

    #region "  Overrides String Handling  "

    protected override ExtractedInfo ExtractFieldString(LineInfo line)
    {
      if (line.CurrentLength == 0)
      {
        if (mIsOptional)
          return ExtractedInfo.Empty;
        else
          throw new BadUsageException("End Of Line found processing the field: " + mFieldInfo.Name + " at line "+ line.mReader.LineNumber.ToString() + ". (You need to mark it as [FieldOptional] if you want to avoid this exception)");
      }
      
      ExtractedInfo res;

      if (line.CurrentLength < this.mFieldLength)
        if (mFixedMode == FixedMode.AllowLessChars || mFixedMode == FixedMode.AllowVariableLength)
          res = new ExtractedInfo(line);
        else
          throw new BadUsageException("The string '" + line.CurrentString + "' (length " + line.CurrentLength.ToString() + ") at line "+ line.mReader.LineNumber.ToString() + " has less chars than the defined for " + mFieldInfo.Name + " (" + mFieldLength.ToString() + "). You can use the [FixedLengthRecord(FixedMode.AllowLessChars)] to avoid this problem.");
      else if (mIsLast && line.CurrentLength > mFieldLength && mFixedMode != FixedMode.AllowMoreChars && mFixedMode != FixedMode.AllowVariableLength)
        throw new BadUsageException("The string '" + line.CurrentString + "' (length " + line.CurrentLength.ToString() + ") at line "+ line.mReader.LineNumber.ToString() + " has more chars than the defined for the last field " + mFieldInfo.Name + " (" + mFieldLength.ToString() + ").You can use the [FixedLengthRecord(FixedMode.AllowMoreChars)] to avoid this problem.");
      else
        res = new ExtractedInfo(line, line.mCurrentPos + mFieldLength);

      return res;
    }

    protected override void CreateFieldString(StringBuilder sb, object fieldValue)
    {
      string field = base.BaseFieldString(fieldValue);

      if (field.Length > mFieldLength)
        field = field.Substring(0, mFieldLength);
        //sb.Length = length + this.mFieldLength;

      if (mAlign.Align == AlignMode.Left)
      {
        sb.Append(field);
        sb.Append(mAlign.AlignChar, mFieldLength - field.Length);
      }
      else if (mAlign.Align == AlignMode.Right)
      {
        sb.Append(mAlign.AlignChar, mFieldLength - field.Length);
        sb.Append(field);
      }
      else
      {
        int middle = (mFieldLength - field.Length) / 2;

        sb.Append(mAlign.AlignChar, middle);
        sb.Append(field);
        sb.Append(mAlign.AlignChar,  mFieldLength - field.Length - middle);
//        if (middle > 0)
//          res = res.PadLeft(mFieldLength - middle, mAlign.AlignChar).PadRight(mFieldLength, mAlign.AlignChar);
      }
    }

    #endregion
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.