-
Notifications
You must be signed in to change notification settings - Fork 1
/
id.go
72 lines (61 loc) · 1.61 KB
/
id.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package primes
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"github.com/MaxTheMonster/PrimeNumberGenerator/config"
"github.com/MaxTheMonster/PrimeNumberGenerator/storage"
)
// Round() is used to round numbers to the nearest x
func Round(x, unit float64) float64 {
return float64(int64(x/unit+0.5)) * unit
}
// GetCurrentId returns the current id, rounded to nearest hundred
func GetCurrentId() uint64 {
maximumId := GetTotalPrimeCount()
currentId := uint64(Round(float64(maximumId), float64(config.MaxBufferSize)))
return currentId
}
// GetTotalPrimeCount finds the number of lines in each file
func GetTotalPrimeCount() uint64 {
var maximumId uint64
openDirectory := storage.OpenDirectory(os.O_RDONLY, 0600)
defer openDirectory.Close()
scanner := bufio.NewScanner(openDirectory)
for scanner.Scan() {
filename := scanner.Text()
file, err := os.Open(storage.FormatFilePath(filename))
if err != nil {
break
}
r := bufio.NewReader(file)
linesInFile, err := getLinesInFile(r)
if err != nil {
config.Logger.Fatal(err)
}
maximumId += uint64(linesInFile)
}
return maximumId
}
// getLinesInFile counts the lines of a given file
func getLinesInFile(r io.Reader) (int, error) {
buf := make([]byte, 32*1024)
count := 0
lineSep := []byte{'\n'}
for {
c, err := r.Read(buf)
count += bytes.Count(buf[:c], lineSep)
switch {
case err == io.EOF:
return count, nil
case err != nil:
return count, err
}
}
}
// ShowCurrentCount displays the n number of primes calculated
func ShowCurrentCount() {
fmt.Printf("Total (to the nearest hundred) prime numbers calculated and stored: #%d\n", config.Id)
}