diff --git a/DbcParserLib/Dbc.cs b/DbcParserLib/Dbc.cs index 2e72df8..56ecb1e 100644 --- a/DbcParserLib/Dbc.cs +++ b/DbcParserLib/Dbc.cs @@ -11,10 +11,10 @@ public class Dbc public IEnumerable Nodes {get;} public IEnumerable Messages {get;} public IEnumerable EnvironmentVariables { get; } - public IEnumerable GlobalProperties { get; } + public IEnumerable GlobalProperties { get; } public Dbc(IEnumerable nodes, IEnumerable messages, IEnumerable environmentVariables, - IEnumerable globalProperties) + IEnumerable globalProperties) { Nodes = nodes; Messages = messages; diff --git a/DbcParserLib/DbcBuilder.cs b/DbcParserLib/DbcBuilder.cs index e620879..ab267b2 100644 --- a/DbcParserLib/DbcBuilder.cs +++ b/DbcParserLib/DbcBuilder.cs @@ -18,6 +18,7 @@ internal class DbcBuilder : IDbcBuilder private readonly IDictionary m_messages = new Dictionary(); private readonly IDictionary> m_signals = new Dictionary>(); private readonly IDictionary m_environmentVariables = new Dictionary(); + private readonly IDictionary m_globalCustomProperties = new Dictionary(); private readonly IDictionary m_namedTablesMap = new Dictionary(); private readonly IDictionary> m_customProperties = new Dictionary>() { @@ -117,6 +118,23 @@ public void AddNodeCustomProperty(string propertyName, string nodeName, string v m_observer.PropertyNameNotFound(propertyName); } + public void AddGlobalCustomProperty(string propertyName, string value, bool isNumeric) + { + if(m_customProperties[CustomPropertyObjectType.Global].TryGetValue(propertyName, out var customPropertyDefinition)) + { + var property = new CustomProperty(customPropertyDefinition); + if(!property.SetCustomPropertyValue(value, isNumeric)) + return; + + if(m_globalCustomProperties.TryGetValue(propertyName, out _)) + m_observer.DuplicatedGlobalProperty(propertyName); + else + m_globalCustomProperties[propertyName] = property; + } + else + m_observer.PropertyNameNotFound(propertyName); + } + public void AddEnvironmentVariableCustomProperty(string propertyName, string variableName, string value, bool isNumeric) { if (m_customProperties[CustomPropertyObjectType.Environment].TryGetValue(propertyName, out var customProperty)) @@ -332,6 +350,35 @@ private void FillNodesNotSetCustomPropertyWithDefault() } } + private void FillEnvironmentVariablesNotSetCustomPropertyWithDefault() + { + var environmentCustomProperties = m_customProperties[CustomPropertyObjectType.Environment]; + foreach (var customProperty in environmentCustomProperties) + { + foreach (var envVariable in m_environmentVariables.Values) + { + if (!envVariable.CustomProperties.TryGetValue(customProperty.Key, out _)) + { + envVariable.CustomProperties[customProperty.Key] = new CustomProperty(customProperty.Value); + envVariable.CustomProperties[customProperty.Key].SetCustomPropertyValueFromDefault(); + } + } + } + } + + private void FillGlobalCustomPropertiesNotSetCustomPropertyWithDefault() + { + var globalCustomProperties = m_customProperties[CustomPropertyObjectType.Global]; + foreach (var customPropertyPair in globalCustomProperties) + { + if (!m_globalCustomProperties.TryGetValue(customPropertyPair.Key, out _)) + { + m_globalCustomProperties[customPropertyPair.Key] = new CustomProperty(customPropertyPair.Value); + m_globalCustomProperties[customPropertyPair.Key].SetCustomPropertyValueFromDefault(); + } + } + } + private void FillMessagesNotSetCustomPropertyWithDefault() { var messageCustomProperties = m_customProperties[CustomPropertyObjectType.Message]; @@ -369,6 +416,8 @@ public Dbc Build() { FillNodesNotSetCustomPropertyWithDefault(); FillMessagesNotSetCustomPropertyWithDefault(); + FillEnvironmentVariablesNotSetCustomPropertyWithDefault(); + FillGlobalCustomPropertiesNotSetCustomPropertyWithDefault(); foreach (var message in m_messages) { @@ -399,7 +448,7 @@ public Dbc Build() //} //return new Dbc(nodes, messages, environmentVariables); - return new Dbc(m_nodes.ToArray(), m_messages.Values.ToArray(), m_environmentVariables.Values.ToArray(), m_customProperties[CustomPropertyObjectType.Global].Values); + return new Dbc(m_nodes.ToArray(), m_messages.Values.ToArray(), m_environmentVariables.Values.ToArray(), m_globalCustomProperties.Values); } } diff --git a/DbcParserLib/IDbcBuilder.cs b/DbcParserLib/IDbcBuilder.cs index 66dcd7b..17c0046 100644 --- a/DbcParserLib/IDbcBuilder.cs +++ b/DbcParserLib/IDbcBuilder.cs @@ -19,6 +19,7 @@ internal interface IDbcBuilder void AddCustomProperty(CustomPropertyObjectType objectType, CustomPropertyDefinition customProperty); void AddCustomPropertyDefaultValue(string propertyName, string value, bool isNumeric); void AddNodeCustomProperty(string propertyName, string nodeName, string value, bool isNumeric); + void AddGlobalCustomProperty(string propertyName, string value, bool isNumeric); void AddEnvironmentVariableCustomProperty(string propertyName, string variableName, string value, bool isNumeric); void AddMessageCustomProperty(string propertyName, uint messageId, string value, bool isNumeric); void AddSignalCustomProperty(string propertyName, uint messageId, string signalName, string value, bool isNumeric); diff --git a/DbcParserLib/Observers/IParseFailureObserver.cs b/DbcParserLib/Observers/IParseFailureObserver.cs index 8f4639e..003752d 100644 --- a/DbcParserLib/Observers/IParseFailureObserver.cs +++ b/DbcParserLib/Observers/IParseFailureObserver.cs @@ -12,6 +12,7 @@ public interface IParseFailureObserver void DuplicatedProperty(string propertyName); void DuplicatedPropertyInNode(string propertyName, string nodeName); void DuplicatedPropertyInEnvironmentVariable(string propertyName, string environmentVariableName); + void DuplicatedGlobalProperty(string propertyName); void DuplicatedPropertyInMessage(string propertyName, uint messageId); void DuplicatedPropertyInSignal(string propertyName, string signalName); void DuplicatedEnvironmentVariableInNode(string environmentVariableName, string nodeName); diff --git a/DbcParserLib/Observers/SilentFailureObserver.cs b/DbcParserLib/Observers/SilentFailureObserver.cs index 2dd0d16..c50f4ec 100644 --- a/DbcParserLib/Observers/SilentFailureObserver.cs +++ b/DbcParserLib/Observers/SilentFailureObserver.cs @@ -36,6 +36,10 @@ public void DuplicatedPropertyInEnvironmentVariable(string propertyName, string { } + public void DuplicatedGlobalProperty(string propertyName) + { + } + public void DuplicatedPropertyInMessage(string propertyName, uint messageId) { } diff --git a/DbcParserLib/Observers/SimpleFailureObserver.cs b/DbcParserLib/Observers/SimpleFailureObserver.cs index 41330e2..abe7db0 100644 --- a/DbcParserLib/Observers/SimpleFailureObserver.cs +++ b/DbcParserLib/Observers/SimpleFailureObserver.cs @@ -54,6 +54,11 @@ public void DuplicatedPropertyInEnvironmentVariable(string propertyName, string AddError($"Duplicated custom property '{propertyName}' in environment variable '{environmentVariableName}'"); } + public void DuplicatedGlobalProperty(string propertyName) + { + AddError($"Duplicated custom property '{propertyName}' among global properties"); + } + public void DuplicatedPropertyInMessage(string propertyName, uint messageId) { AddError($"Duplicated custom property '{propertyName}' in message (ID {messageId})"); diff --git a/DbcParserLib/Parsers/PropertiesLineParser.cs b/DbcParserLib/Parsers/PropertiesLineParser.cs index 4a09bcb..00b6f9d 100644 --- a/DbcParserLib/Parsers/PropertiesLineParser.cs +++ b/DbcParserLib/Parsers/PropertiesLineParser.cs @@ -37,6 +37,10 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin builder.AddMessageCustomProperty(match.Groups[1].Value, uint.Parse(match.Groups[5].Value, CultureInfo.InvariantCulture), stringValue, isNumeric); 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, stringValue, isNumeric); + else + { + builder.AddGlobalCustomProperty(match.Groups[1].Value, match.Groups[9].Value, isNumeric); + } } else m_observer.PropertySyntaxError();