Skip to content
Merged
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
15 changes: 14 additions & 1 deletion Rapr/CSVExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public class CsvExporter : IExport
{
private const string CsvQuote = "\"";

// Spreadsheet apps treat a cell starting with one of these as a formula. Provider and
// device names come from third-party INF files, so they shouldn't be able to become one.
private static readonly char[] FormulaPrefixes = { '=', '+', '-', '@', '\t', '\r' };

public string Export(List<DriverStoreEntry> driverStoreEntries)
{
if (driverStoreEntries == null)
Expand All @@ -36,7 +40,9 @@ public string Export(List<DriverStoreEntry> driverStoreEntries)
string csvFileName = saveFileDialog.FileName;

using (FileStream fileStream = new FileStream(csvFileName, FileMode.Create))
using (StreamWriter fileWriter = new StreamWriter(fileStream, new UTF8Encoding(false)))
// Excel only reads the file as UTF-8 when it starts with a BOM; without it,
// non-ASCII provider and device names are garbled.
using (StreamWriter fileWriter = new StreamWriter(fileStream, new UTF8Encoding(true)))
{
// Write the header once
string headerLine = string.Join(csvDelimiter, Sanitize(DriverStoreEntry.GetFieldNames(), csvDelimiter));
Expand Down Expand Up @@ -67,6 +73,13 @@ private static string[] Sanitize(string[] values, string csvDelimiter)
continue;
}

if (values[i].Length > 0
&& Array.IndexOf(FormulaPrefixes, values[i][0]) >= 0
&& !double.TryParse(values[i], NumberStyles.Float, CultureInfo.CurrentCulture, out _))
{
values[i] = "'" + values[i];
}

if (values[i].Contains(csvDelimiter)
|| values[i].Contains(CsvQuote)
|| values[i].Contains("\r")
Expand Down
6 changes: 6 additions & 0 deletions Rapr/Utils/DriverStoreEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ public int? OemId
}
}

// Driver Files is not exported: a single GPU driver package can list over 10,000 files,
// far more than an Excel cell holds (32,767 characters).
public static string[] GetFieldNames()
{
return new[] {
Expand All @@ -270,6 +272,8 @@ public static string[] GetFieldNames()
"Device Name",
"Device Present",
"Install Date",
"Boot Critical",
"Extension Id",
};
}

Expand All @@ -290,6 +294,8 @@ public string[] GetFieldValues()
this.DeviceName ?? string.Empty,
this.DevicePresent?.ToString() ?? string.Empty,
this.InstallDate?.ToString("d") ?? string.Empty,
this.BootCritical?.ToString() ?? string.Empty,
this.DriverExtensionId == Guid.Empty ? string.Empty : this.DriverExtensionId.ToString(),
};
}

Expand Down
12 changes: 12 additions & 0 deletions RaprTests/CsvExporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ public void ExportedFieldsRoundTripAsOneRecord(string delimiter, string value)
}
}

[TestMethod]
[DataRow("=cmd|' /C calc'!A0", "'=cmd|' /C calc'!A0")]
[DataRow("+Provider", "'+Provider")]
[DataRow("@SUM(1,1)", "'@SUM(1,1)")]
[DataRow("-Provider", "'-Provider")]
[DataRow("-1", "-1")]
[DataRow("Contoso", "Contoso")]
public void ValuesThatLookLikeFormulasAreEscaped(string value, string expected)
{
Assert.AreEqual(expected, Sanitize(new[] { value }, ";")[0]);
}

private static string NormalizeNewlines(string value)
{
return value.Replace("\r\n", "\n").Replace('\r', '\n');
Expand Down
24 changes: 24 additions & 0 deletions RaprTests/DriverStoreEntryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@ namespace Rapr.Tests
[TestClass]
public class DriverStoreEntryTests
{
[TestMethod]
public void ExportedValuesLineUpWithTheHeader()
{
var extensionId = new Guid("1d1b2b1c-7f6e-4d8a-9c3f-1a2b3c4d5e6f");
var entry = new DriverStoreEntry
{
BootCritical = true,
DriverExtensionId = extensionId,
};

string[] names = DriverStoreEntry.GetFieldNames();
string[] values = entry.GetFieldValues();

Assert.AreEqual(names.Length, values.Length);
CollectionAssert.DoesNotContain(names, "Driver Files");
Assert.AreEqual(bool.TrueString, values[Array.IndexOf(names, "Boot Critical")]);
Assert.AreEqual(extensionId.ToString(), values[Array.IndexOf(names, "Extension Id")]);

// Not every backend fills these in.
values = new DriverStoreEntry().GetFieldValues();
Assert.AreEqual(string.Empty, values[Array.IndexOf(names, "Boot Critical")]);
Assert.AreEqual(string.Empty, values[Array.IndexOf(names, "Extension Id")]);
}

[TestMethod]
public void InstallDateIsTruncatedToTheDayItDisplays()
{
Expand Down
Loading