<commit_before>﻿// Copyright (c) <NAME>. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#pragma once

#include <string>
#include <functional>
#include "utilities\Download.h"
#include "utilities\StringHelper.h"
#include "utilities\Thread.h"
#include "utilities\Utilities.h"
#include "configuration\ToolsList.h"
#include "configuration\FormatsList.h"
#include "configuration\Configuration.h"

namespace worker
{
    class CToolUtilities
    {
    public:
        volatile bool bDownload;
    public:
        CToolUtilities() { }
        virtual ~CToolUtilities() { }
    public:
        bool Download(config::CTool& tool, bool bExtract, bool bInstall, int nIndex, config::CConfiguration *pConfig, std::function<void(int, std::wstring)> callback = nullptr)
        {
            util::CDownload m_Download;
            std::wstring szUrl = tool.szUrl;
            std::wstring szFilePath = util::Utilities::CombinePath(app::m_App.szToolsPath, tool.szFile);
            std::wstring szFolderPath = util::Utilities::CombinePath(app::m_App.szToolsPath, util::Utilities::GetOnlyFileName(tool.szFile));

            bool bResult = m_Download.Download(szUrl, szFilePath,
                [nIndex, pConfig, callback](int nProgress, std::wstring szStatus)
            {
                if (callback != nullptr)
                {
                    for (int s = 0; s < 8; s++)
                    {
                        auto str = config::m_Strings.at(0x00400001 + s);
                        auto pos = szStatus.find(str);
                        if (pos != std::string::npos)
                        {
                            std::wstring szTranslation = pConfig->GetString(0x00400001 + s);
                            szStatus.replace(pos, str.length(), szTranslation);
                        }
                    }
                    callback(nIndex, szStatus);
                }
            });

            if (bResult == false)
            {
                return false;
            }

            if (util::StringHelper::CompareNoCase(tool.szExtract, _T("install")))
            {
                if (bInstall == true)
                {
                    util::Utilities::LaunchAndWait(szFilePath, _T(""), TRUE);
                    return true;
                }
            }

            if (util::StringHelper::CompareNoCase(tool.szExtract, _T("zip")))
            {
                if (bExtract == true)
                {
                    CComBSTR file(szFilePath.c_str());
                    CComBSTR folder(szFolderPath.c_str());

                    if (!util::Utilities::DirectoryExists(szFolderPath))
                    {
                        if (util::Utilities::MakeFullPath(szFolderPath) == false)
                        {
                            if (callback != nullptr)
                            {
                                std::wstring szStatus = pConfig->GetString(0x00410001);
                                callback(nIndex, szStatus);
                            }
                            return false;
                        }
                    }

                    if (util::Utilities::DirectoryExists(szFolderPath) == TRUE)
                    {
                        bool bUnzipResult = util::Utilities::Unzip2Folder(file, folder);
                        if (bUnzipResult == true)
                        {
                            if (callback != nullptr)
                            {
                                std::wstring szStatus = pConfig->GetString(0x00410002);
                                callback(nIndex, szStatus);
                            }
                            return true;
                        }
                        else
                        {
                            if (callback != nullptr)
                            {
                                std::wstring szStatus = pConfig->GetString(0x00410003);
                                callback(nIndex, szStatus);
                            }
                            return false;
                        }
                    }
                }
            }

            return true;
        }
        int FindTool(config::CToolsList& m_Tools, const std::wstring& szPlatform, const std::wstring& szFormatId)
        {
            int nTool = m_Tools.GetToolByFormatAndPlatform(szFormatId, szPlatform);
            if (nTool >= 0)
            {
                return nTool;
            }
            return -1;
        }
        int FindTool(config::CToolsList& m_Tools, const std::wstring& szFormatId)
        {
#if defined(_WIN32) & !defined(_WIN64)
            const std::wstring szPlatform = _T("x86");
#else
            const std::wstring szPlatform = _T("x64");
#endif
            return FindTool(m_Tools, szPlatform, szFormatId);
        }
        void SetFormatPaths(config::CFormatsList& m_Formats, config::CToolsList& m_Tools, const std::wstring& szPlatform)
        {
            int nFormats = m_Formats.Count();
            for (int i = 0; i < nFormats; i++)
            {
                config::CFormat& format = m_Formats.Get(i);
                int nTool = m_Tools.GetToolByFormatAndPlatform(format.szId, szPlatform);
                if (nTool >= 0)
                {
                    config::CTool& tool = m_Tools.Get(nTool);
                    format.szPath = tool.szPath;
                }<commit_msg>Try to find x86 tool if x64 was not found<commit_after>﻿// Copyright (c) <NAME>. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#pragma once

#include <string>
#include <functional>
#include "utilities\Download.h"
#include "utilities\StringHelper.h"
#include "utilities\Thread.h"
#include "utilities\Utilities.h"
#include "configuration\ToolsList.h"
#include "configuration\FormatsList.h"
#include "configuration\Configuration.h"

namespace worker
{
    class CToolUtilities
    {
    public:
        volatile bool bDownload;
    public:
        CToolUtilities() { }
        virtual ~CToolUtilities() { }
    public:
        bool Download(config::CTool& tool, bool bExtract, bool bInstall, int nIndex, config::CConfiguration *pConfig, std::function<void(int, std::wstring)> callback = nullptr)
        {
            util::CDownload m_Download;
            std::wstring szUrl = tool.szUrl;
            std::wstring szFilePath = util::Utilities::CombinePath(app::m_App.szToolsPath, tool.szFile);
            std::wstring szFolderPath = util::Utilities::CombinePath(app::m_App.szToolsPath, util::Utilities::GetOnlyFileName(tool.szFile));

            bool bResult = m_Download.Download(szUrl, szFilePath,
                [nIndex, pConfig, callback](int nProgress, std::wstring szStatus)
            {
                if (callback != nullptr)
                {
                    for (int s = 0; s < 8; s++)
                    {
                        auto str = config::m_Strings.at(0x00400001 + s);
                        auto pos = szStatus.find(str);
                        if (pos != std::string::npos)
                        {
                            std::wstring szTranslation = pConfig->GetString(0x00400001 + s);
                            szStatus.replace(pos, str.length(), szTranslation);
                        }
                    }
                    callback(nIndex, szStatus);
                }
            });

            if (bResult == false)
            {
                return false;
            }

            if (util::StringHelper::CompareNoCase(tool.szExtract, _T("install")))
            {
                if (bInstall == true)
                {
                    util::Utilities::LaunchAndWait(szFilePath, _T(""), TRUE);
                    return true;
                }
            }

            if (util::StringHelper::CompareNoCase(tool.szExtract, _T("zip")))
            {
                if (bExtract == true)
                {
                    CComBSTR file(szFilePath.c_str());
                    CComBSTR folder(szFolderPath.c_str());

                    if (!util::Utilities::DirectoryExists(szFolderPath))
                    {
                        if (util::Utilities::MakeFullPath(szFolderPath) == false)
                        {
                            if (callback != nullptr)
                            {
                                std::wstring szStatus = pConfig->GetString(0x00410001);
                                callback(nIndex, szStatus);
                            }
                            return false;
                        }
                    }

                    if (util::Utilities::DirectoryExists(szFolderPath) == TRUE)
                    {
                        bool bUnzipResult = util::Utilities::Unzip2Folder(file, folder);
                        if (bUnzipResult == true)
                        {
                            if (callback != nullptr)
                            {
                                std::wstring szStatus = pConfig->GetString(0x00410002);
                                callback(nIndex, szStatus);
                            }
                            return true;
                        }
                        else
                        {
                            if (callback != nullptr)
                            {
                                std::wstring szStatus = pConfig->GetString(0x00410003);
                                callback(nIndex, szStatus);
                            }
                            return false;
                        }
                    }
                }
            }

            return true;
        }
        int FindTool(config::CToolsList& m_Tools, const std::wstring& szPlatform, const std::wstring& szFormatId)
        {
            int nTool = m_Tools.GetToolByFormatAndPlatform(szFormatId, szPlatform);
            if (nTool >= 0)
            {
                return nTool;
            }
            return -1;
        }
        int FindTool(config::CToolsList& m_Tools, const std::wstring& szFormatId)
        {
            const std::wstring szPlatformX86 = L"x86";
            const std::wstring szPlatformX64 = L"x64";
#if defined(_WIN32) & !defined(_WIN64)
            return FindTool(m_Tools, szPlatformX86, szFormatId);
#else
            int nTool = FindTool(m_Tools, szPlatformX64, szFormatId);
            if (nTool == -1)
                return FindTool(m_Tools, szPlatformX86, szFormatId);
            return nTool;
#endif
        }
        void SetFormatPaths(config::CFormatsList& m_Formats, config::CToolsList& m_Tools, const std::wstring& szPlatform)
        {
            int nFormats = m_Formats.Count();
            for (int i = 0; i < nFormats; i++)
            {
                config::CFormat& format = m_Formats.Get(i);
                int nTool = m_Tools.GetToolByFormatAndPlatform(format.szId, szPlatform);
                if (nTool >= 0)
                {
                    config::CTool& tool = m_Tools.Get(nTool);
                    format.szPath = tool.szPath;
                }
{'fraction_non_alphanumeric': 0.0976677687427397, 'fraction_numerical': 0.012063265123760164, 'mean_word_length': 1.2678824721377913, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7430790', 'n_tokens_mistral': 2941, 'n_tokens_neox': 2775, 'n_words': 569}