private static void AddBlockLine(string line, LifePatternBlock currentPatternBlock) { if (currentPatternBlock == null) { throw new InvalidDataException("Invalid Life format."); } currentPatternBlock.Lines.Add(ParseBlockLine(line)); }
public static LifePattern ParseFile(string path) { if (!File.Exists(path)) { throw new ArgumentException("Could not find file.", "path"); } var pattern = new LifePattern(); using (var reader = new StreamReader(path)) { LifePatternBlock currentPatternBlock = null; string line; while ((line = reader.ReadLine()) != null) { var parts = line.Split(' '); switch (parts[0]) { case "#Life": pattern.Version = parts[1]; break; case "#D": if (parts.Length > 1) { pattern.AddDescription(parts[1]); } break; case "#N": pattern.Rules = LifeRules.Normal; break; case "#R": pattern.Rules = CreateRule(parts[1]); break; case "#P": if (currentPatternBlock != null) { pattern.Blocks.Add(currentPatternBlock); } currentPatternBlock = CreatePatternBlock(parts[1], parts[2]); break; default: AddBlockLine(parts[0], currentPatternBlock); break; } } if (currentPatternBlock != null) { pattern.Blocks.Add(currentPatternBlock); } } NormalizeBlockLines(pattern); NormalizeBlockOffsets(pattern); return(pattern); }
private static void AddBlockLine(string line, LifePatternBlock currentPatternBlock) { if (currentPatternBlock == null) throw new InvalidDataException("Invalid Life format."); currentPatternBlock.Lines.Add(ParseBlockLine(line)); }