-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.go
More file actions
54 lines (45 loc) · 1.48 KB
/
split.go
File metadata and controls
54 lines (45 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
)
// splitFileImpl is the actual implementation of the split functionality.
func splitFileImpl(filename string, outputDir string) error {
origFilename := filepath.Join(outputDir, fmt.Sprintf("%s.orig", filepath.Base(filename)))
altFilename := filepath.Join(outputDir, fmt.Sprintf("%s.alt", filepath.Base(filename)))
// Ensure output directory exists
if err := os.MkdirAll(outputDir, 0755); err != nil {
return fmt.Errorf("error creating directory %s: %v", outputDir, err)
}
origFile, err := os.Create(origFilename)
if err != nil {
return fmt.Errorf("error creating file %s: %v", origFilename, err)
}
defer origFile.Close()
altFile, err := os.Create(altFilename)
if err != nil {
return fmt.Errorf("error creating file %s: %v", altFilename, err)
}
defer altFile.Close()
inputFile, err := os.Open(filename)
if err != nil {
return fmt.Errorf("error opening file %s: %v", filename, err)
}
defer inputFile.Close()
scanner := bufio.NewScanner(inputFile)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "orig: ") {
origFile.WriteString(strings.TrimSuffix(strings.TrimPrefix(line, "orig: "), "%K%P") + "\n")
} else if strings.HasPrefix(line, "alt(en): ") {
altFile.WriteString(strings.TrimSuffix(strings.TrimPrefix(line, "alt(en): "), "%K%P") + "\n")
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("error reading file %s: %v", filename, err)
}
return nil
}