Ooops
March 01, 2021, 11:01:45 PM

Author Topic: [bb] StringList by Mahan [ 1+ years ago ]  (Read 584 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] StringList by Mahan [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : StringList
Author : Mahan
Posted : 1+ years ago

Description : Lists of strings are very useful in programming.

examples:
* read a complete text-file and examine line-by-line in memory.
* split strings by some separator and get a list of all the elements.
* Add strings to the list during some operation for later processing.
* Storing the text lines of a chat/status window.

Note: The implementation is not very optimized or advanced. It will work well for small to moderate amounts of information, but I wouldn't recommend using it for 10's or 100's of megabytes of text.
code examples:
Code: [Select]

Include "strlist.bb"

;Example 1 - Make a list of the components of a path, using  as separator
sl = CreateStrings()
AddSplitString(sl, "c:program estfile.txt", "")
For i = 0 To CountStrings(sl)-1
Print GetString(sl, i)
Next

;Example 2 - Reformat and tidy up a sentence rebuilding spaces
sl2 = CreateStrings()
AddSplitString(sl2, "    Mobilis     in   Mobile    ", " ")
DeleteString(sl2, "")
Print "Reformatted string: '" + StringFromStrings(sl2, " ") + "'"

;Example 3 - Read up text file
sl3 = CreateStrings()
If FileType("strlist.bb")=0 Then End
f=ReadFile("strlist.bb")
ReadStrings(sl3, f)
CloseFile(f)
;For i = 0 To CountStrings(sl3)-1
; Print GetString(sl3, i)
;Next
Print "Lines in file: " + CountStrings(sl3)

FreeStrings(sl)
FreeStrings(sl2)
FreeStrings(sl3)


Code :
Code: BlitzBasic
  1. ;-------------------------------------------------
  2. ; strlist.bb - stringlist functions
  3. ;
  4. ; purpose:
  5. ;       A very basic StringList.
  6. ;
  7. ;   Beware. Performance will degrade if used with
  8. ;   large amounts of Data.
  9. ;
  10. ;-------------------------------------------------
  11.  
  12. Global sfHdlCounter_=-1
  13.  
  14. ;INTERNAL: This is a contaner type for all strings in all stringlists.
  15. Type TStrings
  16.         Field hdl; this object belongs to this list-handle
  17.         Field string$
  18. End Type
  19.  
  20. ;-------------------------------------------------
  21. ; Creates a new stringlist.
  22. ;
  23. ; Returns handle to new stringlist.
  24. ;
  25. Function CreateStrings()
  26.         sfHdlCounter_=sfHdlCounter_+1
  27.         Return sfHdlCounter_
  28. End Function
  29.  
  30. ;-------------------------------------------------
  31. ; Frees (releases) a stringlist by handle.
  32. ;
  33. ; hdl - handle to previously created stringlist.
  34. Function FreeStrings(hdl)
  35.         For s.TStrings = Each TStrings
  36.                 If shdl=hdl Then Delete s
  37.         Next
  38. End Function
  39.  
  40. ;-------------------------------------------------
  41. ; Adds a new string to the stringlist.
  42. ;
  43. ; hdl - stringlist handle
  44. ; s$ - the string to add
  45. Function AddString(hdl, s$)
  46.         n.TStrings = New TStrings
  47.         nhdl=hdl
  48.         nstring=s
  49. End Function
  50.  
  51. ;-------------------------------------------------
  52. ; Returns the current number of strings in the
  53. ; stringlist.
  54. ;
  55. ; hdl - stringlist handle
  56. Function CountStrings(hdl)
  57.         cnt=0
  58.         For s.TStrings = Each TStrings
  59.                 If shdl = hdl Then cnt=cnt+1
  60.         Next
  61.         Return cnt
  62. End Function
  63.  
  64. ;-------------------------------------------------
  65. ; Returns the string at a current index in the
  66. ; stringlist.
  67. ;
  68. ; hdl - stringlist handle
  69. ; idx - index of a string between 0 and
  70. ;       CountStrings(hdl)-1.
  71. ;
  72. ; NOTE: Passing an invalid index will return ""
  73. Function GetString$(hdl, idx)
  74.         cnt=0
  75.         For s.TStrings = Each TStrings
  76.                 If (shdl = hdl) And (cnt=idx) Then Return s.TStringsString
  77.                 If shdl = hdl Then cnt=cnt+1
  78.         Next
  79.         Return ""
  80. End Function
  81.  
  82.  
  83. ;-------------------------------------------------
  84. ; Splits a string based on a separator into a
  85. ; stringlist.
  86. ;
  87. ; hdl - stringlist handle
  88. ; s$ - the string to split
  89. ; sep$ - the separator
  90. Function AddSplitString(hdl, s$, sep$)
  91.         ls=Len(sep)
  92.         p=Instr(s, sep)
  93.         While p>0
  94.                 AddString(hdl, Left$(s, p-1))
  95.                 ;s=Right$(s, p+ls+1)
  96.                 s$=Mid$(s$, p+ls, 2000000000) ; just copy the rest of the string
  97.                 p=Instr(s, sep)
  98.         Wend
  99.         If Len(s) > 0 Then AddString(hdl, s)
  100. End Function
  101.  
  102. ;-------------------------------------------------
  103. ; Deletes a string on a certain index in the
  104. ; stringlist.
  105. ;
  106. ; hdl - stringlist handle
  107. ; idx - index of a string between 0 and
  108. ;       CountStrings(hdl)-1.
  109. ;
  110. ; NOTE: Passing an invalid index will just waste
  111. ; cpu-cycles and do nothing.
  112. Function DeleteStringIdx(hdl, idx)
  113.         cnt=0
  114.         For s.TStrings = Each TStrings
  115.                 If (shdl = hdl) And (cnt=idx) Then
  116.                         Delete s
  117.                         Return
  118.                 EndIf
  119.                 If shdl = hdl Then cnt=cnt+1
  120.         Next
  121. End Function
  122.  
  123.  
  124. ;-------------------------------------------------
  125. ; Deletes all strings equal to s$ in a stringlist.
  126. ;
  127. ; hdl - stringlist handle
  128. ; s$ - string to delete
  129. Function DeleteString(hdl, s$)
  130.         For st.TStrings = Each TStrings
  131.                 If (sthdl = hdl) And (stString=s) Then Delete st
  132.         Next
  133. End Function
  134.  
  135. ;-------------------------------------------------
  136. ; Build a new string from all strings in the
  137. ; stringlist. A separator may be used between each
  138. ; element of the stringlist
  139. ;
  140. ; hdl - stringlist handle
  141. ; sep$ - optional separator
  142. Function StringFromStrings$(hdl, sep$="")
  143.         res$=""
  144.         For s.TStrings = Each TStrings
  145.                 If (shdl = hdl) Then
  146.                         res=res+sString+sep
  147.                 EndIf
  148.         Next
  149.         l = Len(res)
  150.         ls = Len(sep)
  151.         If (ls>0) And (l>0) Then res = Left$(res, l-Len(sep)) ;remove last separator
  152.         Return res
  153. End Function
  154.  
  155.  
  156. ;-------------------------------------------------
  157. ; Read in a whole ascii-stream into a stringlist
  158. ;
  159. ; hdl - stringlist handle
  160. ; stream - stream/file handle.
  161. Function ReadStrings(hdl, stream)
  162.         While Not Eof(stream)
  163.                 AddString(hdl, ReadLine(stream))
  164.         Wend
  165. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal