"Best" way handling txt file?

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

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?

Global MyArray$[MAXFOLGEN]

Function GetDetails( File$ )
	Local hFile
	Local temp:String
	
	hFile=OpenFile(File$)
	If hFile
		Temp=LoadString(hFile)
		CloseFile hFile
		MyArray[Current_Folge]=Temp 'put data string in array
	
		Temp=Null
		Release hFile			
		Return True
	EndIf
		
	Return False
End Function


Any suggs?

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


Load it into a Bank, and step through it one character at a time?

I was able to modify Rex functions a bit to meet my purposes.
Took some hours. But now it works. :D

Thanx guys!

Grisu, care to share a working example? Thanks.

Mine works just fine... it just isn't as efficient as what it could be.

Graphics 800,600,1

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

Global t:SString = New SSTRING
Global LongTextmsg$="Der neueste Auftrag an die drei Detektive hört sich recht harmlos an: sie sollen einen entflogenen Papagei suchen. Doch kaum beginnen sie mit ihren Nachforschungen, da scheinen sich plötzlich noch einige andere Leute sehr für diesen Papagei zu interessieren.Vielleicht deshalb, weil er lateinische Sprüche zitieren kann? Aber bald geht es nicht mehr nur um einen, sondern um sieben Papageien - und alle sieben führen höchst seltsame Reden. Ob da nicht eine geheime Botschaft hintersteckt? Jedenfalls sind auch ein jähzorniger Kunsthändler und ein berüchtigter Meisterdieb hinter den Vögeln her. Die drei ??? müssen sich ganz schön die hellen Köpfe zerbrechen, ehe sie auch diesen abenteuerlichen Fall aufklären und eine wohlverdiente Belohnung einheimsen..."   



   SetColor 255,255,255
   DrawText "Wordwarp example: ",20,20

   T.Print(LongTextmsg$,20,60,300)
   T.Draw()

   Flip
   WaitKey 
End


Here u go!

Excellent. Thanks to both of you for the great work. Been looking for one of these for ages.

Grisu:

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

thanks for sharing !! :)