EdzUp File Handling

Monkey Forums/Monkey Code/EdzUp File Handling

EdzUp(Posted 2014) [#1]
File handling made easy

'
'	FileHandle.monkey - Copyright (C)EdzUp
'	Programmed by Ed 'EdzUp' Upton
'
Import brl.filestream
Import brl.filesystem

Const FILE_USEMONKEY = True			'Set to true for the monkey://internal and monkey://external use on android etc

Class FileHandlerClass
	Field FileHand:FileStream
	Field FileOk:Bool
	Field writing:Bool
	
	Method FileExists:Bool( filename:String )
		Select FileType( filename )
		Case FILETYPE_FILE
			Return( True )
		Default
			Return( False )
		End
		
		Return( False )
	End
	
	Method OpenFile( filename:String, writeable:Bool = False, internal:Bool = False )
		Local ActualFile:String
		
		If FILE_USEMONKEY = True
			If internal = True
				ActualFile = "monkey://internal/"+filename
			Else
				ActualFile = "monkey://external/"+filename
			Endif
		Else
			ActualFile = filename
		Endif
		
		FileOk = False
		
		If FileExists( ActualFile ) = True
			If writeable = True
				'create a file for writing
				FileHand = FileStream.Open( ActualFile, "w" )
				'New FileStream( ActualFile, "w" )
			Else
				'create a file for reading
				FileHand = FileStream.Open( ActualFile, "r" )
				'New FileStream( ActualFile, "r" )
			Endif
			writing = writeable
			FileOk = True
		Else
			If writeable = True
				'nothing here so make it
				FileHand = FileStream.Open( ActualFile, "w" )
				writing = writeable
				FileOk = True
			Endif
		Endif
	End
	
	Method IsOpen:Bool()
		Return( FileOk )
	End
	
	Method CloseFile( )
		FileHand.Close()
	End
	
	Method UseInt:Int( value:Int = 0 )
		If writing = True
			FileHand.WriteInt( value )
		Else
			Return( FileHand.ReadInt() )
		Endif
		
		Return( 0 )
	End
	
	Method UseByte:Int( value:Int = 0 )
		If writing = True
			FileHand.WriteByte( value )
		Else
			Return( FileHand.ReadByte() )
		Endif
		
		Return( 0 )
	End
	
	Method UseShort:Int( value:Int = 0 )
		If writing = True
			FileHand.WriteShort( value )
		Else
			Return( FileHand.ReadShort() )
		Endif
		
		Return( 0 )
	End
	
	Method UseFloat:Float( value:Float = 0.0 )
		If writing = True
			FileHand.WriteFloat( value )
		Else
			Return( FileHand.ReadFloat() )
		Endif
		
		Return( 0.0 )
	End
	
	Method UseString:String( value:String = "" )
		Local LL:Int
		
		If writing = True
			FileHand.WriteInt( value.Length )
			FileHand.WriteString( value )
		Else
			LL = FileHand.ReadInt()
			Return( FileHand.ReadString( LL ) )
		Endif
		
		Return( "" )
	End
	
	Method UseBool:Bool( value:Bool = False )
		Local UBI:Int
		
		If writing = True
			If value = True
				FileHand.WriteInt( 1 )
			Else
				FileHand.WriteInt( 0 )
			Endif
		Else
			UBI = FileHand.ReadInt()
			If UBI = 0
				Return( False )
			Else
				Return( True )
			Endif
		Endif
		
		Return( False )
	End
End

Global FileHandler:FileHandlerClass = New FileHandlerClass


Works on most targets BUT NOT ON HTML5 :)

Call FileHandler.OpenFile( "MyFile.txt", True, True ) would make a internal file in any application, internal means that it cannot be seen by file handlers on things like android etc. If you set the last True to False it will be created in places like /storage/0/ on android which can be found on file handlers.

Once you created the file you can use things like FileHandler.UseInt( 30 ) to write a file to it.

Reading from files you just call FileHandler.OpenFile( "MyFile.txt", false, True )
and then use MyInt:Int = FileHandler.UseInt()

Afer all that just call FileHandler.CloseFile() to close once its closed thats it. All of these have been tested on Android and work perfectly (My Star Rogue Editor uses it :) ), I put it here to make it easier on monkey to use files and also so I dont lose it LOL


Steve Ancell(Posted 2014) [#2]
I also constructed a file handler when I did Mutant Monty, this one's only designed for reading a file though; very quick and dirty. I'll post it below, with a usage example, in addition to Edz contribution. Anyone's welcome to use it if it's any good to them. ;)


Import mojo


Function Main:Int()
	New C_Game()
	Return 0
	
End




Class C_Game extends App
	Field file:FileLoaderClass
	Field dataList:String[]
	
	
	Method OnCreate:Int()
		file = New FileLoaderClass("testfile.txt")
		
		While Not file.Eof()
			dataList = dataList.Resize(dataList.Length() + 1)
			dataList[dataList.Length() - 1] = file.ReadLine()
			
		Wend
		
		Return 0
		
	End
	
	
	Method OnUpdate:Int()
		Return 0
		
	End
	
	
	Method OnRender:Int()
		Local i:Int
		
		
		Cls
		
		For i = 0 to (dataList.Length() - 1)
			DrawText(dataList[i], 0, i * 20)
			
		Next
		
		Return 0
		
	End
	
End




Class FileLoaderClass
	Field fileData:String[]
	Field currentLine:Int
	
	
	Method New(fileName:String)
		fileData = LoadString(fileName).Split("~n")
		currentLine = 0
		
	End
	
	
	Method ReadLine:String()
		Local currentData:String
		
		
		if Eof() = False
			currentData = Self.fileData[Self.currentLine]
			currentLine = (currentLine + 1)
			
		Else
			currentData = "Data Exhausted!"
			
		EndIf
		
		Return currentData.Trim()
		
	End
	
	
	Method Eof:Bool()
		if Self.currentLine > (fileData.Length() - 1)
			Return True
			
		Else
			Return False
			
		EndIf
	End
	
	
	Method Empty:Void()
		Local counter:Int
		
		
		For counter = 0 to (fileData.Length() - 1)
			fileData[counter] = ""
			
		Next
		
		fileData = fileData.Resize(0)
		
	End
End



MonkeyPlotter(Posted 2015) [#3]
Edzup & Steve, thank you for the example code, really appreciated ;)


EdzUp MkII(Posted 2015) [#4]
Your welcome and merry xmas ;)