diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c0e5b20 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/dustinpianalto/wc-go + +go 1.15 + +require github.com/droundy/goopt v0.0.0-20170604162106-0b8effe182da // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2a063ec --- /dev/null +++ b/go.sum @@ -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= diff --git a/pkg/wc/counter.go b/pkg/wc/counter.go new file mode 100644 index 0000000..e0b577b --- /dev/null +++ b/pkg/wc/counter.go @@ -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) + } + } +}