<commit_before>#include "RhodesAppBase.h"
#include "common/RhoFilePath.h"
#include "common/RhoFile.h"
#include "unzip/unzip.h"

namespace rho {
namespace common{

IMPLEMENT_LOGCLASS(CRhodesAppBase,"RhodesApp");
CRhodesAppBase* CRhodesAppBase::m_pInstance = 0;

/*static*/ CRhodesAppBase* CRhodesAppBase::Create(const String& strRootPath)
{
    if ( m_pInstance != null) 
        return m_pInstance;

    m_pInstance = new CRhodesAppBase(strRootPath);
    return m_pInstance;
}

/*static*/void CRhodesAppBase::Destroy()
{
    if ( m_pInstance )
        delete m_pInstance;

    m_pInstance = 0;
}

CRhodesAppBase::CRhodesAppBase(const String& strRootPath) : CRhoThread()
{
    m_strRhoRootPath = strRootPath;

    initAppUrls();
}
	
void CRhodesAppBase::initAppUrls() 
{
#ifndef RHODES_EMULATOR
    m_strBlobsDirPath = getRhoRootPath() + "db/db-files";
	m_strDBDirPath = getRhoRootPath() + "db";

    m_strAppRootPath = getRhoRootPath() + "apps";
    m_strRhodesPath = "";
#else
    m_strBlobsDirPath = getRhoRootPath() + RHO_EMULATOR_DIR"/db/db-files";
    m_strDBDirPath = getRhoRootPath() + RHO_EMULATOR_DIR"/db";

    m_strAppRootPath = getRhoRootPath();
#endif
}

String CRhodesAppBase::getRelativeDBFilesPath(const String& strFilePath)
{
#ifndef RHODES_EMULATOR
    String strDbFileRoot = getRhoRootPath();
#else
    String strDbFileRoot = getRhoRootPath() + RHO_EMULATOR_DIR;
#endif

    if ( !String_startsWith(strFilePath, strDbFileRoot) )
        return strFilePath;

    return strFilePath.substr(strDbFileRoot.length());
}

String CRhodesAppBase::resolveDBFilesPath(const String& strFilePath)
{
#ifndef RHODES_EMULATOR
    String strDbFileRoot = getRhoRootPath();
#else
    String strDbFileRoot = getRhoRootPath() + RHO_EMULATOR_DIR;
#endif

    if ( String_startsWith(strFilePath, strDbFileRoot) )
        return strFilePath;

    return CFilePath::join(strDbFileRoot, strFilePath);
}

String CRhodesAppBase::canonicalizeRhoUrl(const String& strUrl) 
{
    if (strUrl.length() == 0 )
        return m_strHomeUrl;

    size_t pos = strUrl.find_first_of(":#");
    if((pos == String::npos) || (strUrl.at(pos) == '#'))
        return CFilePath::join(m_strHomeUrl,strUrl);

    return strUrl;
}

} //namespace common
} //namespace rho

extern "C" {

int rho_unzip_file(const char* szZipPath)
{
#ifdef  UNICODE
    rho::StringW strZipPathW;
    rho::common::convertToStringW(szZipPath, strZipPathW);
    HZIP hz = OpenZipFile(strZipPathW.c_str(), "");
    if ( !hz )
        return 0;

	// Set base for unziping
    SetUnzipBaseDir(hz, rho::common::convertToStringW(RHODESAPPBASE().getDBDirPath()).c_str() );
#else
    HZIP hz = OpenZipFile(szZipPath, "");
    if ( !hz )
        return 0;

	// Set base for unziping
    SetUnzipBaseDir(hz, RHODESAPPBASE().getDBDirPath().c_str() );
#endif

    ZIPENTRY ze;
    ZRESULT res = 0;
	// Get info about the zip
	// -1 gives overall information about the zipfile
	res = GetZipItem(hz,-1,&ze);
	int numitems = ze.index;

	// Iterate through items and unzip them
	for (int zi = 0; zi<numitems; zi++)
	{ 
		// fetch individual details, e.g. the item's name.<commit_msg>fix unzip_file: use file folder as output dir<commit_after>#include "RhodesAppBase.h"
#include "common/RhoFilePath.h"
#include "common/RhoFile.h"
#include "unzip/unzip.h"

namespace rho {
namespace common{

IMPLEMENT_LOGCLASS(CRhodesAppBase,"RhodesApp");
CRhodesAppBase* CRhodesAppBase::m_pInstance = 0;

/*static*/ CRhodesAppBase* CRhodesAppBase::Create(const String& strRootPath)
{
    if ( m_pInstance != null) 
        return m_pInstance;

    m_pInstance = new CRhodesAppBase(strRootPath);
    return m_pInstance;
}

/*static*/void CRhodesAppBase::Destroy()
{
    if ( m_pInstance )
        delete m_pInstance;

    m_pInstance = 0;
}

CRhodesAppBase::CRhodesAppBase(const String& strRootPath) : CRhoThread()
{
    m_strRhoRootPath = strRootPath;

    initAppUrls();
}
	
void CRhodesAppBase::initAppUrls() 
{
#ifndef RHODES_EMULATOR
    m_strBlobsDirPath = getRhoRootPath() + "db/db-files";
	m_strDBDirPath = getRhoRootPath() + "db";

    m_strAppRootPath = getRhoRootPath() + "apps";
    m_strRhodesPath = "";
#else
    m_strBlobsDirPath = getRhoRootPath() + RHO_EMULATOR_DIR"/db/db-files";
    m_strDBDirPath = getRhoRootPath() + RHO_EMULATOR_DIR"/db";

    m_strAppRootPath = getRhoRootPath();
#endif
}

String CRhodesAppBase::getRelativeDBFilesPath(const String& strFilePath)
{
#ifndef RHODES_EMULATOR
    String strDbFileRoot = getRhoRootPath();
#else
    String strDbFileRoot = getRhoRootPath() + RHO_EMULATOR_DIR;
#endif

    if ( !String_startsWith(strFilePath, strDbFileRoot) )
        return strFilePath;

    return strFilePath.substr(strDbFileRoot.length());
}

String CRhodesAppBase::resolveDBFilesPath(const String& strFilePath)
{
#ifndef RHODES_EMULATOR
    String strDbFileRoot = getRhoRootPath();
#else
    String strDbFileRoot = getRhoRootPath() + RHO_EMULATOR_DIR;
#endif

    if ( String_startsWith(strFilePath, strDbFileRoot) )
        return strFilePath;

    return CFilePath::join(strDbFileRoot, strFilePath);
}

String CRhodesAppBase::canonicalizeRhoUrl(const String& strUrl) 
{
    if (strUrl.length() == 0 )
        return m_strHomeUrl;

    size_t pos = strUrl.find_first_of(":#");
    if((pos == String::npos) || (strUrl.at(pos) == '#'))
        return CFilePath::join(m_strHomeUrl,strUrl);

    return strUrl;
}

} //namespace common
} //namespace rho

extern "C" {

int rho_unzip_file(const char* szZipPath)
{
    rho::common::CFilePath oPath(szZipPath);
    rho::String strBaseDir = oPath.getFolderName();
#ifdef  UNICODE
    rho::StringW strZipPathW;
    rho::common::convertToStringW(szZipPath, strZipPathW);
    HZIP hz = OpenZipFile(strZipPathW.c_str(), "");
    if ( !hz )
        return 0;

	// Set base for unziping
    SetUnzipBaseDir(hz, rho::common::convertToStringW(strBaseDir).c_str());
#else
    HZIP hz = OpenZipFile(szZipPath, "");
    if ( !hz )
        return 0;

	// Set base for unziping
    SetUnzipBaseDir(hz,strBaseDir.c_str() );
#endif

    ZIPENTRY ze;
    ZRESULT res = 0;
	// Get info about the zip
	// -1 gives overall information about the zipfile
	res = GetZipItem(hz,-1,&ze);
	int numitems = ze.index;

	// Iterate through items and unzip them
	for (int zi = 0; zi<numitems; zi++)
	{ 
		// fetch individual details, e.g. the item's name.
{'fraction_non_alphanumeric': 0.12197437114380635, 'fraction_numerical': 0.0028476506881822496, 'mean_word_length': 4.229114971050455, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, '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': '399127', 'n_tokens_mistral': 2408, 'n_tokens_neox': 2320, 'n_words': 448}