<commit_before>#include "pch.h"
#include "zip_packages.h"




std::unordered_map< std::string, std::shared_ptr< zip > > ZipPackagesCache::_packages;


/**
 * Gets the fully resolved path.
 * If the path is relative then it will be prefixed with the resource path.
 * Aliases will be converted to a relative path.
 * 
 * @param path The path to resolve.
 * @param fullPath The full resolved path. (out param)
 */
static void getFullPath(const char* path, std::string& fullPath)
{
    if (gameplay::FileSystem::isAbsolutePath(path))
    {
        fullPath.assign(path);
    }
    else
    {
        fullPath.assign(gameplay::FileSystem::getResourcePath());
        fullPath += gameplay::FileSystem::resolvePath(path);
    }
}



zip * ZipPackagesCache::findOrOpenPackage(const char * packageName)
{
    if (packageName == NULL || *packageName == '\0')
        return NULL;
    
    auto package = _packages.find(packageName);
    if (package == _packages.end())
    {
#ifndef __ANDROID__
        std::string fullPath;
        getFullPath(packageName, fullPath);
#else
        std::string fullPath(packageName);
#endif

        int err = 0;
        zip * res = zip_open(fullPath.c_str(), 0, &err);

        if (!res)
        {
            GP_WARN("Can't open package %s", packageName);
            return NULL;
        }

        _packages.insert(std::make_pair(std::string(packageName), std::shared_ptr<zip>(res, zip_close)));
        return res;
    }

    return (*package).second.get();
}

bool ZipPackagesCache::hasFile(const char * packageName, const char * filename)
{
    zip * package = findOrOpenPackage(packageName);
    if (!package)
        return NULL;

    return zip_name_locate(package, filename, ZIP_FL_ENC_GUESS | ZIP_FL_NOCASE) >= 0;
}

void ZipPackagesCache::closePackage(const char * packageName)
{<commit_msg>fixed filename lookup in zip packages<commit_after>#include "pch.h"
#include "zip_packages.h"




std::unordered_map< std::string, std::shared_ptr< zip > > ZipPackagesCache::_packages;


/**
 * Gets the fully resolved path.
 * If the path is relative then it will be prefixed with the resource path.
 * Aliases will be converted to a relative path.
 * 
 * @param path The path to resolve.
 * @param fullPath The full resolved path. (out param)
 */
static void getFullPath(const char* path, std::string& fullPath)
{
    if (gameplay::FileSystem::isAbsolutePath(path))
    {
        fullPath.assign(path);
    }
    else
    {
        fullPath.assign(gameplay::FileSystem::getResourcePath());
        fullPath += gameplay::FileSystem::resolvePath(path);
    }
}



zip * ZipPackagesCache::findOrOpenPackage(const char * packageName)
{
    if (packageName == NULL || *packageName == '\0')
        return NULL;
    
    auto package = _packages.find(packageName);
    if (package == _packages.end())
    {
#ifndef __ANDROID__
        std::string fullPath;
        getFullPath(packageName, fullPath);
#else
        std::string fullPath(packageName);
#endif

        int err = 0;
        zip * res = zip_open(fullPath.c_str(), 0, &err);

        if (!res)
        {
            GP_WARN("Can't open package %s", packageName);
            return NULL;
        }

        _packages.insert(std::make_pair(std::string(packageName), std::shared_ptr<zip>(res, zip_close)));
        return res;
    }

    return (*package).second.get();
}

bool ZipPackagesCache::hasFile(const char * packageName, const char * filename)
{
    if (!filename || *filename == 0 || filename[strlen(filename) - 1] == '/')   // ignore empty string, for a directory lookup method always returns false
        return false;

    zip * package = findOrOpenPackage(packageName);
    if (!package)
        return false;

    return zip_name_locate(package, filename, ZIP_FL_ENC_GUESS | ZIP_FL_NOCASE) >= 0;
}

void ZipPackagesCache::closePackage(const char * packageName)
{
{'fraction_non_alphanumeric': 0.1238021238021238, 'fraction_numerical': 0.00259000259000259, 'mean_word_length': 3.2207650273224044, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '6706328', 'n_tokens_mistral': 1211, 'n_tokens_neox': 1168, 'n_words': 302}