Change Supported to Enum

This commit is contained in:
Matt Nadareski
2020-08-22 13:31:13 -07:00
parent b30173ba55
commit d4be402380
9 changed files with 196 additions and 68 deletions

View File

@@ -640,7 +640,7 @@ namespace SabreTools.Library.DatFiles
#region SoftwareList #region SoftwareList
case "supported": case "supported":
machine.Supported = jtr.ReadAsString().AsYesNo(); machine.Supported = jtr.ReadAsString().AsSupported();
break; break;
case "sharedfeat": case "sharedfeat":
@@ -663,7 +663,7 @@ namespace SabreTools.Library.DatFiles
break; break;
case "dipswitches": case "dipswitches":
machine.DipSwitches = new List<SoftwareListDipSwitch>(); machine.DipSwitches = new List<ListXMLDipSwitch>();
jtr.Read(); // Start Array jtr.Read(); // Start Array
while (!sr.EndOfStream) while (!sr.EndOfStream)
{ {
@@ -678,7 +678,7 @@ namespace SabreTools.Library.DatFiles
jtr.Read(); // Mask Key jtr.Read(); // Mask Key
string mask = jtr.ReadAsString(); string mask = jtr.ReadAsString();
var dip = new SoftwareListDipSwitch(name, tag, mask); var dip = new ListXMLDipSwitch(name, tag, mask);
jtr.Read(); // Start dipvalues object jtr.Read(); // Start dipvalues object
while (!sr.EndOfStream) while (!sr.EndOfStream)
@@ -695,7 +695,7 @@ namespace SabreTools.Library.DatFiles
bool? def = jtr.ReadAsString().AsYesNo(); bool? def = jtr.ReadAsString().AsYesNo();
jtr.Read(); // End object jtr.Read(); // End object
dip.Values.Add(new SoftwareListDipValue(valname, value, def)); dip.Values.Add(new ListXMLDipValue(valname, value, def));
} }
jtr.Read(); // End object jtr.Read(); // End object
@@ -1843,17 +1843,22 @@ namespace SabreTools.Library.DatFiles
#region SoftwareList #region SoftwareList
if (!Header.ExcludeFields.Contains(Field.Supported) && datItem.Machine.Supported != null) if (!Header.ExcludeFields.Contains(Field.Supported) && datItem.Machine.Supported != Supported.NULL)
{ {
if (datItem.Machine.Supported == true) switch (datItem.Machine.Supported)
{ {
jtw.WritePropertyName("supported"); case Supported.No:
jtw.WriteValue("yes"); jtw.WritePropertyName("supported");
} jtw.WriteValue("yes");
else if (datItem.Machine.Supported == false) break;
{ case Supported.Partial:
jtw.WritePropertyName("supported"); jtw.WritePropertyName("supported");
jtw.WriteValue("no"); jtw.WriteValue("partial");
break;
case Supported.Yes:
jtw.WritePropertyName("supported");
jtw.WriteValue("no");
break;
} }
} }
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SharedFeatures, Header.ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SharedFeatures, Header.ExcludeFields)))
@@ -1885,7 +1890,7 @@ namespace SabreTools.Library.DatFiles
jtw.WriteValue(dip.Mask); jtw.WriteValue(dip.Mask);
jtw.WriteStartArray(); jtw.WriteStartArray();
foreach (SoftwareListDipValue dipval in dip.Values) foreach (ListXMLDipValue dipval in dip.Values)
{ {
jtw.WriteStartObject(); jtw.WriteStartObject();
jtw.WritePropertyName("name"); jtw.WritePropertyName("name");

View File

@@ -386,23 +386,23 @@ namespace SabreTools.Library.DatFiles
break; break;
case "dipswitch": case "dipswitch":
// TODO: Make a new object for this // TODO: Use these dipswitches
// string dipswitch_name = reader.GetAttribute("name"); var dipSwitch = new ListXMLDipSwitch(reader.GetAttribute("name"), reader.GetAttribute("tag"), reader.GetAttribute("mask"));
// string dipswitch_tag = reader.GetAttribute("tag"); ReadDipSwitch(reader.ReadSubtree(), dipSwitch);
// string dipswitch_mask = reader.GetAttribute("mask");
// // While the subtree contains <diplocation> elements... // // While the subtree contains <diplocation> elements...
// TODO: Make a new object for this // TODO: Populate the dipswitch with the diplocations
// string diplocation_name = reader.GetAttribute("name"); // string diplocation_name = reader.GetAttribute("name");
// string diplocation_number = reader.GetAttribute("number"); // string diplocation_number = reader.GetAttribute("number");
// bool? diplocation_inverted = Utilities.GetYesNo(reader.GetAttribute("inverted")); // bool? diplocation_inverted = Utilities.GetYesNo(reader.GetAttribute("inverted"));
// // While the subtree contains <dipvalue> elements... // // While the subtree contains <dipvalue> elements...
// TODO: Make a new object for this // TODO: Populate the dipswitch with the dipvalues
// string dipvalue_name = reader.GetAttribute("name"); // string dipvalue_name = reader.GetAttribute("name");
// string dipvalue_value = reader.GetAttribute("value"); // string dipvalue_value = reader.GetAttribute("value");
// bool? dipvalue_default = Utilities.GetYesNo(reader.GetAttribute("default")); // bool? dipvalue_default = Utilities.GetYesNo(reader.GetAttribute("default"));
// Skip the dipswitch now that we've processed it
reader.Skip(); reader.Skip();
break; break;
@@ -584,6 +584,59 @@ namespace SabreTools.Library.DatFiles
} }
} }
/// <summary>
/// Read DipSwitch DipValues information
/// </summary>
/// <param name="reader">XmlReader representing a diskarea block</param>
/// <param name="dipSwitch">ListXMLDipSwitch to populate</param>
private void ReadDipSwitch(XmlReader reader, ListXMLDipSwitch dipSwitch)
{
// If we have an empty trurip, skip it
if (reader == null)
return;
// Get lists ready
dipSwitch.Locations = new List<ListXMLDipLocation>();
dipSwitch.Values = new List<ListXMLDipValue>();
// Otherwise, add what is possible
reader.MoveToContent();
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get the information from the dipswitch
switch (reader.Name)
{
case "diplocation":
dipSwitch.Locations.Add(new ListXMLDipLocation(
reader.GetAttribute("name"),
reader.GetAttribute("number"),
reader.GetAttribute("inverted").AsYesNo()));
reader.Read();
break;
case "dipvalue":
dipSwitch.Values.Add(new ListXMLDipValue(
reader.GetAttribute("name"),
reader.GetAttribute("value"),
reader.GetAttribute("default").AsYesNo()));
reader.Read();
break;
default:
reader.Read();
break;
}
}
}
/// <summary> /// <summary>
/// Create and open an output file for writing direct from a dictionary /// Create and open an output file for writing direct from a dictionary
/// </summary> /// </summary>

View File

@@ -483,7 +483,7 @@ namespace SabreTools.Library.DatFiles
#region SoftwareList #region SoftwareList
case "Machine.Supported": case "Machine.Supported":
machine.Supported = value.AsYesNo(); machine.Supported = value.AsSupported();
break; break;
case "Machine.SharedFeatures": case "Machine.SharedFeatures":
@@ -498,7 +498,7 @@ namespace SabreTools.Library.DatFiles
break; break;
case "Machine.DipSwitches": case "Machine.DipSwitches":
machine.DipSwitches = new List<SoftwareListDipSwitch>(); machine.DipSwitches = new List<ListXMLDipSwitch>();
// TODO: There is no way this would work... Just use empty for now // TODO: There is no way this would work... Just use empty for now
break; break;

View File

@@ -142,12 +142,12 @@ namespace SabreTools.Library.DatFiles
{ {
Name = reader.GetAttribute("name"), Name = reader.GetAttribute("name"),
Description = reader.GetAttribute("name"), Description = reader.GetAttribute("name"),
Supported = reader.GetAttribute("supported").AsYesNo(), // (yes|partial|no) "yes" Supported = reader.GetAttribute("supported").AsSupported(),
CloneOf = reader.GetAttribute("cloneof") ?? string.Empty, CloneOf = reader.GetAttribute("cloneof") ?? string.Empty,
Infos = new List<ListXmlInfo>(), Infos = new List<ListXmlInfo>(),
SharedFeatures = new List<SoftwareListSharedFeature>(), SharedFeatures = new List<SoftwareListSharedFeature>(),
DipSwitches = new List<SoftwareListDipSwitch>(), DipSwitches = new List<ListXMLDipSwitch>(),
MachineType = (machineType == MachineType.NULL ? MachineType.None : machineType), MachineType = (machineType == MachineType.NULL ? MachineType.None : machineType),
}; };
@@ -248,7 +248,7 @@ namespace SabreTools.Library.DatFiles
areaEndinaness; areaEndinaness;
long? areasize = null; long? areasize = null;
var features = new List<SoftwareListFeature>(); var features = new List<SoftwareListFeature>();
var dipswitches = new List<SoftwareListDipSwitch>(); var dipswitches = new List<ListXMLDipSwitch>();
bool containsItems = false; bool containsItems = false;
while (!reader.EOF) while (!reader.EOF)
@@ -334,13 +334,8 @@ namespace SabreTools.Library.DatFiles
case "dipswitch": case "dipswitch":
// TODO: Use these dipswitches // TODO: Use these dipswitches
var dip = new SoftwareListDipSwitch( var dipSwitch = new ListXMLDipSwitch(reader.GetAttribute("name"), reader.GetAttribute("tag"), reader.GetAttribute("mask"));
reader.GetAttribute("name"), ReadDipSwitch(reader.ReadSubtree(), dipSwitch);
reader.GetAttribute("tag"),
reader.GetAttribute("mask"));
dip.Values = ReadDipSwitch(reader.ReadSubtree());
dipswitches.Add(dip);
// Skip the dipswitch now that we've processed it // Skip the dipswitch now that we've processed it
reader.Skip(); reader.Skip();
@@ -568,15 +563,16 @@ namespace SabreTools.Library.DatFiles
/// <summary> /// <summary>
/// Read DipSwitch DipValues information /// Read DipSwitch DipValues information
/// </summary> /// </summary>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param> /// <param name="reader">XmlReader representing a diskarea block</param>
private List<SoftwareListDipValue> ReadDipSwitch(XmlReader reader) /// <param name="dipSwitch">ListXMLDipSwitch to populate</param>
private void ReadDipSwitch(XmlReader reader, ListXMLDipSwitch dipSwitch)
{ {
// If we have an empty trurip, skip it // If we have an empty trurip, skip it
if (reader == null) if (reader == null)
return null; return;
// Get list ready // Get list ready
List<SoftwareListDipValue> dipValues = new List<SoftwareListDipValue>(); dipSwitch.Values = new List<ListXMLDipValue>();
// Otherwise, add what is possible // Otherwise, add what is possible
reader.MoveToContent(); reader.MoveToContent();
@@ -594,10 +590,11 @@ namespace SabreTools.Library.DatFiles
switch (reader.Name) switch (reader.Name)
{ {
case "dipvalue": case "dipvalue":
dipValues.Add(new SoftwareListDipValue( dipSwitch.Values.Add(new ListXMLDipValue(
reader.GetAttribute("name"), reader.GetAttribute("name"),
reader.GetAttribute("value"), reader.GetAttribute("value"),
reader.GetAttribute("default").AsYesNo())); reader.GetAttribute("default").AsYesNo()));
reader.Read();
break; break;
default: default:
@@ -605,8 +602,6 @@ namespace SabreTools.Library.DatFiles
break; break;
} }
} }
return dipValues;
} }
/// <summary> /// <summary>
@@ -792,12 +787,18 @@ namespace SabreTools.Library.DatFiles
if (!Header.ExcludeFields.Contains(Field.Supported)) if (!Header.ExcludeFields.Contains(Field.Supported))
{ {
if (datItem.Machine.Supported == true) switch (datItem.Machine.Supported)
xtw.WriteAttributeString("supported", "yes"); {
else if (datItem.Machine.Supported == false) case Supported.No:
xtw.WriteAttributeString("supported", "no"); xtw.WriteAttributeString("supported", "no");
else break;
xtw.WriteAttributeString("supported", "partial"); case Supported.Partial:
xtw.WriteAttributeString("supported", "partial");
break;
case Supported.Yes:
xtw.WriteAttributeString("supported", "yes");
break;
}
} }
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Description, Header.ExcludeFields))) if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Description, Header.ExcludeFields)))
@@ -833,14 +834,14 @@ namespace SabreTools.Library.DatFiles
if (!Header.ExcludeFields.Contains(Field.DipSwitches) && datItem.Machine.DipSwitches != null && datItem.Machine.DipSwitches.Count > 0) if (!Header.ExcludeFields.Contains(Field.DipSwitches) && datItem.Machine.DipSwitches != null && datItem.Machine.DipSwitches.Count > 0)
{ {
foreach (SoftwareListDipSwitch dip in datItem.Machine.DipSwitches) foreach (ListXMLDipSwitch dip in datItem.Machine.DipSwitches)
{ {
xtw.WriteStartElement("dipswitch"); xtw.WriteStartElement("dipswitch");
xtw.WriteAttributeString("name", dip.Name); xtw.WriteAttributeString("name", dip.Name);
xtw.WriteAttributeString("tag", dip.Tag); xtw.WriteAttributeString("tag", dip.Tag);
xtw.WriteAttributeString("mask", dip.Mask); xtw.WriteAttributeString("mask", dip.Mask);
foreach (SoftwareListDipValue dipval in dip.Values) foreach (ListXMLDipValue dipval in dip.Values)
{ {
xtw.WriteStartElement("dipvalue"); xtw.WriteStartElement("dipvalue");
xtw.WriteAttributeString("name", dipval.Name); xtw.WriteAttributeString("name", dipval.Name);

View File

@@ -45,37 +45,58 @@ namespace SabreTools.Library.DatItems
#endregion #endregion
#region SoftwareList #region ListXML
/// <summary> /// <summary>
/// Represents one SoftwareList dipswitch /// Represents one ListXML dipswitch
/// </summary> /// </summary>
public class SoftwareListDipSwitch /// <remarks>Also used by SoftwareList</remarks>
public class ListXMLDipSwitch
{ {
public string Name { get; set; } public string Name { get; set; }
public string Tag { get; set; } public string Tag { get; set; }
public string Mask { get; set; } public string Mask { get; set; }
public List<SoftwareListDipValue> Values { get; set; } public List<ListXMLDipLocation> Locations { get; set; }
public List<ListXMLDipValue> Values { get; set; }
public SoftwareListDipSwitch(string name, string tag, string mask) public ListXMLDipSwitch(string name, string tag, string mask)
{ {
Name = name; Name = name;
Tag = tag; Tag = tag;
Mask = mask; Mask = mask;
Values = new List<SoftwareListDipValue>(); Locations = new List<ListXMLDipLocation>();
Values = new List<ListXMLDipValue>();
} }
} }
/// <summary> /// <summary>
/// Represents one SoftwareList dipswitch /// Represents one ListXML diplocation
/// </summary> /// </summary>
public class SoftwareListDipValue public class ListXMLDipLocation
{
public string Name { get; set; }
public string Number { get; set; }
public bool? Inverted { get; set; }
public ListXMLDipLocation(string name, string number, bool? inverted)
{
Name = name;
Number = number;
Inverted = inverted;
}
}
/// <summary>
/// Represents one ListXML dipvalue
/// </summary>
/// <remarks>Also used by SoftwareList</remarks>
public class ListXMLDipValue
{ {
public string Name { get; set; } public string Name { get; set; }
public string Value { get; set; } public string Value { get; set; }
public bool? Default { get; set; } public bool? Default { get; set; }
public SoftwareListDipValue(string name, string value, bool? def) public ListXMLDipValue(string name, string value, bool? def)
{ {
Name = name; Name = name;
Value = value; Value = value;
@@ -83,6 +104,10 @@ namespace SabreTools.Library.DatItems
} }
} }
#endregion
#region SoftwareList
/// <summary> /// <summary>
/// Represents one SoftwareList shared feature object /// Represents one SoftwareList shared feature object
/// </summary> /// </summary>

View File

@@ -240,4 +240,16 @@ namespace SabreTools.Library.DatItems
Device = 1 << 2, Device = 1 << 2,
Mechanical = 1 << 3, Mechanical = 1 << 3,
} }
/// <summary>
/// Determine machine support status
/// </summary>
[Flags]
public enum Supported
{
NULL,
No,
Partial,
Yes,
}
} }

View File

@@ -272,9 +272,8 @@ namespace SabreTools.Library.DatItems
/// <summary> /// <summary>
/// Support status /// Support status
/// </summary> /// </summary>
/// <remarks>yes = true, partial = null, no = false</remarks>
[JsonProperty("supported")] [JsonProperty("supported")]
public bool? Supported { get; set; } = true; public Supported Supported { get; set; } = Supported.NULL;
/// <summary> /// <summary>
/// List of shared feature items /// List of shared feature items
@@ -287,7 +286,7 @@ namespace SabreTools.Library.DatItems
/// </summary> /// </summary>
/// <remarks>Also in SoftwareList</remarks> /// <remarks>Also in SoftwareList</remarks>
[JsonProperty("dipswitches")] [JsonProperty("dipswitches")]
public List<SoftwareListDipSwitch> DipSwitches { get; set; } = null; public List<ListXMLDipSwitch> DipSwitches { get; set; } = null;
#endregion #endregion
@@ -451,7 +450,7 @@ namespace SabreTools.Library.DatItems
#region SoftwareList #region SoftwareList
case Field.Supported: case Field.Supported:
fieldValue = Supported?.ToString(); fieldValue = Supported.ToString();
break; break;
case Field.SharedFeatures: case Field.SharedFeatures:
fieldValue = string.Join(";", (SharedFeatures ?? new List<SoftwareListSharedFeature>()).Select(i => $"{i.Name}={i.Value}")); fieldValue = string.Join(";", (SharedFeatures ?? new List<SoftwareListSharedFeature>()).Select(i => $"{i.Name}={i.Value}"));
@@ -640,7 +639,7 @@ namespace SabreTools.Library.DatItems
#region SoftwareList #region SoftwareList
if (mappings.Keys.Contains(Field.Supported)) if (mappings.Keys.Contains(Field.Supported))
Supported = mappings[Field.Supported].AsYesNo(); Supported = mappings[Field.Supported].AsSupported();
if (mappings.Keys.Contains(Field.SharedFeatures)) if (mappings.Keys.Contains(Field.SharedFeatures))
{ {
@@ -658,7 +657,7 @@ namespace SabreTools.Library.DatItems
if (mappings.Keys.Contains(Field.DipSwitches)) if (mappings.Keys.Contains(Field.DipSwitches))
{ {
if (DipSwitches == null) if (DipSwitches == null)
DipSwitches = new List<SoftwareListDipSwitch>(); DipSwitches = new List<ListXMLDipSwitch>();
// TODO: There's no way this will work... just create the new list for now // TODO: There's no way this will work... just create the new list for now
} }
@@ -1058,7 +1057,9 @@ namespace SabreTools.Library.DatItems
#region SoftwareList #region SoftwareList
// Filter on supported // Filter on supported
if (filter.Supported.MatchesNeutral(null, Supported) == false) if (filter.SupportedStatus.MatchesPositive(Supported.NULL, Supported) == false)
return false;
if (filter.SupportedStatus.MatchesNegative(Supported.NULL, Supported) == true)
return false; return false;
#endregion #endregion
@@ -1210,7 +1211,7 @@ namespace SabreTools.Library.DatItems
#region SoftwareList #region SoftwareList
if (fields.Contains(Field.Supported)) if (fields.Contains(Field.Supported))
Supported = null; Supported = Supported.NULL;
if (fields.Contains(Field.SharedFeatures)) if (fields.Contains(Field.SharedFeatures))
SharedFeatures = null; SharedFeatures = null;

View File

@@ -229,7 +229,7 @@ namespace SabreTools.Library.Filtering
/// <summary> /// <summary>
/// Include or exclude items with the "Supported" tag /// Include or exclude items with the "Supported" tag
/// </summary> /// </summary>
public FilterItem<bool?> Supported { get; private set; } = new FilterItem<bool?>() { Neutral = null }; public FilterItem<Supported> SupportedStatus { get; private set; } = new FilterItem<Supported>() { Positive = Supported.NULL, Negative = Supported.NULL };
// TODO: Machine.SharedFeatures - List<SoftwareListSharedFeature> // TODO: Machine.SharedFeatures - List<SoftwareListSharedFeature>
@@ -838,10 +838,10 @@ namespace SabreTools.Library.Filtering
#region SoftwareList #region SoftwareList
case Field.Supported: case Field.Supported:
if (negate || value.Equals("false", StringComparison.OrdinalIgnoreCase)) if (negate)
Supported.Neutral = false; SupportedStatus.Negative |= value.AsSupported();
else else
Supported.Neutral = true; SupportedStatus.Positive |= value.AsSupported();
break; break;
#endregion #endregion

View File

@@ -819,6 +819,37 @@ namespace SabreTools.Library.Tools
#endif #endif
} }
/// <summary>
/// Get Supported value from input string
/// </summary>
/// <param name="supported">String to get value from</param>
/// <returns>Supported value corresponding to the string</returns>
public static Supported AsSupported(this string supported)
{
#if NET_FRAMEWORK
switch (supported?.ToLowerInvariant())
{
case "no":
return Supported.No;
case "partial":
return Supported.Partial;
case "yes":
return Supported.Yes;
default:
return Supported.NULL;
}
#else
return supported?.ToLowerInvariant() switch
{
"no" => Supported.No,
"partial" => Supported.Partial,
"yes" => Supported.Yes,
_ => Supported.NULL,
};
#endif
}
/// <summary> /// <summary>
/// Get bool? value from input string /// Get bool? value from input string
/// </summary> /// </summary>