This shows you the differences between two versions of the page.
sample_code:hexview [2015/07/29 21:17] stevegarman Handle large files in chunks |
sample_code:hexview [2015/07/30 05:17] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== File Hex Viewer ====== | ||
- | ===== Sample code ===== | ||
- | <code JavaScript hexView.js> | ||
- | var picker, | ||
- | // | ||
- | function OnStart() | ||
- | { | ||
- | // | ||
- | var lay = app.CreateLayout( " | ||
- | var scroll = app.CreateScroller( 1.0, | ||
- | lay.AddChild( scroll ); | ||
- | |||
- | // | ||
- | var layScroll = app.CreateLayout( " | ||
- | scroll.AddChild( layScroll ); | ||
- | |||
- | view=app.CreateWebView(1.5, | ||
- | layScroll.AddChild(view); | ||
- | // | ||
- | app.AddLayout( lay ); | ||
- | var path="/ | ||
- | hexViewStart(path, | ||
- | } | ||
- | |||
- | function hexViewStart(fullPath, | ||
- | { | ||
- | hv={chunksize: | ||
- | hv.webView=webView; | ||
- | hv.file = app.CreateFile( hv.path, | ||
- | hv.len = hv.file.GetLength(); | ||
- | hv.html="< | ||
- | hv.file.Seek( 0 ); | ||
- | app.ShowProgress(" | ||
- | hexViewChunk(); | ||
- | } | ||
- | |||
- | function hexViewChunk() | ||
- | { | ||
- | var pos=hv.file.GetPointer(); | ||
- | var whole = hv.file.ReadData( hv.chunksize," | ||
- | if(whole == undefined) | ||
- | { | ||
- | hexViewEnd(); | ||
- | return; | ||
- | } | ||
- | var bull="& | ||
- | var int, | ||
- | do | ||
- | { | ||
- | var data = whole.slice(0, | ||
- | whole=whole.slice(8); | ||
- | // | ||
- | if(data==undefined) break; | ||
- | var len = data.length; | ||
- | hbuff=""; | ||
- | abuff="< | ||
- | var pos8="< | ||
- | (" | ||
- | pos.toString(16)).slice(-8).toUpperCase() + | ||
- | "</ | ||
- | for(var i=0; | ||
- | { | ||
- | int=data[i]; | ||
- | hex=(" | ||
- | asc=bull; | ||
- | if(int> | ||
- | asc=String.fromCharCode(int); | ||
- | if(asc=="<" | ||
- | hbuff+="< | ||
- | abuff+=asc; | ||
- | } | ||
- | abuff+="</ | ||
- | row="< | ||
- | if(len> | ||
- | pos+=len; | ||
- | } | ||
- | while(len > | ||
- | setTimeout(" | ||
- | } | ||
- | |||
- | function hexViewEnd() | ||
- | { | ||
- | hv.html+="</ | ||
- | app.HideProgress(); | ||
- | hv.webView.LoadHtml(hv.html); | ||
- | hv.file.Close(); | ||
- | } | ||
- | |||
- | function pickit() | ||
- | { | ||
- | if( ! picker ) | ||
- | { | ||
- | picker = new FilePicker(openit,"/" | ||
- | picker.SetFolder("/ | ||
- | picker.SetHideFiles(false); | ||
- | } | ||
- | picker.Show(picker); | ||
- | |||
- | } | ||
- | function openit(pth) | ||
- | { | ||
- | hexViewStart(pth, | ||
- | } | ||
- | |||
- | |||
- | </ |