Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable exception throwing on parsing error #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions DotNetDBF.Enumerable/Enumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ public static void AddRecord(this DBFWriter writer, Enumerable.IDBFInterceptor v
/// <typeparam name="T"></typeparam>
/// <param name="reader">The reader.</param>
/// <param name="prototype">The prototype. Anonymous class instance</param>
/// <param name="throwOnParsingError">Indicates if parsing error throws an exception. Default: true</param>
/// <returns></returns>
public static IEnumerable<T> AllRecords<T>(this DBFReader reader, T prototype = null) where T : class
public static IEnumerable<T> AllRecords<T>(this DBFReader reader, T prototype = null, bool throwOnParsingError = true) where T : class
{
var tType = typeof(T);

Expand All @@ -194,25 +195,25 @@ public static IEnumerable<T> AllRecords<T>(this DBFReader reader, T prototype =

if (tType.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Any())
{
var tAnon = reader.NextRecord(tProps, tOrderedProps);
var tAnon = reader.NextRecord(tProps, tOrderedProps, throwOnParsingError);
while (tAnon != null)
{
tReturn.Add((T) Activator.CreateInstance(tType, tAnon));
tAnon = reader.NextRecord(tProps, tOrderedProps);
tAnon = reader.NextRecord(tProps, tOrderedProps, throwOnParsingError);
}


return tReturn;
}

var t = reader.NextRecord(tProps, tOrderedProps);
var t = reader.NextRecord(tProps, tOrderedProps, throwOnParsingError);

while (t != null)
{
var interceptor = new Enumerable.DBFInterceptor(t, tProperties.Select(it => it.Name).ToArray());

tReturn.Add(interceptor.ActLike<T>(typeof(Enumerable.IDBFInterceptor)));
t = reader.NextRecord(tProps, tOrderedProps);
t = reader.NextRecord(tProps, tOrderedProps, throwOnParsingError);
}


Expand All @@ -225,9 +226,10 @@ public static IEnumerable<T> AllRecords<T>(this DBFReader reader, T prototype =
/// <param name="reader">The reader.</param>
/// <param name="whereColumn">The where column name.</param>
/// <param name="whereColumnEquals">What the were column should equal.</param>
/// <param name="throwOnParsingError">Indicates if parsing error throws an exception. Default: true</param>
/// <returns></returns>
public static IEnumerable<dynamic> DynamicAllRecords(this DBFReader reader, string whereColumn = null,
dynamic whereColumnEquals = null)
dynamic whereColumnEquals = null, bool throwOnParsingError = true)
{
var props = reader.GetSelectFields().Select(it => it.Name).ToArray();

Expand All @@ -240,7 +242,7 @@ public static IEnumerable<dynamic> DynamicAllRecords(this DBFReader reader, stri


var tReturn = new List<object>();
var t = reader.NextRecord();
var t = reader.NextRecord(throwOnParsingError);

while (t != null)
{
Expand All @@ -249,7 +251,7 @@ public static IEnumerable<dynamic> DynamicAllRecords(this DBFReader reader, stri
dynamic tO = t[i];
if (!tO.Equals(whereColumnEquals))
{
t = reader.NextRecord();
t = reader.NextRecord(throwOnParsingError);
continue;
}
}
Expand All @@ -259,7 +261,7 @@ public static IEnumerable<dynamic> DynamicAllRecords(this DBFReader reader, stri


tReturn.Add(interceptor);
t = reader.NextRecord();
t = reader.NextRecord(throwOnParsingError);
}


Expand Down
24 changes: 14 additions & 10 deletions DotNetDBF/DBFReader.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
DBFReader
Class for reading the records assuming that the given
InputStream comtains DBF data.
InputStream contains DBF data.

This file is part of DotNetDBF packege.
This file is part of DotNetDBF package.

original author (javadbf): anil@linuxense.com 2004/03/31

Expand Down Expand Up @@ -226,13 +226,12 @@ @returns The next row as an Object array. Types of the elements
these arrays follow the convention mentioned in the class description.
*/

public object[] NextRecord()
public object[] NextRecord(bool throwOnParsingError = true)
{
return NextRecord(_selectFields, _orderedSelectFields);
return NextRecord(_selectFields, _orderedSelectFields, throwOnParsingError);
}


internal object[] NextRecord(IEnumerable<int> selectIndexes, IList<int> sortedIndexes)
internal object[] NextRecord(IEnumerable<int> selectIndexes, IList<int> sortedIndexes, bool throwOnParsingError = true)
{
if (_isClosed)
{
Expand Down Expand Up @@ -365,8 +364,10 @@ internal object[] NextRecord(IEnumerable<int> selectIndexes, IList<int> sortedIn
}
catch (FormatException e)
{
throw new DBFException("Failed to parse Float",
e);
if (throwOnParsingError)
throw new DBFException("Failed to parse Float", e);

recordObjects[i] = default(decimal);
}

break;
Expand Down Expand Up @@ -399,8 +400,11 @@ internal object[] NextRecord(IEnumerable<int> selectIndexes, IList<int> sortedIn
}
catch (FormatException e)
{
throw new DBFException(
"Failed to parse Number", e);
if (throwOnParsingError)
throw new DBFException(
"Failed to parse Number", e);

recordObjects[i] = default(decimal);
}

break;
Expand Down