begin implementing separate metadata parsing
This commit is contained in:
parent
709a2738f9
commit
35c14f09c0
6 changed files with 192 additions and 2 deletions
|
@ -1,6 +1,8 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
@ -56,3 +58,25 @@ func CopyFile(inPath string, outPath string) error {
|
|||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// ReadNLines reads the first N lines from a file as a single byte array
|
||||
func ReadNLines(filename string, n int) ([]byte, error) {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var buffer bytes.Buffer
|
||||
scanner := bufio.NewScanner(file)
|
||||
for i := 0; i < 3 && scanner.Scan(); i++ {
|
||||
buffer.Write(scanner.Bytes())
|
||||
buffer.WriteByte('\n')
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return buffer.Bytes(), nil
|
||||
}
|
||||
|
|
26
internal/util/queue.go
Normal file
26
internal/util/queue.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package util
|
||||
|
||||
// Enqueue appends an int to the queue
|
||||
func Enqueue(queue []int, element int) []int {
|
||||
queue = append(queue, element)
|
||||
return queue
|
||||
}
|
||||
|
||||
// Dequeue pops the first element of the queue
|
||||
func Dequeue(queue []int) (int, []int) {
|
||||
element := queue[0] // The first element is the one to be dequeued.
|
||||
if len(queue) == 1 {
|
||||
tmp := []int{}
|
||||
return element, tmp
|
||||
}
|
||||
return element, queue[1:] // Slice off the element once it is dequeued.
|
||||
}
|
||||
|
||||
func Tail(queue []int) int {
|
||||
l := len(queue)
|
||||
if l == 0 {
|
||||
return -1
|
||||
} else {
|
||||
return l - 1
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue