Refactoring

This commit is contained in:
Oleg Korshul
2023-07-25 13:30:08 +03:00
parent 94fd1d94bd
commit c72e44f44e
11 changed files with 440 additions and 530 deletions

View File

@ -31,25 +31,25 @@
*/
#if defined(_WIN32) || defined (_WIN64)
#include "windows.h"
#include "windef.h"
#include <shlobj.h>
#include <Rpc.h>
#include "windows.h"
#include "windef.h"
#include <shlobj.h>
#include <Rpc.h>
#elif __linux__
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#elif MAC
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#elif _IOS
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#endif
#include <string.h>
@ -58,187 +58,187 @@
namespace NSDirectory
{
#if !defined(_WIN32) && !defined (_WIN64)
static bool is_directory_exist(char* dir)
{
struct stat st;
bool bRes = (0 == stat(dir, &st)) && S_ISDIR(st.st_mode);
return bRes;
}
static bool is_directory_exist(char* dir)
{
struct stat st;
bool bRes = (0 == stat(dir, &st)) && S_ISDIR(st.st_mode);
return bRes;
}
static bool _mkdir (const char *dir)
{
char tmp[MAX_PATH];
char *p = NULL;
size_t len;
bool res = true;
static bool _mkdir (const char *dir)
{
char tmp[MAX_PATH];
char *p = NULL;
size_t len;
bool res = true;
snprintf(tmp, sizeof(tmp),"%s",dir);
len = strlen(tmp);
if(tmp[len - 1] == '/')
tmp[len - 1] = 0;
for(p = tmp + 1; *p; p++)
if(*p == '/') {
*p = 0;
res = is_directory_exist(tmp);
if (!res)
res = (0 == mkdir(tmp, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
*p = '/';
if (!res)
break;
}
if (res)
res = (0 == mkdir(tmp, S_IRWXU));
return res;
}
snprintf(tmp, sizeof(tmp),"%s",dir);
len = strlen(tmp);
if(tmp[len - 1] == '/')
tmp[len - 1] = 0;
for(p = tmp + 1; *p; p++)
if(*p == '/') {
*p = 0;
res = is_directory_exist(tmp);
if (!res)
res = (0 == mkdir(tmp, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
*p = '/';
if (!res)
break;
}
if (res)
res = (0 == mkdir(tmp, S_IRWXU));
return res;
}
#endif
void GetFiles2(std::wstring strDirectory, std::vector<std::wstring>& oArray, bool bIsRecursion)
{
void GetFiles2(std::wstring strDirectory, std::vector<std::wstring>& oArray, bool bIsRecursion)
{
#if defined(_WIN32) || defined (_WIN64)
WIN32_FIND_DATAW oFD;
WIN32_FIND_DATAW oFD;
std::wstring sSpec = strDirectory + L"\\*.*";
HANDLE hRes = FindFirstFileW( sSpec.c_str(), &oFD );
if( INVALID_HANDLE_VALUE == hRes )
return;
do
{
sSpec = oFD.cFileName;
if (sSpec != L"." && sSpec != L"..")
{
sSpec = strDirectory + L"\\" + sSpec;
if( !( oFD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) )
{
oArray.push_back(sSpec);
}
else if (bIsRecursion)
{
GetFiles2(sSpec, oArray, bIsRecursion);
}
}
} while( FindNextFileW( hRes, &oFD ) );
FindClose( hRes );
std::wstring sSpec = strDirectory + L"\\*.*";
HANDLE hRes = FindFirstFileW( sSpec.c_str(), &oFD );
if( INVALID_HANDLE_VALUE == hRes )
return;
do
{
sSpec = oFD.cFileName;
if (sSpec != L"." && sSpec != L"..")
{
sSpec = strDirectory + L"\\" + sSpec;
if( !( oFD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) )
{
oArray.push_back(sSpec);
}
else if (bIsRecursion)
{
GetFiles2(sSpec, oArray, bIsRecursion);
}
}
} while( FindNextFileW( hRes, &oFD ) );
FindClose( hRes );
#endif
#ifdef __linux__
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strDirectory.c_str(), strDirectory.length(), pUtf8, lLen, false);
DIR *dp;
struct dirent *dirp;
if((dp = opendir((char*)pUtf8)) != NULL)
{
while ((dirp = readdir(dp)) != NULL)
{
int nType = 0;
if(DT_REG == dirp->d_type)
nType = 2;
else if (DT_DIR == dirp->d_type)
nType = 1;
else if (DT_UNKNOWN == dirp->d_type)
{
// XFS problem
struct stat buff;
std::string sTmp = std::string((char*)pUtf8) + "/" + std::string(dirp->d_name);
stat(sTmp.c_str(), &buff);
if (S_ISREG(buff.st_mode))
nType = 2;
else if (S_ISDIR(buff.st_mode))
nType = 1;
}
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strDirectory.c_str(), strDirectory.length(), pUtf8, lLen, false);
DIR *dp;
struct dirent *dirp;
if((dp = opendir((char*)pUtf8)) != NULL)
{
while ((dirp = readdir(dp)) != NULL)
{
int nType = 0;
if(DT_REG == dirp->d_type)
nType = 2;
else if (DT_DIR == dirp->d_type)
nType = 1;
else if (DT_UNKNOWN == dirp->d_type)
{
// XFS problem
struct stat buff;
std::string sTmp = std::string((char*)pUtf8) + "/" + std::string(dirp->d_name);
stat(sTmp.c_str(), &buff);
if (S_ISREG(buff.st_mode))
nType = 2;
else if (S_ISDIR(buff.st_mode))
nType = 1;
}
if (2 == nType)
{
std::wstring sName = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)dirp->d_name, strlen(dirp->d_name));
oArray.push_back(strDirectory + L"/" + sName);
}
if (2 == nType)
{
std::wstring sName = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)dirp->d_name, strlen(dirp->d_name));
oArray.push_back(strDirectory + L"/" + sName);
}
if (bIsRecursion && (1 == nType))
{
if(dirp->d_name[0] != '.')
{
std::wstring sName = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)dirp->d_name, strlen(dirp->d_name));
GetFiles2(strDirectory + L"/" + sName, oArray, bIsRecursion);
}
}
}
closedir(dp);
}
delete [] pUtf8;
if (bIsRecursion && (1 == nType))
{
if(dirp->d_name[0] != '.')
{
std::wstring sName = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)dirp->d_name, strlen(dirp->d_name));
GetFiles2(strDirectory + L"/" + sName, oArray, bIsRecursion);
}
}
}
closedir(dp);
}
delete [] pUtf8;
#endif
#if defined(MAC) || defined (_IOS)
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strDirectory.c_str(), strDirectory.length(), pUtf8, lLen, false);
DIR *dp;
struct dirent *dirp;
if((dp = opendir((char*)pUtf8)) != NULL)
{
while ((dirp = readdir(dp)) != NULL)
{
if(DT_REG == dirp->d_type)
{
std::wstring sName = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)dirp->d_name, strlen(dirp->d_name));
oArray.push_back(strDirectory + L"/" + sName);
}
if (bIsRecursion && DT_DIR == dirp->d_type)
{
if(dirp->d_name[0] != '.')
{
std::wstring sName = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)dirp->d_name, strlen(dirp->d_name));
GetFiles2(strDirectory + L"/" + sName, oArray, bIsRecursion);
}
}
}
closedir(dp);
}
delete [] pUtf8;
return;
#endif
}
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strDirectory.c_str(), strDirectory.length(), pUtf8, lLen, false);
DIR *dp;
struct dirent *dirp;
if((dp = opendir((char*)pUtf8)) != NULL)
{
while ((dirp = readdir(dp)) != NULL)
{
if(DT_REG == dirp->d_type)
{
std::wstring sName = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)dirp->d_name, strlen(dirp->d_name));
oArray.push_back(strDirectory + L"/" + sName);
}
std::vector<std::wstring> GetFiles(std::wstring strDirectory, bool bIsRecursion)
if (bIsRecursion && DT_DIR == dirp->d_type)
{
if(dirp->d_name[0] != '.')
{
std::wstring sName = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)dirp->d_name, strlen(dirp->d_name));
GetFiles2(strDirectory + L"/" + sName, oArray, bIsRecursion);
}
}
}
closedir(dp);
}
delete [] pUtf8;
return;
#endif
}
std::vector<std::wstring> GetFiles(std::wstring strDirectory, bool bIsRecursion)
{
std::vector<std::wstring> oArray;
if (!strDirectory.empty())
{
{
GetFiles2(strDirectory, oArray, bIsRecursion);
}
return oArray;
}
std::vector<std::wstring> GetDirectories(std::wstring strDirectory)
std::vector<std::wstring> GetDirectories(std::wstring strDirectory)
{
std::vector<std::wstring> oArray;
if (strDirectory.empty()) return oArray;
#if defined(_WIN32) || defined (_WIN64)
WIN32_FIND_DATAW oFD;
WIN32_FIND_DATAW oFD;
std::wstring sSpec = strDirectory + L"\\*";
HANDLE hRes = FindFirstFileW( sSpec.c_str(), &oFD );
if( INVALID_HANDLE_VALUE == hRes )
return oArray;
do
do
{
sSpec = oFD.cFileName;
if (sSpec != L"." && sSpec != L"..")
{
sSpec = strDirectory + L"\\" + sSpec;
if( oFD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
if( oFD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
oArray.push_back(sSpec);
}
}
} while( FindNextFileW( hRes, &oFD ) );
} while( FindNextFileW( hRes, &oFD ) );
FindClose( hRes );
#elif __linux__
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strDirectory.c_str(), strDirectory.length(), pUtf8, lLen, false);
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strDirectory.c_str(), strDirectory.length(), pUtf8, lLen, false);
DIR *dp;
struct dirent *dirp;
if((dp = opendir((char*)pUtf8)) != NULL)
@ -270,54 +270,54 @@ namespace NSDirectory
}
delete [] pUtf8;
#elif MAC
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strDirectory.c_str(), strDirectory.length(), pUtf8, lLen, false);
DIR *dp;
struct dirent *dirp;
if((dp = opendir((char*)pUtf8)) != NULL)
{
while ((dirp = readdir(dp)) != NULL)
{
if(DT_DIR == dirp->d_type)
{
if(dirp->d_name[0] != '.')
{
std::wstring sName = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)dirp->d_name, strlen(dirp->d_name));
oArray.push_back(strDirectory + L"/" + sName);
}
}
}
closedir(dp);
}
delete [] pUtf8;
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strDirectory.c_str(), strDirectory.length(), pUtf8, lLen, false);
DIR *dp;
struct dirent *dirp;
if((dp = opendir((char*)pUtf8)) != NULL)
{
while ((dirp = readdir(dp)) != NULL)
{
if(DT_DIR == dirp->d_type)
{
if(dirp->d_name[0] != '.')
{
std::wstring sName = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)dirp->d_name, strlen(dirp->d_name));
oArray.push_back(strDirectory + L"/" + sName);
}
}
}
closedir(dp);
}
delete [] pUtf8;
#endif
return oArray;
}
bool Exists(const std::wstring& strDirectory)
bool Exists(const std::wstring& strDirectory)
{
#if defined(_WIN32) || defined (_WIN64)
DWORD dwAttrib = ::GetFileAttributesW(strDirectory.c_str());
return (dwAttrib != INVALID_FILE_ATTRIBUTES && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
#elif __linux__
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strDirectory.c_str(), strDirectory.length(), pUtf8, lLen, false);
bool bRes = is_directory_exist((char*)pUtf8);
delete [] pUtf8;
return bRes;
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strDirectory.c_str(), strDirectory.length(), pUtf8, lLen, false);
bool bRes = is_directory_exist((char*)pUtf8);
delete [] pUtf8;
return bRes;
#elif MAC
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strDirectory.c_str(), strDirectory.length(), pUtf8, lLen, false);
struct stat st;
bool bRes = is_directory_exist((char*)pUtf8);
delete [] pUtf8;
return bRes;
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strDirectory.c_str(), strDirectory.length(), pUtf8, lLen, false);
struct stat st;
bool bRes = is_directory_exist((char*)pUtf8);
delete [] pUtf8;
return bRes;
#endif
return false;
return false;
}
bool CreateDirectory(const std::wstring& strDirectory)
bool CreateDirectory(const std::wstring& strDirectory)
{
if (Exists(strDirectory) == true) return true;
@ -326,7 +326,7 @@ namespace NSDirectory
#else
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strDirectory.c_str(), strDirectory.length(), pUtf8, lLen, false);
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strDirectory.c_str(), strDirectory.length(), pUtf8, lLen, false);
struct stat st;
int nRes = 0;
if (stat((char*)pUtf8, &st) == -1) {
@ -336,7 +336,7 @@ namespace NSDirectory
return 0 == nRes;
#endif
}
bool CreateDirectories(const std::wstring& strDirectory)
bool CreateDirectories(const std::wstring& strDirectory)
{
if (Exists(strDirectory) == true) return true;
@ -367,119 +367,119 @@ namespace NSDirectory
#endif
return false;
}
bool CopyDirectory(const std::wstring& strSrc, const std::wstring& strDst, bool bIsRecursion)
{
if (!NSDirectory::Exists(strDst))
NSDirectory::CreateDirectory(strDst);
bool CopyDirectory(const std::wstring& strSrc, const std::wstring& strDst, bool bIsRecursion)
{
if (!NSDirectory::Exists(strDst))
NSDirectory::CreateDirectory(strDst);
#ifdef WIN32
WIN32_FIND_DATAW oFD;
WIN32_FIND_DATAW oFD;
std::wstring sSpec = strSrc + L"\\*.*";
HANDLE hRes = FindFirstFileW( sSpec.c_str(), &oFD );
if( INVALID_HANDLE_VALUE == hRes )
return false;
do
{
sSpec = oFD.cFileName;
if (sSpec != L"." && sSpec != L"..")
{
if( !( oFD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) )
{
NSFile::CFileBinary::Copy(strSrc + L"/" + sSpec, strDst + L"/" + sSpec);
}
else if (bIsRecursion)
{
CopyDirectory(strSrc + L"/" + sSpec, strDst + L"/" + sSpec, bIsRecursion);
}
}
} while( FindNextFileW( hRes, &oFD ) );
FindClose( hRes );
return true;
std::wstring sSpec = strSrc + L"\\*.*";
HANDLE hRes = FindFirstFileW( sSpec.c_str(), &oFD );
if( INVALID_HANDLE_VALUE == hRes )
return false;
do
{
sSpec = oFD.cFileName;
if (sSpec != L"." && sSpec != L"..")
{
if( !( oFD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) )
{
NSFile::CFileBinary::Copy(strSrc + L"/" + sSpec, strDst + L"/" + sSpec);
}
else if (bIsRecursion)
{
CopyDirectory(strSrc + L"/" + sSpec, strDst + L"/" + sSpec, bIsRecursion);
}
}
} while( FindNextFileW( hRes, &oFD ) );
FindClose( hRes );
return true;
#endif
#ifdef __linux__
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strSrc.c_str(), strSrc.length(), pUtf8, lLen, false);
DIR *dp;
struct dirent *dirp;
if((dp = opendir((char*)pUtf8)) != NULL)
{
while ((dirp = readdir(dp)) != NULL)
{
int nType = 0;
if(DT_REG == dirp->d_type)
nType = 2;
else if (DT_DIR == dirp->d_type)
nType = 1;
else if (DT_UNKNOWN == dirp->d_type)
{
// XFS problem
struct stat buff;
std::string sTmp = std::string((char*)pUtf8) + "/" + std::string(dirp->d_name);
stat(sTmp.c_str(), &buff);
if (S_ISREG(buff.st_mode))
nType = 2;
else if (S_ISDIR(buff.st_mode))
nType = 1;
}
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strSrc.c_str(), strSrc.length(), pUtf8, lLen, false);
DIR *dp;
struct dirent *dirp;
if((dp = opendir((char*)pUtf8)) != NULL)
{
while ((dirp = readdir(dp)) != NULL)
{
int nType = 0;
if(DT_REG == dirp->d_type)
nType = 2;
else if (DT_DIR == dirp->d_type)
nType = 1;
else if (DT_UNKNOWN == dirp->d_type)
{
// XFS problem
struct stat buff;
std::string sTmp = std::string((char*)pUtf8) + "/" + std::string(dirp->d_name);
stat(sTmp.c_str(), &buff);
if (S_ISREG(buff.st_mode))
nType = 2;
else if (S_ISDIR(buff.st_mode))
nType = 1;
}
if (2 == nType)
{
std::wstring sName = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)dirp->d_name, strlen(dirp->d_name));
NSFile::CFileBinary::Copy(strSrc + L"/" + sName, strDst + L"/" + sName);
}
if (2 == nType)
{
std::wstring sName = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)dirp->d_name, strlen(dirp->d_name));
NSFile::CFileBinary::Copy(strSrc + L"/" + sName, strDst + L"/" + sName);
}
if (bIsRecursion && (1 == nType))
{
if(dirp->d_name[0] != '.')
{
std::wstring sName = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)dirp->d_name, strlen(dirp->d_name));
CopyDirectory(strSrc + L"/" + sName, strDst + L"/" + sName, bIsRecursion);
}
}
}
closedir(dp);
}
delete [] pUtf8;
return true;
if (bIsRecursion && (1 == nType))
{
if(dirp->d_name[0] != '.')
{
std::wstring sName = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)dirp->d_name, strlen(dirp->d_name));
CopyDirectory(strSrc + L"/" + sName, strDst + L"/" + sName, bIsRecursion);
}
}
}
closedir(dp);
}
delete [] pUtf8;
return true;
#endif
#if defined(MAC) || defined (_IOS)
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strSrc.c_str(), strSrc.length(), pUtf8, lLen, false);
DIR *dp;
struct dirent *dirp;
if((dp = opendir((char*)pUtf8)) != NULL)
{
while ((dirp = readdir(dp)) != NULL)
{
if(DT_REG == dirp->d_type)
{
std::wstring sName = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)dirp->d_name, strlen(dirp->d_name));
NSFile::CFileBinary::Copy(strSrc + L"/" + sName, strDst + L"/" + sName);
}
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strSrc.c_str(), strSrc.length(), pUtf8, lLen, false);
DIR *dp;
struct dirent *dirp;
if((dp = opendir((char*)pUtf8)) != NULL)
{
while ((dirp = readdir(dp)) != NULL)
{
if(DT_REG == dirp->d_type)
{
std::wstring sName = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)dirp->d_name, strlen(dirp->d_name));
NSFile::CFileBinary::Copy(strSrc + L"/" + sName, strDst + L"/" + sName);
}
if (bIsRecursion && DT_DIR == dirp->d_type)
{
if(dirp->d_name[0] != '.')
{
std::wstring sName = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)dirp->d_name, strlen(dirp->d_name));
CopyDirectory(strSrc + L"/" + sName, strDst + L"/" + sName, bIsRecursion);
}
}
}
closedir(dp);
}
delete [] pUtf8;
return true;
if (bIsRecursion && DT_DIR == dirp->d_type)
{
if(dirp->d_name[0] != '.')
{
std::wstring sName = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)dirp->d_name, strlen(dirp->d_name));
CopyDirectory(strSrc + L"/" + sName, strDst + L"/" + sName, bIsRecursion);
}
}
}
closedir(dp);
}
delete [] pUtf8;
return true;
#endif
return false;
}
void DeleteDirectory(const std::wstring& strDirectory, bool deleteRoot)
return false;
}
void DeleteDirectory(const std::wstring& strDirectory, bool deleteRoot)
{
if (strDirectory.empty()) return;
@ -498,22 +498,22 @@ namespace NSDirectory
#elif __linux__
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strDirectory.c_str(), strDirectory.length(), pUtf8, lLen, false);
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strDirectory.c_str(), strDirectory.length(), pUtf8, lLen, false);
rmdir((char*)pUtf8);
delete [] pUtf8;
if (deleteRoot = false)CreateDirectory(strDirectory);
#elif MAC
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strDirectory.c_str(), strDirectory.length(), pUtf8, lLen, false);
rmdir((char*)pUtf8);
delete [] pUtf8;
if (deleteRoot = false)CreateDirectory(strDirectory);
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strDirectory.c_str(), strDirectory.length(), pUtf8, lLen, false);
rmdir((char*)pUtf8);
delete [] pUtf8;
if (deleteRoot = false)CreateDirectory(strDirectory);
#endif
}
std::wstring GetFolderPath(const std::wstring& wsFolderPath)
std::wstring GetFolderPath(const std::wstring& wsFolderPath)
{
int n1 = (int)wsFolderPath.rfind('\\');
if (n1 < 0)
@ -527,64 +527,64 @@ namespace NSDirectory
}
return wsFolderPath.substr(0, n1);
}
std::wstring CreateTempFileWithUniqueName (const std::wstring & strFolderPathRoot, std::wstring Prefix)
{
return NSFile::CFileBinary::CreateTempFileWithUniqueName(strFolderPathRoot, Prefix);
}
std::wstring CreateDirectoryWithUniqueName (const std::wstring & strFolderPathRoot)
{
std::wstring CreateTempFileWithUniqueName (const std::wstring & strFolderPathRoot, std::wstring Prefix)
{
return NSFile::CFileBinary::CreateTempFileWithUniqueName(strFolderPathRoot, Prefix);
}
std::wstring CreateDirectoryWithUniqueName (const std::wstring & strFolderPathRoot)
{
#if defined(_WIN32) || defined (_WIN64)
UUID uuid;
RPC_WSTR str_uuid;
UuidCreate (&uuid);
UuidToStringW (&uuid, &str_uuid);
std::wstring pcTemplate = strFolderPathRoot + FILE_SEPARATOR_STR;
pcTemplate += (wchar_t *) str_uuid;
RpcStringFreeW (&str_uuid);
UUID uuid;
RPC_WSTR str_uuid;
UuidCreate (&uuid);
UuidToStringW (&uuid, &str_uuid);
std::wstring pcTemplate = strFolderPathRoot + FILE_SEPARATOR_STR;
pcTemplate += (wchar_t *) str_uuid;
RpcStringFreeW (&str_uuid);
int attemps = 10;
while (!CreateDirectory(pcTemplate))
{
UuidCreate (&uuid);
UuidToStringW (&uuid, &str_uuid);
pcTemplate = strFolderPathRoot + FILE_SEPARATOR_STR;
pcTemplate += (wchar_t *) str_uuid;
RpcStringFreeW (&str_uuid);
attemps--;
int attemps = 10;
while (!CreateDirectory(pcTemplate))
{
UuidCreate (&uuid);
UuidToStringW (&uuid, &str_uuid);
pcTemplate = strFolderPathRoot + FILE_SEPARATOR_STR;
pcTemplate += (wchar_t *) str_uuid;
RpcStringFreeW (&str_uuid);
attemps--;
if (0 == attemps)
{
pcTemplate = L"";
break;
}
}
return pcTemplate;
if (0 == attemps)
{
pcTemplate = L"";
break;
}
}
return pcTemplate;
#else
std::string pcTemplate = U_TO_UTF8(strFolderPathRoot) + "/ascXXXXXX";
char *pcRes = mkdtemp(const_cast <char *> (pcTemplate.c_str()));
if (NULL == pcRes)
return L"";
std::string pcTemplate = U_TO_UTF8(strFolderPathRoot) + "/ascXXXXXX";
char *pcRes = mkdtemp(const_cast <char *> (pcTemplate.c_str()));
if (NULL == pcRes)
return L"";
std::string sRes = pcRes;
return NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)sRes.c_str(), (LONG)sRes.length());
std::string sRes = pcRes;
return NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)sRes.c_str(), (LONG)sRes.length());
#endif
}
std::wstring GetTempPath()
{
return NSFile::CFileBinary::GetTempPath();
}
}
std::wstring GetTempPath()
{
return NSFile::CFileBinary::GetTempPath();
}
int GetFilesCount(const std::wstring& path, const bool& recursive)
{
std::vector<std::wstring> arrFiles = NSDirectory::GetFiles(path, recursive);
int GetFilesCount(const std::wstring& path, const bool& recursive)
{
std::vector<std::wstring> arrFiles = NSDirectory::GetFiles(path, recursive);
#if defined(_WIN32) || defined (_WIN64)
return (int)arrFiles.size();
return (int)arrFiles.size();
#endif
return (int)arrFiles.size() + 1;
// ???
}
bool PathIsDirectory(const std::wstring& pathName)
{
return Exists(pathName);
}
return (int)arrFiles.size() + 1;
// ???
}
bool PathIsDirectory(const std::wstring& pathName)
{
return Exists(pathName);
}
}

View File

@ -46,41 +46,45 @@
#include "File.h"
#ifndef FILE_SEPARATOR
#if defined(_WIN32) || defined(_WIN64)
#define FILE_SEPARATOR
#define FILE_SEPARATOR_CHAR '\\'
#define FILE_SEPARATOR_STR L"\\"
#define FILE_SEPARATOR_STRA "\\"
#else
#define FILE_SEPARATOR
#define FILE_SEPARATOR_CHAR '/'
#define FILE_SEPARATOR_STR L"/"
#define FILE_SEPARATOR_STRA "/"
#endif
#if defined(_WIN32) || defined(_WIN64)
#define FILE_SEPARATOR
#define FILE_SEPARATOR_CHAR '\\'
#define FILE_SEPARATOR_STR L"\\"
#define FILE_SEPARATOR_STRA "\\"
#else
#define FILE_SEPARATOR
#define FILE_SEPARATOR_CHAR '/'
#define FILE_SEPARATOR_STR L"/"
#define FILE_SEPARATOR_STRA "/"
#endif
#endif
#include "../../Common/kernel_config.h"
namespace NSDirectory
{
KERNEL_DECL std::vector<std::wstring> GetFiles(std::wstring strDirectory, bool bIsRecursion = false);
KERNEL_DECL void GetFiles2(std::wstring strDirectory, std::vector<std::wstring>& oArray, bool bIsRecursion = false);
KERNEL_DECL std::vector<std::wstring> GetDirectories(std::wstring strDirectory);
KERNEL_DECL bool Exists(const std::wstring& strDirectory);
KERNEL_DECL bool CreateDirectory(const std::wstring& strDirectory);
KERNEL_DECL bool CreateDirectories(const std::wstring& strDirectory);
KERNEL_DECL bool CopyDirectory(const std::wstring& strSrc, const std::wstring& strDst, bool bIsRecursion = true);
KERNEL_DECL void DeleteDirectory(const std::wstring& strDirectory, bool deleteRoot = true);
KERNEL_DECL std::wstring GetFolderPath(const std::wstring& wsFolderPath);
KERNEL_DECL std::wstring CreateTempFileWithUniqueName (const std::wstring & strFolderPathRoot, std::wstring Prefix);
KERNEL_DECL std::wstring CreateDirectoryWithUniqueName (const std::wstring & strFolderPathRoot);
KERNEL_DECL std::wstring GetTempPath();
KERNEL_DECL int GetFilesCount(const std::wstring& path, const bool& recursive);
KERNEL_DECL bool PathIsDirectory(const std::wstring& pathName);
#ifdef _IOS
KERNEL_DECL void GetFiles2_ios(std::wstring strDirectory, std::vector<std::wstring>& oArray, bool bIsRecursion);
namespace IOS
{
KERNEL_DECL void GetFiles2(std::wstring strDirectory, std::vector<std::wstring>& oArray, bool bIsRecursion = false);
}
#endif
KERNEL_DECL void GetFiles2(std::wstring strDirectory, std::vector<std::wstring>& oArray, bool bIsRecursion = false);
KERNEL_DECL std::vector<std::wstring> GetFiles(std::wstring strDirectory, bool bIsRecursion = false);
KERNEL_DECL std::vector<std::wstring> GetDirectories(std::wstring strDirectory);
KERNEL_DECL bool Exists(const std::wstring& strDirectory);
KERNEL_DECL bool CreateDirectory(const std::wstring& strDirectory);
KERNEL_DECL bool CreateDirectories(const std::wstring& strDirectory);
KERNEL_DECL bool CopyDirectory(const std::wstring& strSrc, const std::wstring& strDst, bool bIsRecursion = true);
KERNEL_DECL void DeleteDirectory(const std::wstring& strDirectory, bool deleteRoot = true);
KERNEL_DECL std::wstring GetFolderPath(const std::wstring& wsFolderPath);
KERNEL_DECL std::wstring CreateTempFileWithUniqueName (const std::wstring & strFolderPathRoot, std::wstring Prefix);
KERNEL_DECL std::wstring CreateDirectoryWithUniqueName (const std::wstring & strFolderPathRoot);
KERNEL_DECL std::wstring GetTempPath();
KERNEL_DECL int GetFilesCount(const std::wstring& path, const bool& recursive);
KERNEL_DECL bool PathIsDirectory(const std::wstring& pathName);
}
#endif //_BUILD_DIRECTORY_CROSSPLATFORM_H_

View File

@ -201,6 +201,13 @@ namespace NSFile
static bool Encode(BYTE* pDataSrc, int nLenSrc, char*& pDataDst, int& nLenDst, DWORD dwFlags = NSBase64::B64_BASE64_FLAG_NONE);
static bool Decode(const char* pDataSrc, int nLenSrc, BYTE*& pDataDst, int& nLenDst);
};
#ifdef _IOS
namespace IOS
{
KERNEL_DECL std::string GetFileSystemRepresentation(const std::wstring& path);
}
#endif
}
namespace NSFile

View File

@ -31,21 +31,49 @@
*/
#ifdef __OBJC__
#import <CoreFoundation/CoreFoundation.h>
#import <CoreFoundation/CoreFoundation.h>
#else
#include <objc/objc.h>
#include <objc/objc.h>
#endif
#include "./File.h"
#include <vector>
#import <Foundation/Foundation.h>
const char* fileSystemRepresentation(const std::wstring& sFileName)
namespace NSFile
{
if (sFileName.empty())
return NULL;
NSString *path = [[NSString alloc] initWithBytes:(char*)sFileName.data()
length:sFileName.size()* sizeof(wchar_t)
encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF32LE)];
namespace IOS
{
std::string GetFileSystemRepresentation(const std::wstring& path)
{
if (path.empty())
return "";
return (const char*)[path fileSystemRepresentation];
NSString *path = [[NSString alloc] initWithBytes:(char*)path.data() length:path.size() * sizeof(wchar_t)
encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF32LE)];
return std::string((const char*)[path fileSystemRepresentation]);
}
}
}
namespace NSDirectory
{
namespace IOS
{
void GetFiles2(std::wstring strDirectory, std::vector<std::wstring>& oArray, bool bIsRecursion)
{
NSStringEncoding pEncode = CFStringConvertEncodingToNSStringEncoding ( kCFStringEncodingUTF32LE );
NSString* directoryPath = [[NSString alloc] initWithBytes : (char*)strDirectory.data()
length : strDirectory.size() * sizeof(wchar_t) encoding : pEncode];
NSArray* directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:NULL];
for (int count = 0; count < (int)[directoryContent count]; count++)
{
NSData* pSData = [[directoryContent objectAtIndex:count] dataUsingEncoding : pEncode];
oArray.emplace_back(std::wstring((wchar_t*)[pSData bytes], [pSData length] / sizeof (wchar_t)));
}
}
}
}

View File

@ -1794,8 +1794,10 @@ std::vector<std::wstring> CApplicationFonts::GetSetupFontFiles()
#endif
#ifdef _IOS
// own realization (objective c code)
return GetSetupFontFiles_ios();
std::vector<std::wstring> _array = NSDirectory::GetFiles2(L"/System/Library/Fonts", oArray, true);
if (_array.empty())
NSDirectory::GetFiles2(L"/Library/Fonts", _array, true);
return _array;
#endif
#ifdef __ANDROID__

View File

@ -1,75 +0,0 @@
/*
* (c) Copyright Ascensio System SIA 2010-2023
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at 20A-6 Ernesta Birznieka-Upish
* street, Riga, Latvia, EU, LV-1050.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
#include "ApplicationFonts.h"
#include "../common/Directory.h"
#ifdef BOOL
#undef BOOL
#endif
#import <UIKit/UIKit.h>
std::vector<std::wstring> CApplicationFonts::GetSetupFontFiles_ios()
{
std::vector<std::wstring> oArray;
NSDirectory::GetFiles2(L"/System/Library/Fonts", oArray, true);
if (oArray.size() == 0)
{
NSDirectory::GetFiles2(L"/Library/Fonts", oArray, true);
}
return oArray;
}
namespace NSDirectory
{
void GetFiles2_ios(std::wstring strDirectory, std::vector<std::wstring>& oArray, bool bIsRecursion)
{
NSString* directoryPath = [ [ NSString alloc ]
initWithBytes : (char*)strDirectory.data()
length : strDirectory.size() * sizeof(wchar_t)
encoding : CFStringConvertEncodingToNSStringEncoding ( kCFStringEncodingUTF32LE ) ];
int count;
NSArray* directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:NULL];
for (count = 0; count < (int)[directoryContent count]; count++)
{
NSStringEncoding pEncode = CFStringConvertEncodingToNSStringEncoding ( kCFStringEncodingUTF32LE );
NSData* pSData = [[ directoryContent objectAtIndex:count] dataUsingEncoding : pEncode ];
std::wstring sTmp( (wchar_t*) [ pSData bytes ], [ pSData length] / sizeof ( wchar_t ) );
oArray.push_back(sTmp);
}
}
}

View File

@ -39,10 +39,6 @@ enable_support_shaper {
# -------------------------------------------------
core_ios {
OBJECTIVE_SOURCES += \
$$FONT_ENGINE_PATH/ApplicationFonts_ios.mm \
LIBS += -framework Foundation
}

View File

@ -64,6 +64,3 @@ HEADERS += \
$$PWD/src/zlib-1.2.11/contrib/minizip/zip.h \
$$PWD/src/zlib-1.2.11/contrib/minizip/ioapibuf.h
core_ios {
OBJECTIVE_SOURCES += $$PWD/src/ZipUtilsCP_ios.mm
}

View File

@ -123,19 +123,23 @@ namespace ZLibZipUtils
return m_unzFile;
}
#ifndef _IOS
zipFile zipOpenHelp(const wchar_t* filename)
{
#if defined(_WIN32) || defined (_WIN64)
zlib_filefunc64_def ffunc;
fill_win32_filefunc64W(&ffunc);
zipFile zf = zipOpen2_64(filename, APPEND_STATUS_CREATE, NULL, &ffunc);
#else
#ifdef _IOS
std::string filePath = NSFile::IOS::GetFileSystemRepresentation(filename);
zipFile zf = filePath.empty() ? NULL : zipOpen(filePath.c_str(), APPEND_STATUS_CREATE);
#else
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(filename, wcslen(filename), pUtf8, lLen, false);
zipFile zf = zipOpen( (char*)pUtf8, APPEND_STATUS_CREATE );
delete [] pUtf8;
#endif
#endif
return zf;
}
@ -145,16 +149,21 @@ namespace ZLibZipUtils
zlib_filefunc64_def ffunc;
fill_win32_filefunc64W(&ffunc);
unzFile uf = unzOpen2_64(filename, &ffunc);
#else
#ifdef _IOS
std::string filePath = NSFile::IOS::GetFileSystemRepresentation(filename);
unzFile zf = filePath.empty() ? NULL : unzOpen(filePath.c_str());
#else
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(filename, wcslen(filename), pUtf8, lLen, false);
unzFile uf = unzOpen( (char*)pUtf8 );
delete [] pUtf8;
#endif
#endif
return uf;
}
#endif
static std::wstring ascii_to_unicode(const char *src)
{
// TODO: check codepage of system (for "bad" archive)

View File

@ -57,7 +57,6 @@ using namespace std;
namespace ZLibZipUtils
{
zipFile zipOpenHelp(const wchar_t* filename);
unzFile unzOpenHelp(const wchar_t* filename);

View File

@ -1,57 +0,0 @@
/*
* (c) Copyright Ascensio System SIA 2010-2023
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at 20A-6 Ernesta Birznieka-Upish
* street, Riga, Latvia, EU, LV-1050.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
#include "ZipUtilsCP.h"
#import <Foundation/Foundation.h>
namespace ZLibZipUtils
{
zipFile zipOpenHelp(const wchar_t* filename)
{
NSString *path =[[NSString alloc] initWithBytes:filename
length:wcslen(filename)*sizeof(*filename)
encoding:NSUTF32LittleEndianStringEncoding];
if (path && path.length > 0) {
return zipOpen( (const char*)[path fileSystemRepresentation], APPEND_STATUS_CREATE );
}
return NULL;
}
unzFile unzOpenHelp(const wchar_t* filename)
{
NSString *path =[[NSString alloc] initWithBytes:filename
length:wcslen(filename)*sizeof(*filename)
encoding:NSUTF32LittleEndianStringEncoding];
if (path && path.length > 0) {
return unzOpen ((const char*)[path fileSystemRepresentation]);
}
return NULL;
}
}