Skip to content

Instantly share code, notes, and snippets.

@mesprague
Created December 15, 2008 10:10
Show Gist options
  • Save mesprague/35918 to your computer and use it in GitHub Desktop.
Save mesprague/35918 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenBabel;
namespace OBDotNetExamples
{
public static class OBReader
{
private static OBConversion obc;
//store the supported file formats.
private static HashSet<string> validExt;
static OBReader()
{
obc = new OBConversion();
validExt = new HashSet<string>();
foreach(string s in obc.GetSupportedInputFormat())
{
validExt.Add(s.Remove(s.IndexOf(' ')));
}
}
public static OBMol ReadMol(string fileName)
{
return ReadFiles(fileName).First();
}
public static IEnumerable<OBMol> ReadFiles(params string[] fileNames)
{
OBMol obMol = new OBMol();
foreach (string fileName in fileNames)
{
if (!File.Exists(fileName))
throw new FileNotFoundException("Could not find file : " + fileName);
string ext = fileName.Substring(fileName.LastIndexOf(".") + 1);
if (!validExt.Contains(ext))
throw new IOException("Unsupported file type: " + fileName);
obc.SetInFormat(ext);
if(obc.ReadFile(obMol,fileName))
yield return obMol;
while(obc.Read(obMol))
yield return obMol;
}
}//end ReadFiles(params string[] files)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment