<commit_before>#include "sourcefile.h"
#include <fstream>
#include <sstream>

const size_t buffer_size=131072;

//Only use on small files
std::string juci::filesystem::read(const std::string &path) {
  std::stringstream ss;
  std::ifstream input(path, std::ofstream::binary);
  if(input) {
    ss << input.rdbuf();
    input.close();
  }
  return ss.str();
}

bool juci::filesystem::read(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer) {
  std::ifstream input(path, std::ofstream::binary);
  if(input) {
    std::vector<char> buffer(buffer_size);
    size_t read_length;
    while((read_length=input.read(&buffer[0], buffer_size).gcount())>0) {
      auto ustr=Glib::ustring(std::string(&buffer[0], read_length));
      if(ustr.validate())
        text_buffer->insert_at_cursor(ustr);
      else {
        input.close();
        return false;
      }
    }
    input.close();
    return true;
  }
  return false;
}

//Only use on small files
std::vector<std::string> juci::filesystem::read_lines(const std::string &path) {
  std::vector<std::string> res;
  std::ifstream input(path, std::ofstream::binary);
  if (input) {
    do { res.emplace_back(); } while(getline(input, res.back()));
  }
  input.close();
  return res;
}

//Only use on small files
bool juci::filesystem::write(const std::string &path, const std::string &new_content) {<commit_msg>Fixed potential crash if file-read accidently splits up an UTF-8 char.<commit_after>#include "sourcefile.h"
#include <fstream>
#include <sstream>

const size_t buffer_size=131072;

//Only use on small files
std::string juci::filesystem::read(const std::string &path) {
  std::stringstream ss;
  std::ifstream input(path, std::ofstream::binary);
  if(input) {
    ss << input.rdbuf();
    input.close();
  }
  return ss.str();
}

bool juci::filesystem::read(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer) {
  std::ifstream input(path, std::ofstream::binary);
  if(input) {
    std::vector<char> buffer(buffer_size);
    size_t read_length;
    std::string buffer_str;
    while((read_length=input.read(&buffer[0], buffer_size).gcount())>0) {
      buffer_str+=std::string(&buffer[0], read_length);
      if(buffer_str.back()>=0) {
        auto ustr=Glib::ustring(buffer_str);
        buffer_str="";
        if(ustr.validate())
          text_buffer->insert_at_cursor(ustr);
        else {
          input.close();
          return false;
        }
      }
    }
    input.close();
    return true;
  }
  return false;
}

//Only use on small files
std::vector<std::string> juci::filesystem::read_lines(const std::string &path) {
  std::vector<std::string> res;
  std::ifstream input(path, std::ofstream::binary);
  if (input) {
    do { res.emplace_back(); } while(getline(input, res.back()));
  }
  input.close();
  return res;
}

//Only use on small files
bool juci::filesystem::write(const std::string &path, const std::string &new_content) {
{'fraction_non_alphanumeric': 0.165587734241908, 'fraction_numerical': 0.0068143100511073255, 'mean_word_length': 4.169014084507042, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 20, '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': '5478255', 'n_tokens_mistral': 1013, 'n_tokens_neox': 1014, 'n_words': 203}