diff options
author | Alon Zakai <azakai@mozilla.com> | 2011-01-16 15:29:06 -0800 |
---|---|---|
committer | Alon Zakai <azakai@mozilla.com> | 2011-01-16 15:29:06 -0800 |
commit | 9eb5dd2a45db42c792bb3948271d42016417404e (patch) | |
tree | 20225e99c25124227f4f3b967558aa93199f3d8b | |
parent | 6a6e842688afe7e15f6a957d4179da982c0f940b (diff) |
support for writing to files in stdio
-rw-r--r-- | src/library.js | 34 | ||||
-rw-r--r-- | src/postamble.js | 2 | ||||
-rw-r--r-- | tests/files.cpp | 20 | ||||
-rw-r--r-- | tests/runner.py | 2 |
4 files changed, 47 insertions, 11 deletions
diff --git a/src/library.js b/src/library.js index 8bc35ac0..075b53d9 100644 --- a/src/library.js +++ b/src/library.js @@ -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; }, diff --git a/src/postamble.js b/src/postamble.js index 87850d96..70c45b98 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -38,3 +38,5 @@ Module['run'] = run; run(args); #endif +// {{POST_RUN_ADDITIONS}} + diff --git a/tests/files.cpp b/tests/files.cpp index a07ef42b..5d915e01 100644 --- a/tests/files.cpp +++ b/tests/files.cpp @@ -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; } diff --git a/tests/runner.py b/tests/runner.py index 817a681a..626cc027 100644 --- a/tests/runner.py +++ b/tests/runner.py @@ -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 |