FsUtils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. export class FsUtils {
  2. static writeText(fn, data) {
  3. if(!fn.startsWith("/storage")) fn = FsUtils.fullPath(fn);
  4. try {
  5. hmFS.remove(fn);
  6. } catch(e) {}
  7. const buffer = FsUtils.strToUtf8(data);
  8. const f = FsUtils.open(fn, hmFS.O_WRONLY | hmFS.O_CREAT);
  9. hmFS.write(f, buffer, 0, buffer.byteLength);
  10. hmFS.close(f);
  11. }
  12. static read(fn, limit=false) {
  13. if(!fn.startsWith("/storage")) fn = FsUtils.fullPath(fn);
  14. const [st, e] = FsUtils.stat(fn);
  15. const f = FsUtils.open(fn, hmFS.O_RDONLY);
  16. const size = limit ? limit : st.size;
  17. const data = new ArrayBuffer(size);
  18. hmFS.read(f, data, 0, size);
  19. hmFS.close(f);
  20. return data;
  21. }
  22. static fetchTextFile(fn, limit=false) {
  23. const data = FsUtils.read(fn, limit);
  24. const view = new Uint8Array(data);
  25. let str = "";
  26. return FsUtils.Utf8ArrayToStr(view);
  27. }
  28. static stat(path) {
  29. if(path.startsWith("/storage")) {
  30. const statPath = "../../../" + path.substring(9);
  31. return hmFS.stat_asset(statPath);
  32. }
  33. return hmFS.stat_asset(path);
  34. }
  35. static open(path, m) {
  36. if(path.startsWith("/storage")) {
  37. const statPath = "../../../" + path.substring(9);
  38. return hmFS.open_asset(statPath, m);
  39. }
  40. return hmFS.open_asset(path, m);
  41. }
  42. static fetchJSON(fn) {
  43. const text = FsUtils.fetchTextFile(fn);
  44. return JSON.parse(text);
  45. }
  46. static getSelfPath() {
  47. return "/storage/js_apps/000049AA";
  48. }
  49. static fullPath(path) {
  50. return FsUtils.getSelfPath() + "/assets/" + path;
  51. }
  52. // https://stackoverflow.com/questions/18729405/how-to-convert-utf8-string-to-byte-array
  53. static strToUtf8(str) {
  54. var utf8 = [];
  55. for (var i=0; i < str.length; i++) {
  56. var charcode = str.charCodeAt(i);
  57. if (charcode < 0x80) utf8.push(charcode);
  58. else if (charcode < 0x800) {
  59. utf8.push(0xc0 | (charcode >> 6),
  60. 0x80 | (charcode & 0x3f));
  61. } else if (charcode < 0xd800 || charcode >= 0xe000) {
  62. utf8.push(0xe0 | (charcode >> 12),
  63. 0x80 | ((charcode>>6) & 0x3f),
  64. 0x80 | (charcode & 0x3f));
  65. } else {
  66. i++;
  67. charcode = 0x10000 + (((charcode & 0x3ff)<<10)
  68. | (str.charCodeAt(i) & 0x3ff));
  69. utf8.push(0xf0 | (charcode >>18),
  70. 0x80 | ((charcode>>12) & 0x3f),
  71. 0x80 | ((charcode>>6) & 0x3f),
  72. 0x80 | (charcode & 0x3f));
  73. }
  74. }
  75. return new Uint8Array(utf8).buffer;
  76. }
  77. // source: https://stackoverflow.com/questions/13356493/decode-utf-8-with-javascript
  78. static Utf8ArrayToStr(array) {
  79. var out, i, len, c;
  80. var char2, char3;
  81. out = "";
  82. len = array.length;
  83. i = 0;
  84. while (i < len) {
  85. c = array[i++];
  86. switch (c >> 4) {
  87. case 0:
  88. case 1:
  89. case 2:
  90. case 3:
  91. case 4:
  92. case 5:
  93. case 6:
  94. case 7:
  95. // 0xxxxxxx
  96. out += String.fromCharCode(c);
  97. break;
  98. case 12:
  99. case 13:
  100. // 110x xxxx 10xx xxxx
  101. char2 = array[i++];
  102. out += String.fromCharCode(
  103. ((c & 0x1f) << 6) | (char2 & 0x3f)
  104. );
  105. break;
  106. case 14:
  107. // 1110 xxxx 10xx xxxx 10xx xxxx
  108. char2 = array[i++];
  109. char3 = array[i++];
  110. out += String.fromCharCode(
  111. ((c & 0x0f) << 12) |
  112. ((char2 & 0x3f) << 6) |
  113. ((char3 & 0x3f) << 0)
  114. );
  115. break;
  116. }
  117. }
  118. return out;
  119. }
  120. }