-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleio.go
162 lines (140 loc) · 2.88 KB
/
simpleio.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package simpleio
import (
"fmt"
"io"
"os"
"strings"
)
//FileHandler represents a text file
type FileHandler struct {
File *os.File
Name string
NameWithOutExtension string
Extension string
EOF bool
}
//OpenFile will open a given file for reading
func (fh *FileHandler) OpenFile(file string) {
fhdl, err := os.Open(file)
check(err)
filename := GetFileNameFromPath(fhdl.Name())
fh.File = fhdl
fh.Name = filename
parts := strings.Split(filename, ".")
length := len(parts)
if length > 0 {
fh.Extension = parts[length-1]
fh.NameWithOutExtension = strings.Replace(filename, "."+parts[length-1], "", -1)
}
}
//GetFileNameFromPath will extract a file name from the given path
func GetFileNameFromPath(path string) string {
parts := strings.Split(path, "\\")
length := len(parts)
if length > 1 {
return parts[length-1]
}
parts = strings.Split(path, "/")
length = len(parts)
if length > 1 {
return parts[length-1]
}
return ""
}
//Close will close file
func (fh *FileHandler) Close() {
fh.File.Close()
}
//ReadLine reads the next line
func (fh *FileHandler) ReadLine() string {
line := ""
buf := make([]byte, 1)
for {
_, err := fh.File.Read(buf)
if err == io.EOF {
fh.EOF = true
return line
}
check(err)
line = line + string(buf)
if strings.Index(line, "\r\n") != -1 {
result := strings.Trim(line, "\r\n")
return result
}
}
}
//ReadLines reads all the next line
func (fh *FileHandler) ReadLines() []string {
line := ""
lines := make([]string, 1)
buf := make([]byte, 1)
fh.File.Seek(0, 0)
for {
_, err := fh.File.Read(buf)
if err == io.EOF {
fh.EOF = true
return lines
}
check(err)
line = line + string(buf)
if strings.Index(line, "\r\n") != -1 {
result := strings.Trim(line, "\r\n")
lines = append(lines, result)
line = ""
}
}
}
//ReadBlock reads a specified amount of characters
func (fh *FileHandler) ReadBlock(start int64, count int64) string {
block := ""
buf := make([]byte, count)
_, err := fh.File.ReadAt(buf, start)
block = string(buf)
if err == io.EOF {
fh.EOF = true
return block
}
check(err)
return block
}
//ReadToEnd reads entier file
func (fh *FileHandler) ReadToEnd() string {
line := ""
buf := make([]byte, 1)
fh.File.Seek(0, 0)
for {
_, err := fh.File.Read(buf)
if err == io.EOF {
fh.EOF = true
return line
}
check(err)
line = line + string(buf)
}
}
//GetLength gets the length of the content
func (fh *FileHandler) GetLength() int64 {
var length int64
length = 0
buf := make([]byte, 1)
fh.File.Seek(0, 0)
for {
_, err := fh.File.Read(buf)
if err == io.EOF {
fh.EOF = true
return length
}
check(err)
length = length + 1
}
}
//Reset will set file reading start position to 0
func (fh *FileHandler) Reset() {
fh.File.Seek(0, 0)
}
func check(e error) {
if e != nil {
fmt.Println(e.Error())
panic(e)
}
}