Created
February 29, 2016 19:31
-
-
Save rasky/93540011c3ce8bc08c2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"path/filepath" | |
"strings" | |
"sync" | |
"sync/atomic" | |
) | |
type Stats struct { | |
Wins int64 | |
Draws int64 | |
Loses int64 | |
} | |
func process(chfn chan string, global *Stats) { | |
var local Stats | |
for fn := range chfn { | |
f, err := os.Open(fn) | |
if err != nil { | |
fmt.Fprintln(os.Stderr, fn, err) | |
return | |
} | |
defer f.Close() | |
scan := bufio.NewScanner(f) | |
for scan.Scan() { | |
line := scan.Text() | |
if strings.HasPrefix(line, "[Result ") { | |
if idx := strings.IndexByte(line, '-'); idx >= 0 { | |
switch line[idx-1] { | |
case '0': | |
local.Loses += 1 | |
case '1': | |
local.Wins += 1 | |
case '2': | |
local.Draws += 1 | |
} | |
} | |
} | |
} | |
} | |
atomic.AddInt64(&global.Wins, local.Wins) | |
atomic.AddInt64(&global.Draws, local.Draws) | |
atomic.AddInt64(&global.Loses, local.Loses) | |
} | |
func main() { | |
var s Stats | |
numfiles := 0 | |
var exitwg sync.WaitGroup | |
chfn := make(chan string) | |
for i := 0; i < 16; i++ { | |
exitwg.Add(1) | |
go func() { | |
process(chfn, &s) | |
exitwg.Done() | |
}() | |
} | |
filepath.Walk(".", func(path string, info os.FileInfo, err error) error { | |
if err == nil && strings.HasSuffix(path, ".pgn") { | |
chfn <- path | |
numfiles++ | |
} | |
return nil | |
}) | |
close(chfn) | |
exitwg.Wait() | |
fmt.Printf("%d %+v\n", numfiles, s) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment