Store file

BlitzMax Forums/BlitzMax Programming/Store file

Heliotrope(Posted 2015) [#1]
Hi all,

Are blitz max types able to store files and if so are there any examples?


degac(Posted 2015) [#2]
Hi

I suppose you mean if you can save 'types' (and values) on external files and load them back?

You should use REFLECTION to read/write objects data. (It's called Serialization)

http://www.blitzbasic.com/codearcs/codearcs.php?code=2262


Heliotrope(Posted 2015) [#3]
Hi degac currently my program goes like
file = OpenFile (cmdpra)
	While Not Eof (file)
		AddTextAreaText (cmdout, ReadLine (file) + "~n")
	Wend
CloseStream (file)

This works but the metadata comes with it. Is there a way for the ReadLine command to go into the memory of a TYPE so that the program will read through the TYPE and deside what to add to the textarea?
Basically I just want the file to go into a buffer.


degac(Posted 2015) [#4]
Hi,

I don't understand clearly your question.
What your file you're reading contain exactly?

If your file is a .txt file one solution is to use LoadText (or LoadString)



Of course you need to check if the file exist (Filetype)
the entire textfile is in 'file'


Brucey(Posted 2015) [#5]
A basic example without error checking or parsing the text.
Type TMyType

    Field text:String

    Method Load(file:String)
        text = LoadText(file)
    End Method
End Type

Local t:TMyType = new TMyType
t.Load("text_file.txt")

The field "text" will hold the contents of the file after loading, including new lines.


Heliotrope(Posted 2015) [#6]
Hi all

I think Brucey's code example does what I need.
Can I then use readline, writeline and a few if statements to get what I need out of the field "text" so that no metadata is displayed on the screen? Right?


grable(Posted 2015) [#7]
No, you would have to parse it yourself.

And what metadata? What exactly are you trying to load?


TomToad(Posted 2015) [#8]
What kind of metadata are you getting? Is it some type of tags, like "{b}This is Bold{/b}"?
If so, then you will need to parse the string and remove the tags. Here is an example.
SuperStrict
Local s:String = "This is a <b>bold</b> statement.  Maybe we can <u>underline</u> the problem."
Print StripTags(s)

Function StripTags:String(s:String)
	Local t:String = ""
	Local i:Int = 0
	Local j:Int = 0
	
	Repeat
		j = s.find("<",i)
		If j < 0 Then Exit
		t :+ s[i..j]
		i = s.find(">",j)+1
	Forever
	If i < s.Length Then t:+ s[i..]
	Return t
End Function



Derron(Posted 2015) [#9]
@TomToad

SuperStrict
Local s:String = "This is a <b>bold</b> statement.  Maybe we can <u>underline</u> the problem."
Print StripTags(s)

'not working
Local oops:String = "I forgot the end tag <"
Print StripTags(oops)

Function StripTags:String(s:String)
	Local t:String = ""
	Local i:Int = 0
	Local j:Int = 0
	
	Repeat
		j = s.find("<",i)
		If j < 0 Then Exit
		t :+ s[i..j]
		i = s.find(">",j)+1
	Forever
	If i < s.Length Then t:+ s[i..]
	Return t
End Function


I bet it is better to have a boolean "open tag" indicator... and to read tag-parts as text if the tag indicator is on "false".

You could fix your code using some checks wether ">" follows on a "<". Also make sure that an "< and now?" does not stumble over negative array indizes.


bye
Ron


TomToad(Posted 2015) [#10]
Small fix: Will remove everything after the last "<". Nested tags wont crash the program either, but will produce strange results. Could be solved by keeping track of the number of "<" and ">"



TomToad(Posted 2015) [#11]
Better version. Will strip nested tags properly.



Derron(Posted 2015) [#12]
I had this "fix" too (but wanted to have something for you you to do during the coffee break :-)).

Print StripTags(">I am the invisible -m-a-n- text")
-> not printed at all


bye
Ron


Brucey(Posted 2015) [#13]
Can I then use readline, writeline and a few if statements to get what I need out of the field "text" so that no metadata is displayed on the screen? Right?

You should probably elaborate more, before these guys spend too much time on some function you don't need :-)


TomToad(Posted 2015) [#14]
You should probably elaborate more, before these guys spend too much time on some function you don't need :-)

Too late. Already created an improvement that allows me to escape the < and > characters. :D To keep this thread on track though, I will refrain from posting it. :)


Heliotrope(Posted 2015) [#15]
Sorry for the delay everyone, I just want to know weather a loadtext object acts in the same way as a file object. Later on I figure out the meta data parsing and mabey play with seeing how many different file types I can parse. But right now the question is does readline work on loadtext like it does on openfile?


Brucey(Posted 2015) [#16]
No, readline only works on streams/files. (read the documentation for ReadLine - it's only parameter is a stream:TStream)

LoadText puts the whole file into a string. (again, the documentation discusses what LoadText does). You then need to parse it.

You can use something like yourstring.Split("~n") to create an array of "lines", which you can then loop through.


If you press F1 on a built-in function, it will show basic help in the status bar at the bottom of the IDE. If you press F1 again, it will display the help page. It's useful to understand how things work.


Heliotrope(Posted 2015) [#17]
Thank you Brucey. Sometimes the docs are vague and occanaly someone wants to do something that isn't covered by the docs. But anyway I think that I have enought to go on now.