<commit_before>#include <stdlib.h>

#include <boost/format.hpp>

#include <fstream>

#include <object/file.hh>
#include <object/path.hh>
#include <runner/raise.hh>

namespace object
{
  using boost::format;

  /*------------.
  | C++ methods |
  `------------*/

  File::value_type File::value_get()
  {
    return path_;
  }


  /*-------------.
  | Urbi methods |
  `-------------*/

  // Construction

  File::File()
    : path_(new Path("/"))
  {
    proto_add(proto);
  }

  File::File(rFile model)
    : path_(model.get()->path_)
  {
    proto_add(model);
  }

  File::File(const std::string& value)
    : path_(new Path(value))
  {
    proto_add(proto ? proto : object_class);
  }

  void File::init(rPath path)
  {
    if (!path->is_reg())
      runner::raise_primitive_error(str(format("Not a regular file: '%s'") %
					path->as_string()));
    path_ = path;
  }

  void File::init(const std::string& path)
  {
    init(new Path(path));
  }

  // Conversions

  rList File::as_list()
  {
    std::ifstream s(path_->as_string().c_str());
    List::value_type res;

    char buf[BUFSIZ + 1];
    std::string line;

    // Split in lines, 'Back in the stone age' version.
    while (!s.eof())
    {
      s.read(buf, sizeof buf - 1);
      buf[s.gcount()] = 0;
      char* str = buf;
      while (char* cut = strstr(str, "\n"))
      {
        *cut = 0;
        res << new String(line + str);
        line = "";
        str = cut + 1;
      }
      line += str;
    }

    // Bad bad bad user! The file does not finish with a \n! Handle it
    // anyway ...
    if (line != "")
      res << new String(line);<commit_msg>Check whether a file was successfully opened before reading it.<commit_after>#include <stdlib.h>

#include <boost/format.hpp>

#include <fstream>

#include <object/file.hh>
#include <object/path.hh>
#include <runner/raise.hh>

namespace object
{
  using boost::format;

  /*------------.
  | C++ methods |
  `------------*/

  File::value_type File::value_get()
  {
    return path_;
  }


  /*-------------.
  | Urbi methods |
  `-------------*/

  // Construction

  File::File()
    : path_(new Path("/"))
  {
    proto_add(proto);
  }

  File::File(rFile model)
    : path_(model.get()->path_)
  {
    proto_add(model);
  }

  File::File(const std::string& value)
    : path_(new Path(value))
  {
    proto_add(proto ? proto : object_class);
  }

  void File::init(rPath path)
  {
    if (!path->is_reg())
      runner::raise_primitive_error(str(format("Not a regular file: '%s'") %
					path->as_string()));
    path_ = path;
  }

  void File::init(const std::string& path)
  {
    init(new Path(path));
  }

  // Conversions

  rList File::as_list()
  {
    std::ifstream s(path_->as_string().c_str());

    if (!s.good())
      runner::raise_primitive_error("File not readable: " + as_string());

    List::value_type res;

    char buf[BUFSIZ + 1];
    std::string line;

    // Split in lines, 'Back in the stone age' version.
    while (!s.eof())
    {
      s.read(buf, sizeof buf - 1);
      buf[s.gcount()] = 0;
      char* str = buf;
      while (char* cut = strstr(str, "\n"))
      {
        *cut = 0;
        res << new String(line + str);
        line = "";
        str = cut + 1;
      }
      line += str;
    }

    // Bad bad bad user! The file does not finish with a \n! Handle it
    // anyway ...
    if (line != "")
      res << new String(line);
{'fraction_non_alphanumeric': 0.19711679905854662, 'fraction_numerical': 0.0029420417769932335, 'mean_word_length': 2.130755064456722, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '6495180', 'n_tokens_mistral': 1230, 'n_tokens_neox': 1176, 'n_words': 272}