"Best" way handling txt file?

BlitzMax Forums/BlitzMax Beginners Area/"Best" way handling txt file?

Grisu(Posted 2005) [#1]
Hi!

I wanted to know what ways you use to load a plain text file. And how to ouput the contens on screen.

Currently I'm getting the file as sting handle which i place in a global array. The problem is, that I dont know how to output the text with word warp afterwards?



Any suggs?


RexRhino(Posted 2005) [#2]
The only way to output text with word wrap to to create a function to do so. Here is the type I created to do word wrapping. Sorry, but I have no time to document it, but it is simple enough so hopefully you can grasp how to use it.

Also, it has not been optimized, so it might be a little slow.

Type SString
	Field word_list:TList = New TList
	Field datastring:String
	
	Field x:Int
	Field y:Int
	Field width:Int
	
	Method Print(in_string:String, in_x:Int, in_y:Int, in_width:Int = 600)
		datastring = in_string.trim()
		x = in_x
		y = in_y
		width = in_width
		wrap()
	End Method
	
	Method wrap()
		Local current_word:String
		Local x:Int
		word_list = New TList
		
		For x = 0 To (datastring.length - 1)
			If datastring[x] = 32
				ListAddLast(word_list, current_word)
				current_word = ""
			ElseIf datastring[x] = 13
				ListAddLast(word_list, current_word)
				current_word = ""
				ListAddLast(word_list, "[br]")
			Else
				current_word = current_word + Chr$(datastring[x])
			EndIf
		Next
		ListAddLast(word_list, current_word)
	End Method
	
	Method draw()
		Local current_word:String
		Local current_line:String
		Local current_y:Int = y
		Local lineheight:Int = TextHeight("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG") + (TextHeight("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG") / 4)
		
		For current_word = EachIn word_list
			If current_word = "[br]"
				DrawText(current_line, x, current_y)
				current_y = current_y + lineheight
				current_line = ""
			ElseIf ( TextWidth(current_line) + TextWidth(" ") + TextWidth(current_word) ) > width
				DrawText(current_line, x, current_y)
				current_y = current_y + lineheight
				current_line = current_word
			Else
				If current_line <> ""
					current_line = current_line + " " + current_word
				Else
					current_line = current_word
				EndIf
			EndIf
		Next
		DrawText(current_line, x, current_y)
	End Method
End Type



FlameDuck(Posted 2005) [#3]
Load it into a Bank, and step through it one character at a time?


Grisu(Posted 2005) [#4]
I was able to modify Rex functions a bit to meet my purposes.
Took some hours. But now it works. :D

Thanx guys!


kmac(Posted 2005) [#5]
Grisu, care to share a working example? Thanks.


RexRhino(Posted 2005) [#6]
Mine works just fine... it just isn't as efficient as what it could be.


Grisu(Posted 2005) [#7]


Here u go!


Rimmsy(Posted 2005) [#8]
Excellent. Thanks to both of you for the great work. Been looking for one of these for ages.


xuchuang(Posted 2006) [#9]
Grisu:

I'm going to use your code as well, thanks! ;)


amonite(Posted 2006) [#10]
thanks for sharing !! :)