using System.Collections.Generic;
public UInt64 cod_arquivo;
public string promo_natal;
public decimal preco_por_unidade;
public decimal preco_promo;
public decimal preco_por_unidade_promo;
public string departamento;
public decimal qt_estoque;
public string status_promocao;
Console.Write( cod_arquivo + " ");
Console.Write( cod_barra + " ");
Console.Write( desc + " ");
Console.Write( sub_items + " ");
Console.Write( promo_natal + " ");
Console.Write( preco + " ");
Console.Write( preco_por_unidade + " ");
Console.Write( preco_promo + " ");
Console.Write( preco_por_unidade_promo + " ");
Console.Write( departamento + " ");
Console.Write( unidade + " ");
Console.Write( qt_estoque + " ");
Console.WriteLine(status_promocao + " ");
const char DELIM_START = '|';
const char DELIM_END = '|';
Dictionary<int,FieldInfo> fields = new Dictionary<int,FieldInfo>();
public Parser(TextReader s) {
fields.Add( 7, typeof(Item).GetField("desc") );
fields.Add( 9510, typeof(Item).GetField("sub_items") );
fields.Add( 9500, typeof(Item).GetField("promo_natal") );
fields.Add( 23, typeof(Item).GetField("preco") );
fields.Add( 45, typeof(Item).GetField("preco_por_unidade") );
fields.Add( 25, typeof(Item).GetField("preco_promo") );
fields.Add( 46, typeof(Item).GetField("preco_por_unidade_promo") );
fields.Add( 5, typeof(Item).GetField("departamento") );
fields.Add( 70, typeof(Item).GetField("unidade") );
fields.Add( 50, typeof(Item).GetField("qt_estoque") );
fields.Add( 121, typeof(Item).GetField("status_promocao") );
public void error(string c, string foo ) {
throw new Exception( "esperado [" + c + "] em [" + foo + "] linha[" + line + "] col[" + col + "] last[" + (int)last + ']' );
last = (char)stream.Read();
public void parseChar( char c, string foo ) {
public void skipSpace() {
while ( last == SPACE ) nextChar();
public ulong parseInt() {
while ( Char.IsDigit(last) ) {
result = (result * 10) + (ulong)(last - '0');
public void parseCampo(Item item) {
var cod_field = (int)parseInt();
parseChar(ZERO, "parseCampo");
parseChar(DELIM_START, "parseCampo");
while (last != EOF && last != DELIM_END) {
parseChar(DELIM_END, "parseCampo");
var campo = fields[cod_field];
error("Codigo de campo valido, mas obteve[" + cod_field + "]", "parseCampo");
var type = campo.FieldType;
campo.SetValue( item, Convert.ChangeType(val, type));
public void parseFimDeLinha() {
parseChar(COMMA, "parseFimDeLinha");
while( last == CR || last == LF || last == SPACE ) {
public Item parseItem() {
Item result = new Item();
result.cod_arquivo = parseInt();
result.cod_barra = parseInt();
while ( Char.IsDigit(last) ) {
public List<Item> parse() {
List<Item> items = new List<Item>();
public static void Main()
String arquivo = "0001 00000000020005 9510 0 |ARCOB1049B18 | 7 0 |VINHO COBOS COCODRILO BLEND 1,5L 2018| 23 0 |55990| 70 0 |GARRAFA| 50 0 |0| 121 0 |NORMAL|,\r\n" +
"0001 00000000020006 9510 0 |ARESC0202A20 77904151278142 | 7 0 |VINHO ESCORIHUELA FAMILIA GASCON CABERNET 2020| 23 0 |9990| 70 0 |GARRAFA| 50 0 |0| 121 0 |NORMAL|,\r\n";
TextReader sr = new StringReader(arquivo);
Parser p = new Parser(sr);