diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/binary-reader-objdump.cc | 18 | ||||
-rw-r--r-- | src/binary-writer-spec.cc | 48 | ||||
-rw-r--r-- | src/filenames.cc | 53 | ||||
-rw-r--r-- | src/filenames.h | 51 | ||||
-rw-r--r-- | src/test-filenames.cc | 61 | ||||
-rw-r--r-- | src/tools/wat2wasm.cc | 24 |
6 files changed, 193 insertions, 62 deletions
diff --git a/src/binary-reader-objdump.cc b/src/binary-reader-objdump.cc index 9bacba58..cef44c18 100644 --- a/src/binary-reader-objdump.cc +++ b/src/binary-reader-objdump.cc @@ -24,6 +24,7 @@ #include <vector> #include "src/binary-reader-nop.h" +#include "src/filenames.h" #include "src/literal.h" namespace wabt { @@ -102,20 +103,9 @@ Result BinaryReaderObjdumpBase::BeginModule(uint32_t version) { printf("Code Disassembly:\n\n"); break; case ObjdumpMode::Prepass: { - const char* last_slash = strrchr(options_->filename, '/'); - const char* last_backslash = strrchr(options_->filename, '\\'); - const char* basename; - if (last_slash && last_backslash) { - basename = std::max(last_slash, last_backslash) + 1; - } else if (last_slash) { - basename = last_slash + 1; - } else if (last_backslash) { - basename = last_backslash + 1; - } else { - basename = options_->filename; - } - - printf("%s:\tfile format wasm %#x\n", basename, version); + string_view basename = GetBasename(options_->filename); + printf("%s:\tfile format wasm %#x\n", basename.to_string().c_str(), + version); break; } case ObjdumpMode::RawData: diff --git a/src/binary-writer-spec.cc b/src/binary-writer-spec.cc index ab6eb947..5e4f95ab 100644 --- a/src/binary-writer-spec.cc +++ b/src/binary-writer-spec.cc @@ -24,39 +24,13 @@ #include "src/binary.h" #include "src/binary-writer.h" #include "src/cast.h" +#include "src/filenames.h" #include "src/ir.h" #include "src/stream.h" #include "src/string-view.h" namespace wabt { -static string_view strip_extension(string_view s) { - // Strip .json or .wasm, but leave other extensions, e.g.: - // - // s = "foo", => "foo" - // s = "foo.json" => "foo" - // s = "foo.wasm" => "foo" - // s = "foo.bar" => "foo.bar" - string_view ext = s.substr(s.find_last_of('.')); - string_view result = s; - - if (ext == ".json" || ext == ".wasm") - result.remove_suffix(ext.length()); - return result; -} - -static string_view get_basename(string_view s) { - // Strip everything up to and including the last slash, e.g.: - // - // s = "/foo/bar/baz", => "baz" - // s = "/usr/local/include/stdio.h", => "stdio.h" - // s = "foo.bar", => "foo.bar" - size_t last_slash = s.find_last_of('/'); - if (last_slash != string_view::npos) - return s.substr(last_slash + 1); - return s; -} - namespace { class BinaryWriterSpec { @@ -93,24 +67,16 @@ class BinaryWriterSpec { const WriteBinarySpecOptions* spec_options_ = nullptr; Result result_ = Result::Ok; size_t num_modules_ = 0; - - static const char* kWasmExtension; - static const char* kWatExtension; }; -// static -const char* BinaryWriterSpec::kWasmExtension = ".wasm"; -// static -const char* BinaryWriterSpec::kWatExtension = ".wat"; - BinaryWriterSpec::BinaryWriterSpec(const char* source_filename, const WriteBinarySpecOptions* spec_options) : spec_options_(spec_options) { source_filename_ = source_filename; - module_filename_noext_ = strip_extension(spec_options_->json_filename - ? spec_options_->json_filename - : source_filename) - .to_string(); + module_filename_noext_ = + StripExtension(spec_options_->json_filename ? spec_options_->json_filename + : source_filename) + .to_string(); write_modules_ = !!spec_options_->json_filename; } @@ -370,7 +336,7 @@ void BinaryWriterSpec::WriteInvalidModule(const ScriptModule& module, WriteSeparator(); std::string filename = GetModuleFilename(extension); WriteKey("filename"); - WriteEscapedString(get_basename(filename)); + WriteEscapedString(GetBasename(filename)); WriteSeparator(); WriteKey("text"); WriteEscapedString(text); @@ -409,7 +375,7 @@ void BinaryWriterSpec::WriteCommands(const Script& script) { WriteSeparator(); } WriteKey("filename"); - WriteEscapedString(get_basename(filename)); + WriteEscapedString(GetBasename(filename)); WriteModule(filename, module); num_modules_++; last_module_index = i; diff --git a/src/filenames.cc b/src/filenames.cc new file mode 100644 index 00000000..a1e53aa9 --- /dev/null +++ b/src/filenames.cc @@ -0,0 +1,53 @@ +/* + * Copyright 2016 WebAssembly Community Group participants + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "src/filenames.h" + +namespace wabt { + +const char* kWasmExtension = ".wasm"; + +const char* kWatExtension = ".wat"; + +string_view StripExtension(string_view filename) { + return filename.substr(0, filename.find_last_of('.')); +} + +string_view GetBasename(string_view filename) { + size_t last_slash = filename.find_last_of('/'); + size_t last_backslash = filename.find_last_of('\\'); + if (last_slash == string_view::npos && last_backslash == string_view::npos) + return filename; + + if (last_slash == string_view::npos) { + if (last_backslash == string_view::npos) + return filename; + last_slash = last_backslash; + } else if (last_backslash != string_view::npos) { + last_slash = std::max(last_slash, last_backslash); + } + + return filename.substr(last_slash + 1); +} + +string_view GetExtension(string_view filename) { + size_t pos = filename.find_last_of('.'); + if (pos == string_view::npos) + return ""; + return filename.substr(pos); +} + +} // namespace wabt diff --git a/src/filenames.h b/src/filenames.h new file mode 100644 index 00000000..87529aa2 --- /dev/null +++ b/src/filenames.h @@ -0,0 +1,51 @@ +/* + * Copyright 2016 WebAssembly Community Group participants + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef WABT_FILENAMES_H_ +#define WABT_FILENAMES_H_ + +#include "src/common.h" + +namespace wabt { + +extern const char* kWasmExtension; +extern const char* kWatExtension; + +// Return only the file extension, e.g.: +// +// "foo.txt", => ".txt" +// "foo" => "" +// "/foo/bar/foo.wasm" => ".wasm" +string_view GetExtension(string_view filename); + +// Strip extension, e.g.: +// +// "foo", => "foo" +// "foo.bar" => "foo" +// "/path/to/foo.bar" => "/path/to/foo" +// "\\path\\to\\foo.bar" => "\\path\\to\\foo" +string_view StripExtension(string_view s); + +// Strip everything up to and including the last slash, e.g.: +// +// "/foo/bar/baz", => "baz" +// "/usr/local/include/stdio.h", => "stdio.h" +// "foo.bar", => "foo.bar" +string_view GetBasename(string_view filename); + +} // namespace wabt + +#endif /* WABT_FILENAMES_H_ */ diff --git a/src/test-filenames.cc b/src/test-filenames.cc new file mode 100644 index 00000000..5b0282fc --- /dev/null +++ b/src/test-filenames.cc @@ -0,0 +1,61 @@ +/* + * Copyright 2017 WebAssembly Community Group participants + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "gtest/gtest.h" + +#include "src/filenames.h" + +using namespace wabt; + +namespace { + +void assert_string_view_eq(const char* s, string_view sv) { + size_t len = std::strlen(s); + ASSERT_EQ(len, sv.size()); + for (size_t i = 0; i < len; ++i) { + ASSERT_EQ(s[i], sv[i]); + } +} + +} // end anonymous namespace + +TEST(filenames, GetBasename) { + assert_string_view_eq("foo.txt", GetBasename("/bar/dir/foo.txt")); + assert_string_view_eq("foo.txt", GetBasename("bar/dir/foo.txt")); + assert_string_view_eq("foo.txt", GetBasename("/foo.txt")); + assert_string_view_eq("foo.txt", GetBasename("\\bar\\dir\\foo.txt")); + assert_string_view_eq("foo.txt", GetBasename("bar\\dir\\foo.txt")); + assert_string_view_eq("foo.txt", GetBasename("\\foo.txt")); + assert_string_view_eq("foo.txt", GetBasename("foo.txt")); + assert_string_view_eq("foo", GetBasename("foo")); + assert_string_view_eq("", GetBasename("")); +} + +TEST(filenames, GetExtension) { + assert_string_view_eq(".txt", GetExtension("/bar/dir/foo.txt")); + assert_string_view_eq(".txt", GetExtension("bar/dir/foo.txt")); + assert_string_view_eq(".txt", GetExtension("foo.txt")); + assert_string_view_eq("", GetExtension("foo")); + assert_string_view_eq("", GetExtension("")); +} + +TEST(filenames, StripExtension) { + assert_string_view_eq("/bar/dir/foo", StripExtension("/bar/dir/foo.txt")); + assert_string_view_eq("bar/dir/foo", StripExtension("bar/dir/foo.txt")); + assert_string_view_eq("foo", StripExtension("foo.txt")); + assert_string_view_eq("foo", StripExtension("foo")); + assert_string_view_eq("", StripExtension("")); +} diff --git a/src/tools/wat2wasm.cc b/src/tools/wat2wasm.cc index 2eb63a42..90c6ccb5 100644 --- a/src/tools/wat2wasm.cc +++ b/src/tools/wat2wasm.cc @@ -27,6 +27,7 @@ #include "src/common.h" #include "src/error-handler.h" #include "src/feature.h" +#include "src/filenames.h" #include "src/ir.h" #include "src/option-parser.h" #include "src/resolve-names.h" @@ -37,7 +38,7 @@ using namespace wabt; static const char* s_infile; -static const char* s_outfile; +static std::string s_outfile; static bool s_dump_module; static int s_verbose; static WriteBinaryOptions s_write_binary_options; @@ -98,7 +99,7 @@ static void ParseOptions(int argc, char* argv[]) { parser.Parse(argc, argv); } -static void WriteBufferToFile(const char* filename, +static void WriteBufferToFile(string_view filename, const OutputBuffer& buffer) { if (s_dump_module) { if (s_verbose) @@ -108,9 +109,15 @@ static void WriteBufferToFile(const char* filename, } } - if (filename) { - buffer.WriteToFile(filename); - } + buffer.WriteToFile(filename); +} + +static std::string DefaultOuputName(string_view input_name) { + // Strip existing extension and add .wasm + std::string result(StripExtension(GetBasename(input_name))); + result += kWasmExtension; + + return result; } int ProgramMain(int argc, char** argv) { @@ -139,8 +146,11 @@ int ProgramMain(int argc, char** argv) { result = WriteBinaryModule(&stream, module.get(), &s_write_binary_options); - if (Succeeded(result)) - WriteBufferToFile(s_outfile, stream.output_buffer()); + if (Succeeded(result)) { + if (s_outfile.empty()) + s_outfile = DefaultOuputName(s_infile); + WriteBufferToFile(s_outfile.c_str(), stream.output_buffer()); + } } } |