Compare commits

..

2 Commits
1.0.1 ... main

Author SHA1 Message Date
60bafd0870 Add collection and history saving 2024-10-15 04:22:01 +02:00
8e9d5e90ba Add string methods 2024-10-11 17:48:31 +02:00
2 changed files with 62 additions and 0 deletions

View File

@ -22,3 +22,29 @@ func ReadFileAndConvertToCollection(filePath string) (*NML, error) {
return &collection, nil return &collection, nil
} }
func CollectionFromString(content string) (*NML, error) {
// Unmarshal the XML content into the Collection struct
var collection NML
err := xml.Unmarshal([]byte(content), &collection)
if err != nil {
return nil, err
}
return &collection, nil
}
func SaveCollectionToFile(collection *NML, filePath string) error {
// Marshal the Collection struct into XML content
content, err := xml.MarshalIndent(collection, "", " ")
if err != nil {
return err
}
// Write the XML content to the file
err = os.WriteFile(filePath, content, 0644)
if err != nil {
return err
}
return nil
}

View File

@ -30,6 +30,22 @@ func ReadFileAndConvertToHistory(filePath string) (*NML, error) {
return nil, errors.New("file is not a history file") return nil, errors.New("file is not a history file")
} }
func HistoryFromString(content string) (*NML, error) {
// Unmarshal the XML content into the NML struct
var nml NML
err := xml.Unmarshal([]byte(content), &nml)
if err != nil {
return nil, err
}
// Check if the file is a history file based on the content
if IsHistoryString(content) {
return &nml, nil
}
return nil, errors.New("file is not a history file")
}
func IsHistoryFile(path string) bool { func IsHistoryFile(path string) bool {
content, err := os.ReadFile(path) content, err := os.ReadFile(path)
if err != nil { if err != nil {
@ -37,3 +53,23 @@ func IsHistoryFile(path string) bool {
} }
return strings.Contains(string(content), "HistoryData") return strings.Contains(string(content), "HistoryData")
} }
func IsHistoryString(content string) bool {
return strings.Contains(content, "HistoryData")
}
func SaveHistoryToFile(history *NML, filePath string) error {
// Marshal the NML struct into XML content
content, err := xml.MarshalIndent(history, "", " ")
if err != nil {
return err
}
// Write the XML content to the file
err = os.WriteFile(filePath, content, 0644)
if err != nil {
return err
}
return nil
}