Skip to content

Commit

Permalink
Added dynamic check for nb of metadata lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Kejneafout committed Apr 7, 2024
1 parent ebf8a36 commit 4fb6517
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/fetchSeriesValues.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,26 @@ func FetchSeriesValues(inputFile string) (data Data, err error) {
var head []string
var body []string

// Read the first 7 lines for metadata
for i := 0; i < 6 && scanner.Scan(); i++ {
var totalLines int

// Read the whole file to count the number of lines
for scanner.Scan() {
totalLines++
}

metadataLines := 4 // Default to 4 lines of metadata

// Decide metadata lines based on total lines
if totalLines >= 22 {
metadataLines = 7
}

// Reset file scanner
file.Seek(0, 0) // Reset file pointer to beginning
scanner = bufio.NewScanner(file)

// Read metadata
for i := 0; i < metadataLines && scanner.Scan(); i++ {
head = append(head, scanner.Text())
}

Expand All @@ -50,7 +68,7 @@ func FetchSeriesValues(inputFile string) (data Data, err error) {
}

title := strings.TrimSpace(head[0])
total := regexp.MustCompile(`\d+`).FindString(head[5])
total := regexp.MustCompile(`\d+`).FindString(head[metadataLines - 2])

data.Metadata.Title = title
data.Metadata.Total = total
Expand Down

0 comments on commit 4fb6517

Please sign in to comment.