Code archives/Miscellaneous/StringList
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
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: Include "strlist.bb" ;Example 1 - Make a list of the components of a path, using \ as separator sl = CreateStrings() AddSplitString(sl, "c:\program\test\file.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) | |||||
;------------------------------------------------- ; strlist.bb - stringlist functions ; ; purpose: ; A very basic StringList. ; ; Beware. Performance will degrade if used with ; large amounts of Data. ; ;------------------------------------------------- Global sfHdlCounter_=-1 ;INTERNAL: This is a contaner type for all strings in all stringlists. Type TStrings Field hdl; this object belongs to this list-handle Field string$ End Type ;------------------------------------------------- ; Creates a new stringlist. ; ; Returns handle to new stringlist. ; Function CreateStrings() sfHdlCounter_=sfHdlCounter_+1 Return sfHdlCounter_ End Function ;------------------------------------------------- ; Frees (releases) a stringlist by handle. ; ; hdl - handle to previously created stringlist. Function FreeStrings(hdl) For s.TStrings = Each TStrings If s\hdl=hdl Then Delete s Next End Function ;------------------------------------------------- ; Adds a new string to the stringlist. ; ; hdl - stringlist handle ; s$ - the string to add Function AddString(hdl, s$) n.TStrings = New TStrings n\hdl=hdl n\string=s End Function ;------------------------------------------------- ; Returns the current number of strings in the ; stringlist. ; ; hdl - stringlist handle Function CountStrings(hdl) cnt=0 For s.TStrings = Each TStrings If s\hdl = hdl Then cnt=cnt+1 Next Return cnt End Function ;------------------------------------------------- ; Returns the string at a current index in the ; stringlist. ; ; hdl - stringlist handle ; idx - index of a string between 0 and ; CountStrings(hdl)-1. ; ; NOTE: Passing an invalid index will return "" Function GetString$(hdl, idx) cnt=0 For s.TStrings = Each TStrings If (s\hdl = hdl) And (cnt=idx) Then Return s.TStrings\String If s\hdl = hdl Then cnt=cnt+1 Next Return "" End Function ;------------------------------------------------- ; Splits a string based on a separator into a ; stringlist. ; ; hdl - stringlist handle ; s$ - the string to split ; sep$ - the separator Function AddSplitString(hdl, s$, sep$) ls=Len(sep) p=Instr(s, sep) While p>0 AddString(hdl, Left$(s, p-1)) ;s=Right$(s, p+ls+1) s$=Mid$(s$, p+ls, 2000000000) ; just copy the rest of the string p=Instr(s, sep) Wend If Len(s) > 0 Then AddString(hdl, s) End Function ;------------------------------------------------- ; Deletes a string on a certain index in the ; stringlist. ; ; hdl - stringlist handle ; idx - index of a string between 0 and ; CountStrings(hdl)-1. ; ; NOTE: Passing an invalid index will just waste ; cpu-cycles and do nothing. Function DeleteStringIdx(hdl, idx) cnt=0 For s.TStrings = Each TStrings If (s\hdl = hdl) And (cnt=idx) Then Delete s Return EndIf If s\hdl = hdl Then cnt=cnt+1 Next End Function ;------------------------------------------------- ; Deletes all strings equal to s$ in a stringlist. ; ; hdl - stringlist handle ; s$ - string to delete Function DeleteString(hdl, s$) For st.TStrings = Each TStrings If (st\hdl = hdl) And (st\String=s) Then Delete st Next End Function ;------------------------------------------------- ; Build a new string from all strings in the ; stringlist. A separator may be used between each ; element of the stringlist ; ; hdl - stringlist handle ; sep$ - optional separator Function StringFromStrings$(hdl, sep$="") res$="" For s.TStrings = Each TStrings If (s\hdl = hdl) Then res=res+s\String+sep EndIf Next l = Len(res) ls = Len(sep) If (ls>0) And (l>0) Then res = Left$(res, l-Len(sep)) ;remove last separator Return res End Function ;------------------------------------------------- ; Read in a whole ascii-stream into a stringlist ; ; hdl - stringlist handle ; stream - stream/file handle. Function ReadStrings(hdl, stream) While Not Eof(stream) AddString(hdl, ReadLine(stream)) Wend End Function |
Comments
None.
Code Archives Forum