IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.3KB

  1. class WBRootDirWrapper {
  2. constructor(rootDir, streams) {
  3. this.rootDir = rootDir;
  4. this.streams = streams;
  5. }
  6. findDir(path) {
  7. if (typeof(path) === 'string')
  8. path = path.split('/');
  9. if (path[0] !== '.')
  10. throw Error('Path must begin with a dot component');
  11. let dir = this.rootDir;
  12. for (let i = 1; i < path.length; i++) {
  13. if (!(path[i] in dir))
  14. throw Error('Directory not found');
  15. if (dir[path[i]] instanceof Array)
  16. throw Error('Path is a file not directory');
  17. dir = dir[path[i]];
  18. }
  19. return dir;
  20. }
  21. listDirectory(path) {
  22. let dir = this.findDir(path);
  23. let keys = Object.keys(dir);
  24. keys.sort();
  25. let subdirs = keys.filter(k => !(dir[k] instanceof Array));
  26. let files = keys.filter(k => (dir[k] instanceof Array));
  27. let res = subdirs.map(k => [ 'd', k, null ]);
  28. res = res.concat(files.map(k => [ 'f', k, dir[k][1] ]));
  29. return res;
  30. }
  31. unescapeName(name) {
  32. return name.replace(/(\\\\|\\[0-9]{3})/g, (_, $1) => ($1 === '\\\\' ? '\\' : String.fromCharCode(parseInt($1.substr(1), 8))));
  33. }
  34. escapeName(name) {
  35. return name.replace(/ /g, '\\040');
  36. }
  37. getFile(path) {
  38. if (typeof(path) === 'string')
  39. path = path.split('/');
  40. if (path.length < 2)
  41. throw Error('Invalid file path');
  42. const name = path[path.length - 1];
  43. const dir = this.findDir(path.slice(0, path.length - 1));
  44. if (!(name in dir))
  45. throw Error('File not found');
  46. if (!(dir[name] instanceof Array))
  47. throw Error('Path points to a directory not a file');
  48. const streams = this.streams;
  49. let file = dir[name];
  50. file = [ file[0].map(seg => {
  51. const stm = streams[seg[0]];
  52. const used = stm.map(loc => !( loc[2] <= seg[1] || loc[1] >= seg[1] + seg[2] ) );
  53. const start = used.indexOf(true);
  54. const end = used.lastIndexOf(true) + 1;
  55. if (start === -1)
  56. return [];
  57. const res = [];
  58. for (let i = start; i < end; i++) {
  59. const loc = stm[i];
  60. res.push([ loc[0], Math.max(0, seg[1] - loc[1]),
  61. Math.min(loc[2], seg[1] + seg[2] - loc[1]) ]);
  62. }
  63. return res;
  64. }), file[1] ];
  65. file[0] = file[0].reduce((a, b) => a.concat(b));
  66. return file;
  67. }
  68. }
  69. export default WBRootDirWrapper;