aboutsummaryrefslogtreecommitdiff
path: root/tests/emscripten_fs_api_browser.cpp
blob: 07469f3477854c26dc99773c8dc75634b2921aec (plain)
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
#include<stdio.h>
#include<emscripten.h>
#include<assert.h>
#include <string.h>

extern "C" {

int result = 1;
int get_count = 0;

void wait_wgets() {
  if (get_count == 2) {
    emscripten_cancel_main_loop();
    REPORT_RESULT();
  }
}

void onLoaded(const char* file) {
  if (strcmp(file, "/tmp/test.html")) {
    result = 0;
  }

  printf("loaded: %s\n", file);

  if (FILE * f = fopen(file, "r")) {
      printf("exists: %s\n", file);
      int c = fgetc (f);
      if (c == EOF) {
        printf("file empty: %s\n", file);
        result = 0;
      }
      fclose(f);
  } else {
    result = 0;
    printf("!exists: %s\n", file);
  }
  
  get_count++;
}

void onError(const char* file) {
  if (strcmp(file, "/tmp/null")) {
    result = 0;
  }

  printf("error: %s\n", file);
  get_count++;
}

int main() {
  emscripten_async_wget(
    "http://localhost:8888/this_is_not_a_file", 
    "/tmp/null",
    onLoaded,
    onError);

  emscripten_async_wget(
    "http://localhost:8888/test.html", 
    "/tmp/test.html",
    onLoaded,
    onError);

  emscripten_set_main_loop(wait_wgets, 0);

  return 0;
}

}