Skip to content

Commit

Permalink
properly error if tier parsing fails
Browse files Browse the repository at this point in the history
  • Loading branch information
vocatart committed Oct 28, 2024
1 parent 7a106d6 commit 21d6cfc
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions textgrid/textgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (tg *TextGrid) GetTier(name string) Tier {

// ReadTextgrid - Takes a path to a .TextGrid file and reads its contents into a TextGrid.
func ReadTextgrid(path string) (TextGrid, error) {
tg := TextGrid{}
var tg = TextGrid{}
tgDeque := deque.New[string]()

// grab the name element from the path
Expand Down Expand Up @@ -138,7 +138,11 @@ func ReadTextgrid(path string) (TextGrid, error) {
// get the number of tiers that exist in this textgrid
numTiers := pullInt(tgDeque.PopFront())

tg.tiers = parseTiers(globalXmin, globalXmax, tgDeque, numTiers)
tiers, err := parseTiers(globalXmin, globalXmax, tgDeque, numTiers)
if err != nil {
return tg, err
}
tg.tiers = tiers

return tg, nil
}
Expand All @@ -147,7 +151,7 @@ func ReadTextgrid(path string) (TextGrid, error) {
// func (tg TextGrid) WriteLong(path string, overwrite ...bool) {
// }

func parseTiers(globalXmin float64, globalXmax float64, content *deque.Deque[string], numTiers int) []Tier {
func parseTiers(globalXmin float64, globalXmax float64, content *deque.Deque[string], numTiers int) ([]Tier, error) {
var tiers []Tier
tierCounter := 0

Expand All @@ -162,10 +166,10 @@ func parseTiers(globalXmin float64, globalXmax float64, content *deque.Deque[str

// check to see if any boundaries are inconsistent
if tierXmin < globalXmin {
log.Fatalf("error: %s %s has xmin %f, when TextGrid xmin is %f", tierType, tierName, tierXmin, globalXmin)
return nil, fmt.Errorf("error: %s %s has xmin %f, when TextGrid xmin is %f", tierType, tierName, tierXmin, globalXmin)
}
if tierXmax > globalXmax {
log.Fatalf("error: %s %s has xmax %f, when TextGrid xmax is %f", tierType, tierName, tierXmax, globalXmax)
return nil, fmt.Errorf("error: %s %s has xmax %f, when TextGrid xmax is %f", tierType, tierName, tierXmax, globalXmax)
}

// the last value before the intervals/points begin should be the number of intervals/points in the unique tier
Expand Down Expand Up @@ -212,12 +216,12 @@ func parseTiers(globalXmin float64, globalXmax float64, content *deque.Deque[str
newPointTier := PointTier{name: tierName, xmin: tierXmin, xmax: tierXmax, points: points}
tiers = append(tiers, &newPointTier)
} else {
log.Fatalf("error: unexpected tier type %s", tierType)
return nil, fmt.Errorf("error: unexpected tier type %s", tierType)
}
tierCounter++
}

return tiers
return tiers, nil
}

// turns textgrid file content into a slice of usable strings.
Expand Down

0 comments on commit 21d6cfc

Please sign in to comment.