Refactiring RecordController.CheckTagValue and Record

This commit is contained in:
lastvoidtemplar
2021-07-08 13:36:34 +03:00
parent 5f4ac5fe02
commit 7f9ae2651e
5 changed files with 85 additions and 47 deletions

View File

@@ -11,11 +11,12 @@ namespace BoerseDataConvert
{ {
private static int count; private static int count;
private static string cur_fileName; private static string cur_fileName;
private TagsTable tagsTable;
public RecordController(string fileName) public RecordController(string fileName)
{ {
count = 1; count = 1;
cur_fileName = fileName; cur_fileName = fileName;
tagsTable = new TagsTable();
} }
public static void NextFile(string fileName) public static void NextFile(string fileName)
{ {
@@ -44,61 +45,61 @@ namespace BoerseDataConvert
} }
private string CheckTagValue(string tag, string value) private string CheckTagValue(string tag, string value)
{ {
string tagname = ""; string[] tagLine;
StreamReader reader = new StreamReader(@"..\..\..\..\tags.txt"); try
using (reader) {
tagLine = tagsTable.GetTagValue(tag);
}
catch (KeyNotFoundException)
{ {
string[] tagLine = null;
while (!reader.EndOfStream)
{
string[] line = reader.ReadLine().Split('|').ToArray();
if (line[0] == tag) tagLine = line;
}
if (tagLine == null) throw new ArgumentException($"WARN: Invalid tag \"{tag}\", {cur_fileName} line {count + 1}");
tagname = tagLine[1];
if (value != "NULL" && tagLine.Length == 3)
{
string[] valueType = tagLine[2].Split('-').ToArray();
if (valueType.Length == 2)
{
if (value.Length > int.Parse(valueType[1]))
{
throw new ArgumentException($"WARN: Too long value \"{tag}\", \"{value}\", max allowed \"{valueType[1]}\", {cur_fileName} line {count + 1}");
}
}
else
{
try
{
double.Parse(value);
}
catch (FormatException)
{
throw new ArgumentException($"WARN: Value is not in a valid format for number \"{tag}\", \"{value}\", {cur_fileName} line {count + 1}");
}
throw new ArgumentException($"WARN: Invalid tag \"{tag}\", {cur_fileName} line {count + 1}");
}
string tagname = tagLine[0];
if (value != "NULL" && tagLine.Length == 2)
{
string[] valueType = tagLine[1].Split('-').ToArray();
if (valueType.Length == 2)
{
if (value.Length > int.Parse(valueType[1]))
{
throw new ArgumentException($"WARN: Too long value \"{tag}\", \"{value}\", max allowed \"{valueType[1]}\", {cur_fileName} line {count + 1}");
} }
} }
else else
{ {
string[] valueRange = tagLine[3].Split('#').ToArray(); try
bool countain = false;
if (value == "") return tagname;
for (int i = 0; i < valueRange.Length; i++)
{ {
if (valueRange[i] == value) double.Parse(value);
{
countain = true;
break;
}
} }
if (!countain) catch (FormatException)
{ {
throw new ArgumentException($"WARN: Value not in range \"{tag}\", \"{value}\", {cur_fileName} line {count + 1}"); throw new ArgumentException($"WARN: Value is not in a valid format for number \"{tag}\", \"{value}\", {cur_fileName} line {count + 1}");
} }
}
}
else
{
string[] valueRange = tagLine[2].Split('#').ToArray();
bool countain = false;
if (value == "") return tagname;
for (int i = 0; i < valueRange.Length; i++)
{
if (valueRange[i] == value)
{
countain = true;
break;
}
}
if (!countain)
{
throw new ArgumentException($"WARN: Value not in range \"{tag}\", \"{value}\", {cur_fileName} line {count + 1}");
} }
} }
return tagname; return tagname;
} }
} }
} }

View File

@@ -8,6 +8,6 @@ namespace BoerseDataConvert
{ {
public class Record public class Record
{ {
public Dictionary<string, string> TagsValues; public List<KeyValuePair<string, string>>TagsValues;
} }
} }

View File

@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BoerseDataConvert
{
public class TagsTable
{
StreamReader reader ;
Dictionary<string, string[]> table;
public TagsTable()
{
reader = new StreamReader(@"..\..\..\..\tags.txt");
table = new Dictionary<string, string[]>();
LoadInfo();
}
private void LoadInfo()
{
using (reader)
{
while (!reader.EndOfStream)
{
string[] line = reader.ReadLine().Split('|').ToArray();
table.Add(line[0], line.Skip(1).ToArray());
}
}
}
public string[] GetTagValue(string tag)
{
return table[tag];
}
}
}

View File

@@ -90,7 +90,7 @@ namespace BoerseDataConvert
string s = a.ConvertToXml(record); string s = a.ConvertToXml(record);
writer.WriteRecord(s); writer.WriteRecord(s);
} }
catch (IndexOutOfRangeException e) catch (IndexOutOfRangeException)
{ {
Reader.EndFile(); Reader.EndFile();
Writer.EndFile(); Writer.EndFile();

View File

@@ -35,11 +35,12 @@ namespace BoerseDataConvert
s = reader.ReadLine(); s = reader.ReadLine();
} }
string[] sr = s.Split("|").ToArray(); string[] sr = s.Split("|").ToArray();
Dictionary<string, string> a = new Dictionary<string, string>(); List<KeyValuePair<string, string>>a = new List<KeyValuePair<string, string>>();
foreach (var item in sr) foreach (var item in sr)
{ {
string[] d = item.Split('#').ToArray(); string[] d = item.Split('#').ToArray();
a.Add(d[0], String.Join('#', d.Skip(1))); var pair = new KeyValuePair<string, string>(d[0], String.Join('#', d.Skip(1)));
a.Add(pair);
} }
Record record = new Record(); Record record = new Record();
record.TagsValues = a; record.TagsValues = a;