File: emscripten_filesystem.cpp

package info (click to toggle)
keyman 18.0.245-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,316 kB
  • sloc: python: 52,784; cpp: 21,278; sh: 7,633; ansic: 4,823; xml: 3,617; perl: 959; makefile: 139; javascript: 138
file content (72 lines) | stat: -rw-r--r-- 1,823 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72


#ifdef __EMSCRIPTEN__

#include <emscripten.h>
#include <iostream>
#include <test_assert.h>

const std::string get_wasm_file_path(const std::string& filename) {
  // Verify that we are passing a fully-qualified path
  // TODO: we need to support relative paths based on CWD
#if _DEBUG_FOPEN
  std::cout << "get_wasm_file_path ENTER (" << filename << ")" << std::endl;
#endif

  test_assert(
    (filename.length() > 0 && filename.at(0) == '/') ||
    (filename.length() > 1 && filename.at(1) == ':')
  );

#if _DEBUG_FOPEN
  std::cout << "get_wasm_file_path test_assert passed " << std::endl;
#endif

  EM_ASM_({
    //console.log('EM_ASM enter');
    let path = Module.UTF8ToString($0);
    const isWin32Path = path.match(/^[a-zA-Z]:/);

    //console.log('EM_ASM path = '+path);
    let root = '/nodefs-mount';
    if(!FS.analyzePath(root).exists) {
      //console.log('EM_ASM mkdir '+root);
      FS.mkdir(root);
      if(!isWin32Path) {
        //console.log('EM_ASM mount '+root);
        FS.mount(NODEFS, {root : '/'}, root);
      }
    }

    if(isWin32Path) {
      // Win32 path, one mount per drive
      root += "/" + path.charAt(0);
      if(!FS.analyzePath(root).exists) {
        //console.log('EM_ASM mkdir '+root);
        FS.mkdir(root);
        //console.log('EM_ASM mount '+root);
        FS.mount(NODEFS, {root : path.substr(0,3) }, root);
      }
    }
  }, filename.c_str());

#if _DEBUG_FOPEN
  std::cout << "get_wasm_file_path EM_ASM_ passed " << std::endl;
#endif

  std::string f = std::string("/nodefs-mount");

  if(filename.length() > 2 && filename.at(1) == ':') {
    f += std::string("/") + filename.at(0) + filename.substr(2);
  } else {
    f += filename;
  }

#if _DEBUG_FOPEN
  std::cout << "get_wasm_file_path opening virtual path: " << f << std::endl;
#endif

  return f;
}

#endif