home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / text_utl / parsed / parse.bas < prev    next >
Encoding:
BASIC Source File  |  1994-10-04  |  8.8 KB  |  271 lines

  1. Option Explicit
  2. Option Compare Text
  3.  
  4. '9/25/94 PWR
  5. 'Routine to accept a string to work on and a delimiter,
  6. 'and fill the passed dynamic array with parsed data.
  7. '
  8. 'Parameters:
  9. '
  10. 'TheString$ - String to parse
  11. 'Delim$ - Delimiting string (may be single or multiple characters)
  12. 'DynArray$() - An 'unredimmed' dynamic array passed by reference
  13. '(VB's default) which is filled with the parsed data which can be
  14. 'accessed by the calling routine.
  15. '
  16. 'Return Value: Number of elements.
  17. '
  18. Function ParseAndFillArray1% (TheString$, Delim$, DynArray$())
  19.     Dim ElementCt%, CurPos%, LenAssigned%, CurStrLen%, LenDelim%
  20.     
  21.     LenDelim% = Len(Delim$)
  22.     'initialize array element count
  23.     ElementCt% = 0
  24.     'starting instr pos
  25.     CurPos% = 1
  26.     LenAssigned% = 1
  27.  
  28.     'before going through the array, add a delimiter to the end of
  29.     'the segment if there is not one there already
  30.     If Right$(TheString$, LenDelim%) <> Delim$ Then
  31.     TheString$ = TheString$ & Delim$
  32.     End If
  33.    
  34.     Do
  35.     'get the current segment (not including delimiter)
  36.     CurStrLen% = InStr(CurPos%, TheString$, Delim$) - CurPos%
  37.     'done if delimiter not found
  38.     If CurStrLen% < 0 Then
  39.         Exit Do
  40.     End If
  41.     'we have a string segment, so
  42.     'increment array index for this element
  43.     ElementCt% = ElementCt% + 1
  44.     'allocate the array element
  45.     ReDim Preserve DynArray$(1 To ElementCt%)
  46.     'assign the value to array
  47.     DynArray$(ElementCt%) = Mid$(TheString$, CurPos%, CurStrLen%)
  48.     'assign start position of next element
  49.     'add length of current string to length assigned var.
  50.     LenAssigned% = LenAssigned% + (Len(DynArray$(ElementCt%)) + LenDelim%)
  51.     'set the new starting position (ahead of current delimiter) for next extraction
  52.     CurPos% = LenAssigned%
  53.     Loop
  54.  
  55.     ParseAndFillArray1% = ElementCt%
  56.     'or ParseAndFillArray1% = UBound(DynArray$)
  57. End Function
  58.  
  59. '9/25/94
  60. 'Routine to accept a string to work on and a delimiter,
  61. 'and fill the passed dynamic array with parsed data.
  62. '
  63. 'Parameters:
  64. '
  65. 'TheString$ - String to parse
  66. 'Delim$ - Delimiting string (may be single or multiple characters)
  67. 'DynArray$() - An 'unredimmed' dynamic array passed by reference
  68. '(VB's default) which is filled with the parsed data which can be
  69. 'accessed by the calling routine.
  70. 'SetReDim% - This is a parameter which sets the frequency of array
  71. 'resizing so performance can be optimized. This parm. is the only
  72. 'difference between this routine and ParseAndFillArray1%
  73. '
  74. '
  75. 'Return Value: Number of elements.
  76. '
  77. '
  78. Function ParseAndFillArray2% (TheString$, Delim$, DynArray$(), SetReDim%)
  79.     Dim ElementCt%, CurPos%, LenAssigned%, CurStrLen%, LenDelim%
  80.     
  81.     LenDelim% = Len(Delim$)
  82.     'initialize array element count
  83.     ElementCt% = 0
  84.     'starting instr pos
  85.     CurPos% = 1
  86.     LenAssigned% = 1
  87.    
  88.     'before going through the array, add a delimiter to the end of
  89.     'the segment if there is not one there already
  90.     If Right$(TheString$, LenDelim%) <> Delim$ Then
  91.     TheString$ = TheString$ & Delim$
  92.     End If
  93.     
  94.     'do initial array alloc.
  95.     ReDim DynArray$(1 To SetReDim%)
  96.  
  97.     Do
  98.     'get the current segment (not including delimiter)
  99.     CurStrLen% = InStr(CurPos%, TheString$, Delim$) - CurPos%
  100.     If CurStrLen% < 0 Then
  101.     'if delimiter not found
  102.         'done
  103.         Exit Do
  104.     End If
  105.     'we have a segment, so
  106.     'increment array index for next element
  107.     ElementCt% = ElementCt% + 1
  108.     'allocate the array element if necessary
  109.     If ElementCt% Mod SetReDim% = 0 Then
  110.         ReDim Preserve DynArray$(1 To ElementCt% + SetReDim%)
  111.     End If
  112.     'assign the value to array
  113.     DynArray$(ElementCt%) = Mid$(TheString$, CurPos%, CurStrLen%)
  114.     'assign start position of next element
  115.     'add length of current string to length assigned var.
  116.     LenAssigned% = LenAssigned% + (Len(DynArray$(ElementCt%)) + LenDelim%)
  117.     'set the new starting position (ahead of current delimiter) for next extraction
  118.     CurPos% = LenAssigned%
  119.    Loop
  120.  
  121.    'return # elements
  122.    ParseAndFillArray2% = ElementCt%
  123.   
  124. End Function
  125.  
  126. '9/25/94
  127. 'Routine to accept a string to work on and a delimiter,
  128. 'and fill the passed listbox with parsed data.
  129. '
  130. 'Parameters:
  131. '
  132. 'TheString$ - String to parse
  133. 'Delim$ - Delimiting string (may be single or multiple characters)
  134. 'L - a listbox to fill with the parsed data.
  135. '
  136. 'Return Value: Number of items in listbox.
  137. '
  138. Function ParseAndFillListBox% (TheString$, Delim$, L As ListBox)
  139.     Dim ListCt%, CurPos%, LenAssigned%, CurStrLen%, LenDelim%
  140.     Dim ret& 'needed only if SendMessage method used
  141.     
  142.     LenDelim% = Len(Delim$)
  143.     'initialize list item count
  144.     ListCt% = 0
  145.     'starting instr pos
  146.     CurPos% = 1
  147.     LenAssigned% = 1
  148.     
  149.     'before going through the list, add a delimiter to the end of
  150.     'the segment if there is not one there already
  151.     If Right$(TheString$, LenDelim%) <> Delim$ Then
  152.     TheString$ = TheString$ & Delim$
  153.     End If
  154.     
  155.     Do
  156.     'get the current segment (not including delimiter)
  157.     CurStrLen% = (InStr(CurPos%, TheString$, Delim$) - CurPos%)
  158.     'exit if no segment
  159.     If CurStrLen% < 0 Then
  160.         Exit Do
  161.     End If
  162.  
  163.     'we have a segment, so increment index for next item
  164.     ListCt% = ListCt% + 1
  165.     'use one of following three lines of code to insert the item
  166.     'into the listbox (keep the other two commented out)
  167.     'ret& = SendMessageByString&(L.hWnd, LB_INSERTSTRING, -1, Mid$(TheString$, CurPos%, CurStrLen%))
  168.     'ret& = SendMessageByString&(L.hWnd, LB_ADDSTRING, 0, Mid$(TheString$, CurPos%, CurStrLen%))
  169.     L.AddItem Mid$(TheString$, CurPos%, CurStrLen%)
  170.  
  171.     'assign start position of next element
  172.     'add length of current string to length assigned var.
  173.     LenAssigned% = LenAssigned% + CurStrLen% + LenDelim%
  174.     'set the new starting position (ahead of current delimiter) for next extraction
  175.     CurPos% = LenAssigned%
  176.     Loop
  177.     
  178.     'return list count
  179.     ParseAndFillListBox% = L.ListCount
  180.     
  181. End Function
  182.  
  183. '9/25/94
  184. 'Function to process a passed string array and return
  185. 'a new string consisting of the passed array elements
  186. 'concatenated with a separator. Used in conjunction with
  187. 'ParseAndFillArray%(). If an element is an empty string
  188. '(this will occur when there are two or more consecutive
  189. 'crlf's in the text), discard the crlf and do not add another space.
  190. '
  191. 'Parameters:
  192. '
  193. 'DynArray$() - An 'unredimmed' dynamic array passed by reference
  194. '(VB's default) which is filled with the processed data which can be
  195. 'accessed by the calling routine.
  196. 'separator$ - Character to put between elements in creating the
  197. 'build string.
  198. 'AdjustedLineCt% - A value passed by reference which will return
  199. 'a line count value which does not consider single CRLF's a line.
  200. '
  201. 'Return Value: The string constructed from the array.
  202. '
  203. Function ProcessArray$ (DynArray$(), Separator$, AdjustedLineCt%)
  204.    Dim BuildStr$
  205.    Dim i%
  206.  
  207.    'go through the array (to the second to last element)
  208.    'adding each element to the build string plus the
  209.    'separator. Elements consisting of an empty string are
  210.    'bypassed.
  211.    For i% = 1 To UBound(DynArray$) - 1
  212.       If DynArray(i%) <> "" Then
  213.      BuildStr$ = BuildStr$ & DynArray(i%) & Separator$
  214.      'increment adjusted linecount
  215.      AdjustedLineCt% = AdjustedLineCt% + 1
  216.       End If
  217.    Next i%
  218.    
  219.    'add the last element - do not append separator
  220.    BuildStr$ = BuildStr$ & DynArray$(UBound(DynArray$))
  221.    'increment adjusted linecount
  222.    AdjustedLineCt% = AdjustedLineCt% + 1
  223.    
  224.    'return the built string
  225.    ProcessArray$ = BuildStr$
  226. End Function
  227.  
  228. '9/25/94
  229. 'Function to process a passed listbox and return
  230. 'a new string consisting of the passed listbox items
  231. 'concatenated with a separator. Used in conjunction with
  232. 'ParseAndFillListBox%(). If an item is an empty string
  233. '(this will occur when there are two or more consecutive
  234. 'crlf's in the text), discard the crlf and do not add another space.
  235. '
  236. 'Parameters:
  237. '
  238. 'L - A listbox to process.
  239. 'separator$ - Character to put between elements in creating the
  240. 'build string.
  241. 'AdjustedLineCt% - A value passed by reference which will return
  242. 'a line count value which does not consider single CRLF's a line.
  243. '
  244. 'Return Value: The string constructed from the listbox.
  245. '
  246. Function ProcessList$ (L As ListBox, Separator$, AdjustedLineCt%)
  247.    Dim BuildStr$
  248.    Dim i%
  249.  
  250.    'go through the listbox (to the second to last element)
  251.    'adding each item to the build string plus the
  252.    'separator. Items in listbox consisting of an empty string are
  253.    'bypassed.
  254.    For i = 0 To L.ListCount - 2
  255.       If L.List(i) <> "" Then
  256.      BuildStr$ = BuildStr$ & L.List(i) & Separator$
  257.      'increment adjusted linecount
  258.      AdjustedLineCt% = AdjustedLineCt% + 1
  259.       End If
  260.    Next i
  261.    
  262.    'add the last item - do not append separator
  263.    BuildStr$ = BuildStr$ & L.List(L.ListCount - 1)
  264.    'increment adjusted linecount
  265.    AdjustedLineCt% = AdjustedLineCt% + 1
  266.    
  267.    'return the built string
  268.    ProcessList$ = BuildStr$
  269. End Function
  270.  
  271.