From 4fb65177592f3bced7a2232d5cffb2f3d8e244a0 Mon Sep 17 00:00:00 2001 From: Kejneafout Date: Sun, 7 Apr 2024 01:21:34 +0000 Subject: [PATCH] Added dynamic check for nb of metadata lines --- src/fetchSeriesValues.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/fetchSeriesValues.go b/src/fetchSeriesValues.go index d5f77d8..d1cd231 100644 --- a/src/fetchSeriesValues.go +++ b/src/fetchSeriesValues.go @@ -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()) } @@ -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