<commit_before>#include "saveable.h"

const Saveable::SAVE_FORMAT Saveable::DEFAULT_SAVE_FORMAT;
/**
 * @brief Saveable::~Saveable
 * virtual constructor
 */
std::string Saveable::full_path() const
{
    return m_full_path;
}

Saveable::Saveable(){

}
/**
 * @brief Saveable::~Saveable
 * virtual destructor
 */
Saveable::~Saveable(){

}

/**
 * @brief FileHandler::save_saveable
 * @param savable
 * @param dir_path
 * @param save_format
 * @return Saves a Json file to provided directory with file_name and save_format
 */
bool Saveable::save_saveable(const std::string& file_name,
                             const std::string& dir_path, const SAVE_FORMAT& save_format){
    QDir dir;
    dir.mkpath(QString::fromStdString(dir_path));
    QFile save_file(save_format == JSON
                    ? QString::fromStdString(dir_path + "/" + file_name +".json")
                    : QString::fromStdString(dir_path + "/" + file_name + ".dat"));
    if(!save_file.open(QIODevice::WriteOnly)){
        qWarning("Couldn't open save file.");
        return false;
    }
    QJsonObject json_saveable;
    this->write(json_saveable);
    QJsonDocument save_doc(json_saveable);
    save_file.write(save_format == JSON
            ? save_doc.toJson()
            : save_doc.toBinaryData());
    m_full_path = save_file.fileName().toStdString();
    return true;
}

/**
 * @brief Saveable::save_saveable
 * @param full_path
 * @param save_format
 * @return true if successfull, false else
 */
bool Saveable::save_saveable(const std::string &full_path, const Saveable::SAVE_FORMAT &save_format)
{
    QFile save_file(save_format == JSON     // Decide what format was used
                    ? QString::fromStdString(full_path + ".json")
                    : QString::fromStdString(full_path + ".dat"));
    if(!save_file.open(QIODevice::WriteOnly)){
        qWarning("Couldn't open save file.");
        return false;
    }
    QJsonObject json_saveable;
    this->write(json_saveable);                        //Write to json
    QJsonDocument save_doc(json_saveable);
    save_file.write(save_format == JSON
            ? save_doc.toJson()
            : save_doc.toBinaryData());
    m_full_path = save_file.fileName().toStdString(); // Set path to saved path
    return true;
}

/**
 * @brief Saveable::delete_saveable
 * @return
 */
bool Saveable::delete_saveable()
{
    QFile file(QString::fromStdString(m_full_path));
    file.remove();
}

/**
 * @brief Saveable::load_savable<commit_msg>Save fix (#14)<commit_after>#include "saveable.h"

const Saveable::SAVE_FORMAT Saveable::DEFAULT_SAVE_FORMAT;
/**
 * @brief Saveable::~Saveable
 * virtual constructor
 */
std::string Saveable::full_path() const
{
    return m_full_path;
}

Saveable::Saveable(){

}
/**
 * @brief Saveable::~Saveable
 * virtual destructor
 */
Saveable::~Saveable(){

}

/**
 * @brief FileHandler::save_saveable
 * @param savable
 * @param dir_path
 * @param save_format
 * @return Saves a Json file to provided directory with file_name and save_format
 */
bool Saveable::save_saveable(const std::string& file_name,
                             const std::string& dir_path, const SAVE_FORMAT& save_format){
    QDir dir;
    dir.mkpath(QString::fromStdString(dir_path));
    QFile save_file(save_format == JSON
                    ? QString::fromStdString(dir_path + "/" + file_name +".json")
                    : QString::fromStdString(dir_path + "/" + file_name + ".dat"));
    if(!save_file.open(QIODevice::WriteOnly)){
        qWarning("Couldn't open save file.");
        return false;
    }
    QJsonObject json_saveable;
    this->write(json_saveable);
    QJsonDocument save_doc(json_saveable);
    save_file.write(save_format == JSON
            ? save_doc.toJson()
            : save_doc.toBinaryData());

    m_full_path = (file_name.find(".json") || file_name.find(".dat"))
            ? dir_path + "/" + file_name
            : save_file.fileName().toStdString();

    return true;
}

/**
 * @brief Saveable::save_saveable
 * @param full_path
 * @param save_format
 * @return true if successfull, false else
 */
bool Saveable::save_saveable(const std::string &full_path, const Saveable::SAVE_FORMAT &save_format)
{
    QFile save_file(save_format == JSON     // Decide what format was used
                    ? QString::fromStdString(full_path + ".json")
                    : QString::fromStdString(full_path + ".dat"));
    if(!save_file.open(QIODevice::WriteOnly)){
        qWarning("Couldn't open save file.");
        return false;
    }
    QJsonObject json_saveable;
    this->write(json_saveable);                        //Write to json
    QJsonDocument save_doc(json_saveable);
    save_file.write(save_format == JSON
            ? save_doc.toJson()
            : save_doc.toBinaryData());
    m_full_path = (full_path.find(".json") || full_path.find(".dat"))
            ? full_path
            : save_file.fileName().toStdString();
    return true;
}

/**
 * @brief Saveable::delete_saveable
 * @return
 */
bool Saveable::delete_saveable()
{
    QFile file(QString::fromStdString(m_full_path));
    file.remove();
}

/**
 * @brief Saveable::load_savable
{'fraction_non_alphanumeric': 0.13364413364413363, 'fraction_numerical': 0.0003885003885003885, 'mean_word_length': 3.106060606060606, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1050742', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1614, 'n_words': 305}