internal static Record GetRecord(RecordPacketData pData) { String[] rawFields = pData.Rows[1].Split(FieldSeparator.GetString, StringSplitOptions.RemoveEmptyEntries); Regex rAllFields = new Regex("^([0-9]{1,4})#(.*)"); Regex firstData = new Regex("^([0-9]{1,10})#"); Record record = new Record(); Match m = firstData.Match(rawFields[0]); record.MFN = m.Groups[0].Value; for (int i = 1; i < rawFields.Length; i++) { m = rAllFields.Match(rawFields[i]); String field = m.Groups[1].Value; String value = m.Groups[2].Value; record.AddField(field, value); } return(record); }
static private ParseResults ParseTemplate(string filename) { /* * MSIS_ID at 0 for 20 type CHAR * ADJ_IND at 20 for 2 type SMALLINT * TOS at 22 for 2 type SMALLINT * TYPE_CLM at 24 for 2 type SMALLINT * DATE_PAID at 26 for 6 type VARCHAR * AMT_PAID at 32 for 4 type INTEGER NULL * BEG_DOS at 36 for 4 type INTEGER NOTNULL * END_DOS at 40 for 8 type LONG * PROV_ID at 42 for 14 type DATETIME "YYYYMMHH24MMSI" NULL * DOLLA_AMOUNT at 56 for 1 type CHAR SUB_IND * SUB_FILLER at 57 for 150 type FILLER * * SUB J * DATE_PAID at 57 for 6 type VARCHAR * AMT_PAID at 63 for 4 type INTEGER NULL * BLARB_INDICATOR at 67 for 4 type INTEGER SUB_IND * SUB_FILLER at 71 for 75 type FILLER * * SUB 45 * ADJ_IND at 57 for 2 type SMALLINT * TOS at 59 for 2 type SMALLINT * TYPE_CLM at 61 for 2 type SMALLINT * * Indicators * must be at the end and followed by filler to reach full length * * * Number of Rows: 1190205 */ ParseResults pr = new ParseResults(); if (filename == String.Empty) { pr.success = false; pr.Error = "\n Empty File Name \n"; return(pr); } string dotout; using (StreamReader reader = new StreamReader(filename)) { try { dotout = reader.ReadToEnd(); } catch (OutOfMemoryException e) { pr.Error = "File too big to be a template"; pr.success = false; return(pr); } } string[] tokens; int ihold; // {FieldName} at {position} for {size} type {datatype} {DATEFORMAT} {NULL/NOTNULL} // 1 2 3 4 5 6 7 Optional Optional int numTokens = 7; // Split DotOut file on spaces tokens = dotout.Split(new char[] { ' ', '\r', '\n', '\t' }, StringSplitOptions.RemoveEmptyEntries); // To hold each field as described in the DotOut Record rec = new Record(); // Process each token for (int i = 0; (i + 7) <= tokens.GetLength(0);) { // Field Name Field newField; string name; int start; int size; string type; bool isTemplateIndicator = false; // Name name = tokens[i++]; // Expected "at" token if (tokens[i++].ToUpper() != "AT") { pr.Error = (("Expected \"at\" on line " + (rec.Count + 1).ToString() + " field " + (rec.Count - ((i - 1) * numTokens)).ToString()) + "\n"); pr.success = false; return(pr); } // Try to parse the position if (!int.TryParse(tokens[i++], out ihold)) { pr.Error = ((@"Unparsable Position on Line " + (rec.Count + 1).ToString() + " field " + (rec.Count - ((i - 1) * numTokens)).ToString()) + "\n"); pr.success = false; return(pr); } else { start = ihold; } // Expected "for" token if (tokens[i++].ToUpper() != "FOR") { pr.Error = (("Expected \"for\" on line " + (rec.Count + 1).ToString() + " field " + (rec.Count - ((i - 1) * numTokens)).ToString()) + "\n"); pr.success = false; return(pr); } // Try to parse the size if (!int.TryParse(tokens[i++], out ihold)) { pr.Error = ((@"Unparsable Field Size on Line " + (rec.Count + 1).ToString() + " Field " + (rec.Count - ((i - 1) * numTokens)).ToString()) + "\n"); pr.success = false; return(pr); } size = ihold; // Expected "type" token if (tokens[i++].ToUpper() != "TYPE") { pr.Error = (("Expected \"type\" on line " + (rec.Count + 1).ToString() + " field " + (rec.Count - ((i - 1) * numTokens)).ToString()) + "\n"); pr.success = false; return(pr); } // Get the type type = tokens[i++]; if (type.ToUpper() == "DATE") { // counts the date formatter which follows date // oracle's date format is incomprehensible to our parsers // technically this should be hidden in the datefield object // but it's too abstracted behind the field object to make it // owrth it. besides this counts as a token i++; } bool isNullable = false; if (tokens.Length > i) { for (int j = 0; j < 3; j++) { if (tokens[i].ToUpper() == "SUB_IND") { isTemplateIndicator = true; i++; } // Optional Null paramter if (tokens[i] == "NULL") { isNullable = true; i++; } if (tokens[i] == "NOTNULL") { isNullable = false; i++; } } } // We have our parameters add our new field newField = rec.AddField(name, start, type, size, isTemplateIndicator, isNullable); if (tokens.Length > i + 1) { if (tokens[i].ToUpper() == "SUB") { i++; // Count the "SUB" token rec.NewSubTemplate(tokens[i++]); // start a new template & count the token } // Check for optional rownumber extra bit } if (tokens.Length > i + 2) { if (tokens[i] == "Number" && tokens[i + 1] == "of" && tokens[i + 2] == "Rows:") { i += 3; } } } pr.record = rec; pr.success = true; // We've made our template return(pr); }
private static ParseResults ParseTemplate(string filename) { /* * MSIS_ID at 0 for 20 type CHAR * ADJ_IND at 20 for 2 type SMALLINT * TOS at 22 for 2 type SMALLINT * TYPE_CLM at 24 for 2 type SMALLINT * DATE_PAID at 26 for 6 type VARCHAR * AMT_PAID at 32 for 4 type INTEGER NULL * BEG_DOS at 36 for 4 type INTEGER NOTNULL * END_DOS at 40 for 8 type LONG * PROV_ID at 42 for 14 type DATETIME "YYYYMMHH24MMSI" NULL * DOLLA_AMOUNT at 56 for 1 type CHAR SUB_IND * SUB_FILLER at 57 for 150 type FILLER * * SUB J * DATE_PAID at 57 for 6 type VARCHAR * AMT_PAID at 63 for 4 type INTEGER NULL * BLARB_INDICATOR at 67 for 4 type INTEGER SUB_IND * SUB_FILLER at 71 for 75 type FILLER * * SUB 45 * ADJ_IND at 57 for 2 type SMALLINT * TOS at 59 for 2 type SMALLINT * TYPE_CLM at 61 for 2 type SMALLINT * * Indicators * must be at the end and followed by filler to reach full length * * Number of Rows: 1190205 */ ParseResults pr = new ParseResults(); if (filename == String.Empty) { pr.success = false; pr.Error = "\n Empty File Name \n"; return pr; } string dotout; using (StreamReader reader = new StreamReader(filename)) { try { dotout = reader.ReadToEnd(); } catch (OutOfMemoryException e) { pr.Error = "File too big to be a template"; pr.success = false; return pr; } } string[] tokens; int ihold; // {FieldName} at {position} for {size} type {datatype} {DATEFORMAT} {NULL/NOTNULL} // 1 2 3 4 5 6 7 Optional Optional int numTokens = 7; // Split DotOut file on spaces tokens = dotout.Split(new char[] { ' ', '\r', '\n', '\t' }, StringSplitOptions.RemoveEmptyEntries); // To hold each field as described in the DotOut Record rec = new Record(); // Process each token for (int i = 0; (i + 7) <= tokens.GetLength(0); ) { // Field Name Field newField; string name; int start; int size; string type; bool isTemplateIndicator = false; // Name name = tokens[i++]; // Expected "at" token if (tokens[i++].ToUpper() != "AT") { pr.Error = (("Expected \"at\" on line " + (rec.Count + 1).ToString() + " field " + (rec.Count - ((i - 1) * numTokens)).ToString()) + "\n"); pr.success = false; return pr; } // Try to parse the position if (!int.TryParse(tokens[i++], out ihold)) { pr.Error = ((@"Unparsable Position on Line " + (rec.Count + 1).ToString() + " field " + (rec.Count - ((i - 1) * numTokens)).ToString()) + "\n"); pr.success = false; return pr; } else start = ihold; // Expected "for" token if (tokens[i++].ToUpper() != "FOR") { pr.Error = (("Expected \"for\" on line " + (rec.Count + 1).ToString() + " field " + (rec.Count - ((i - 1) * numTokens)).ToString()) + "\n"); pr.success = false; return pr; } // Try to parse the size if (!int.TryParse(tokens[i++], out ihold)) { pr.Error = ((@"Unparsable Field Size on Line " + (rec.Count + 1).ToString() + " Field " + (rec.Count - ((i - 1) * numTokens)).ToString()) + "\n"); pr.success = false; return pr; } size = ihold; // Expected "type" token if (tokens[i++].ToUpper() != "TYPE") { pr.Error = (("Expected \"type\" on line " + (rec.Count + 1).ToString() + " field " + (rec.Count - ((i - 1) * numTokens)).ToString()) + "\n"); pr.success = false; return pr; } // Get the type type = tokens[i++]; if (type.ToUpper() == "DATE") { // counts the date formatter which follows date // oracle's date format is incomprehensible to our parsers // technically this should be hidden in the datefield object // but it's too abstracted behind the field object to make it // owrth it. besides this counts as a token i++; } bool isNullable = false; if (tokens.Length > i) { for (int j = 0; j < 3; j++) { if (tokens[i].ToUpper() == "SUB_IND") { isTemplateIndicator = true; i++; } // Optional Null paramter if (tokens[i] == "NULL") { isNullable = true; i++; } if (tokens[i] == "NOTNULL") { isNullable = false; i++; } } } // We have our parameters add our new field newField = rec.AddField(name, start, type, size, isTemplateIndicator, isNullable); if (tokens.Length > i + 1) { if (tokens[i].ToUpper() == "SUB") { i++; // Count the "SUB" token rec.NewSubTemplate(tokens[i++]); // start a new template & count the token } // Check for optional rownumber extra bit } if (tokens.Length > i + 2) { if (tokens[i] == "Number" && tokens[i + 1] == "of" && tokens[i + 2] == "Rows:") { i += 3; } } } pr.record = rec; pr.success = true; // We've made our template return pr; }