Ooops
March 05, 2021, 07:26:40 AM

Author Topic: [bb] Text and Data Utilities by podperson [ 1+ years ago ]  (Read 447 times)

Offline BlitzBot

[bb] Text and Data Utilities by podperson [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Text and Data Utilities
Author : podperson
Posted : 1+ years ago

Description : It's not glamorous, but it is very useful stuff.

E.g. suppose you want to store all the information about a character in an RPG in a TYPE somewhere. The character can have a dozen attributes, any number of skills, and an inventory full of junk. Defining a TYPE to hold all that could get ugly; so could reading and writing it all to a file.

With the code here you can (a) shove it all in a single string, (b) easily manipulate and update it, (c) save it into a text file that you can easily edit using your favorite text editor, (d) bring it back in and read values from it safely, etc. etc.

Text Parsing Utilities
Unless I'm missing something, Blitz doesn't have commands for conveniently tokenising text. E.g. getting the values out of a TAB-delimited text field. Here they are.

E.g.

CountFields("Dude, where's my car?", " ") returns 4
nthField("Dude, where's my car?", " ", 3) returns "my"

Soup Utilities
Most of the development tools I use have some kind of support for associative arrays (or "soups"); Blitz didn't, now it does. Store pretty much all the data you want in a single string and get it back easily. Now you can encode all the meta-data you want about an entity in its name!

Edit: changed so you can just use plain ordinary strings. A little uglier but handly simplification (e.g. if you want to add an indeterminate number of attributes to some TYPE, just whack a string in there and use it as a soup).

E.g.
s$ = ""
s = setprop(s, "name", "Fred")
s = setprop(s, "address", "Ohio")
getprop(s, "name") returns "Fred"
s = setprop(s, "name", "Wilma")
getprop(s, "name") returns "Wilma"

Text File I/O
Rather than do it over and over, here are one-line commands for reading and writing text files.

LoadText("mygame.ini") returns contents of file mygame.ini (without the linefeeds; oh dear how sad never mind...)
SaveText("mygame.ini", s$) stores contents of s$ in "mygame.ini"

Note how easy it is to parse an INI file using these commands!


Code :
Code: BlitzBasic
  1. ; NOTE cannot assign function results to constants
  2. global TAB = chr(9)
  3. global CR = chr(13)
  4.  
  5. ; TEXT UTILITIES
  6. .TextParsing
  7.  
  8. ; Given a string and a delimiter tells you how many fields there are in it
  9. Function CountFields(s$, delim$)
  10.         o = 1
  11.         c = 0
  12.         While o > 0
  13.                 o = o + 1
  14.                 o = Instr(s, delim, o)
  15.                 c = c + 1
  16.         Wend
  17.         Return c
  18. End Function
  19.  
  20. ; Given a string, a delimiter, and a n -- returns the nth field
  21. Function NthField$(s$, delim$, n)
  22.         o = 1
  23.         For i = 1 To n - 1
  24.                 o = Instr(s, delim, o)
  25.                 If o = 0 Then
  26.                         Return ""
  27.                 End If
  28.                 o = o + 1
  29.         Next
  30.         p = Instr(s, delim, o)
  31.         If p = 0 Then
  32.                 Return Mid(s, o)
  33.         Else
  34.                 Return Mid(s, o, p - o)
  35.         End If
  36. End Function
  37.  
  38. ; SOUP UTILITIES
  39. ; A soup is a string that is being treated as a tagged array
  40. ; With some minor limitations you can just shove named properties into
  41. ; it and retrieve them later. The restriction: no TAB or CR in the name of
  42. ; a property or its content.
  43. ;
  44. ; Modifying this code to allow TAB and CR in properties would be pretty
  45. ; trivial BUT incur a (slight) performance hit.
  46. .Soups
  47.  
  48. ; Soups are simply tagged arrays; stick any string you like in them and get it back using
  49. ; the name you used.
  50.  
  51. ; Set a value (replacing the existing value if necessary)
  52. ; Function returns TRUE if a value of that name already existed (and was replaced)
  53. ; in case that's useful...
  54. Function SetProp(s$, label$, value$)
  55.         o = Instr(s, CR + label + TAB)
  56.         If o = 0 Then
  57.                 ; APPEND THE VALUE
  58.                 s = s + CR + label + TAB + value
  59.                 Return s
  60.         Else
  61.                 p = Instr(s, CR, o + Len(label) + 2)
  62.                 If p = 0 Then
  63.                         ; CHOP AND APPEND
  64.                         s = Left(s, o) + label + TAB + value
  65.                 Else
  66.                         ; SPLICE
  67.                         s = Left(s, o) + label + TAB + value + Mid(s, p)
  68.                 End If
  69.                 Return s
  70.         End If 
  71. End Function
  72.  
  73. ; Retrieve a value from the soup if it exists; if not an empty string is returned
  74. Function GetProp$(s$, label$)
  75.         o = Instr(s, CR + label + TAB)
  76.         v$ = ""
  77.         If o > 0 Then
  78.                 o = o + Len(label) + 2
  79.                 p = Instr(s, CR, o)
  80.                 If p = 0 Then
  81.                         v = Mid(s, o)
  82.                 Else
  83.                         v = Mid(s, o, p - o)
  84.                 End If
  85.         End If
  86.         Return v
  87. End Function
  88.  
  89. ; TEXT FILES
  90. .textfiles
  91.  
  92. Function LoadText$( file$ )
  93.         t$ = ""
  94.        
  95.         If FileType( file ) = 0 Then
  96.                 DebugLog "File: " + file + " does Not exist!"
  97.                 Return ""
  98.         End If
  99.        
  100.         filein = ReadFile( file )
  101.         While Not Eof(filein)
  102.                 t = t + ReadLine(filein) + Chr(13)
  103.         Wend
  104.         CloseFile filein
  105.        
  106.         Return t
  107. End Function
  108.  
  109. Function SaveText( file$, content$ )
  110.         Select FileType( file )
  111.         Case 1
  112.                 DeleteFile file
  113.         Case 2
  114.                 DebugLog "File: " + file + " could not be written. A directory of that name exists."
  115.                 Return
  116.         End Select
  117.        
  118.         fileout = WriteFile ( file )
  119.         WriteLine fileout, content
  120.         CloseFile fileout
  121. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal