<commit_before>#include <list>
#include <string>
#include <exception>
#include "parse-cache.h"
#include "parser/all.h"
#include "ast/all.h"
#include "ast/extra.h"
#include "globals.h"
#include "util/file-system.h"
#include "util/system.h"
#include "util/debug.h"

using namespace std;

namespace Mugen{

static int replaceSlash(int what){
    if (what == '/'){
        return '-';
    }

    if (what == '\\'){
        return '-';
    }

    return what;
}

const char * MUGEN_CACHE = "mugen-cache";

/* true if path1 has a newer modification time than path2 */
static bool newer(const Filesystem::AbsolutePath & path1, const Filesystem::AbsolutePath & path2){
    return System::getModificationTime(path1.path()) >= System::getModificationTime(path2.path());
}

static list<Ast::Section*> * loadCached(const string & path){
    string converted = path;
    std::transform(converted.begin(), converted.end(), converted.begin(), replaceSlash);
    Filesystem::AbsolutePath fullPath = Filesystem::userDirectory().join(Filesystem::RelativePath(MUGEN_CACHE)).join(Filesystem::RelativePath(converted));
    if (!newer(fullPath, Filesystem::AbsolutePath(path))){
        throw MugenException("File is old");
    }
    TokenReader reader;
    Global::debug(1, "mugen-parse-cache") << "Loading from cache " << fullPath.path() << endl;
    return Ast::AstParse::deserialize(reader.readTokenFromFile(fullPath.path().c_str()));
}

static void saveParse(const Filesystem::AbsolutePath & path, list<Ast::Section*> * parse){
    ofstream out(path.path().c_str());
    Token * serial = Ast::AstParse::serialize(parse);
    serial->toStringCompact(out);
    out << endl;
    out.close();
    delete serial;
}

static void saveCached(list<Ast::Section*> * parse, const string & path){
    string converted = path;<commit_msg>dont load from the parse cache if the original file is gone<commit_after>#include <list>
#include <string>
#include <exception>
#include "parse-cache.h"
#include "parser/all.h"
#include "ast/all.h"
#include "ast/extra.h"
#include "globals.h"
#include "util/file-system.h"
#include "util/system.h"
#include "util/debug.h"

using namespace std;

namespace Mugen{

static int replaceSlash(int what){
    if (what == '/'){
        return '-';
    }

    if (what == '\\'){
        return '-';
    }

    return what;
}

const char * MUGEN_CACHE = "mugen-cache";

/* true if path1 has a newer modification time than path2 */
static bool newer(const Filesystem::AbsolutePath & path1, const Filesystem::AbsolutePath & path2){
    return System::getModificationTime(path1.path()) >= System::getModificationTime(path2.path());
}

static list<Ast::Section*> * loadCached(const string & path){
    string converted = path;
    std::transform(converted.begin(), converted.end(), converted.begin(), replaceSlash);
    if (!Filesystem::exists(Filesystem::AbsolutePath(path))){
        throw MugenException("Original file does not exist");
    }
    Filesystem::AbsolutePath fullPath = Filesystem::userDirectory().join(Filesystem::RelativePath(MUGEN_CACHE)).join(Filesystem::RelativePath(converted));
    if (!newer(fullPath, Filesystem::AbsolutePath(path))){
        throw MugenException("File is old");
    }
    TokenReader reader;
    Global::debug(1, "mugen-parse-cache") << "Loading from cache " << fullPath.path() << endl;
    return Ast::AstParse::deserialize(reader.readTokenFromFile(fullPath.path().c_str()));
}

static void saveParse(const Filesystem::AbsolutePath & path, list<Ast::Section*> * parse){
    ofstream out(path.path().c_str());
    Token * serial = Ast::AstParse::serialize(parse);
    serial->toStringCompact(out);
    out << endl;
    out.close();
    delete serial;
}

static void saveCached(list<Ast::Section*> * parse, const string & path){
    string converted = path;
{'fraction_non_alphanumeric': 0.1571729957805907, 'fraction_numerical': 0.003691983122362869, 'mean_word_length': 4.619259259259259, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 19, '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': '1328669', 'n_tokens_mistral': 1188, 'n_tokens_neox': 1165, 'n_words': 268}