func (p *Parser) parseFile() { if p.trace { defer un(trace(p, "parseFile")) } fileCollection := p.addNode(0, CollectionNode) for (p.tok != EOF) && (p.limitReached != true) { p.expect(LPAREN) p.parseGame(fileCollection) } if p.treeNodes[fileCollection].Children == nilTreeNodeIdx { p.errors.Add(p.pos, "file contains no games") } if p.errors.ErrorCount() > 0 { p.errors.RemoveMultiples() ah.PrintError(os.Stderr, p.errors) ah.PrintError(os.Stdout, p.errors) } if p.warnings.ErrorCount() > 0 { p.warnings.RemoveMultiples() ah.PrintError(os.Stderr, p.warnings) ah.PrintError(os.Stdout, p.warnings) } return }
func WriteSGFFile(r *DirectoryProcessRequest, fName string, b []byte) { fullFileName := r.dir + "/" + fName prsr, errL := sgf.ParseFile(fullFileName, b, r.dbReq.PModeReq, r.dbReq.MoveLimit) r.cntf += 1 if len(errL) != 0 { fmt.Printf("%s Error(s) during parsing: %s\n", r.dbReq.Requester, fullFileName) ah.PrintError(os.Stdout, errL) return // cntF, cntT, cntE, errL // stop on first error? } inDir := r.dir idx := strings.LastIndex(inDir, "/") if idx >= 0 { inDir = inDir[idx+1:] } outDir := r.dbReq.DBOutName + inDir outFileName := outDir + "/" + fName // Check the output directory. If missing, create it. _, errS := os.Stat(outDir) if errS != nil { err2 := os.MkdirAll(outDir, os.ModeDir|os.ModePerm) if err2 != nil { fmt.Println(r.dbReq.Requester, "Error:", err2, "trying to create test output directory:", outDir) fmt.Println("Original Error:", errS, "trying os.Stat") return // cntF, cntT, cntE, err2 // stop on first error? } } err := prsr.GameTree.WriteFile(outFileName, r.dbReq.NumPerLine) if err != nil { fmt.Printf("%s Error writing: %s, %s\n", r.dbReq.Requester, outFileName, err) return // cntF, cntT, cntE, err } }
// ReadDirectoryAndBuildPatterns func ReadDirectoryAndBuildPatterns(dir_Name string, subDir_Name string, Pattern_dir string, patternTree *sgf.GameTree, pattern_typ ah.PatternType, fileLimit int, moveLimit int, skipFiles int) (*sgf.GameTree, error) { defer un(trace("ReadDirectoryAndBuildPatterns"), nil) var err error fmt.Printf("Reading directory %s\n", subDir_Name) fils, err := ioutil.ReadDir(dir_Name + subDir_Name) if err != nil && err != io.EOF { fmt.Printf("Error reading Database directory: %s, %s\n", dir_Name+subDir_Name, err) return patternTree, err } filesRead := 0 for i, fil := range fils { if strings.Index(fil.Name(), ".sgf") >= 0 { if skipFiles > 0 { fmt.Printf("Skipping file index %d: %s\n", i, fil.Name()) skipFiles -= 1 } else { if fileLimit == 0 || filesRead < fileLimit { filesRead += 1 fmt.Printf("Reading file %d, index %d: %s\n", filesRead, i, fil.Name()) fileName := dir_Name + subDir_Name + "/" + fil.Name() b, err := ioutil.ReadFile(fileName) if err != nil && err != io.EOF { fmt.Printf("Error reading File: %s, %s\n", fileName, err) return patternTree, err } // Use first call to turn on tracing, second to play while reading, third for GoGoD checking: // and combinations. // prsr,errL := sgf.ParseFile(fileName, b, sgf.ParseComments+sgf.TraceParser, moveLimit) // prsr,errL := sgf.ParseFile(fileName, b, sgf.ParseComments+sgf.ParserPlay+sgf.ParserDbStat, moveLimit) // prsr,errL := sgf.ParseFile(fileName, b, sgf.ParseComments+sgf.ParserGoGoD, moveLimit) /* prsr */ _, errL := sgf.ParseFile(fileName, b, sgf.ParseComments+sgf.ParserGoGoD+sgf.ParserPlay, moveLimit) if len(errL) != 0 { fmt.Printf("Error(s) during parsing: %s\n", fileName) ah.PrintError(os.Stdout, errL) return patternTree, err // stop on first error? } /* TODO: replace this "write output" logic with pattern add logic if outDir != "" { outFileName := outDir + "/" + f.Name() err = prsr.GameTree.WriteFile(outFileName, dbReq.NumPerLine) if err != nil { fmt.Printf("Error writing: %s, %s\n", outFileName, err) return patternTree, err } } */ } else { fmt.Printf("file limit reached: %d\n", filesRead) break } } } } return patternTree, err }