Initial line counter

master
DustyP 5 years ago
parent 0481beda0a
commit 681dc0445d

@ -0,0 +1,5 @@
module github.com/dustinpianalto/wc-go
go 1.15
require github.com/droundy/goopt v0.0.0-20170604162106-0b8effe182da // indirect

@ -0,0 +1,2 @@
github.com/droundy/goopt v0.0.0-20170604162106-0b8effe182da h1:79H+mNJWOObWrQgbkSvvZ3t/D2lKWaTi9mu/v7fNRvg=
github.com/droundy/goopt v0.0.0-20170604162106-0b8effe182da/go.mod h1:ytRJ64WkuW4kf6/tuYqBATBCRFUP8X9+LDtgcvE+koI=

@ -0,0 +1,54 @@
package wc
import (
"bufio"
"fmt"
"log"
"os"
)
type Counter struct {
FileReader *bufio.Reader
Words int64
Chars int64
Lines int64
Bytes int64
MaxLineLength int64
}
func Count(filename string, cw, cc, cl, cb, mll bool) {
if !cw && !cc && !cl && !cb && !mll {
cw = true
cc = false
cl = true
cb = true
mll = false
}
file, err := os.Open(filename)
if err != nil {
fmt.Println(err)
}
defer file.Close()
processLine := cw || cc
var c = Counter{FileReader: bufio.NewReader(file)}
if cl && !processLine {
c.CountLines(cb)
}
fmt.Printf("%d %s\n", c.Lines, filename)
}
func (c *Counter) CountLines(cb bool) {
for {
r, s, err := c.FileReader.ReadRune()
log.Printf("%#v, %#v, %#v", r, s, err)
if err != nil {
break
}
if r == '\n' {
c.Lines++
}
if cb {
c.Bytes += int64(s)
}
}
}
Loading…
Cancel
Save