demozenfs.lpr 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. program demozenfs;
  2. {$mode objfpc}
  3. uses
  4. {$IFDEF FPC_DOTTEDUNITS}
  5. Browser.Console, JSApi.JS, Api.ZenFS.Core, Api.ZenFS.Dom;
  6. {$ELSE}
  7. BrowserConsole, JS, libzenfs, libzenfsdom;
  8. {$ENDIF}
  9. Procedure DoTest; async;
  10. var
  11. Buf : TZenFSBuffer;
  12. aView : TJSDataView;
  13. BufStr : String absolute Buf;
  14. ArrayBuf : TJSArrayBuffer;
  15. I,Errs : Integer;
  16. begin
  17. await(tjsobject, ZenFS.configure(
  18. new(
  19. ['mounts', new([
  20. '/', DomBackends.WebStorage
  21. ])
  22. ])
  23. )
  24. );
  25. if ZenFS.existsSync('/something') then
  26. Writeln('Text file already exists')
  27. else
  28. begin
  29. Writeln('Creating new file');
  30. ZenFS.writeFileSync('/something', 'a nice text', TZenFSBufferEncoding.UTF8);
  31. end;
  32. Buf:=ZenFS.readFileSync('/something',TZenFSBufferEncoding.UTF8);
  33. if IsString(Buf) then
  34. Writeln('Read : ',BufStr)
  35. else
  36. Writeln('Got buffer with ',Buf.length,' bytes');
  37. if ZenFS.existsSync('/data.dat') then
  38. Writeln('Binary file already exists')
  39. else
  40. begin
  41. Writeln('Creating new binary file');
  42. ArrayBuf:=TJSarrayBuffer.new(3);
  43. Buf:=TZenFSBuffer.New(ArrayBuf);
  44. Buf._set([1,2,3]);
  45. aView:=TJSDataView.New(ArrayBuf);
  46. ZenFS.writeFileSync('/data.dat', aView);
  47. end;
  48. Writeln('Reading from binary file');
  49. Buf:=ZenFS.readFileSync('/data.dat');
  50. aView:=TJSDataView.New(Buf.buffer);
  51. Writeln('Read : ',aView.byteLength,' bytes');
  52. Errs:=0;
  53. For I:=1 to 3 do
  54. If aView.getInt8(i-1)<>i then
  55. begin
  56. Writeln('Error, expected ',I,' got ',aView.getInt8(i-1));
  57. inc(Errs);
  58. end;
  59. if Errs=0 then
  60. Writeln('All binary data read OK')
  61. else
  62. Writeln('Got ',Errs,' errors when reading binary');
  63. end;
  64. begin
  65. ConsoleStyle:=DefaultCRTConsoleStyle;
  66. HookConsole;
  67. DoTest;
  68. end.