Skip to content

Commit a2177fc

Browse files
committed
Turned the primes document into an embedded resource.
1 parent 1cc477e commit a2177fc

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

DataStructures/Common/PrimesList.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.IO;
55
using System.Reflection;
6+
using System.Text;
67

78
namespace DataStructures.Common
89
{
@@ -22,7 +23,6 @@ public sealed class PrimesList
2223

2324
//
2425
// INSTANCE VARIABLES
25-
private static string _primesDocPath = string.Empty;
2626
private readonly static List<int> _primes = new List<int>();
2727

2828
// Picked the HashPrime to be (101) because it is prime, and if the ‘hashSize - 1’ is not a multiple of this HashPrime, which is
@@ -64,10 +64,9 @@ public static PrimesList Instance
6464
/// </summary>
6565
private static void _initializeData()
6666
{
67-
_primesDocPath = Path.Combine(Path.GetDirectoryName(typeof(PrimesList).GetTypeInfo().Assembly.Location), @"Data/PrimesDocument_10K.csv");
68-
string[] lines = File.ReadAllLines(_primesDocPath);
67+
string[] lines = _readResource("DataStructures.Data.PrimesDocument_10K.csv");
6968

70-
foreach (var line in lines)
69+
foreach (var line in lines)
7170
{
7271
// Split the line by commas and convert the collection to a list.
7372
var numbersAsStrings = line.Split(',').ToList<string>();
@@ -209,6 +208,24 @@ public void CopyTo(int[] array, int index = 0)
209208
}
210209
}
211210

212-
}
211+
/// <summary>
212+
/// Reads an embedded resource as a text file.
213+
/// </summary>
214+
/// <returns></returns>
215+
private static string[] _readResource(string resourceName)
216+
{
217+
try
218+
{
219+
using (var stream = typeof(PrimesList).GetTypeInfo().Assembly.GetManifestResourceStream(resourceName))
220+
using (var reader = new StreamReader(stream ?? throw new InvalidOperationException("Failed to read resource"), Encoding.UTF8))
221+
return reader.ReadToEnd().Split("\r\n");
222+
}
223+
catch (Exception ex)
224+
{
225+
throw new Exception($"Failed to read resource {resourceName}", ex);
226+
}
227+
}
228+
229+
}
213230

214231
}

DataStructures/DataStructures.csproj

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,8 @@
44
</PropertyGroup>
55

66
<ItemGroup>
7-
<Content Include="Data\PrimesDocument_10K.csv">
8-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
9-
</Content>
10-
</ItemGroup>
11-
12-
<ItemGroup>
13-
<None Update="Data\PrimesDocument_10K.csv">
14-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
15-
</None>
7+
<EmbeddedResource Include="Data\PrimesDocument_10K.csv">
8+
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
9+
</EmbeddedResource>
1610
</ItemGroup>
1711
</Project>

0 commit comments

Comments
 (0)