File: emscripten_fs_api_browser.cpp

package info (click to toggle)
emscripten 2.0.12~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 108,440 kB
  • sloc: ansic: 510,324; cpp: 384,763; javascript: 84,341; python: 51,362; sh: 50,019; pascal: 4,159; makefile: 3,409; asm: 2,150; lisp: 1,869; ruby: 488; cs: 142
file content (158 lines) | stat: -rw-r--r-- 3,703 bytes parent folder | download
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2012 The Emscripten Authors.  All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License.  Both these licenses can be
// found in the LICENSE file.

#include <stdio.h>
#include <emscripten.h>
#include <assert.h>
#include <string.h>
#include <SDL/SDL.h>
#include "SDL/SDL_image.h"
#include <sys/stat.h>
#include <unistd.h>
 
extern "C" {

int result = 1;
int get_count = 0;
int data_ok = 0;
int data_bad = 0;

void onLoadedData(void *arg, void *buffer, int size) {
  printf("onLoadedData %d\n", (int)arg);
  get_count++;
  assert(size == 329895);
  assert((int)arg == 135);
  unsigned char *b = (unsigned char*)buffer;
  assert(b[0] == 137);
  assert(b[1122] == 128);
  assert(b[1123] == 201);
  assert(b[202125] == 218);
  data_ok = 1;
}

void onErrorData(void *arg) {
  printf("onErrorData %d\n", (int)arg);
  get_count++;
  assert((int)arg == 246);
  data_bad = 1;
}

int counter = 0;
void wait_wgets() {
  if (counter++ == 60) {
    printf("%d\n", get_count);
    counter = 0;
  }

  if (get_count == 6) {
    static bool fired = false;
    if (!fired) {
      fired = true;
      emscripten_async_wget_data(
        "http://localhost:8888/screenshot.png",
        (void*)135,
        onLoadedData,
        onErrorData);
      emscripten_async_wget_data(
        "http://localhost:8888/fail_me",
        (void*)246,
        onLoadedData,
        onErrorData);
    }
  } else if (get_count == 8) {
    assert(IMG_Load("/tmp/screen_shot.png"));
    assert(data_ok == 1 && data_bad == 1);
    emscripten_cancel_main_loop();
    REPORT_RESULT(result);
  }
  assert(get_count <= 8);
}

void onLoaded(const char* file) {
  if (strcmp(file, "/tmp/test.html") && strcmp(file, "/tmp/screen_shot.png")
     && strcmp(file, "/this_directory_does_not_exist_and_should_be_created_by_wget/test.html")
     && strcmp(file, "/path/this_directory_is_relative_to_cwd/test.html")) {
    result = 0;
  }

  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++;
  printf("onLoaded %s\n", file);
}

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

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

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);

  // Try downloading the same file a second time
  emscripten_async_wget(
    "http://localhost:8888/test.html",
    "/tmp/test.html",
    onLoaded,
    onError);

  // Try downloading a file to a destination directory that does not exist.
  emscripten_async_wget(
    "http://localhost:8888/test.html",
    "/this_directory_does_not_exist_and_should_be_created_by_wget/test.html",
    onLoaded,
    onError);

  mkdir("/path", 0777);
  chdir("/path");

  // Try downloading a file to a destination directory that is a nonexisting path relative to CWD.
  emscripten_async_wget(
    "http://localhost:8888/test.html",
    "this_directory_is_relative_to_cwd/test.html",
    onLoaded,
    onError);

  char name[40];
  strcpy(name, "/tmp/screen_shot.png"); // test for issue #2349, name being free'd
  emscripten_async_wget(
    "http://localhost:8888/screenshot.png", 
    name,
    onLoaded,
    onError);
  memset(name, 0, 30);

  emscripten_set_main_loop(wait_wgets, 0, 0);

  return 0;
}

}