private void ParseFile(string[] dataTypes, string[] fieldNames, int lineIndexToBeginRead, string pattern) { if (debugOutput) { Console.WriteLine("reading " + this.Filename); } if (lines.Length <= 0 || lineIndexToBeginRead >= lines.Length) { return; } DataRow myDataRow; widths = new int[dataTypes.Length]; decimals = new int[dataTypes.Length]; Debug.Assert(fieldNames.Length == dataTypes.Length, "Error: in " + "'" + Filename + "'", "line 1:" + lines[0] + "\n" + "line 2:" + lines[1]); int patternMatchFailureCount = 0; //int i; CreateColumns(dataTypes, fieldNames); int col = 0; int row = lineIndexToBeginRead; DataRowIO io = new DataRowIO(true); try { for (; row < lines.Length; row++) { if (lines[row].Trim() == "") { continue; } if (pattern != "" && !Regex.IsMatch(lines[row], pattern)) { patternMatchFailureCount++; continue; } string[] val = CsvFile.ParseCSV(lines[row]); myDataRow = this.NewRow(); for (col = 0; col < fieldNames.Length; col++) { if (col >= val.Length) { myDataRow[col] = DBNull.Value; continue; } switch (dataTypes[col]) { case "System.Int32": io.SaveInt(myDataRow, fieldNames[col], val[col]); break; case "System.Int16": io.SaveInt(myDataRow, fieldNames[col], val[col]); break; case "System.Integer": io.SaveInt(myDataRow, fieldNames[col], val[col]); break; case "System.Double": io.SaveDouble(myDataRow, fieldNames[col], val[col]); break; case "System.Single": io.SaveFloat(myDataRow, fieldNames[col], val[col]); break; case "System.String": myDataRow[col] = val[col]; break; case "System.DateTime": myDataRow[col] = Convert.ToDateTime(val[col]); break; case "System.Boolean": if (val[col].ToString() == "") { myDataRow[col] = DBNull.Value; } else { if (val[col].ToString() == "0") { myDataRow[col] = false; } else if (val[col].ToString() == "1") { myDataRow[col] = true; } else { myDataRow[col] = Convert.ToBoolean(val[col]); } } break; default: Console.WriteLine("Invalid selection : '" + dataTypes[col] + "'"); break; } } this.Rows.Add(myDataRow); } } catch (Exception ex) { Console.WriteLine(ex.Message); if (lines.Length < row) { Console.WriteLine("'" + lines[row] + "'"); } throw new Exception("Error reading data at row " + row + " and column " + col + " file name = " + this.Filename); } if (patternMatchFailureCount > 0) { Logger.WriteLine(patternMatchFailureCount + " lines skipped that did not match pattern " + pattern); } if (io.Messages.Trim().Length > 0) { Logger.WriteLine(io.Messages); } }
private void ParseFile(string[] dataTypes, string[] fieldNames, int lineIndexToBeginRead, string pattern) { if (debugOutput) Console.WriteLine("reading " + this.Filename); if (lines.Length <= 0 || lineIndexToBeginRead >= lines.Length) { return; } DataRow myDataRow; widths = new int[dataTypes.Length]; decimals = new int[dataTypes.Length]; Debug.Assert(fieldNames.Length == dataTypes.Length, "Error: in " + "'" + Filename + "'", "line 1:" + lines[0] + "\n" + "line 2:" + lines[1]); int patternMatchFailureCount = 0; //int i; CreateColumns(dataTypes, fieldNames); int col = 0; int row = lineIndexToBeginRead; DataRowIO io = new DataRowIO(true); try { for (; row < lines.Length; row++) { if (lines[row].Trim() == "") { continue; } if (pattern != "" && !Regex.IsMatch(lines[row], pattern)) { patternMatchFailureCount++; continue; } string[] val = CsvFile.ParseCSV(lines[row]); myDataRow = this.NewRow(); for (col = 0; col < fieldNames.Length; col++) { if (col >= val.Length) { myDataRow[col] = DBNull.Value; continue; } switch (dataTypes[col]) { case "System.Int32": io.SaveInt(myDataRow, fieldNames[col], val[col]); break; case "System.Int16": io.SaveInt(myDataRow, fieldNames[col], val[col]); break; case "System.Integer": io.SaveInt(myDataRow, fieldNames[col], val[col]); break; case "System.Double": io.SaveDouble(myDataRow, fieldNames[col], val[col]); break; case "System.Single": io.SaveFloat(myDataRow, fieldNames[col], val[col]); break; case "System.String": myDataRow[col] = val[col]; break; case "System.DateTime": myDataRow[col] = Convert.ToDateTime(val[col]); break; case "System.Boolean": if (val[col].ToString() == "") myDataRow[col] = DBNull.Value; else { if (val[col].ToString() == "0") myDataRow[col] = false; else if (val[col].ToString() == "1") myDataRow[col] = true; else myDataRow[col] = Convert.ToBoolean(val[col]); } break; default: Console.WriteLine("Invalid selection : '" + dataTypes[col] + "'"); break; } } this.Rows.Add(myDataRow); } } catch (Exception ex) { Console.WriteLine(ex.Message); if (lines.Length < row) { Console.WriteLine("'" + lines[row] + "'"); } throw new Exception("Error reading data at row " + row + " and column " + col + " file name = " + this.Filename); } if (patternMatchFailureCount > 0) { Logger.WriteLine(patternMatchFailureCount + " lines skipped that did not match pattern " + pattern); } if( io.Messages.Trim().Length >0) Logger.WriteLine(io.Messages); }