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
|