document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Globalization;
  7.  
  8. namespace Parse720
  9. {
  10.     [AttributeUsage(System.AttributeTargets.Field)]
  11.     class FP : Attribute
  12.     {
  13.         public int startOffset;
  14.         public int endOffset;
  15.         public bool hasDecimal;
  16.         public bool hasSign;
  17.  
  18.         public FP(int startOffset)
  19.         {
  20.             this.startOffset = startOffset;
  21.             this.endOffset = startOffset;
  22.         }
  23.  
  24.         public FP(int startOffset, int endOffset)
  25.         {
  26.             this.startOffset = startOffset;
  27.             this.endOffset = endOffset;
  28.         }
  29.     }
  30.  
  31.     public class RegistroDeclarante
  32.     {
  33.         [FP(1)]
  34.         public string TipoDeRegistro;
  35.         [FP(2,4)]
  36.         public string ModeloDeclaracion;
  37.         [FP(5, 8)]
  38.         public string Ejercicio;
  39.         [FP(9, 17)]
  40.         public string NifDeclarante;
  41.         [FP(18,57)]
  42.         public string ApellidosYNombre;
  43.         [FP(58)]
  44.         public string TipoDeSoporte;
  45.         [FP(59,67)]
  46.         public string Rel_Telefono;
  47.         [FP(68,107)]
  48.         public string Rel_ApellidosYNombre;
  49.         [FP(108,120)]
  50.         public string NiDeclaracion;
  51.         [FP(121)]
  52.         public string DecComplementaria;
  53.         [FP(122)]
  54.         public string DecSustitutiva;
  55.         [FP(123,135)]
  56.         public string NiDeclaracionAnterior;
  57.         [FP(136,144)]
  58.         public decimal NumeroTotalDeRegistrosDecl;
  59.         [FP(145,162, hasSign=true, hasDecimal=true)]
  60.         public decimal SumaTotalValoracion1;
  61.         [FP(163,180, hasSign=true, hasDecimal=true)]
  62.         public decimal SumaTotalValoracion2;
  63.         [FP(181,500)]
  64.         public string blanco;
  65.     }
  66.  
  67.     public class RegistroDetalle
  68.     {
  69.         [FP(1)]
  70.         public string Tipo;
  71.         [FP(2,4)]
  72.         public string Modelo;
  73.         [FP(5,8)]
  74.         public string Ejercicio;
  75.         [FP(9, 17)]
  76.         public string NIF_Declarante;
  77.         [FP(18, 26)]
  78.         public string NIF_Declarado;
  79.         [FP(27, 35)]
  80.         public string NIF_RepresentanteLegal;
  81.         [FP(36, 75)]
  82.         public string ApellidosYNombreDeclarado;
  83.         [FP(76)]
  84.         public string ClaveDeclarante;
  85.         [FP(77, 101)]
  86.         public string TipoDeTitualidad;
  87.         [FP(102)]
  88.         public string ClaveDeBien;
  89.         [FP(103)]
  90.         public string SubclaveDeBien;
  91.         [FP(104, 128)]
  92.         public string TipoDeDerechoInmueble;
  93.         [FP(129, 130)]
  94.         public string CodigoDePais;
  95.         [FP(131)]
  96.         public string ClaveId;
  97.         [FP(132, 143)]
  98.         public string IdValores;
  99.         [FP(144)]
  100.         public string ClaveDeIdentifacation;
  101.         [FP(144, 155)]
  102.         public string CodigoBIC;
  103.         [FP(156, 189)]
  104.         public string CodigoCuenta;
  105.         [FP(190, 230)]
  106.         public string IdentificationEntidad;
  107.         [FP(231, 250)]
  108.         public string NIFEnPaisResidencial;
  109.         [FP(251, 302)]
  110.         public string DomicilioEntidad_Via;
  111.         [FP(303, 342)]
  112.         public string DomicilioEntidad_Comp;
  113.         [FP(343, 372)]
  114.         public string DomicilioEntidad_Ciudad;
  115.         [FP(373, 402)]
  116.         public string DomicilioEntidad_Provincia;
  117.         [FP(403, 412)]
  118.         public string DomicilioEntidad_CodigoPostal;
  119.         [FP(413, 414)]
  120.         public string DomicilioEntidad_CodigoPais;
  121.         [FP(415, 422)]
  122.         public DateTime FechaIncorporacion;
  123.         [FP(423)]
  124.         public string OrigenDelBien;
  125.         [FP(424, 431)]
  126.         public DateTime FechaExtincion;
  127.         [FP(432,446, hasSign=true, hasDecimal=true)]
  128.         public decimal Valoracion1;
  129.         [FP(447,461, hasSign=true, hasDecimal=true)]
  130.         public decimal Valoracion2;
  131.         [FP(462)]
  132.         public string RepresentacionDeValores;
  133.         [FP(463,474, hasDecimal=true)]
  134.         public decimal NumeroDeValores;
  135.         [FP(475)]
  136.         public string TipoInmueble;
  137.         [FP(476,480, hasDecimal=true)]
  138.         public decimal PorcentageParticipacion;
  139.         [FP(481,500)]
  140.         public string Blancos;
  141.     }
  142.  
  143.     class Process720
  144.     {
  145.         static void WriteBuffer<T>(T item, char[] buffer)
  146.         {
  147.             for(int i=0;i<500;i++)
  148.             {
  149.                 buffer[i] = \' \';
  150.             }
  151.  
  152.             foreach (var field in typeof(T).GetFields())
  153.             {
  154.                 object[] attr = field.GetCustomAttributes(typeof(FP), false);
  155.                 if (attr == null || attr.Length == 0)
  156.                     continue;
  157.  
  158.                 string v = "";
  159.                 FP fp = (FP)attr[0];
  160.                 if (field.FieldType == typeof(string))
  161.                 {
  162.                     v = (string)field.GetValue(item);
  163.                     if (v == null)
  164.                         v = "";
  165.                 }
  166.                 else if (field.FieldType == typeof(decimal))
  167.                 {
  168.                     decimal dv = (decimal)field.GetValue(item);
  169.                     v = "";
  170.                     int lengthLeft = fp.endOffset - fp.startOffset + 1;
  171.                     if (fp.hasSign)
  172.                     {
  173.                         if (dv < 0)
  174.                         {
  175.                             v = "N";
  176.                         }
  177.                         else
  178.                         {
  179.                             v = " ";
  180.                         }
  181.                         lengthLeft--;
  182.                     }
  183.  
  184.                     if (fp.hasDecimal)
  185.                     {
  186.                         v += dv.ToString(new string(\'0\', lengthLeft - 2) + ".00", CultureInfo.InvariantCulture).Replace(".", "");
  187.                     }
  188.                     else
  189.                     {
  190.                         v += dv.ToString(new string(\'0\', lengthLeft), CultureInfo.InvariantCulture);
  191.                     }
  192.                 }
  193.                 else if (field.FieldType == typeof(DateTime))
  194.                 {
  195.                     DateTime dv = (DateTime)field.GetValue(item);
  196.                     if (dv == default(DateTime))
  197.                     {
  198.                         v = "00000000";
  199.                     }
  200.                     else
  201.                     {
  202.                         v = dv.ToString("yyyyMMdd", CultureInfo.InvariantCulture);
  203.                     }
  204.                 }
  205.                 else
  206.                 {
  207.                     throw new ApplicationException("unknown field type");
  208.                 }
  209.  
  210.                 if (v.Length > fp.endOffset - fp.startOffset + 1)
  211.                 {
  212.                     throw new ApplicationException("too big");
  213.                 }
  214.  
  215.                 for (int i = 0; i < v.Length; i++ )
  216.                 {
  217.                     buffer[i + fp.startOffset - 1] = v[i];
  218.                 }
  219.             }
  220.         }
  221.  
  222.  
  223.         static T ParseBuffer<T>(char[] buffer) where T : new()
  224.         {
  225.             T t = new T();
  226.  
  227.             foreach( var field in typeof(T).GetFields())
  228.             {
  229.                 object[] attr = field.GetCustomAttributes(typeof(FP), false);
  230.                 if (attr == null || attr.Length == 0)
  231.                     continue;
  232.  
  233.                 FP fp = (FP)attr[0];
  234.  
  235.                 string v = new string(buffer, fp.startOffset - 1, fp.endOffset - fp.startOffset + 1);
  236.                 if (field.FieldType == typeof(string))
  237.                 {
  238.                     field.SetValue(t, v);
  239.                 }
  240.                 else if (field.FieldType == typeof(decimal))
  241.                 {
  242.                     if (fp.hasSign)
  243.                     {
  244.                         if (v[0] == \'N\')
  245.                         {
  246.                             v = \'-\' + v.Substring(1);
  247.                         }
  248.                         else
  249.                         {
  250.                             v = v.Substring(1);
  251.                         }
  252.                     }
  253.                     if (fp.hasDecimal)
  254.                     {
  255.                         v = v.Substring(0, v.Length - 2) + \'.\' + v.Substring(v.Length - 2, 2);
  256.                     }
  257.                     field.SetValue(t, (decimal)decimal.Parse(v, CultureInfo.InvariantCulture));
  258.                 }
  259.                 else if (field.FieldType == typeof(DateTime))
  260.                 {
  261.                     DateTime dv;
  262.                     if (v == "00000000")
  263.                     {
  264.                         dv = default(DateTime);
  265.                     }
  266.                     else
  267.                     {
  268.                         dv = DateTime.ParseExact(v, "yyyyMMdd", CultureInfo.InvariantCulture);
  269.                     }
  270.                     field.SetValue(t, dv);
  271.                 }
  272.                 else
  273.                 {
  274.                     throw new ApplicationException("unknown type");
  275.                 }
  276.  
  277.             }
  278.             return t;
  279.         }
  280.  
  281.         public static void WriteFile(string filename, RegistroDeclarante declarante, List<RegistroDetalle> detalle)
  282.         {
  283.             declarante.NumeroTotalDeRegistrosDecl = detalle.Count;
  284.             declarante.SumaTotalValoracion1 = detalle.Sum(x => x.Valoracion1);
  285.             declarante.SumaTotalValoracion2 = detalle.Sum(x => x.Valoracion2);
  286.  
  287.             using (StreamWriter file = new StreamWriter(filename, false, Encoding.GetEncoding(28591)))
  288.             {
  289.                 char[] buffer = new char[500];
  290.  
  291.                 WriteBuffer<RegistroDeclarante>(declarante, buffer);
  292.                 file.Write(buffer);
  293.  
  294.                 foreach (RegistroDetalle det in detalle)
  295.                 {
  296.                     WriteBuffer<RegistroDetalle>(det, buffer);
  297.                     file.Write(buffer);
  298.                 }
  299.             }
  300.         }
  301.  
  302.  
  303.         public static void ReadFile(string filename, out RegistroDeclarante declarante, out List<RegistroDetalle> detalle )
  304.         {
  305.             declarante = null;
  306.             detalle = new List<RegistroDetalle>();
  307.  
  308.             using (StreamReader file = new StreamReader(filename, Encoding.GetEncoding(28591)))
  309.             {
  310.                 char[] buffer = new char[500];
  311.                 while (true)
  312.                 {
  313.                     int amountRead = file.ReadBlock(buffer, 0, 500);
  314.                     if (amountRead == 0)
  315.                         break;
  316.  
  317.                     if (amountRead != 500)
  318.                         throw new ApplicationException("Corrupt file");
  319.  
  320.                     if (buffer[0] == \'1\')
  321.                     {
  322.                         declarante = ParseBuffer<RegistroDeclarante>(buffer);
  323.                     }
  324.                     else if (buffer[0] == \'2\')
  325.                     {
  326.                         detalle.Add(ParseBuffer<RegistroDetalle>(buffer));
  327.                     }
  328.                     else
  329.                     {
  330.                         throw new ApplicationException("corrupt file");
  331.                     }
  332.                 }
  333.             }
  334.         }
  335.     }
  336. }
');