Platforms to show: All Mac Windows Linux Cross-Platform
/Util/Filemapping/Filemapping Example
Required plugins for this example: MBS Util Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Util/Filemapping/Filemapping Example
This example is the version from Sun, 17th Mar 2012.
Project "Filemapping Example.xojo_binary_project"
FileTypes
Filetype special/any
End FileTypes
Class Window1 Inherits Window
Control output Inherits TextArea
ControlInstance output Inherits TextArea
End Control
Control available Inherits Label
ControlInstance available Inherits Label
End Control
Control OpenButton Inherits PushButton
ControlInstance OpenButton Inherits PushButton
EventHandler Sub Action()
openfile
End EventHandler
End Control
Control info Inherits Label
ControlInstance info Inherits Label
End Control
Control Scroll Inherits ScrollBar
ControlInstance Scroll Inherits ScrollBar
EventHandler Sub ValueChanged()
loadat me.value
End EventHandler
End Control
Control cursor Inherits Label
ControlInstance cursor Inherits Label
End Control
Control CheckBox1 Inherits CheckBox
ControlInstance CheckBox1 Inherits CheckBox
EventHandler Sub Action()
loadat scroll.value
End EventHandler
End Control
Function clear(s as string) As string
// remove control characters
dim m1,m2 as MemoryBlock
dim l,i,c,n,ch as integer
l=lenb(s)
m1=NewMemoryBlock(l)
m1.StringValue(0,l)=s
m2=NewMemoryBlock(l*2)
c=l-1
n=0
for i=0 to c
ch=m1.Byte(i)
if ch<32 or ch>127 then // limit to visible ASCII
ch=32
end if
m2.Byte(n)=ch
n=n+1
if i mod 100=99 then
m2.Byte(n)=13
n=n+1
end if
next
Return m2.StringValue(0,n)
End Function
Sub loadat(pos as integer)
dim s as string
System.DebugLog "Position: "+str(pos)
'get bytes
dim offset as Int64 = pos * pagesize // position in bytes
dim size as integer = pagesize // how much to take
dim v as FileMappingViewMBS = f.MapView(offset, size)
if v<>Nil then
dim d as int64 = filesize-Offset
if d<size then // on the end limi the reading to the bytes in the file to avoid crash.
size = d
end if
s=v.Memory.StringValue(0,size)
'show bytes
if CheckBox1.Value then
// hex
output.text=HexstringMBS(s, 2, 16, "", chr(13), " ", " ")
else
// normal
output.text=clear(s)
end if
'show position
cursor.text="Showing "+format(lenb(s),"0")+" Bytes from position "+format(pos*pagesize,"0")+"."
end if
End Sub
Sub openfile()
dim file as folderItem
const maxscrollbar=32767
'maximum value for scrollbar.maximum!
file=getopenFolderItem("special/any")
if file<>nil then
f=new FilemappingMBS(file, false)
if f.Lasterror = 0 then
Title=file.DisplayName
info.text=format(file.Length,"0")+" Bytes mapped"
else
info.text="Error while opening the file. "+str(f.LastError)+" "+f.LasterrorString
f=nil
Return
end if
if not f.OpenFileMapping then
MsgBox "Failed to open file mapping."
f = nil
Return
end if
'pagesize is how many bytes we show per page.
'
filesize = file.Length
pagesize=4096
if file.length/pagesize>maxscrollbar then
'too big for scrollbar
pagesize=4096*ceil(file.length/4096/maxscrollbar)
'How often do me need to add 4096 bytes to get it into a scrollbar?
end if
scroll.value=0
scroll.lineStep=1
scroll.pageStep=1
scroll.maximum=floor(file.length/pagesize)
output.enabled=true
scroll.enabled=true
'load first page
loadat 0
end if
End Sub
Property f As filemappingMBS
Property filesize As int64
Property pagesize As integer
End Class
MenuBar Menu
MenuItem UntitledMenu3 = ""
MenuItem UntitledMenu2 = "File"
MenuItem FileQuit = "Quit"
MenuItem UntitledMenu0 = "Edit"
MenuItem EditUndo = "Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cut"
MenuItem EditCopy = "Copy"
MenuItem EditPaste = "Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class App Inherits Application
End Class
End Project
The items on this page are in the following plugins: MBS Util Plugin.