FileStream file extentions and ReadLine() (?).

Monkey Forums/Monkey Programming/FileStream file extentions and ReadLine() (?).

Richard Betson(Posted 2015) [#1]
Howdy,

I have noticed that Monkey-X does not move certain files to the build data folder. Are only certain file extensions allowed? In my case text files with the extension ' .skin ' are not moved or even allowed access to using FileStream.Renaming the file with an extension of ' .txt' works as expected.

Also there is no ReadLine() function so does anyone have an example a custom written function. I have looked in the forms and think I have a handle on it but while I'm here I thought I would ask. ;)

Rock on,


ImmutableOctet(SKNG)(Posted 2015) [#2]
Well, 'ReadLine' is platform/standard dependent, and implementations can be rather slow. So, most people just use 'LoadString', then split/parse based on the line-ending character ("~n", from what I remember). As far as custom formats go (Non-text), you can just write the length of a string, then read that many characters ('ReadString' and 'WriteString' can be used with this structure).

As for custom formats/extensions, your "CONFIG.monkey" file has preprocessor variables which specify the white-list for files. You can use asterisks as wild-card characters, thus allowing you to specify file extensions. These are generally target-dependent, but you can set them manually (Either in "CONFIG.monkey", or another file/module). I don't remember if the '+=' operator works with these, but if it does, you can just use that.

Implementations of commands like 'ReadLine' vary, but here's a rough version I wrote a while ago. You can use that as an example; it relies on some of my other modules, though. Here's the old version. Weirdly enough, that implementation ran faster for me when using '+=' on a 'String', instead of seeking.


Richard Betson(Posted 2015) [#3]
Thanks ImmutableOctet(SKNG),

Got it. So in my case it would be #TEXT_FILES="*.txt|*.xml|*.json|*.skin" (GFLW).

As to ReadLine() I will just do as you suggest and what some of the examples mirror.

It's been several years since I rocked the Monkey.. Shaking off the rust. ;)


Richard Betson(Posted 2015) [#4]
This works well for me in reading a line. :) Although if you are reading large files it may not work.

Local stream:=FileStream.Open(mytextfile,"r")
Local mystring:String
mystring=stream.ReadString()
mystring.Trim()

Local strend:Int=mystring.Length()-1		
Local start:Int=1
Local strcnt:Int

For Local ts:Int=1 To strend
Local mys:String=mystring[ts..(ts+1)]
	If mys=String.FromChar(KEY_ENTER)
	Local line:String=mystring[start..(ts)]
		start=ts+2
		Print line
	Endif
Next

stream.Close()