2020-12-08 09:09:09 +00:00
|
|
|
package fileutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-12-11 21:14:09 +00:00
|
|
|
"bytes"
|
2020-12-08 09:09:09 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-12-11 10:00:29 +00:00
|
|
|
func InputParserInts(filename string) []int {
|
|
|
|
file, err := os.Open(filename)
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
values := make([]int, 0)
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
x, err := strconv.Atoi(scanner.Text())
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf(err.Error())
|
|
|
|
}
|
|
|
|
values = append(values, x)
|
|
|
|
}
|
|
|
|
|
|
|
|
return values
|
|
|
|
}
|
|
|
|
|
2020-12-08 09:09:09 +00:00
|
|
|
func InputParserIntMap(filename string) map[int]bool {
|
|
|
|
file, err := os.Open(filename)
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
values := make(map[int]bool)
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
x, err := strconv.Atoi(scanner.Text())
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf(err.Error())
|
|
|
|
}
|
|
|
|
values[x] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return values
|
|
|
|
}
|
|
|
|
|
|
|
|
func InputParserStrings(filename string) []string {
|
|
|
|
data, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf(err.Error())
|
|
|
|
}
|
2020-12-12 05:46:31 +00:00
|
|
|
output := strings.Split(string(data), "\n")
|
|
|
|
return output[:len(output)-1]
|
2020-12-08 09:09:09 +00:00
|
|
|
}
|
2020-12-11 21:14:09 +00:00
|
|
|
|
|
|
|
func InputParserBytes(filename string) [][]byte {
|
|
|
|
data, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf(err.Error())
|
|
|
|
}
|
|
|
|
output := bytes.Split(data, []byte("\n"))
|
|
|
|
return output[:len(output)-1]
|
|
|
|
}
|