|
|
|
@ -1,14 +1,15 @@
|
|
|
|
package wc
|
|
|
|
package wc
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const BufferSize = 1024 * 1024
|
|
|
|
|
|
|
|
|
|
|
|
type Counter struct {
|
|
|
|
type Counter struct {
|
|
|
|
FileReader *bufio.Reader
|
|
|
|
|
|
|
|
Words int64
|
|
|
|
Words int64
|
|
|
|
Chars int64
|
|
|
|
Chars int64
|
|
|
|
Lines int64
|
|
|
|
Lines int64
|
|
|
|
@ -27,28 +28,82 @@ func Count(filename string, cw, cc, cl, cb, mll bool) {
|
|
|
|
file, err := os.Open(filename)
|
|
|
|
file, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
fi, err := file.Stat()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
size := fi.Size()
|
|
|
|
|
|
|
|
|
|
|
|
processLine := cw || cc
|
|
|
|
processLine := cw || cc
|
|
|
|
var c = Counter{FileReader: bufio.NewReader(file)}
|
|
|
|
|
|
|
|
if cl && !processLine {
|
|
|
|
var c = &Counter{}
|
|
|
|
c.CountLines(cb)
|
|
|
|
numWorkers := runtime.NumCPU()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
chunks := make(chan []byte, numWorkers)
|
|
|
|
|
|
|
|
counts := make(chan int64, numWorkers)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i := 0; i < numWorkers; i++ {
|
|
|
|
|
|
|
|
go ConcurrentChunkCounter(chunks, counts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Printf("%d %s\n", c.Lines, filename)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (c *Counter) CountLines(cb bool) {
|
|
|
|
if cl && !processLine {
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
r, s, err := c.FileReader.ReadRune()
|
|
|
|
buf := make([]byte, BufferSize)
|
|
|
|
log.Printf("%#v, %#v, %#v", r, s, err)
|
|
|
|
bytes, err := file.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
chunks <- buf[:bytes]
|
|
|
|
break
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
|
|
|
if r == '\n' {
|
|
|
|
break
|
|
|
|
c.Lines++
|
|
|
|
} else {
|
|
|
|
|
|
|
|
panic(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cb {
|
|
|
|
close(chunks)
|
|
|
|
c.Bytes += int64(s)
|
|
|
|
for i := 0; i < numWorkers; i++ {
|
|
|
|
|
|
|
|
c.Lines += <-counts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(counts)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if cb {
|
|
|
|
|
|
|
|
c.Bytes = size
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if c.Lines > 0 {
|
|
|
|
|
|
|
|
fmt.Printf("%d ", c.Lines)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Words > 0 {
|
|
|
|
|
|
|
|
fmt.Printf("%d ", c.Words)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Chars > 0 {
|
|
|
|
|
|
|
|
fmt.Printf("%d ", c.Words)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Bytes > 0 {
|
|
|
|
|
|
|
|
fmt.Printf("%d ", c.Bytes)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.MaxLineLength > 0 {
|
|
|
|
|
|
|
|
fmt.Printf("%d ", c.MaxLineLength)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%s\n", filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//func (c *Counter) CountLines() {
|
|
|
|
|
|
|
|
// for {
|
|
|
|
|
|
|
|
// _, p, err := c.FileReader.ReadLine()
|
|
|
|
|
|
|
|
// if err != nil && err != io.EOF {
|
|
|
|
|
|
|
|
// panic(err)
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if err == io.EOF {
|
|
|
|
|
|
|
|
// break
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if !p {
|
|
|
|
|
|
|
|
// c.Lines++
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// c.Lines--
|
|
|
|
|
|
|
|
//}
|
|
|
|
|