<commit_before>#include "htmlext/File.h"


namespace htmlext {


FileError::FileError(const std::string& msg)
: std::runtime_error(msg)
{
}


std::string ReadFileOrThrow(const std::string& path)
{
  std::ifstream file(path, std::ios::in | std::ios::binary);

  if( file.fail() )
    throw FileError(
      "cannot access '" + path + "': " + std::strerror(errno)
    );

  std::stringstream buffer;
  buffer << file.rdbuf();

  if( file.fail() )
    throw FileError(
      "cannot read '" + path + "': " + std::strerror(errno)
    );

  return buffer.str();
}

<commit_msg>html-extract: accept - (dash) as a synonym for /dev/stdin when reading files<commit_after>#include "htmlext/File.h"


namespace htmlext {


FileError::FileError(const std::string& msg)
: std::runtime_error(msg)
{
}


std::string ReadFileOrThrow(const std::string& path)
{
  std::ifstream file(path, std::ios::in | std::ios::binary);

  if( file.fail() )
  {
    if( path == "-" )
      return ReadFileOrThrow("/dev/stdin");
    else
      throw FileError(
        "cannot access '" + path + "': " + std::strerror(errno)
      );
  }

  std::stringstream buffer;
  buffer << file.rdbuf();

  if( file.fail() )
    throw FileError(
      "cannot read '" + path + "': " + std::strerror(errno)
    );

  return buffer.str();
}


{'fraction_non_alphanumeric': 0.17824074074074073, 'fraction_numerical': 0.0, 'mean_word_length': 3.457044673539519, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '943985', 'n_tokens_mistral': 475, 'n_tokens_neox': 455, 'n_words': 95}