Skip to content

Commit 2e51e66

Browse files
committed
Update CSVinterface.cls
1 parent b72ce55 commit 2e51e66

File tree

1 file changed

+66
-43
lines changed

1 file changed

+66
-43
lines changed

src/CSVinterface.cls

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -272,18 +272,18 @@ ErrHandler_CloseConnection:
272272
P_ERROR_NUMBER = Err.number
273273
P_ERROR_SOURCE = Err.source
274274
End Sub
275-
Private Sub CreateJagged(ByRef ArrVar() As Variant, VectorSize As Long)
276-
Attribute CreateJagged.VB_Description = "Creates an empty array of vector, each of which have a fixed custom size."
277-
Dim arrUB As Long, arrLB As Long, Vector() As String
275+
Public Sub CreateJagged(ByRef ArrVar() As Variant, ArraySize As Long, VectorSize As Long)
276+
Attribute CreateJagged.VB_Description = "Creates an empty array of vectors, each of which having a fixed custom size."
277+
Dim Vector() As String
278278
Dim arrPointer As Long
279279

280+
ReDim ArrVar(0 To ArraySize)
280281
ReDim Vector(0 To VectorSize)
281-
282-
arrUB = UBound(ArrVar)
283-
arrLB = LBound(ArrVar)
284-
For arrPointer = arrLB To arrUB
282+
283+
For arrPointer = 0 To ArraySize
285284
ArrVar(arrPointer) = Vector()
286285
Next arrPointer
286+
Erase Vector
287287
End Sub
288288
Private Function CSVcolumns(csvArray() As String, _
289289
Optional FieldsDelimiter As String = CHR_COMMA) As Long
@@ -661,8 +661,7 @@ Attribute ImportFromCSV.VB_Description = "Imports the content of a CSV file, loc
661661
'Read whole file content
662662
GetCSVtext FileContent
663663
If LenB(FileContent) = 0 Then
664-
ReDim P_CSV_DATA(0)
665-
Call CreateJagged(P_CSV_DATA, 0)
664+
Call CreateJagged(P_CSV_DATA, 0, 0)
666665
P_CSV_DATA(0) = Split(FileContent, vbCrLf)
667666
Exit Sub
668667
End If
@@ -708,8 +707,7 @@ Public Sub ImportFromCSVString(ByRef CSVString As String, _
708707
Attribute ImportFromCSVString.VB_Description = "Imports the content of a CSV file, holded in a string variable, to the current instance."
709708
If PassControlToOS Then DoEvents 'Pass the control to the Operative System
710709
If LenB(CSVString) = 0 Then
711-
ReDim P_CSV_DATA(0)
712-
Call CreateJagged(P_CSV_DATA, 0)
710+
Call CreateJagged(P_CSV_DATA, 0, 0)
713711
P_CSV_DATA(0) = Split(CSVString, vbCrLf)
714712
Exit Sub
715713
End If
@@ -751,16 +749,18 @@ Attribute IsANSI.VB_Description = "Verifies the charset for ANSI encoding."
751749
Erase bytFile
752750
IsANSI = (lngIndx > lngUprBnd)
753751
End Function
754-
Private Function IsJaggedArray(arr As Variant) As Boolean
752+
Public Function IsJaggedArray(Arr As Variant) As Boolean
755753
Attribute IsJaggedArray.VB_Description = "Checks if the given array is an array of arrays."
756754
On Error GoTo IsJaggedArray_Err_Handler
757-
If MultiDimensional(arr) Then
758-
IsJaggedArray = False
759-
Else
760-
Dim BoundingTest As Variant
761-
BoundingTest = arr(LBound(arr))
762-
BoundingTest = LBound(BoundingTest)
763-
IsJaggedArray = True
755+
If IsArray(Arr) Then
756+
If MultiDimensional(Arr) Then
757+
IsJaggedArray = False
758+
Else
759+
Dim BoundingTest As Variant
760+
BoundingTest = Arr(LBound(Arr))
761+
BoundingTest = LBound(BoundingTest)
762+
IsJaggedArray = True
763+
End If
764764
End If
765765
Exit Function
766766
IsJaggedArray_Err_Handler:
@@ -784,9 +784,8 @@ Attribute IsWorkbookOpen.VB_Description = "Checks if the given Workbook is opene
784784
Next
785785
IsWorkbookOpen = BookMatching
786786
End Function
787-
Private Sub JaggedToTwoDimArray(ByRef JaggedArray As Variant)
788-
Attribute JaggedToTwoDimArray.VB_Description = "Turns a jagged array to a 2D string array."
789-
Dim TwoDimArray() As Variant
787+
Public Sub JaggedToTwoDimArray(ByRef JaggedArray() As Variant, ByRef TwoDimArray() As String)
788+
Attribute JaggedToTwoDimArray.VB_Description = "Deconstructs a jagged array and puts its content into a 2D string array."
790789
Dim UBj1 As Long, LBj1 As Long
791790
Dim UBj2 As Long, LBj2 As Long
792791
Dim MaxDim1 As Long, MaxDim2 As Long
@@ -814,8 +813,6 @@ Attribute JaggedToTwoDimArray.VB_Description = "Turns a jagged array to a 2D str
814813
Next jgdCounter2
815814
Erase JaggedArray(jgdCounter1) 'Free memory
816815
Next jgdCounter1
817-
JaggedArray = TwoDimArray
818-
Erase TwoDimArray
819816
JaggedToTwoDimArray_Err_Handler:
820817
End Sub
821818
Private Function JoinRecordsFields(ByRef RecordsArray As Variant) As String
@@ -943,11 +940,42 @@ Attribute JoinRecordsFields.VB_Description = "Joins all fields for each record i
943940
'Free the memory
944941
Erase ConcatenatedArray
945942
Erase buffer
946-
Else 'Recursive call
943+
Else
944+
'@----------------------------------------------------------------------------
945+
'Set array sizes
946+
ReDim ConcatenatedArray(LB1 To UB1)
947+
'@----------------------------------------------------------------------------
948+
'Concatenate fields
949+
Dim tmpBuffer As Variant
950+
For iLCounter = LB1 To UB1
951+
tmpBuffer = RecordsArray(iLCounter)
952+
LB2 = LBound(tmpBuffer)
953+
UB2 = UBound(tmpBuffer)
954+
ReDim buffer(LB2 To UB2)
955+
For jLCounter = LB2 To UB2
956+
If InStrB(1, tmpBuffer(jLCounter), P_FIELDS_DELIMITER) Then
957+
JoinBuffer(1) = tmpBuffer(jLCounter)
958+
buffer(jLCounter) = Join$(JoinBuffer, vbNullString)
959+
ElseIf InStrB(1, tmpBuffer(jLCounter), CoerceChr) Then
960+
JoinBuffer(1) = tmpBuffer(jLCounter)
961+
buffer(jLCounter) = Join$(JoinBuffer, vbNullString)
962+
ElseIf InStrB(1, tmpBuffer(jLCounter), P_RECORDS_DELIMITER) Then
963+
JoinBuffer(1) = tmpBuffer(jLCounter)
964+
buffer(jLCounter) = Join$(JoinBuffer, vbNullString)
965+
Else
966+
buffer(jLCounter) = tmpBuffer(jLCounter)
967+
End If
968+
JoinBuffer(1) = vbNullString
969+
Next jLCounter
970+
ConcatenatedArray(iLCounter) = Join$(buffer, P_FIELDS_DELIMITER)
971+
Next iLCounter
947972
'@----------------------------------------------------------------------------
948-
'Transform array
949-
Call JaggedToTwoDimArray(RecordsArray)
950-
JoinRecordsFields = JoinRecordsFields(RecordsArray)
973+
'Concatenate records
974+
JoinRecordsFields = Join$(ConcatenatedArray, P_RECORDS_DELIMITER)
975+
'@----------------------------------------------------------------------------
976+
'Free the memory
977+
Erase ConcatenatedArray
978+
Erase buffer
951979
End If
952980
Case Else
953981
If Not IsJagged Then
@@ -1311,9 +1339,7 @@ Attribute ParseCriticalCSV.VB_Description = "Parses text strings CSV
13111339
HeaderSize = CurrentBufferIndex
13121340
P_MAX_JAGG_INDEX = HeaderSize
13131341
CurrentRecordSize = P_MAX_JAGG_INDEX
1314-
'ReDim P_CSV_DATA(LBound(tmpCSV) To ImportDepth - 1&, 0 To CurrentRecordSize)
1315-
ReDim P_CSV_DATA(LBound(tmpCSV) To ImportDepth - 1&)
1316-
Call CreateJagged(P_CSV_DATA, HeaderSize)
1342+
Call CreateJagged(P_CSV_DATA, ImportDepth - 1&, HeaderSize)
13171343
For Counter1 = 0 To HeaderSize
13181344
P_CSV_DATA(0)(Counter1) = buffer(Counter1)
13191345
Next Counter1
@@ -1629,8 +1655,7 @@ Attribute ParseCSV.VB_Description = "Parses text strings CSV
16291655
lRows = lngUB - lngLB
16301656
lColumns = CSVcolumns(csvArray, DataDelimiter) - 1
16311657
lCounter = lngLB + hPos
1632-
ReDim P_CSV_DATA(0 To lRows - hPos)
1633-
Call CreateJagged(P_CSV_DATA, lColumns)
1658+
Call CreateJagged(P_CSV_DATA, lRows - hPos, lColumns)
16341659
P_MAX_JAGG_INDEX = lColumns
16351660
'@----------------------------------------------------------------------------
16361661
'Process the data
@@ -1732,7 +1757,7 @@ Attribute ResetToDefault.VB_Description = "Resets the all the options to its def
17321757
P_SUCCESSFUL_IMPORT = False
17331758
End Sub
17341759
Private Sub SkipUnwantedLines(ByRef Idx As Long, ByRef MaxIdx As Long, _
1735-
ByRef arr() As String)
1760+
ByRef Arr() As String)
17361761
Attribute SkipUnwantedLines.VB_Description = "Ignores empty, blanks and commented lines."
17371762
Dim CurLength As Long
17381763
Dim CharCode As Long
@@ -1743,13 +1768,13 @@ Attribute SkipUnwantedLines.VB_Description = "Ignores empty, blanks and commente
17431768
'Skip commented and blank lines if needed
17441769
If Idx <= MaxIdx Then
17451770
Do
1746-
CurLength = LenB(arr(Idx))
1771+
CurLength = LenB(Arr(Idx))
17471772
UnwantedLine = False
17481773
StrPointer = 1&
17491774
If CurLength = 0 Then 'Empty line found
17501775
UnwantedLine = True
17511776
Else
1752-
CharCode = AscW(arr(Idx))
1777+
CharCode = AscW(Arr(Idx))
17531778
If CharCode = P_COMMENTLINEINDICATOR Then 'Commented line found
17541779
UnwantedLine = True
17551780
Else
@@ -1761,19 +1786,19 @@ Attribute SkipUnwantedLines.VB_Description = "Ignores empty, blanks and commente
17611786
UnwantedLine = True
17621787
Exit Do
17631788
End If
1764-
CharCode = AscW(MidB$(arr(Idx), StrPointer, 2))
1789+
CharCode = AscW(MidB$(Arr(Idx), StrPointer, 2))
17651790
Loop
17661791
If CharCode = P_COMMENTLINEINDICATOR Then UnwantedLine = True
17671792
End If
17681793
End If
17691794
If UnwantedLine Then
17701795
Idx = Idx + 1&
1771-
CurLength = LenB(arr(Idx))
1796+
CurLength = LenB(Arr(Idx))
17721797
End If
17731798
Loop While UnwantedLine And Idx <= MaxIdx
17741799
End If
17751800
End Sub
1776-
Public Sub TwoDimToJaggedArray(ByRef TwoDimArray() As Variant, ByRef JaggedArray() As Variant)
1801+
Public Sub TwoDimToJaggedArray(ByRef TwoDimArray() As String, ByRef JaggedArray() As Variant)
17771802
Attribute TwoDimToJaggedArray.VB_Description = "Deconstructs a 2D string array and puts its content into a jagged array."
17781803
Dim UBj1 As Long, LBj1 As Long
17791804
Dim UBj2 As Long, LBj2 As Long
@@ -1792,8 +1817,7 @@ Attribute TwoDimToJaggedArray.VB_Description = "Deconstructs a 2D string array a
17921817
MaxDim2 = Abs(UBj2 - LBj2) 'Dimension Two in base 0
17931818
'@----------------------------------------------
17941819
'Create the jagged array
1795-
ReDim JaggedArray(0 To MaxDim1)
1796-
Call CreateJagged(JaggedArray, MaxDim2)
1820+
Call CreateJagged(JaggedArray, MaxDim1, MaxDim2)
17971821
'@----------------------------------------------
17981822
'Deconstruct and dump the data
17991823
jgdCounter1 = 0
@@ -1813,8 +1837,7 @@ Attribute TwoDimToJaggedArray.VB_Description = "Deconstructs a 2D string array a
18131837
MaxDim2 = 0 'Dimension Two in base 0
18141838
'@----------------------------------------------
18151839
'Create the jagged array
1816-
ReDim JaggedArray(0 To MaxDim1)
1817-
Call CreateJagged(JaggedArray, MaxDim2)
1840+
Call CreateJagged(JaggedArray, MaxDim1, MaxDim2)
18181841
'@----------------------------------------------
18191842
'Deconstruct and dump the data
18201843
jgdCounter1 = 0

0 commit comments

Comments
 (0)