Skip to content

Commit

Permalink
Merge pull request #4 from fbiagi/master
Browse files Browse the repository at this point in the history
Throw descriptive exception when finding duplicate value labels
  • Loading branch information
fbiagi committed Feb 3, 2016
2 parents f793e7b + 1641a28 commit f9078ba
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions SpssLib/FileParser/SavFileParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,17 @@ private Variable GetVariable(int variableIndex, int dictionaryIndex, MetaData me
{
foreach (var label in valueLabelRecord.Labels)
{
variable.ValueLabels.Add(BitConverter.ToDouble(label.Key, 0), label.Value.Replace("\0", string.Empty).Trim());
var key = BitConverter.ToDouble(label.Key, 0);
var value = label.Value.Replace("\0", string.Empty).Trim();

if (variable.ValueLabels.ContainsKey(key))
{
var existingValue = variable.ValueLabels[key];
var varibleName = GetLongName(metaData, variableRecord);
throw new SpssFileFormatException(string.Format("Variable {0} has a duplicate key for value label {1}, found values \"{2}\" and \"{3}\"", varibleName, key, existingValue, value), dictionaryIndex);
}

variable.ValueLabels.Add(key, value);
}
}

Expand All @@ -421,16 +431,22 @@ private Variable GetVariable(int variableIndex, int dictionaryIndex, MetaData me
variable.Width = variable.PrintFormat.FieldWidth;
}

variable.Name = variableRecord.Name;
variable.Name = GetLongName(metaData, variableRecord);

return variable;
}

private static string GetLongName(MetaData metaData, VariableRecord variableRecord)
{
string longName;
// Look for the right name
if (metaData.LongVariableNames != null
&& metaData.LongVariableNames.Dictionary.TryGetValue(variable.Name, out longName))
// Look for the right (long) name if there is one
if (metaData.LongVariableNames != null
&& metaData.LongVariableNames.Dictionary.TryGetValue(variableRecord.Name, out longName))
{
variable.Name = longName;
return longName;
}

return variable;
// If not, just return the short name
return variableRecord.Name;
}


Expand Down

0 comments on commit f9078ba

Please sign in to comment.