diff --git a/src/Parameters/Shared/SharedParameterFile.CSV.cs b/src/Parameters/Shared/SharedParameterFile.CSV.cs
index 4809d58..11e516e 100644
--- a/src/Parameters/Shared/SharedParameterFile.CSV.cs
+++ b/src/Parameters/Shared/SharedParameterFile.CSV.cs
@@ -44,12 +44,11 @@ static SharedParameterFile()
};
#if !NET45
- // Allow the usage of ANSI encoding other than the default one
+ // Allow the usage of ANSI encoding other than the default one
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
#endif
}
-
///
/// Initializes a new instance of the class.
///
@@ -71,6 +70,9 @@ public SharedParameterFile(string sharedParameterFile, Encoding encoding = null)
sharedParameterFile = File.ReadAllText(sharedParameterFile, Encoding);
}
+ if (!SectionRegex.IsMatch(sharedParameterFile))
+ throw new InvalidDataException("Please make sure you supplied a correct file path or file content string.");
+
var sharedParamsFileLines = SectionRegex
.Split(sharedParameterFile)
.Where(line => !line.StartsWith("#")) // Exclude comment lines
@@ -92,83 +94,75 @@ public SharedParameterFile(string sharedParameterFile, Encoding encoding = null)
foreach (var section in sharedParamsFileSections)
{
- using (var stringReader = new StringReader(section.Value))
+ using var stringReader = new StringReader(section.Value);
+ using var csvReader = new CsvReader(stringReader, CsvConfiguration);
+ csvReader.Configuration.TrimOptions = TrimOptions.Trim;
+ csvReader.Configuration.BadDataFound = BadDataFound;
+
+ // TODO implement
+ // csvReader.Configuration.AllowComments = true;
+ // csvReader.Configuration.Comment = '#';
+
+ var originalHeaderValidated = csvReader.Configuration.HeaderValidated;
+ csvReader.Configuration.HeaderValidated = (isValid, headerNames, headerIndex, context) =>
{
- using (var csvReader = new CsvReader(stringReader, CsvConfiguration))
- {
- csvReader.Configuration.TrimOptions = TrimOptions.Trim;
- csvReader.Configuration.BadDataFound = BadDataFound;
-
- // TODO implement
- // csvReader.Configuration.AllowComments = true;
- // csvReader.Configuration.Comment = '#';
-
- var originalHeaderValidated = csvReader.Configuration.HeaderValidated;
- csvReader.Configuration.HeaderValidated = (isValid, headerNames, headerIndex, context) =>
- {
- // Everything is OK, just go out
- if (isValid)
- return;
-
- // Allow DESCRIPTION header to be missing (it's actually missing in older shared parameter files)
- if (nameof(ParameterDefinition.Description).Equals(headerNames?.FirstOrDefault(),
- StringComparison.OrdinalIgnoreCase))
- return;
-
- // Allow USERMODIFIABLE header to be missing (it's actually missing in older shared parameter files)
- if (nameof(ParameterDefinition.UserModifiable).Equals(headerNames?.FirstOrDefault(),
- StringComparison.OrdinalIgnoreCase))
- return;
-
- originalHeaderValidated(false, headerNames, headerIndex, context);
- };
-
- var originalMissingFieldFound = csvReader.Configuration.MissingFieldFound;
- csvReader.Configuration.MissingFieldFound = (headerNames, index, context) =>
- {
- // Allow DESCRIPTION header to be missing (it's actually missing in older shared parameter files)
- if (nameof(ParameterDefinition.Description).Equals(headerNames?.FirstOrDefault(),
- StringComparison.OrdinalIgnoreCase))
- return;
-
- // Allow USERMODIFIABLE header to be missing (it's actually missing in older shared parameter files)
- if (nameof(ParameterDefinition.UserModifiable).Equals(headerNames?.FirstOrDefault(),
- StringComparison.OrdinalIgnoreCase))
- return;
-
- originalMissingFieldFound(headerNames, index, context);
- };
-
- switch (section.Key)
- {
- // Parse *META section
- case Sections.META:
- csvReader.Configuration.RegisterClassMap();
- Metadata = csvReader.GetRecords().FirstOrDefault();
- break;
-
- // Parse *GROUP section
- case Sections.GROUPS:
- csvReader.Configuration.RegisterClassMap();
- _groups = csvReader.GetRecords().ToList();
- break;
-
- // Parse *PARAM section
- case Sections.PARAMS:
- csvReader.Configuration.RegisterClassMap();
- Parameters = new ParameterCollection(this, csvReader.GetRecords().ToList());
- break;
-
- default:
- Debug.WriteLine($"Unknown section type: {section.Key}");
- continue;
- }
- }
+ // Everything is OK, just go out
+ if (isValid)
+ return;
+
+ // Allow DESCRIPTION header to be missing (it's actually missing in older shared parameter files)
+ if (nameof(ParameterDefinition.Description).Equals(headerNames?.FirstOrDefault(), StringComparison.OrdinalIgnoreCase))
+ return;
+
+ // Allow USERMODIFIABLE header to be missing (it's actually missing in older shared parameter files)
+ if (nameof(ParameterDefinition.UserModifiable).Equals(headerNames?.FirstOrDefault(), StringComparison.OrdinalIgnoreCase))
+ return;
+
+ originalHeaderValidated(false, headerNames, headerIndex, context);
+ };
+
+ var originalMissingFieldFound = csvReader.Configuration.MissingFieldFound;
+ csvReader.Configuration.MissingFieldFound = (headerNames, index, context) =>
+ {
+ // Allow DESCRIPTION header to be missing (it's actually missing in older shared parameter files)
+ if (nameof(ParameterDefinition.Description).Equals(headerNames?.FirstOrDefault(), StringComparison.OrdinalIgnoreCase))
+ return;
+
+ // Allow USERMODIFIABLE header to be missing (it's actually missing in older shared parameter files)
+ if (nameof(ParameterDefinition.UserModifiable).Equals(headerNames?.FirstOrDefault(), StringComparison.OrdinalIgnoreCase))
+ return;
+
+ originalMissingFieldFound(headerNames, index, context);
+ };
+
+ switch (section.Key)
+ {
+ // Parse *META section
+ case Sections.META:
+ csvReader.Configuration.RegisterClassMap();
+ Metadata = csvReader.GetRecords().FirstOrDefault();
+ break;
+
+ // Parse *GROUP section
+ case Sections.GROUPS:
+ csvReader.Configuration.RegisterClassMap();
+ _groups = csvReader.GetRecords().ToList();
+ break;
+
+ // Parse *PARAM section
+ case Sections.PARAMS:
+ csvReader.Configuration.RegisterClassMap();
+ Parameters = new ParameterCollection(this, csvReader.GetRecords().ToList());
+ break;
+
+ default:
+ Debug.WriteLine($"Unknown section type: {section.Key}");
+ continue;
}
}
- // Post-process parameters by assigning group names using group IDs
- // and recover UnitType from ParameterType
+ // Post-process parameters by assigning group names using group IDs
+ // and recover UnitType from ParameterType
Parameters = new ParameterCollection(
this, // Parent shared parameter file
Parameters
diff --git a/tests/SharedParameterFileTests.cs b/tests/SharedParameterFileTests.cs
index 991b1c1..8e1df06 100644
--- a/tests/SharedParameterFileTests.cs
+++ b/tests/SharedParameterFileTests.cs
@@ -220,12 +220,12 @@ public void FileIsSerializedProperly()
var simpleSharedFromDisk = File
.ReadAllText(SharedParameterFiles.FirstOrDefault(f => f.EndsWith(@"SimpleShared_1.txt", StringComparison.InvariantCulture)));
- var simpleSharedFromBuilt = new SharedParameterFile(new Dictionary { { "Identity Data", 100 } } );
+ var simpleSharedFromBuilt = new SharedParameterFile(new Dictionary { { "Identity Data", 100 } });
#region Identity Data
simpleSharedFromBuilt.Parameters.Add(
- new Guid("61ff3d56-09d7-4049-8c78-4abe745e4e5a"),"EquipmentName",
+ new Guid("61ff3d56-09d7-4049-8c78-4abe745e4e5a"), "EquipmentName",
"Identity Data", // Passing group by name
ParameterType.Text
);