TraktorNmlUtils/models/history/HistoryHandler.go
2024-10-11 15:25:16 +02:00

40 lines
805 B
Go

package history
import (
"encoding/xml"
"errors"
"os"
"strings"
)
// ReadFileAndConvertToHistory reads a file and converts it to the NML struct
func ReadFileAndConvertToHistory(filePath string) (*NML, error) {
// Read the file content
fileContent, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
// Unmarshal the XML content into the NML struct
var nml NML
err = xml.Unmarshal(fileContent, &nml)
if err != nil {
return nil, err
}
// Check if the file is a history file based on the content
if IsHistoryFile(filePath) {
return &nml, nil
}
return nil, errors.New("file is not a history file")
}
func IsHistoryFile(path string) bool {
content, err := os.ReadFile(path)
if err != nil {
return false
}
return strings.Contains(string(content), "HistoryData")
}