public bool ReadFile(string FileName) { m_fileName = FileName; FileInfo finfo = new FileInfo(m_fileName); //is the file really there? if (!finfo.Exists) return false; Surfaces = new List<Surface>(); string[] FileLines; string fullFile; //Use a read-only filestream in case we have any write conflicts using (var fs = new FileStream(m_fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var sr = new StreamReader(fs)) fullFile = sr.ReadToEnd(); FileLines = fullFile.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); FileLines = StripComments(FileLines); int lineNum = 0; this.Title = FileLines[0];//pull the title line, always the first double.TryParse(FileLines[1], out Mach);//mach number is the second line for( int i=2; i<5;i++) { double[] headerArgs = PullArgs(FileLines[i]); if (headerArgs.Length == 3) { if (i == 2)//third line is the symmetry { IYSym = headerArgs[0]; IZSym = headerArgs[1]; ZSym = headerArgs[2]; } else if (i == 3)//forth line is the reference sizes { Sref = headerArgs[0]; Cref = headerArgs[1]; Bref = headerArgs[2]; } else if (i == 4)//fifth non-blank/non-comment line is C.G. locations { Xref = headerArgs[0]; Yref = headerArgs[1]; Zref = headerArgs[2]; } } } //special for the optional CDp parameter, if we can parse it as a number, good, otherwise continue down to the surfaces if (double.TryParse(FileLines[5], out CDp)) lineNum = 6; else lineNum = 5; List<int> KeyLines = new List<int>(); List<string> KeyWords = new List<string>(); for (int i = lineNum; i < FileLines.Length; i++) { if (FileLines[i].ToUpperInvariant().StartsWith("SURF")) { KeyLines.Add(i); KeyWords.Add("SURF"); } else if (FileLines[i].ToUpperInvariant().StartsWith("BODY")) { KeyLines.Add(i); KeyWords.Add("BODY"); } else if (FileLines[i].ToUpperInvariant().StartsWith("SECT")) { KeyLines.Add(i); KeyWords.Add("SECT"); } } for( int i=0; i<KeyLines.Count; i++) { int length = i == KeyLines.Count-1 ? FileLines.Length - KeyLines[i] : KeyLines[i+1] - KeyLines[i]; string[] content = new string[length]; for( int j=0; j<content.Length; j++) content[j] = FileLines[KeyLines[i]+j]; if(KeyWords[i] == "SURF") { //should have code somewhere to handle if the surfaces have the same name. Surface surf = new Surface(content[1], this); surf.ParseContent(content); this.Surfaces.Add(surf); } else if(KeyWords[i] == "SECT") { Surface.Section sec = new Surface.Section(this.Surfaces[Surfaces.Count - 1]); sec.ParseContent(content); this.Surfaces[Surfaces.Count - 1].Sections.Add(sec); } } return true; }
public bool ReadFile(string FileName) { m_fileName = FileName; FileInfo finfo = new FileInfo(m_fileName); //is the file really there? if (!finfo.Exists) { return(false); } Surfaces = new List <Surface>(); string[] FileLines; string fullFile; //Use a read-only filestream in case we have any write conflicts using (var fs = new FileStream(m_fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var sr = new StreamReader(fs)) fullFile = sr.ReadToEnd(); FileLines = fullFile.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); FileLines = StripComments(FileLines); int lineNum = 0; this.Title = FileLines[0]; //pull the title line, always the first double.TryParse(FileLines[1], out Mach); //mach number is the second line for (int i = 2; i < 5; i++) { double[] headerArgs = PullArgs(FileLines[i]); if (headerArgs.Length == 3) { if (i == 2)//third line is the symmetry { IYSym = headerArgs[0]; IZSym = headerArgs[1]; ZSym = headerArgs[2]; } else if (i == 3)//forth line is the reference sizes { Sref = headerArgs[0]; Cref = headerArgs[1]; Bref = headerArgs[2]; } else if (i == 4)//fifth non-blank/non-comment line is C.G. locations { Xref = headerArgs[0]; Yref = headerArgs[1]; Zref = headerArgs[2]; } } } //special for the optional CDp parameter, if we can parse it as a number, good, otherwise continue down to the surfaces if (double.TryParse(FileLines[5], out CDp)) { lineNum = 6; } else { lineNum = 5; } List <int> KeyLines = new List <int>(); List <string> KeyWords = new List <string>(); for (int i = lineNum; i < FileLines.Length; i++) { if (FileLines[i].ToUpperInvariant().StartsWith("SURF")) { KeyLines.Add(i); KeyWords.Add("SURF"); } else if (FileLines[i].ToUpperInvariant().StartsWith("BODY")) { KeyLines.Add(i); KeyWords.Add("BODY"); } else if (FileLines[i].ToUpperInvariant().StartsWith("SECT")) { KeyLines.Add(i); KeyWords.Add("SECT"); } } for (int i = 0; i < KeyLines.Count; i++) { int length = i == KeyLines.Count - 1 ? FileLines.Length - KeyLines[i] : KeyLines[i + 1] - KeyLines[i]; string[] content = new string[length]; for (int j = 0; j < content.Length; j++) { content[j] = FileLines[KeyLines[i] + j]; } if (KeyWords[i] == "SURF") { //should have code somewhere to handle if the surfaces have the same name. Surface surf = new Surface(content[1], this); surf.ParseContent(content); this.Surfaces.Add(surf); } else if (KeyWords[i] == "SECT") { Surface.Section sec = new Surface.Section(this.Surfaces[Surfaces.Count - 1]); sec.ParseContent(content); this.Surfaces[Surfaces.Count - 1].Sections.Add(sec); } } return(true); }