Skip to content

Commit

Permalink
Merge pull request #19 from TNSGlobal/development
Browse files Browse the repository at this point in the history
windows-31j & empty file fixes
  • Loading branch information
fbiagi authored Apr 10, 2017
2 parents c11ff44 + 10d7f00 commit 6632645
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
6 changes: 6 additions & 0 deletions SpssLib/FileParser/Records/CharacterEncodingRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ private Encoding GetEncoding(string strEncoding)
if (info != null)
return info.GetEncoding();
}

if (strEncoding.Equals("windows-31j", StringComparison.InvariantCultureIgnoreCase))
{
// 932 - Japanese (Shift-JIS)
return Encoding.GetEncoding(932);
}

throw new SpssFileFormatException("Encoding not recognized: " + strEncoding);
}
Expand Down
48 changes: 35 additions & 13 deletions SpssLib/FileParser/SavFileParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,50 @@ public void ParseMetaData()
IList<IRecord> records = new List<IRecord>(1000);

MetaData = new MetaData();

RecordType readRecordType;
IRecord record;
IRecordParser recordParser;

// Read the header record and validate this file
try
{
readRecordType = _reader.ReadRecordType();
if (readRecordType != RecordType.HeaderRecord)
{
throw new SpssFileFormatException("No header record is present. A header record is required. Is this a valid SPSS file?");
}
recordParser = parsers.GetParser(readRecordType);
record = recordParser.ParseRecord(_reader);
record.RegisterMetadata(MetaData);
records.Add(record);
}
catch (EndOfStreamException)
{
throw new SpssFileFormatException("No header record is present. A header record is required. Is your file empty?");
}

// Read the rest of the records
do
{
readRecordType = _reader.ReadRecordType();
var recordParser = parsers.GetParser(readRecordType);
var record = recordParser.ParseRecord(_reader);
recordParser = parsers.GetParser(readRecordType);
record = recordParser.ParseRecord(_reader);
record.RegisterMetadata(MetaData);
records.Add(record);
} while (readRecordType != RecordType.End);


try
{
_dataStartPosition = Stream.Position;
}
catch (NotSupportedException)
{
// Some stream types don't support the Position property (CryptoStream...)
_dataStartPosition = 0;
}
{
_dataStartPosition = Stream.Position;
}
catch (NotSupportedException)
{
// Some stream types don't support the Position property (CryptoStream...)
_dataStartPosition = 0;
}

SetDataRecordStream();
MetaDataParsed = true;
}
Expand Down
2 changes: 1 addition & 1 deletion SpssLib/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.4")]
[assembly: AssemblyVersion("1.1.5")]
2 changes: 1 addition & 1 deletion SpssLib/SpssLib.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>$id$</id>
<version>1.1.4</version>
<version>1.1.5</version>
<title>$title$</title>
<authors>Roeland Nieuwenhuis, Francisco Biagi</authors>
<owners>$author$</owners>
Expand Down
12 changes: 10 additions & 2 deletions Test.SpssLib/TestSpssReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SpssLib.DataReader;
using SpssLib.FileParser;
using SpssLib.SpssDataset;

namespace Test.SpssLib
Expand Down Expand Up @@ -64,7 +65,14 @@ public void TestReadFile()
Assert.AreEqual(rowCount, 3, "Rows count does not match");
}


[TestMethod]
[ExpectedException(typeof(SpssFileFormatException))]
public void TestEmptyStream()
{
int varCount;
int rowCount;
ReadData(new MemoryStream(new byte[0]), out varCount, out rowCount);
}

[TestMethod]
[DeploymentItem(@"TestFiles\MissingValues.sav")]
Expand Down Expand Up @@ -118,7 +126,7 @@ public void TestReadMissingValuesAsNull()
}
}

internal static void ReadData(FileStream fileStream, out int varCount, out int rowCount,
internal static void ReadData(Stream fileStream, out int varCount, out int rowCount,
IDictionary<int, Action<int, Variable>> variableValidators = null, IDictionary<int, Action<int, int, Variable, object>> valueValidators = null)
{
SpssReader spssDataset = new SpssReader(fileStream);
Expand Down

0 comments on commit 6632645

Please sign in to comment.