support for writing to files in stdio
This commit is contained in:
Родитель
6a6e842688
Коммит
9eb5dd2a45
|
@ -228,19 +228,19 @@ var Library = {
|
|||
_stdin = Pointer_make([0], null, ALLOC_STATIC);
|
||||
IHEAP[_stdin] = this.prepare('<<stdin>>');
|
||||
_stdout = Pointer_make([0], null, ALLOC_STATIC);
|
||||
IHEAP[_stdout] = this.prepare('<<stdout>>');
|
||||
IHEAP[_stdout] = this.prepare('<<stdout>>', null, true);
|
||||
_stderr = Pointer_make([0], null, ALLOC_STATIC);
|
||||
IHEAP[_stderr] = this.prepare('<<stderr>>');
|
||||
IHEAP[_stderr] = this.prepare('<<stderr>>', null, true);
|
||||
},
|
||||
prepare: function(filename, data) {
|
||||
prepare: function(filename, data, print_) {
|
||||
var stream = this.counter++;
|
||||
this.streams[stream] = {
|
||||
filename: filename,
|
||||
data: data,
|
||||
data: data ? data : [],
|
||||
position: 0,
|
||||
eof: 0,
|
||||
error: 0,
|
||||
print: 1 // true for stdout and stderr - we print when receiving data for them
|
||||
print: print_ // true for stdout and stderr - we print when receiving data for them
|
||||
};
|
||||
this.filenames[filename] = stream;
|
||||
return stream;
|
||||
|
@ -249,9 +249,19 @@ var Library = {
|
|||
|
||||
fopen__deps: ['STDIO'],
|
||||
fopen: function(filename, mode) {
|
||||
var str = Pointer_stringify(filename);
|
||||
//assert(str in this._STDIO.filenames, 'No information for file: ' + str);
|
||||
return this._STDIO.filenames[str];
|
||||
filename = Pointer_stringify(filename);
|
||||
mode = Pointer_stringify(mode);
|
||||
if (mode.indexOf('r') >= 0) {
|
||||
//assert(filename in this._STDIO.filenames, 'No information for file: ' + filename);
|
||||
var stream = this._STDIO.filenames[filename];
|
||||
var info = this._STDIO.streams[stream];
|
||||
info.position = info.error = info.eof = 0;
|
||||
return stream;
|
||||
} else if (mode.indexOf('w') >= 0) {
|
||||
return this._STDIO.prepare(filename);
|
||||
} else {
|
||||
assert(false, 'fopen with odd params: ' + mode);
|
||||
}
|
||||
},
|
||||
__01fopen64_: 'fopen',
|
||||
|
||||
|
@ -304,7 +314,13 @@ var Library = {
|
|||
var info = this._STDIO.streams[stream];
|
||||
if (info.print) {
|
||||
__print__(intArrayToString(Array_copy(ptr, count*size)));
|
||||
} // XXX
|
||||
} else {
|
||||
for (var i = 0; i < size*count; i++) {
|
||||
info.data[info.position] = HEAP[ptr];
|
||||
info.position++;
|
||||
ptr++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
},
|
||||
|
||||
|
|
|
@ -38,3 +38,5 @@ Module['run'] = run;
|
|||
run(args);
|
||||
#endif
|
||||
|
||||
// {{POST_RUN_ADDITIONS}}
|
||||
|
||||
|
|
|
@ -2,7 +2,10 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
// Reading
|
||||
|
||||
FILE *file = fopen("somefile.binary", "rb");
|
||||
assert(file);
|
||||
|
||||
|
@ -25,9 +28,24 @@ int main() {
|
|||
fclose (file);
|
||||
free (buffer);
|
||||
|
||||
// Standard streams
|
||||
|
||||
fwrite("texto\n", 1, 6, stdout);
|
||||
fwrite("texte\n", 1, 6, stderr);
|
||||
|
||||
// Writing
|
||||
|
||||
char data[5] = { 10, 30, 20, 11, 88 };
|
||||
FILE *outf = fopen("go.out", "wb");
|
||||
fwrite(data, 1, 5, outf);
|
||||
fclose(outf);
|
||||
|
||||
char data2[10];
|
||||
FILE *inf = fopen("go.out", "rb");
|
||||
int num = fread(data2, 1, 10, inf);
|
||||
fclose(inf);
|
||||
printf("%d : %d,%d,%d,%d,%d\n", num, data2[0], data2[1], data2[2], data2[3], data2[4]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -1302,7 +1302,7 @@ if 'benchmark' not in sys.argv:
|
|||
)
|
||||
open(filename, 'w').write(src)
|
||||
src = open(path_from_root('tests', 'files.cpp'), 'r').read()
|
||||
self.do_test(src, 'size: 7\ndata: 100,200,50,25,10,77,123\ntexto\ntexte\n', post_build=post)
|
||||
self.do_test(src, 'size: 7\ndata: 100,200,50,25,10,77,123\ntexto\ntexte\n5 : 10,30,20,11,88\n', post_build=post)
|
||||
|
||||
### 'Big' tests
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче