From 9f289d4731697a3a3fb2d57dfc1c5ec81ef34d9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=C3=A1n=20C=20McCord?= Date: Mon, 26 Apr 2021 10:46:17 -0700 Subject: [PATCH] initial commit --- go.mod | 3 +++ main.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test.txt | 7 +++++++ 3 files changed, 74 insertions(+) create mode 100644 go.mod create mode 100644 main.go create mode 100644 test.txt diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..40d8473 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.cycore.io/kl/srt-indexer + +go 1.16 diff --git a/main.go b/main.go new file mode 100644 index 0000000..7037780 --- /dev/null +++ b/main.go @@ -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) + } + } + } + + +} diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..c1efa08 --- /dev/null +++ b/test.txt @@ -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.