Skip to content

Commit d0ae4a2

Browse files
committed
Merge pull request WebAssembly#303 from WebAssembly/binary-files-for-windows
Set the binary bit on files we need to open in binary mode
2 parents 57bd72b + 286b5be commit d0ae4a2

8 files changed

Lines changed: 27 additions & 22 deletions

File tree

src/asm2wasm-main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ int main(int argc, const char *argv[]) {
7777

7878
Asm2WasmPreProcessor pre;
7979
auto input(
80-
read_file<std::vector<char>>(options.extra["infile"], options.debug));
80+
read_file<std::vector<char>>(options.extra["infile"], false, options.debug));
8181
char *start = pre.process(input.data());
8282

8383
if (options.debug) std::cerr << "parsing..." << std::endl;
@@ -101,7 +101,7 @@ int main(int argc, const char *argv[]) {
101101
}
102102

103103
if (options.debug) std::cerr << "printing..." << std::endl;
104-
Output output(options.extra["output"], options.debug);
104+
Output output(options.extra["output"], false, options.debug);
105105
printWasm(&wasm, output.getStream());
106106

107107
if (mappedGlobals) {

src/binaryen-shell.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ int main(int argc, const char* argv[]) {
372372
}
373373
options.parse(argc, argv);
374374

375-
auto input(read_file<std::vector<char>>(options.extra["infile"], options.debug));
375+
auto input(read_file<std::vector<char>>(options.extra["infile"], false, options.debug));
376376

377377
if (options.debug) std::cerr << "parsing text to s-expressions...\n";
378378
SExpressionParser parser(input.data());

src/s2wasm-main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ int main(int argc, const char *argv[]) {
7474
});
7575
options.parse(argc, argv);
7676

77-
auto input(read_file<std::string>(options.extra["infile"], options.debug));
77+
auto input(read_file<std::string>(options.extra["infile"], false, options.debug));
7878

7979
if (options.debug) std::cerr << "Parsing and wasming..." << std::endl;
8080
AllocatingModule wasm;
@@ -103,7 +103,7 @@ int main(int argc, const char *argv[]) {
103103
s2wasm.emscriptenGlue(meta);
104104

105105
if (options.debug) std::cerr << "Printing..." << std::endl;
106-
Output output(options.extra["output"], options.debug);
106+
Output output(options.extra["output"], false, options.debug);
107107
printWasm(&wasm, output.getStream());
108108
output << meta.str() << std::endl;
109109

src/support/file.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
#include <limits>
2121

2222
template <typename T>
23-
T wasm::read_file(const std::string &filename, bool debug) {
23+
T wasm::read_file(const std::string &filename, bool binary, bool debug) {
2424
if (debug) std::cerr << "Loading '" << filename << "'..." << std::endl;
25-
std::ifstream infile(filename);
25+
std::ifstream infile;
26+
auto flags = std::ifstream::in;
27+
if (binary) flags |= std::ifstream::binary;
28+
infile.open(filename, flags);
2629
if (!infile.is_open()) {
2730
std::cerr << "Failed opening '" << filename << "'" << std::endl;
2831
exit(EXIT_FAILURE);
@@ -41,15 +44,17 @@ T wasm::read_file(const std::string &filename, bool debug) {
4144
}
4245

4346
// Explicit instantiations for the explicit specializations.
44-
template std::string wasm::read_file<>(const std::string &, bool);
45-
template std::vector<char> wasm::read_file<>(const std::string &, bool);
47+
template std::string wasm::read_file<>(const std::string &, bool, bool);
48+
template std::vector<char> wasm::read_file<>(const std::string &, bool, bool);
4649

47-
wasm::Output::Output(const std::string &filename, bool debug)
48-
: outfile(), out([this, filename, debug]() {
50+
wasm::Output::Output(const std::string &filename, bool binary, bool debug)
51+
: outfile(), out([this, filename, binary, debug]() {
4952
std::streambuf *buffer;
5053
if (filename.size()) {
5154
if (debug) std::cerr << "Opening '" << filename << std::endl;
52-
outfile.open(filename, std::ofstream::out | std::ofstream::trunc);
55+
auto flags = std::ofstream::out | std::ofstream::trunc;
56+
if (binary) flags |= std::ofstream::binary;
57+
outfile.open(filename, flags);
5358
if (!outfile.is_open()) {
5459
std::cerr << "Failed opening '" << filename << "'" << std::endl;
5560
exit(EXIT_FAILURE);

src/support/file.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@
2929

3030
namespace wasm {
3131
template <typename T>
32-
T read_file(const std::string &filename, bool debug);
32+
T read_file(const std::string &filename, bool binary, bool debug);
3333
// Declare the valid explicit specializations.
34-
extern template std::string read_file<>(const std::string &, bool);
35-
extern template std::vector<char> read_file<>(const std::string &, bool);
34+
extern template std::string read_file<>(const std::string &, bool, bool);
35+
extern template std::vector<char> read_file<>(const std::string &, bool, bool);
3636

3737
class Output {
3838
public:
3939
// An empty filename will open stdout instead.
40-
Output(const std::string &filename, bool debug);
40+
Output(const std::string &filename, bool binary, bool debug);
4141
~Output() = default;
4242
template <typename T>
4343
std::ostream &operator<<(const T &v) {

src/wasm-as.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int main(int argc, const char *argv[]) {
4141
});
4242
options.parse(argc, argv);
4343

44-
auto input(read_file<std::string>(options.extra["infile"], options.debug));
44+
auto input(read_file<std::string>(options.extra["infile"], false, options.debug));
4545

4646
if (options.debug) std::cerr << "s-parsing..." << std::endl;
4747
SExpressionParser parser(const_cast<char*>(input.c_str()));
@@ -57,7 +57,7 @@ int main(int argc, const char *argv[]) {
5757
writer.write();
5858

5959
if (options.debug) std::cerr << "writing to output..." << std::endl;
60-
Output output(options.extra["output"], options.debug);
60+
Output output(options.extra["output"], true, options.debug);
6161
buffer.writeTo(output);
6262

6363
if (options.debug) std::cerr << "Done." << std::endl;

src/wasm-dis.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ int main(int argc, const char *argv[]) {
4141
});
4242
options.parse(argc, argv);
4343

44-
auto input(read_file<std::vector<char>>(options.extra["infile"], options.debug));
44+
auto input(read_file<std::vector<char>>(options.extra["infile"], true, options.debug));
4545

4646
if (options.debug) std::cerr << "parsing binary..." << std::endl;
4747
AllocatingModule wasm;
4848
WasmBinaryBuilder parser(wasm, input, options.debug);
4949
parser.read();
5050

5151
if (options.debug) std::cerr << "Printing..." << std::endl;
52-
Output output(options.extra["output"], options.debug);
52+
Output output(options.extra["output"], false, options.debug);
5353
printWasm(&wasm, output.getStream());
5454
output << '\n';
5555

src/wasm2asm-main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ int main(int argc, const char *argv[]) {
4444
options.parse(argc, argv);
4545

4646
auto input(
47-
read_file<std::vector<char>>(options.extra["infile"], options.debug));
47+
read_file<std::vector<char>>(options.extra["infile"], false, options.debug));
4848

4949
if (options.debug) std::cerr << "s-parsing..." << std::endl;
5050
SExpressionParser parser(input.data());
@@ -67,7 +67,7 @@ int main(int argc, const char *argv[]) {
6767
if (options.debug) std::cerr << "j-printing..." << std::endl;
6868
JSPrinter jser(true, true, asmjs);
6969
jser.printAst();
70-
Output output(options.extra["output"], options.debug);
70+
Output output(options.extra["output"], false, options.debug);
7171
output << jser.buffer << std::endl;
7272

7373
if (options.debug) std::cerr << "done." << std::endl;

0 commit comments

Comments
 (0)