-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (84 loc) · 2.01 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"flag"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"filip/gocsv/convert/xls"
"filip/gocsv/convert/xlsx"
)
const (
ThreadsMultipler = 2
ThreadsPerWorker = 2
)
var (
delimiter string
skip int
filenames []string
)
func init() {
flag.StringVar(&delimiter, "d", ";", "Znak do rozdzielenia kolumn w CSV")
flag.IntVar(&skip, "s", 0, "Liczba wierszy do pominięcia")
flag.Parse()
filenames = flag.Args()
if len(filenames) == 0 {
log.Println("Nie podano plików do przetworzenia")
flag.PrintDefaults()
os.Exit(0)
}
}
func main() {
s0 := time.Now()
runtime.GOMAXPROCS(runtime.NumCPU())
var threadCount = runtime.GOMAXPROCS(-1) * ThreadsMultipler / ThreadsPerWorker
var xlsxCount int
var xlsCount int
for _, file := range filenames {
switch strings.ToLower(filepath.Ext(file)) {
case ".xlsx":
xlsxCount++
break
case ".xls":
xlsCount++
break
}
}
doneResults := make(chan bool, 100)
xlsxFileChan := make(chan string, 100)
xlsFileChan := make(chan string, 100)
allFilesCount := xlsxCount + xlsCount
if allFilesCount == 0 {
log.Println("Brak plików do przetworzenia. Koniec")
os.Exit(0)
}
maxXlsxThreads := int(xlsxCount / (allFilesCount) * threadCount)
maxXlsThreads := int(xlsCount / (allFilesCount) * threadCount)
log.Printf("Tworzenie %d wątków dla plików xlsx\n", maxXlsxThreads)
log.Printf("Tworzenie %d wątków dla plików xls\n", maxXlsThreads)
for i := 0; i < maxXlsxThreads; i++ {
go xlsx.NewXlsxConvertWorker(i, xlsxFileChan, doneResults, rune(delimiter[0]), skip)
}
for i := 0; i < maxXlsThreads; i++ {
go xls.NewXlsConvertWorker(i, xlsFileChan, doneResults, rune(delimiter[0]), skip)
}
for _, file := range filenames {
switch strings.ToLower(filepath.Ext(file)) {
case ".xlsx":
xlsxFileChan <- file
break
case ".xls":
xlsFileChan <- file
break
}
}
close(xlsxFileChan)
close(xlsFileChan)
for i := 0; i < threadCount; i++ {
<-doneResults
}
log.Println("Prace trwały", time.Now().Sub(s0))
os.Exit(0)
}