Code archives/Miscellaneous/Text and Data Utilities

This code has been declared by its author to be Public Domain code.

Download source code

Text and Data Utilities by podperson2003
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!
; NOTE cannot assign function results to constants
global TAB = chr(9)
global CR = chr(13)

; TEXT UTILITIES
.TextParsing

; Given a string and a delimiter tells you how many fields there are in it
Function CountFields(s$, delim$)
	o = 1
	c = 0
	While o > 0
		o = o + 1
		o = Instr(s, delim, o)
		c = c + 1
	Wend
	Return c
End Function

; Given a string, a delimiter, and a n -- returns the nth field
Function NthField$(s$, delim$, n)
	o = 1
	For i = 1 To n - 1
		o = Instr(s, delim, o)
		If o = 0 Then
			Return ""
		End If
		o = o + 1
	Next
	p = Instr(s, delim, o)
	If p = 0 Then
		Return Mid(s, o)
	Else
		Return Mid(s, o, p - o)
	End If
End Function

; SOUP UTILITIES
; A soup is a string that is being treated as a tagged array
; With some minor limitations you can just shove named properties into
; it and retrieve them later. The restriction: no TAB or CR in the name of
; a property or its content.
;
; Modifying this code to allow TAB and CR in properties would be pretty
; trivial BUT incur a (slight) performance hit.
.Soups

; Soups are simply tagged arrays; stick any string you like in them and get it back using
; the name you used.

; Set a value (replacing the existing value if necessary)
; Function returns TRUE if a value of that name already existed (and was replaced)
; in case that's useful...
Function SetProp(s$, label$, value$)
	o = Instr(s, CR + label + TAB)
	If o = 0 Then
		; APPEND THE VALUE
		s = s + CR + label + TAB + value
		Return s
	Else
		p = Instr(s, CR, o + Len(label) + 2)
		If p = 0 Then
			; CHOP AND APPEND
			s = Left(s, o) + label + TAB + value
		Else
			; SPLICE
			s = Left(s, o) + label + TAB + value + Mid(s, p)
		End If
		Return s
	End If	
End Function

; Retrieve a value from the soup if it exists; if not an empty string is returned
Function GetProp$(s$, label$)
	o = Instr(s, CR + label + TAB)
	v$ = ""
	If o > 0 Then
		o = o + Len(label) + 2
		p = Instr(s, CR, o)
		If p = 0 Then
			v = Mid(s, o)
		Else
			v = Mid(s, o, p - o)
		End If
	End If
	Return v
End Function 

; TEXT FILES
.textfiles

Function LoadText$( file$ )
	t$ = ""
	
	If FileType( file ) = 0 Then
		DebugLog "File: " + file + " does Not exist!"
		Return ""
	End If
	
	filein = ReadFile( file )
	While Not Eof(filein)
		t = t + ReadLine(filein) + Chr(13)
	Wend
	CloseFile filein
	
	Return t
End Function

Function SaveText( file$, content$ )
	Select FileType( file )
	Case 1
		DeleteFile file
	Case 2
		DebugLog "File: " + file + " could not be written. A directory of that name exists."
		Return
	End Select
	
	fileout = WriteFile ( file )
	WriteLine fileout, content
	CloseFile fileout
End Function

Comments

None.

Code Archives Forum