-
Notifications
You must be signed in to change notification settings - Fork 0
/
test2.go
202 lines (166 loc) · 5.13 KB
/
test2.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/transform"
)
func sanitizeString(input string, replacement rune) (string, error) {
// Create a decoder for the specific encoding (e.g., Windows-1252)
decoder := charmap.Windows1252.NewDecoder()
// Convert the input string to bytes
inputBytes := []byte(input)
// Decode the bytes using the specified decoder
decodedBytes, err := decoder.Bytes(inputBytes)
if err != nil {
return "", err
}
// Convert the decoded bytes back to string
sanitizedString := string(decodedBytes)
// Replace non-UTF-8 characters with the replacement character
//utf8String, err := encoding.UTF8Validator.Transform([]byte(sanitizedString), replacement) //Replace([]byte(sanitizedString), replacement)
if err != nil {
return "", err
}
//return string(utf8String), nil
return sanitizedString, nil
}
func sanitizeString2(input string, replacement rune) (string, error) {
// Create a decoder for the specific encoding (e.g., Windows-1252)
decoder := charmap.Windows1252.NewDecoder()
// Convert the input string to bytes
inputBytes := []byte(input)
// Decode the bytes using the specified decoder
decodedBytes, _, err := transform.Bytes(decoder, inputBytes)
if err != nil {
return "", err
}
// Replace non-UTF-8 characters with the replacement character
for i := range decodedBytes {
if decodedBytes[i] >= 0x80 && decodedBytes[i] <= 0xBF {
decodedBytes[i] = byte(replacement)
}
}
// Convert the decoded bytes back to string
sanitizedString := string(decodedBytes)
return sanitizedString, nil
}
// Check if the line ending is valid
func isValidLineEnding(lineEnding string) bool {
trimmed := strings.TrimSpace(lineEnding)
return trimmed == "2#EndOfLine#" || strings.HasPrefix(trimmed, "2#EndOfLine#")
}
func replaceInvalidSymbol(text string) string {
replacedText := strings.ReplaceAll(text, "�", "_")
return replacedText
}
func sanitizeFile(filename string) error {
// Read the file content
content, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
// Replace "�" with "_"
modifiedContent := strings.ReplaceAll(string(content), "�", "_")
// Write the modified content back to the file
err = ioutil.WriteFile(filename, []byte(modifiedContent), 0644)
if err != nil {
return err
}
return nil
}
func main() {
// Assuming the input string is stored in the 'data' variable
if sanitizeFile("msg_as2_att.txt") != nil {
_, err := sanitizeString("msg_as2_att.txt", '_')
fmt.Println("Failed to sanitize file:", err)
os.Exit(1)
}
data, err := ioutil.ReadFile("msg_as2_att.txt")
if err != nil {
fmt.Println("Failed to read file:", err)
os.Exit(1)
}
// Convert the data to a string
dataStr := string(data)
// Split the data into rows based on the newline character
rows := strings.Split(dataStr, "\n")
for i, row := range rows {
// Split the row into columns using the separator ";," or any whitespace
columns := regexp.MustCompile(`[;\s]*,[;\s]*`).Split(row, -1)
if len(columns) != 10 {
fmt.Println("Invalid number of columns")
os.Exit(1)
}
// Check if the line ending is valid
lineEnd := columns[9]
if !isValidLineEnding(lineEnd) {
fmt.Println("Invalid line ending")
os.Exit(1)
}
// Retrieve column values
column1 := columns[0]
column2 := columns[1]
column3 := columns[2]
column4 := columns[3]
column5 := columns[4]
column6 := columns[5]
column7 := columns[6]
column8 := columns[7]
column9 := columns[8]
column10 := columns[9]
// If line ends with "2#EndOfLine#", skip to the next line
if strings.HasSuffix(lineEnd, "2#EndOfLine#") {
continue
}
fmt.Println("Row", i+1)
fmt.Println("Column 1:", column1)
fmt.Println("Column 2:", column2)
fmt.Println("Column 3:", column3)
fmt.Println("Column 4:", column4)
fmt.Println("Column 5:", column5)
fmt.Println("Column 6:", column6)
fmt.Println("Column 7:", column7)
fmt.Println("Column 8:", column8)
fmt.Println("Column 9:", column9)
fmt.Println("Column 10:", column10)
// New attempt
// Split the data into columns based on the separator " ;,"
columns := strings.Split(data, ";,")
// Retrieve the last column value
lastColumn := columns[len(columns)-1]
// Decode the Base64 encoded content
decodedContent, err := base64.StdEncoding.DecodeString(lastColumn)
if err != nil {
fmt.Println("Failed to decode Base64 content:", err)
return
}
// end of new attempt
// Decode the base64 encoded file content
fileContent, err := base64.StdEncoding.DecodeString(column9)
if err != nil {
fmt.Println("Failed to decode file content:", err)
os.Exit(1)
}
// Write the file content to a file
os.Mkdir("files", 0777)
sanitized1, err := sanitizeString2(column6, '_')
if err != nil {
fmt.Println("Failed to sanitize string:", err)
return
}
sanitized_name, err := replaceInvalidSymbol(sanitized1), nil
files := "files/" + strconv.Itoa(i) + "_" + sanitized_name
err = ioutil.WriteFile(files, fileContent, 0644)
if err != nil {
fmt.Println("Failed to write file:", err)
os.Exit(1)
}
fmt.Println("File generated successfully.")
}
}