public ReaperProject ParseProject(string fileName) { PopulateElements(); var project = new ReaperProject(); var lines = File.ReadAllLines(fileName); lines = lines.Select(l => l.TrimStart()).ToArray(); ReaperElement current = null; foreach (var data in lines) { if (data.StartsWith("<")) // new element and up a level { if (current == null) { current = CreateElement(data, null); } else { var newElement = CreateElement(data, current); current.AddElement(newElement); current = newElement; } } else if (data.StartsWith(">")) // down a level { current.CompleteParse(); if (current.ParentElement != null) { current = current.ParentElement; } } else if (data.StartsWith("WAK")) { // this is a nasty hack as the file format gets weird around // the effects chain current.CompleteParse(); var currentType = current.ElementName; current = current.ParentElement; var newElement = CreateElement(currentType, current); current.AddElement(newElement); current = newElement; } else // current level { var newElement = CreateElement(data, current); current.AddProperty(data); current.AddElement(newElement); } } return((ReaperProject)current); }
public static async Task <ReaperProject> Read(string filename) { ReaperProject reaperProjet = null; using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) using (var reader = new StreamReader(stream)) { Stack <Element> elementStack = new Stack <Element>(); string line = await reader.ReadLineAsync(); var root = ParseElement(line); reaperProjet = ReaperProject.New(root.name, root.attributes); elementStack.Push(reaperProjet); while (line != null) { Element currentElement = elementStack.Peek(); line = line.Trim(); if (line.StartsWith(Constants.START)) { var(name, attributes) = ParseElement(line); if (name != ReaperProject.REAPER_PROJECT) { var newElement = currentElement.AddElement(name, attributes); elementStack.Push(newElement); } } else if (line.StartsWith(Constants.END)) { if (elementStack.Count > 0) { elementStack.Pop(); } } else { var property = ParseProperty(line); currentElement.Properties.Add(property); } line = await reader.ReadLineAsync(); } if (elementStack.Any()) { throw new FormatException(); } } return(reaperProjet); }
public static ReaperProject New(string name, string[] attributes) { if (name != REAPER_PROJECT) { throw new ArgumentException(nameof(name)); } var project = new ReaperProject(); if (attributes != null) { project._attributes.AddRange(attributes); } return(project); }