-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'hotfix/StlReader_opens_binary_stl_with_ascii_reader'
- Loading branch information
Showing
10 changed files
with
148 additions
and
22 deletions.
There are no files selected for viewing
Binary file added
BIN
+684 Bytes
...s/BinaryStlWriterTests.Write_WithLargeHeader_ReturnsExpectedResult.approved.stl
Binary file not shown.
Binary file added
BIN
+684 Bytes
...s/BinaryStlWriterTests.Write_WithSmallHeader_ReturnsExpectedResult.approved.stl
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using GenericStl.Tests.TestDataStructures; | ||
using NUnit.Framework; | ||
|
||
namespace GenericStl.Tests | ||
{ | ||
[TestFixture] | ||
public class StlFileTests | ||
{ | ||
public IEnumerable IsBinaryFileTestCases | ||
{ | ||
get | ||
{ | ||
yield return new TestCaseData(@".\TestData\ascii_block.stl").SetName("Ascii file").Returns(false); | ||
yield return new TestCaseData(@".\TestData\binary_block.stl").SetName("Binary block file").Returns(true); | ||
yield return new TestCaseData(@".\TestData\binary_block_with_solid_header.stl").SetName("Binary file with solid header").Returns(true); | ||
} | ||
} | ||
|
||
[TestCaseSource("IsBinaryFileTestCases")] | ||
public bool IsBinaryFile_ReturnsExpected(string filename) | ||
{ | ||
return StlFile.IsBinaryFile(filename); | ||
} | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace GenericStl | ||
{ | ||
public static class StlFile | ||
{ | ||
public static bool IsBinaryFile(string filename) | ||
{ | ||
if (!File.Exists(filename)) | ||
{ | ||
throw new FileNotFoundException("filename could not be found.", filename); | ||
} | ||
|
||
using (var fs = File.OpenRead(filename)) | ||
{ | ||
return IsBinary(fs); | ||
} | ||
} | ||
|
||
public static bool IsBinary(Stream stream) | ||
{ | ||
try | ||
{ | ||
using(var r = new BinaryReader(stream, Encoding.UTF8, true)) | ||
{ | ||
var firstChars = new string(r.ReadChars(5)); | ||
|
||
if (!string.Equals(firstChars, "solid", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return true; | ||
} | ||
|
||
var numberOfCharsToReadAtEnd = "endsolid".Length + 300; | ||
numberOfCharsToReadAtEnd = numberOfCharsToReadAtEnd > stream.Length ? (int)stream.Length : numberOfCharsToReadAtEnd; | ||
|
||
stream.Seek(-numberOfCharsToReadAtEnd , SeekOrigin.End); | ||
|
||
var lastChars = new string(r.ReadChars(numberOfCharsToReadAtEnd)); | ||
|
||
return !lastChars.Contains("endsolid"); | ||
} | ||
} | ||
finally | ||
{ | ||
stream.Seek(0, SeekOrigin.Begin); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters