Ejemplo n.º 1
0
        public static SGFTree LoadFromStream(Stream baseStream)
        {
            var stream = new StreamReader(
                baseStream,
                DefaultEncodingConfiguration);

            SGFTree tree = null;

            try
            {
                var loader = new SgfReader(stream);
                if (loader.ReadUntil('(') == '(')
                {
                    tree = new SGFTree();
                    if (!loader.ReadTree(tree))
                    {
                        tree = null;
                    }
                }
            }
            finally
            {
                stream.Close();
            }

            if (tree != null)
            {
                return(tree);
            }
            throw new Exception("Couldn't load sgf from stream");
        }
Ejemplo n.º 2
0
        private static IEnumerable <SGFTree> LoadAndParse(string path, List <int> index)
        {
            if (File.Exists(path))
            {
                var stream = new StreamReader(
                    File.OpenRead(path),
                    DefaultEncodingConfiguration);

                try
                {
                    var loader = new SgfReader(stream);

                    if (index == null)
                    {
                        while (loader.ReadUntil('(') == '(')
                        {
                            var tree = new SGFTree();
                            if (loader.ReadTree(tree))
                            {
                                yield return(tree);
                            }
                        }
                    }
                    else
                    {
                        while (index.Count > 0)
                        {
                            stream.BaseStream.Seek(index[0], SeekOrigin.Begin);
                            stream.DiscardBufferedData();
                            index.RemoveAt(0);
                            loader.ReadUntil('(');
                            var tree = new SGFTree();
                            if (loader.ReadTree(tree))
                            {
                                yield return(tree);
                            }
                        }
                    }
                }
                finally
                {
                    stream.Close();
                }
            }
        }
Ejemplo n.º 3
0
        public static SGFTree LoadAndParseSingle(string path)
        {
            if (File.Exists(path))
            {
                var stream = new StreamReader(
                    File.OpenRead(path),
                    DefaultEncodingConfiguration);

                SGFTree tree = null;

                try
                {
                    var loader = new SgfReader(stream);

                    if (loader.ReadUntil('(') == '(')
                    {
                        tree = new SGFTree();

                        if (!loader.ReadTree(tree))
                        {
                            tree = null;
                        }
                    }
                }
                finally
                {
                    stream.Close();
                }

                if (tree != null)
                {
                    return(tree);
                }
                throw new Exception("Couldn't load sgf from " + path);
            }
            throw new Exception("Couldn't open " + path);
        }