-
Notifications
You must be signed in to change notification settings - Fork 1
/
Redelimiter.vb
130 lines (94 loc) · 4.54 KB
/
Redelimiter.vb
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
Imports System.IO
Public Class Redelimiter
Public Shared Function Redelimit(ByVal delimiter As String, ByVal hasHeaders As Boolean, ByVal addPk As Boolean, ByVal InputFile As String, ByVal OutputFile As String) As Integer
Dim objReader As StreamReader = Nothing
Dim count As Integer = 0
Try
objReader = New StreamReader(File.OpenRead(InputFile), System.Text.Encoding.Default)
Catch ex As Exception
MsgBox(ex.Message)
End Try
If objReader Is Nothing Then
MsgBox("Invalid file: " & InputFile)
count = -1
Exit Function
End If
Redelimiter.SaveTextToFile(Redelimiter.ProcessFile(objReader, delimiter, hasHeaders, addPk, count), OutputFile)
objReader.Close()
End Function
Public Shared Function ProcessFile(ByVal reader As StreamReader, ByVal delimiter As String, ByVal hasHeaders As Boolean, ByVal addPk As Boolean, ByRef count As Integer) As String
Dim ph1 As String = delimiter
Dim sb As New System.Text.StringBuilder
'grab the first line
Dim line = Trim(reader.ReadLine)
'advance to the next line if the first line is column headings
If hasHeaders Then
line = Trim(reader.ReadLine)
End If
While Not String.IsNullOrEmpty(line) 'loop through each line
count += 1
'Replace commas with our custom-made delimiter
line = line.Replace(",", ph1)
'Find a quoted part of the line, which could legitimately contain commas.
'In that case we will need to identify the quoted section and swap commas back in for our custom placeholder.
Dim starti = line.IndexOf(ph1 & """", 0)
If line.IndexOf("""",0) = 0 then starti=0
While starti > -1 'loop through quoted fields
Dim FieldTerminatorFound As Boolean = False
'Find end quote token (originally a ",)
Dim endi As Integer = line.IndexOf("""" & ph1, starti)
If endi < 0 Then
FieldTerminatorFound = True
If endi < 0 Then endi = line.Length - 1
End If
While Not FieldTerminatorFound
'Find any more quotes that are part of that sequence, if any
Dim backChar As String = """" 'thats one quote
Dim quoteCount = 0
While backChar = """"
quoteCount += 1
backChar = line.Chars(endi - quoteCount)
End While
If quoteCount Mod 2 = 1 Then 'odd number of quotes. real field terminator
FieldTerminatorFound = True
Else 'keep looking
endi = line.IndexOf("""" & ph1, endi + 1)
End If
End While
'Grab the quoted field from the line, now that we have the start and ending indices
Dim source As String
If starti = 0 Then
source = line.Substring(starti, endi - starti + 1)
Else
source = line.Substring(starti + ph1.Length, endi - starti - ph1.Length + 1)
End If
'And swap the commas back in
line = line.Replace(source, source.Replace(ph1, ","))
'Find the next quoted field
' If endi >= line.Length - 1 Then endi = line.Length 'During the swap, the length of line shrinks so an endi value at the end of the line will fail
starti = line.IndexOf(ph1 & """", starti + ph1.Length)
End While
'Add our primary key to the line
If addPk Then
line = String.Concat(count.ToString, ph1, line)
End If
line = line.Replace(delimiter & " ", delimiter)
sb.AppendLine(line)
line = Trim(reader.ReadLine)
End While
Return Left(sb.ToString, sb.ToString.Length - 2)
End Function
Public Shared Function SaveTextToFile(ByVal strData As String, ByVal FullPath As String) As Boolean
Dim bAns As Boolean = False
Dim objReader As StreamWriter
Try
objReader = New StreamWriter(FullPath, False, System.Text.Encoding.Default)
objReader.Write(strData)
objReader.Close()
bAns = True
Catch Ex As Exception
Throw Ex
End Try
Return bAns
End Function
End Class