Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#47 Fixed enum assignment by index #48

Closed
wants to merge 2 commits into from
Closed
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
17 changes: 17 additions & 0 deletions DbcParserLib.Tests/PropertiesLineParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ public void MsgCustomPropertyIsParsedTest()
Assert.AreEqual(100, dbc.Messages.First().CycleTime);
}

[Test]
public void MsgCustomPropertyEnumIndexedValueIsParsedTest()
{
var builder = new DbcBuilder(new SilentFailureObserver());
var message = new Message { ID = 2394947585 };
builder.AddMessage(message);

var customPropertyLineParsers = CreateParser();
var nextLineProvider = new NextLineProvider(new StringReader(string.Empty));
Assert.IsTrue(ParseLine(@"BA_DEF_ BO_ ""AttributeName"" ENUM ""Val1"",""Val2"",""Val3"";", customPropertyLineParsers, builder, nextLineProvider));
Assert.IsTrue(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" ""Val1"";", customPropertyLineParsers, builder, nextLineProvider));
Assert.IsTrue(ParseLine(@"BA_ ""AttributeName"" BO_ 2394947585 1;", customPropertyLineParsers, builder, nextLineProvider));

var dbc = builder.Build();
Assert.AreEqual("Val2", dbc.Messages.First().CustomProperties["AttributeName"].EnumCustomProperty.Value);
}

[Test]
public void SigCustomPropertyIsParsedTest()
{
Expand Down
10 changes: 5 additions & 5 deletions DbcParserLib/Model/CustomProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@ public void SetCustomPropertyValue(string value)
case CustomPropertyDataType.Integer:
IntegerCustomProperty = new CustomPropertyValue<int>()
{
Value = int.Parse(value, CultureInfo.InvariantCulture)
Value = int.Parse(value.Replace("\"", ""), CultureInfo.InvariantCulture)
};
break;
case CustomPropertyDataType.Hex:
HexCustomProperty = new CustomPropertyValue<int>()
{
Value = int.Parse(value, CultureInfo.InvariantCulture)
Value = int.Parse(value.Replace("\"", ""), CultureInfo.InvariantCulture)
};
break;
case CustomPropertyDataType.Float:
FloatCustomProperty = new CustomPropertyValue<double>()
{
Value = float.Parse(value, CultureInfo.InvariantCulture)
Value = float.Parse(value.Replace("\"", ""), CultureInfo.InvariantCulture)
};
break;
case CustomPropertyDataType.String:
StringCustomProperty = new CustomPropertyValue<string>()
{
Value = value
Value = value.Replace("\"", "")
};
break;
case CustomPropertyDataType.Enum:
EnumCustomProperty = new CustomPropertyValue<string>()
{
Value = value
Value = CustomPropertyDefinition.TryGetEnumValue(value, out var enumValue) ? enumValue : value.Replace("\"", "")
};
break;
}
Expand Down
31 changes: 26 additions & 5 deletions DbcParserLib/Model/CustomPropertyDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,43 @@ public void SetCustomPropertyDefaultValue(string value)
switch (DataType)
{
case CustomPropertyDataType.Integer:
IntegerCustomProperty.Default = int.Parse(value, CultureInfo.InvariantCulture);
IntegerCustomProperty.Default = int.Parse(value.Replace("\"", ""), CultureInfo.InvariantCulture);
break;
case CustomPropertyDataType.Hex:
HexCustomProperty.Default = int.Parse(value, CultureInfo.InvariantCulture);
HexCustomProperty.Default = int.Parse(value.Replace("\"", ""), CultureInfo.InvariantCulture);
break;
case CustomPropertyDataType.Float:
FloatCustomProperty.Default = float.Parse(value, CultureInfo.InvariantCulture);
FloatCustomProperty.Default = float.Parse(value.Replace("\"", ""), CultureInfo.InvariantCulture);
break;
case CustomPropertyDataType.String:
StringCustomProperty.Default = value;
StringCustomProperty.Default = value.Replace("\"", "");
break;
case CustomPropertyDataType.Enum:
EnumCustomProperty.Default = value;
EnumCustomProperty.Default = TryGetEnumValue(value, out var enumValue) ? enumValue : value.Replace("\"", "");
break;
}
}

internal bool TryGetEnumValue(string value, out string enumValue)
{
enumValue = null;

if (value.Contains("\""))
return false;

if (EnumCustomProperty == null)
return false;

if (!int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var index))
return false;

if (index < 0 || index >= EnumCustomProperty.Values.Length)
return false;

enumValue = EnumCustomProperty.Values[index];

return true;
}
}

public class NumericCustomPropertyDefinition<T>
Expand Down
2 changes: 1 addition & 1 deletion DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin
{
var match = Regex.Match(cleanLine, PropertyDefinitionDefaultParsingRegex);
if (match.Success)
builder.AddCustomPropertyDefaultValue(match.Groups[1].Value, match.Groups[2].Value.Replace("\"", ""));
builder.AddCustomPropertyDefaultValue(match.Groups[1].Value, match.Groups[2].Value);
else
m_observer.PropertyDefaultSyntaxError();
return true;
Expand Down
8 changes: 4 additions & 4 deletions DbcParserLib/Parsers/PropertiesLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin
if (match.Success)
{
if (match.Groups[2].Value == "BU_")
builder.AddNodeCustomProperty(match.Groups[1].Value, match.Groups[3].Value, match.Groups[9].Value.Replace("\"", ""));
builder.AddNodeCustomProperty(match.Groups[1].Value, match.Groups[3].Value, match.Groups[9].Value);
else if (match.Groups[2].Value == "EV_")
builder.AddEnvironmentVariableCustomProperty(match.Groups[1].Value, match.Groups[3].Value, match.Groups[9].Value.Replace("\"", ""));
builder.AddEnvironmentVariableCustomProperty(match.Groups[1].Value, match.Groups[3].Value, match.Groups[9].Value);
else if (match.Groups[4].Value == "BO_")
{
builder.AddMessageCustomProperty(match.Groups[1].Value, uint.Parse(match.Groups[5].Value, CultureInfo.InvariantCulture), match.Groups[9].Value.Replace("\"", ""));
builder.AddMessageCustomProperty(match.Groups[1].Value, uint.Parse(match.Groups[5].Value, CultureInfo.InvariantCulture), match.Groups[9].Value);
if (match.Groups[1].Value == "GenMsgCycleTime")
builder.AddMessageCycleTime(uint.Parse(match.Groups[5].Value, CultureInfo.InvariantCulture), int.Parse(match.Groups[9].Value, CultureInfo.InvariantCulture));
}
else if (match.Groups[6].Value == "SG_")
{
builder.AddSignalCustomProperty(match.Groups[1].Value, uint.Parse(match.Groups[7].Value, CultureInfo.InvariantCulture), match.Groups[8].Value, match.Groups[9].Value.Replace("\"", ""));
builder.AddSignalCustomProperty(match.Groups[1].Value, uint.Parse(match.Groups[7].Value, CultureInfo.InvariantCulture), match.Groups[8].Value, match.Groups[9].Value);
if (match.Groups[1].Value == "GenSigStartValue")
builder.AddSignalInitialValue(uint.Parse(match.Groups[7].Value, CultureInfo.InvariantCulture), match.Groups[8].Value, double.Parse(match.Groups[9].Value, CultureInfo.InvariantCulture));
}
Expand Down
Loading