Repository navigation
Expand file tree
/
Copy pathZXMemoryMap.cs
More file actions
67 lines (52 loc) · 2.25 KB
/
Copy pathZXMemoryMap.cs
File metadata and controls
67 lines (52 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using AvaloniaEdit.Rendering;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ZXBasicStudio.BuildSystem
{
public class ZXMemoryMap
{
static Regex regLine = new Regex("^([0-9A-F]+):[^\\n]*file__([0-9a-zA-Z_]+)__([0-9]*)", RegexOptions.Multiline);
List<ZXCodeLine> lines = new List<ZXCodeLine>();
public IEnumerable<ZXCodeLine> Lines { get { return lines; } }
public ZXMemoryMap(string MapFile, IEnumerable<ZXCodeFile> Files)
{
string mapContent = File.ReadAllText(MapFile);
var matches = regLine.Matches(mapContent);
Dictionary<string, ZXCodeFile> files = new Dictionary<string, ZXCodeFile>();
foreach (var file in Files)
files[file.FileGuid] = file;
foreach (Match m in matches)
{
var address = m.Groups[1].Value;
var fileId = m.Groups[2].Value;
var lineNumber = m.Groups[3].Value;
if (!files.ContainsKey(fileId))
continue;
var file = files[fileId];
if (!ushort.TryParse(address, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out var _))
{
throw new LineOutOfRangeException($"Invalid address {address} in {file.Name} at line {lineNumber}.");
}
var line = new ZXCodeLine(file.FileType, file.AbsolutePath, int.Parse(lineNumber), ushort.Parse(address, System.Globalization.NumberStyles.HexNumber));
lines.Add(line);
}
/*
var dupes = lines.GroupBy(l => l.fi l.Address).Where(g => g.Count() > 1).ToArray();
foreach (var dupe in dupes)
{
var dLines = dupe.OrderBy(d => d.LineNumber).Take(dupe.Count() - 1);
foreach (var dline in dLines)
lines.Remove(dline);
}*/
}
}
public class LineOutOfRangeException : Exception
{
public LineOutOfRangeException(string message) : base(message) { }
}
}