initial commit
This commit is contained in:
commit
9f289d4731
3 changed files with 74 additions and 0 deletions
3
go.mod
Normal file
3
go.mod
Normal file
|
@ -0,0 +1,3 @@
|
|||
module git.cycore.io/kl/srt-indexer
|
||||
|
||||
go 1.16
|
64
main.go
Normal file
64
main.go
Normal file
|
@ -0,0 +1,64 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) != 2 {
|
||||
log.Fatalln("one argument: file name")
|
||||
}
|
||||
|
||||
f, err := os.Open(os.Args[1])
|
||||
if err != nil {
|
||||
log.Fatalf("failed to open file %s: %s", os.Args[1], err.Error())
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var i int
|
||||
|
||||
o := os.Stdout
|
||||
defer o.Close()
|
||||
|
||||
// File should always start with '1'
|
||||
i++
|
||||
if _, err = o.WriteString(fmt.Sprintf("%d\n", i)); err != nil {
|
||||
log.Fatalln("failed to write index:", err)
|
||||
}
|
||||
|
||||
r := bufio.NewReader(f)
|
||||
|
||||
var emptyLine bool
|
||||
for {
|
||||
emptyLine = false
|
||||
|
||||
l, err := r.ReadString('\n')
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatalln("failed to read line:", err)
|
||||
}
|
||||
|
||||
if len(l) == 1 {
|
||||
emptyLine = true
|
||||
}
|
||||
|
||||
if _, err = o.WriteString(l); err != nil {
|
||||
log.Fatalln("failed to write to output:", err)
|
||||
}
|
||||
|
||||
if emptyLine {
|
||||
i++
|
||||
if _, err := o.WriteString(fmt.Sprintf("%d\n", i)); err != nil {
|
||||
log.Fatalln("failed to write index:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
7
test.txt
Normal file
7
test.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
This is a first line
|
||||
This is a first additional line.
|
||||
|
||||
This is a second stanza. There are more
|
||||
stanzae to be had.
|
||||
|
||||
There are times in which authorities should be held accountable.
|
Loading…
Reference in a new issue