<commit_before>/*
 * 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.
 */

//
// Abstracts reading and writing, supporting both text and binary
// depending on the suffix.
//
// When the suffix is unclear, writing defaults to text (this
// allows odd suffixes, which we use in the test suite), while
// reading will check the magic number and default to text if not
// binary.
//

#include "wasm-io.h"
#include "wasm-s-parser.h"
#include "wasm-binary.h"
#include "support/file.h"

namespace wasm {

void ModuleReader::readText(std::string filename, Module& wasm) {
  if (debug) std::cerr << "reading text from " << filename << "\n";
  auto input(read_file<std::string>(filename, Flags::Text, debug ? Flags::Debug : Flags::Release));
  SExpressionParser parser(const_cast<char*>(input.c_str()));
  Element& root = *parser.root;
  SExpressionWasmBuilder builder(wasm, *root[0]);
}

void ModuleReader::readBinary(std::string filename, Module& wasm) {
  if (debug) std::cerr << "reading binary from " << filename << "\n";
  auto input(read_file<std::vector<char>>(filename, Flags::Binary, debug ? Flags::Debug : Flags::Release));
  WasmBinaryBuilder parser(wasm, input, debug);
  parser.read();
}

void ModuleReader::read(std::string filename, Module& wasm) {
  // see if this is a binary
  auto contents = read_file<std::vector<char>>(filename, Flags::Binary, debug ? Flags::Debug : Flags::Release);
  if (contents.size() >= 4 && contents[0] == '\0' && contents[1] == 'a' && contents[2] == 's' && contents[3] == 'm') {
    readBinary(filename, wasm);
  } else {
    // default to text<commit_msg>only read first 4 bytes to check if a file is a wasm binary (#894)<commit_after>/*
 * 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.
 */

//
// Abstracts reading and writing, supporting both text and binary
// depending on the suffix.
//
// When the suffix is unclear, writing defaults to text (this
// allows odd suffixes, which we use in the test suite), while
// reading will check the magic number and default to text if not
// binary.
//

#include "wasm-io.h"
#include "wasm-s-parser.h"
#include "wasm-binary.h"
#include "support/file.h"

namespace wasm {

void ModuleReader::readText(std::string filename, Module& wasm) {
  if (debug) std::cerr << "reading text from " << filename << "\n";
  auto input(read_file<std::string>(filename, Flags::Text, debug ? Flags::Debug : Flags::Release));
  SExpressionParser parser(const_cast<char*>(input.c_str()));
  Element& root = *parser.root;
  SExpressionWasmBuilder builder(wasm, *root[0]);
}

void ModuleReader::readBinary(std::string filename, Module& wasm) {
  if (debug) std::cerr << "reading binary from " << filename << "\n";
  auto input(read_file<std::vector<char>>(filename, Flags::Binary, debug ? Flags::Debug : Flags::Release));
  WasmBinaryBuilder parser(wasm, input, debug);
  parser.read();
}

void ModuleReader::read(std::string filename, Module& wasm) {
  // see if this is a wasm binary
  std::ifstream infile;
  std::ios_base::openmode flags = std::ifstream::in | std::ifstream::binary;
  infile.open(filename, flags);
  char buffer[4] = { 1, 2, 3, 4 };
  infile.read(buffer, 4);
  infile.close();
  if (buffer[0] == '\0' && buffer[1] == 'a' && buffer[2] == 's' && buffer[3] == 'm') {
    readBinary(filename, wasm);
  } else {
    // default to text
{'fraction_non_alphanumeric': 0.1371004052228726, 'fraction_numerical': 0.008779828905898244, 'mean_word_length': 4.340144230769231, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7180677', 'n_tokens_mistral': 1349, 'n_tokens_neox': 1321, 'n_words': 478}