<commit_before>/*
*  Author(s):
*  - <NAME> <<EMAIL>>
*
*  Copyright (c) 2012 Aldebaran Robotics. All rights reserved.
*/

#include <iostream>
#include <boost/filesystem.hpp>

#include <qi/qi.hpp>
#include <qimessaging/file.hpp>

#define MAX_SIZE_STREAM_CONTENT 1024

namespace qi
{
  class PrivateFile
  {
  public:
    PrivateFile()
      : path("")
      , fileName("")
      , flags(qi::Flag_Invalid)
    {
    };

  public:
    std::string path;
    std::string fileName;
    int         flags;
  };

  File::File()
    : p(new PrivateFile())
  {
  }

  File::~File()
  {
    delete p;
  }

  const std::string &File::path() const
  {
    return p->path;
  }

  const std::string &File::fileName() const
  {
    return p->fileName;
  }

  int File::flags() const
  {
    return p->flags;
  }

  bool File::open(int flags)
  {
    p->flags = flags;
    return true;
  }

  bool File::open(const std::string &path,
                  int                flags)
  {
    p->flags = flags;
    p->path = path;
    p->fileName = boost::filesystem::path(p->path, qi::unicodeFacet()).filename().string(qi::unicodeFacet());

    return boost::filesystem::exists(path);
  }

  bool File::save(const std::string &dst)
  {
    try
    {
      boost::filesystem::path filePath(p->path, qi::unicodeFacet());
      boost::filesystem::path oldFilePath(dst, qi::unicodeFacet());

      // Here, we need to be careful with windows and UTF-16 local encoding.
#ifndef _WIN32
      boost::filesystem::copy_file(filePath.make_preferred().string(qi::unicodeFacet()),
                                   oldFilePath.make_preferred().string(qi::unicodeFacet()),
                                   boost::filesystem::copy_option::overwrite_if_exists);
#else
      boost::filesystem::copy_file(filePath.make_preferred().wstring(qi::unicodeFacet()),
                                   oldFilePath.make_preferred().wstring(qi::unicodeFacet()),
                                   boost::filesystem::copy_option::overwrite_if_exists);
#endif
    }
    catch (...)
    {
      return false;
    }
    return true;
  }

  qi::DataStream &operator<<(qi::DataStream &stream,
                             const qi::File &sfile)
  {
    stream << sfile.flags();
    stream << sfile.fileName();

    boost::filesystem::path filePath(sfile.p->path, qi::unicodeFacet());
    FILE *f = qi::os::fopen(filePath.string(qi::unicodeFacet()).c_str(), "r");
    int readSize = boost::filesystem::file_size(filePath);
    char *buf = new char[readSize + 1];
    if (buf == NULL)
    {
      qiLogError("qifile.stream") << "Cannot allocate enough memory for the buffer.";
      return stream;
    }
    readSize = fread(buf, 1, readSize, f);
    buf[readSize] = '\0';
    stream << buf;

    fclose(f);
    delete[] buf;
    return stream;
  }<commit_msg>fix cast warning on windows<commit_after>/*
*  Author(s):
*  - <NAME> <<EMAIL>>
*
*  Copyright (c) 2012 Aldebaran Robotics. All rights reserved.
*/

#include <iostream>
#include <boost/filesystem.hpp>

#include <qi/qi.hpp>
#include <qimessaging/file.hpp>

#define MAX_SIZE_STREAM_CONTENT 1024

namespace qi
{
  class PrivateFile
  {
  public:
    PrivateFile()
      : path("")
      , fileName("")
      , flags(qi::Flag_Invalid)
    {
    };

  public:
    std::string path;
    std::string fileName;
    int         flags;
  };

  File::File()
    : p(new PrivateFile())
  {
  }

  File::~File()
  {
    delete p;
  }

  const std::string &File::path() const
  {
    return p->path;
  }

  const std::string &File::fileName() const
  {
    return p->fileName;
  }

  int File::flags() const
  {
    return p->flags;
  }

  bool File::open(int flags)
  {
    p->flags = flags;
    return true;
  }

  bool File::open(const std::string &path,
                  int                flags)
  {
    p->flags = flags;
    p->path = path;
    p->fileName = boost::filesystem::path(p->path, qi::unicodeFacet()).filename().string(qi::unicodeFacet());

    return boost::filesystem::exists(path);
  }

  bool File::save(const std::string &dst)
  {
    try
    {
      boost::filesystem::path filePath(p->path, qi::unicodeFacet());
      boost::filesystem::path oldFilePath(dst, qi::unicodeFacet());

      // Here, we need to be careful with windows and UTF-16 local encoding.
#ifndef _WIN32
      boost::filesystem::copy_file(filePath.make_preferred().string(qi::unicodeFacet()),
                                   oldFilePath.make_preferred().string(qi::unicodeFacet()),
                                   boost::filesystem::copy_option::overwrite_if_exists);
#else
      boost::filesystem::copy_file(filePath.make_preferred().wstring(qi::unicodeFacet()),
                                   oldFilePath.make_preferred().wstring(qi::unicodeFacet()),
                                   boost::filesystem::copy_option::overwrite_if_exists);
#endif
    }
    catch (...)
    {
      return false;
    }
    return true;
  }

  qi::DataStream &operator<<(qi::DataStream &stream,
                             const qi::File &sfile)
  {
    stream << sfile.flags();
    stream << sfile.fileName();

    boost::filesystem::path filePath(sfile.p->path, qi::unicodeFacet());
    FILE *f = qi::os::fopen(filePath.string(qi::unicodeFacet()).c_str(), "r");
    int readSize = static_cast<int>(boost::filesystem::file_size(filePath));
    char *buf = new char[readSize + 1];
    if (buf == NULL)
    {
      qiLogError("qifile.stream") << "Cannot allocate enough memory for the buffer.";
      return stream;
    }
    readSize = fread(buf, 1, readSize, f);
    buf[readSize] = '\0';
    stream << buf;

    fclose(f);
    delete[] buf;
    return stream;
  }
{'fraction_non_alphanumeric': 0.1555083244775062, 'fraction_numerical': 0.005313496280552604, 'mean_word_length': 2.553807426054122, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3367203', 'n_tokens_mistral': 1925, 'n_tokens_neox': 1827, 'n_words': 354}