mirror of
https://github.com/ONLYOFFICE/core.git
synced 2026-02-10 18:05:41 +08:00
5
This commit is contained in:
@ -35,6 +35,8 @@ core_windows {
|
||||
|
||||
LIBS += -lAdvapi32
|
||||
LIBS += -lurlmon
|
||||
LIBS += -lRpcrt4
|
||||
LIBS += -lShell32
|
||||
}
|
||||
core_linux {
|
||||
SOURCES += \
|
||||
@ -72,3 +74,28 @@ HEADERS += \
|
||||
|
||||
SOURCES += \
|
||||
./../DesktopEditor/graphics/Timer.cpp
|
||||
|
||||
# PATH
|
||||
DEFINES += PATH_USE_DYNAMIC_LIBRARY
|
||||
HEADERS += ./../DesktopEditor/common/Path.h
|
||||
SOURCES += ./../DesktopEditor/common/Path.cpp
|
||||
|
||||
# STRINGS
|
||||
DEFINES += STRINGBUILDER_USE_DYNAMIC_LIBRARY
|
||||
HEADERS += ./../DesktopEditor/common/StringBuilder.h
|
||||
SOURCES += ./../DesktopEditor/common/StringBuilder.cpp
|
||||
|
||||
# BASE64
|
||||
DEFINES += BASE64_USE_DYNAMIC_LIBRARY
|
||||
HEADERS += ./../DesktopEditor/common/Base64.h
|
||||
SOURCES += ./../DesktopEditor/common/Base64.cpp
|
||||
|
||||
# FILE
|
||||
DEFINES += FILE_USE_DYNAMIC_LIBRARY
|
||||
HEADERS += ./../DesktopEditor/common/File.h
|
||||
SOURCES += ./../DesktopEditor/common/File.cpp
|
||||
|
||||
# DIRECTORY
|
||||
DEFINES += DIRECTORY_USE_DYNAMIC_LIBRARY
|
||||
HEADERS += ./../DesktopEditor/common/Directory.h
|
||||
SOURCES += ./../DesktopEditor/common/Directory.cpp
|
||||
|
||||
242
DesktopEditor/common/Base64.cpp
Normal file
242
DesktopEditor/common/Base64.cpp
Normal file
@ -0,0 +1,242 @@
|
||||
/*
|
||||
* (c) Copyright Ascensio System SIA 2010-2018
|
||||
*
|
||||
* 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 Lubanas st. 125a-25, Riga, Latvia,
|
||||
* EU, LV-1021.
|
||||
*
|
||||
* 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 "Base64.h"
|
||||
|
||||
namespace NSBase64
|
||||
{
|
||||
int Base64EncodeGetRequiredLength(int nSrcLen, DWORD dwFlags)
|
||||
{
|
||||
T_LONG64 nSrcLen4 = static_cast<T_LONG64>(nSrcLen)*4;
|
||||
if (nSrcLen4 > INT_MAX)
|
||||
return -1;
|
||||
|
||||
int nRet = static_cast<int>(nSrcLen4/3);
|
||||
|
||||
if ((dwFlags & B64_BASE64_FLAG_NOPAD) == 0)
|
||||
nRet += nSrcLen % 3;
|
||||
|
||||
int nCRLFs = nRet / 76 + 1;
|
||||
int nOnLastLine = nRet % 76;
|
||||
|
||||
if (nOnLastLine)
|
||||
{
|
||||
if (nOnLastLine % 4)
|
||||
nRet += 4-(nOnLastLine % 4);
|
||||
}
|
||||
|
||||
nCRLFs *= 2;
|
||||
|
||||
if ((dwFlags & B64_BASE64_FLAG_NOCRLF) == 0)
|
||||
nRet += nCRLFs;
|
||||
|
||||
return nRet;
|
||||
}
|
||||
|
||||
int Base64DecodeGetRequiredLength(int nSrcLen)
|
||||
{
|
||||
return nSrcLen;
|
||||
}
|
||||
|
||||
int Base64Encode(const BYTE *pbSrcData, int nSrcLen, BYTE* szDest, int *pnDestLen, DWORD dwFlags)
|
||||
{
|
||||
static const char s_chBase64EncodingTable[64] = {
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
|
||||
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
|
||||
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
|
||||
'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
|
||||
|
||||
if (!pbSrcData || !szDest || !pnDestLen)
|
||||
return FALSE;
|
||||
|
||||
if (*pnDestLen < Base64EncodeGetRequiredLength(nSrcLen, dwFlags))
|
||||
return FALSE;
|
||||
|
||||
int nWritten( 0 );
|
||||
int nLen1( (nSrcLen/3)*4 );
|
||||
int nLen2( nLen1/76 );
|
||||
int nLen3( 19 );
|
||||
|
||||
for (int i=0; i<=nLen2; i++)
|
||||
{
|
||||
if (i==nLen2)
|
||||
nLen3 = (nLen1%76)/4;
|
||||
|
||||
for (int j=0; j<nLen3; j++)
|
||||
{
|
||||
UINT dwCurr(0);
|
||||
for (int n=0; n<3; n++)
|
||||
{
|
||||
dwCurr |= *pbSrcData++;
|
||||
dwCurr <<= 8;
|
||||
}
|
||||
for (int k=0; k<4; k++)
|
||||
{
|
||||
BYTE b = (BYTE)(dwCurr>>26);
|
||||
*szDest++ = s_chBase64EncodingTable[b];
|
||||
dwCurr <<= 6;
|
||||
}
|
||||
}
|
||||
nWritten+= nLen3*4;
|
||||
|
||||
if ((dwFlags & B64_BASE64_FLAG_NOCRLF)==0)
|
||||
{
|
||||
*szDest++ = '\r';
|
||||
*szDest++ = '\n';
|
||||
nWritten+= 2;
|
||||
}
|
||||
}
|
||||
|
||||
if (nWritten && (dwFlags & B64_BASE64_FLAG_NOCRLF)==0)
|
||||
{
|
||||
szDest-= 2;
|
||||
nWritten -= 2;
|
||||
}
|
||||
|
||||
nLen2 = (nSrcLen%3) ? (nSrcLen%3 + 1) : 0;
|
||||
if (nLen2)
|
||||
{
|
||||
UINT dwCurr(0);
|
||||
for (int n=0; n<3; n++)
|
||||
{
|
||||
if (n<(nSrcLen%3))
|
||||
dwCurr |= *pbSrcData++;
|
||||
dwCurr <<= 8;
|
||||
}
|
||||
for (int k=0; k<nLen2; k++)
|
||||
{
|
||||
BYTE b = (BYTE)(dwCurr>>26);
|
||||
*szDest++ = s_chBase64EncodingTable[b];
|
||||
dwCurr <<= 6;
|
||||
}
|
||||
nWritten+= nLen2;
|
||||
if ((dwFlags & B64_BASE64_FLAG_NOPAD)==0)
|
||||
{
|
||||
nLen3 = nLen2 ? 4-nLen2 : 0;
|
||||
for (int j=0; j<nLen3; j++)
|
||||
{
|
||||
*szDest++ = '=';
|
||||
}
|
||||
nWritten+= nLen3;
|
||||
}
|
||||
}
|
||||
|
||||
*pnDestLen = nWritten;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int DecodeBase64Char(unsigned int ch)
|
||||
{
|
||||
// returns -1 if the character is invalid
|
||||
// or should be skipped
|
||||
// otherwise, returns the 6-bit code for the character
|
||||
// from the encoding table
|
||||
if (ch >= 'A' && ch <= 'Z')
|
||||
return ch - 'A' + 0; // 0 range starts at 'A'
|
||||
if (ch >= 'a' && ch <= 'z')
|
||||
return ch - 'a' + 26; // 26 range starts at 'a'
|
||||
if (ch >= '0' && ch <= '9')
|
||||
return ch - '0' + 52; // 52 range starts at '0'
|
||||
if (ch == '+')
|
||||
return 62;
|
||||
if (ch == '/')
|
||||
return 63;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int Base64Decode(const char* szSrc, int nSrcLen, BYTE *pbDest, int *pnDestLen)
|
||||
{
|
||||
// walk the source buffer
|
||||
// each four character sequence is converted to 3 bytes
|
||||
// CRLFs and =, and any characters not in the encoding table
|
||||
// are skiped
|
||||
|
||||
if (szSrc == NULL || pnDestLen == NULL)
|
||||
return FALSE;
|
||||
|
||||
const char* szSrcEnd = szSrc + nSrcLen;
|
||||
int nWritten = 0;
|
||||
|
||||
INT bOverflow = (pbDest == NULL) ? TRUE : FALSE;
|
||||
|
||||
while (szSrc < szSrcEnd &&(*szSrc) != 0)
|
||||
{
|
||||
DWORD dwCurr = 0;
|
||||
int i;
|
||||
int nBits = 0;
|
||||
for (i=0; i<4; i++)
|
||||
{
|
||||
if (szSrc >= szSrcEnd)
|
||||
break;
|
||||
int nCh = DecodeBase64Char(*szSrc);
|
||||
szSrc++;
|
||||
if (nCh == -1)
|
||||
{
|
||||
// skip this char
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
dwCurr <<= 6;
|
||||
dwCurr |= nCh;
|
||||
nBits += 6;
|
||||
}
|
||||
|
||||
if(!bOverflow && nWritten + (nBits/8) > (*pnDestLen))
|
||||
bOverflow = TRUE;
|
||||
|
||||
// dwCurr has the 3 bytes to write to the output buffer
|
||||
// left to right
|
||||
dwCurr <<= 24-nBits;
|
||||
for (i=0; i<nBits/8; i++)
|
||||
{
|
||||
if(!bOverflow)
|
||||
{
|
||||
*pbDest = (BYTE) ((dwCurr & 0x00ff0000) >> 16);
|
||||
pbDest++;
|
||||
}
|
||||
dwCurr <<= 8;
|
||||
nWritten++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
*pnDestLen = nWritten;
|
||||
|
||||
if(bOverflow)
|
||||
{
|
||||
// if (pbDest != NULL) ATLASSERT(FALSE);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
@ -36,6 +36,13 @@
|
||||
#include <limits.h>
|
||||
#include "Types.h"
|
||||
|
||||
#ifndef BASE64_USE_DYNAMIC_LIBRARY
|
||||
#define BASE64_DECL_EXPORT
|
||||
#else
|
||||
#include "./base_export.h"
|
||||
#define BASE64_DECL_EXPORT Q_DECL_EXPORT
|
||||
#endif
|
||||
|
||||
namespace NSBase64
|
||||
{
|
||||
const int B64_BASE64_FLAG_NONE = 0;
|
||||
@ -44,212 +51,15 @@ namespace NSBase64
|
||||
|
||||
#define _BASE64_INT_MAX 2147483647
|
||||
|
||||
inline int Base64EncodeGetRequiredLength(int nSrcLen, DWORD dwFlags = B64_BASE64_FLAG_NONE)
|
||||
{
|
||||
T_LONG64 nSrcLen4 = static_cast<T_LONG64>(nSrcLen)*4;
|
||||
if (nSrcLen4 > INT_MAX)
|
||||
return -1;
|
||||
BASE64_DECL_EXPORT int Base64EncodeGetRequiredLength(int nSrcLen, DWORD dwFlags = B64_BASE64_FLAG_NONE);
|
||||
|
||||
int nRet = static_cast<int>(nSrcLen4/3);
|
||||
BASE64_DECL_EXPORT int Base64DecodeGetRequiredLength(int nSrcLen);
|
||||
|
||||
if ((dwFlags & B64_BASE64_FLAG_NOPAD) == 0)
|
||||
nRet += nSrcLen % 3;
|
||||
BASE64_DECL_EXPORT int Base64Encode(const BYTE *pbSrcData, int nSrcLen, BYTE* szDest, int *pnDestLen, DWORD dwFlags = B64_BASE64_FLAG_NONE);
|
||||
|
||||
int nCRLFs = nRet / 76 + 1;
|
||||
int nOnLastLine = nRet % 76;
|
||||
BASE64_DECL_EXPORT int DecodeBase64Char(unsigned int ch);
|
||||
|
||||
if (nOnLastLine)
|
||||
{
|
||||
if (nOnLastLine % 4)
|
||||
nRet += 4-(nOnLastLine % 4);
|
||||
}
|
||||
|
||||
nCRLFs *= 2;
|
||||
|
||||
if ((dwFlags & B64_BASE64_FLAG_NOCRLF) == 0)
|
||||
nRet += nCRLFs;
|
||||
|
||||
return nRet;
|
||||
}
|
||||
|
||||
inline int Base64DecodeGetRequiredLength(int nSrcLen)
|
||||
{
|
||||
return nSrcLen;
|
||||
}
|
||||
|
||||
inline INT Base64Encode(const BYTE *pbSrcData, int nSrcLen, BYTE* szDest, int *pnDestLen, DWORD dwFlags = B64_BASE64_FLAG_NONE)
|
||||
{
|
||||
static const char s_chBase64EncodingTable[64] = {
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
|
||||
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
|
||||
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
|
||||
'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
|
||||
|
||||
if (!pbSrcData || !szDest || !pnDestLen)
|
||||
return FALSE;
|
||||
|
||||
if (*pnDestLen < Base64EncodeGetRequiredLength(nSrcLen, dwFlags))
|
||||
return FALSE;
|
||||
|
||||
int nWritten( 0 );
|
||||
int nLen1( (nSrcLen/3)*4 );
|
||||
int nLen2( nLen1/76 );
|
||||
int nLen3( 19 );
|
||||
|
||||
for (int i=0; i<=nLen2; i++)
|
||||
{
|
||||
if (i==nLen2)
|
||||
nLen3 = (nLen1%76)/4;
|
||||
|
||||
for (int j=0; j<nLen3; j++)
|
||||
{
|
||||
UINT dwCurr(0);
|
||||
for (int n=0; n<3; n++)
|
||||
{
|
||||
dwCurr |= *pbSrcData++;
|
||||
dwCurr <<= 8;
|
||||
}
|
||||
for (int k=0; k<4; k++)
|
||||
{
|
||||
BYTE b = (BYTE)(dwCurr>>26);
|
||||
*szDest++ = s_chBase64EncodingTable[b];
|
||||
dwCurr <<= 6;
|
||||
}
|
||||
}
|
||||
nWritten+= nLen3*4;
|
||||
|
||||
if ((dwFlags & B64_BASE64_FLAG_NOCRLF)==0)
|
||||
{
|
||||
*szDest++ = '\r';
|
||||
*szDest++ = '\n';
|
||||
nWritten+= 2;
|
||||
}
|
||||
}
|
||||
|
||||
if (nWritten && (dwFlags & B64_BASE64_FLAG_NOCRLF)==0)
|
||||
{
|
||||
szDest-= 2;
|
||||
nWritten -= 2;
|
||||
}
|
||||
|
||||
nLen2 = (nSrcLen%3) ? (nSrcLen%3 + 1) : 0;
|
||||
if (nLen2)
|
||||
{
|
||||
UINT dwCurr(0);
|
||||
for (int n=0; n<3; n++)
|
||||
{
|
||||
if (n<(nSrcLen%3))
|
||||
dwCurr |= *pbSrcData++;
|
||||
dwCurr <<= 8;
|
||||
}
|
||||
for (int k=0; k<nLen2; k++)
|
||||
{
|
||||
BYTE b = (BYTE)(dwCurr>>26);
|
||||
*szDest++ = s_chBase64EncodingTable[b];
|
||||
dwCurr <<= 6;
|
||||
}
|
||||
nWritten+= nLen2;
|
||||
if ((dwFlags & B64_BASE64_FLAG_NOPAD)==0)
|
||||
{
|
||||
nLen3 = nLen2 ? 4-nLen2 : 0;
|
||||
for (int j=0; j<nLen3; j++)
|
||||
{
|
||||
*szDest++ = '=';
|
||||
}
|
||||
nWritten+= nLen3;
|
||||
}
|
||||
}
|
||||
|
||||
*pnDestLen = nWritten;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
inline int DecodeBase64Char(unsigned int ch)
|
||||
{
|
||||
// returns -1 if the character is invalid
|
||||
// or should be skipped
|
||||
// otherwise, returns the 6-bit code for the character
|
||||
// from the encoding table
|
||||
if (ch >= 'A' && ch <= 'Z')
|
||||
return ch - 'A' + 0; // 0 range starts at 'A'
|
||||
if (ch >= 'a' && ch <= 'z')
|
||||
return ch - 'a' + 26; // 26 range starts at 'a'
|
||||
if (ch >= '0' && ch <= '9')
|
||||
return ch - '0' + 52; // 52 range starts at '0'
|
||||
if (ch == '+')
|
||||
return 62;
|
||||
if (ch == '/')
|
||||
return 63;
|
||||
return -1;
|
||||
}
|
||||
|
||||
inline INT Base64Decode(const char* szSrc, int nSrcLen, BYTE *pbDest, int *pnDestLen)
|
||||
{
|
||||
// walk the source buffer
|
||||
// each four character sequence is converted to 3 bytes
|
||||
// CRLFs and =, and any characters not in the encoding table
|
||||
// are skiped
|
||||
|
||||
if (szSrc == NULL || pnDestLen == NULL)
|
||||
return FALSE;
|
||||
|
||||
const char* szSrcEnd = szSrc + nSrcLen;
|
||||
int nWritten = 0;
|
||||
|
||||
INT bOverflow = (pbDest == NULL) ? TRUE : FALSE;
|
||||
|
||||
while (szSrc < szSrcEnd &&(*szSrc) != 0)
|
||||
{
|
||||
DWORD dwCurr = 0;
|
||||
int i;
|
||||
int nBits = 0;
|
||||
for (i=0; i<4; i++)
|
||||
{
|
||||
if (szSrc >= szSrcEnd)
|
||||
break;
|
||||
int nCh = DecodeBase64Char(*szSrc);
|
||||
szSrc++;
|
||||
if (nCh == -1)
|
||||
{
|
||||
// skip this char
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
dwCurr <<= 6;
|
||||
dwCurr |= nCh;
|
||||
nBits += 6;
|
||||
}
|
||||
|
||||
if(!bOverflow && nWritten + (nBits/8) > (*pnDestLen))
|
||||
bOverflow = TRUE;
|
||||
|
||||
// dwCurr has the 3 bytes to write to the output buffer
|
||||
// left to right
|
||||
dwCurr <<= 24-nBits;
|
||||
for (i=0; i<nBits/8; i++)
|
||||
{
|
||||
if(!bOverflow)
|
||||
{
|
||||
*pbDest = (BYTE) ((dwCurr & 0x00ff0000) >> 16);
|
||||
pbDest++;
|
||||
}
|
||||
dwCurr <<= 8;
|
||||
nWritten++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
*pnDestLen = nWritten;
|
||||
|
||||
if(bOverflow)
|
||||
{
|
||||
// if (pbDest != NULL) ATLASSERT(FALSE);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
BASE64_DECL_EXPORT int Base64Decode(const char* szSrc, int nSrcLen, BYTE *pbDest, int *pnDestLen);
|
||||
}
|
||||
|
||||
#endif//_BUILD_BASE64_CROSSPLATFORM_DEFINE
|
||||
#endif//_BUILD_BASE64_CROSSPLATFORM_DEFINE
|
||||
|
||||
472
DesktopEditor/common/Directory.cpp
Normal file
472
DesktopEditor/common/Directory.cpp
Normal file
@ -0,0 +1,472 @@
|
||||
/*
|
||||
* (c) Copyright Ascensio System SIA 2010-2018
|
||||
*
|
||||
* 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 Lubanas st. 125a-25, Riga, Latvia,
|
||||
* EU, LV-1021.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
*/
|
||||
|
||||
#if defined(_WIN32) || defined (_WIN64)
|
||||
#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>
|
||||
#elif MAC
|
||||
#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>
|
||||
#endif
|
||||
|
||||
#include "Directory.h"
|
||||
|
||||
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 _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;
|
||||
}
|
||||
#endif
|
||||
|
||||
void GetFiles2(std::wstring strDirectory, std::vector<std::wstring>& oArray, bool bIsRecursion)
|
||||
{
|
||||
#if defined(_WIN32) || defined (_WIN64)
|
||||
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 );
|
||||
#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;
|
||||
}
|
||||
|
||||
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;
|
||||
#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
|
||||
}
|
||||
|
||||
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> oArray;
|
||||
|
||||
#if defined(_WIN32) || defined (_WIN64)
|
||||
WIN32_FIND_DATAW oFD;
|
||||
|
||||
std::wstring sSpec = strDirectory + L"\\*";
|
||||
HANDLE hRes = FindFirstFileW( sSpec.c_str(), &oFD );
|
||||
if( INVALID_HANDLE_VALUE == hRes )
|
||||
return oArray;
|
||||
do
|
||||
{
|
||||
sSpec = oFD.cFileName;
|
||||
if (sSpec != L"." && sSpec != L"..")
|
||||
{
|
||||
sSpec = strDirectory + L"\\" + sSpec;
|
||||
if( oFD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
|
||||
{
|
||||
oArray.push_back(sSpec);
|
||||
}
|
||||
}
|
||||
} 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);
|
||||
DIR *dp;
|
||||
struct dirent *dirp;
|
||||
if((dp = opendir((char*)pUtf8)) != NULL)
|
||||
{
|
||||
while ((dirp = readdir(dp)) != NULL)
|
||||
{
|
||||
bool bIsDir = false;
|
||||
if (DT_DIR == dirp->d_type)
|
||||
bIsDir = true;
|
||||
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_ISDIR(buff.st_mode))
|
||||
bIsDir = true;
|
||||
}
|
||||
if(bIsDir)
|
||||
{
|
||||
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;
|
||||
#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;
|
||||
#endif
|
||||
return oArray;
|
||||
}
|
||||
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;
|
||||
#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;
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
bool CreateDirectory(const std::wstring& strDirectory)
|
||||
{
|
||||
if (Exists(strDirectory) == true) return true;
|
||||
|
||||
#if defined(_WIN32) || defined (_WIN64)
|
||||
return FALSE != ::CreateDirectoryW(strDirectory.c_str(), NULL);
|
||||
#else
|
||||
BYTE* pUtf8 = NULL;
|
||||
LONG lLen = 0;
|
||||
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strDirectory.c_str(), strDirectory.length(), pUtf8, lLen, false);
|
||||
struct stat st;
|
||||
int nRes = 0;
|
||||
if (stat((char*)pUtf8, &st) == -1) {
|
||||
nRes = mkdir((char*)pUtf8, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
||||
}
|
||||
delete [] pUtf8;
|
||||
return 0 == nRes;
|
||||
#endif
|
||||
}
|
||||
bool CreateDirectories(const std::wstring& strDirectory)
|
||||
{
|
||||
if (Exists(strDirectory) == true) return true;
|
||||
|
||||
#if defined(_WIN32) || defined (_WIN64)
|
||||
DWORD dwAttrib = ::GetFileAttributesW(strDirectory.c_str());
|
||||
if (dwAttrib != INVALID_FILE_ATTRIBUTES && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) return true;
|
||||
|
||||
int codeResult = ERROR_SUCCESS;
|
||||
|
||||
SECURITY_ATTRIBUTES sa={};
|
||||
|
||||
if (strDirectory.find(L"./") == 0)
|
||||
{
|
||||
std::wstring sDir = NSFile::GetProcessDirectory() + L"/" + strDirectory;
|
||||
codeResult = SHCreateDirectoryExW(NULL, sDir.c_str(), &sa);
|
||||
}
|
||||
else
|
||||
codeResult = SHCreateDirectoryExW(NULL, strDirectory.c_str(), &sa);
|
||||
|
||||
bool created = false;
|
||||
if (codeResult == ERROR_SUCCESS)
|
||||
created = true;
|
||||
|
||||
return created;
|
||||
#else
|
||||
std::string pathUtf8 = U_TO_UTF8(strDirectory);
|
||||
return _mkdir (pathUtf8.c_str());
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
void DeleteDirectory(const std::wstring& strDirectory, bool deleteRoot)
|
||||
{
|
||||
std::vector<std::wstring> aFiles = GetFiles(strDirectory);
|
||||
for(size_t i = 0; i < aFiles.size(); ++i)
|
||||
{
|
||||
NSFile::CFileBinary::Remove(aFiles[i]);
|
||||
}
|
||||
std::vector<std::wstring> aDirectories = GetDirectories(strDirectory);
|
||||
for(size_t i = 0; i < aDirectories.size(); ++i)
|
||||
{
|
||||
DeleteDirectory(aDirectories[i]);
|
||||
}
|
||||
#if defined(_WIN32) || defined (_WIN64)
|
||||
if (deleteRoot) RemoveDirectoryW(strDirectory.c_str());
|
||||
#elif __linux__
|
||||
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);
|
||||
#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);
|
||||
#endif
|
||||
}
|
||||
std::wstring GetFolderPath(const std::wstring& wsFolderPath)
|
||||
{
|
||||
int n1 = (int)wsFolderPath.rfind('\\');
|
||||
if (n1 < 0)
|
||||
{
|
||||
n1 = (int)wsFolderPath.rfind('/');
|
||||
if (n1 < 0)
|
||||
{
|
||||
return L"";
|
||||
}
|
||||
return wsFolderPath.substr(0, n1);
|
||||
}
|
||||
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)
|
||||
{
|
||||
#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);
|
||||
|
||||
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"";
|
||||
}
|
||||
}
|
||||
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 sRes = pcRes;
|
||||
return NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)sRes.c_str(), (LONG)sRes.length());
|
||||
#endif
|
||||
}
|
||||
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);
|
||||
#if defined(_WIN32) || defined (_WIN64)
|
||||
return (int)arrFiles.size();
|
||||
#endif
|
||||
return (int)arrFiles.size() + 1;
|
||||
// ???
|
||||
}
|
||||
bool PathIsDirectory(const std::wstring& pathName)
|
||||
{
|
||||
return Exists(pathName);
|
||||
}
|
||||
}
|
||||
@ -37,28 +37,6 @@
|
||||
#include <vector>
|
||||
#include "File.h"
|
||||
|
||||
#if defined(_WIN32) || defined (_WIN64)
|
||||
#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>
|
||||
#elif MAC
|
||||
#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>
|
||||
#endif
|
||||
|
||||
#ifndef FILE_SEPARATOR
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#define FILE_SEPARATOR
|
||||
@ -71,426 +49,33 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef DIRECTORY_USE_DYNAMIC_LIBRARY
|
||||
#define DIRECTORY_DECL_EXPORT
|
||||
#else
|
||||
#include "./base_export.h"
|
||||
#define DIRECTORY_DECL_EXPORT Q_DECL_EXPORT
|
||||
#endif
|
||||
|
||||
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 _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;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef _IOS
|
||||
void GetFiles2_ios(std::wstring strDirectory, std::vector<std::wstring>& oArray, bool bIsRecursion);
|
||||
DIRECTORY_DECL_EXPORT void GetFiles2_ios(std::wstring strDirectory, std::vector<std::wstring>& oArray, bool bIsRecursion);
|
||||
#endif
|
||||
static void GetFiles2(std::wstring strDirectory, std::vector<std::wstring>& oArray, bool bIsRecursion = false)
|
||||
{
|
||||
#if defined(_WIN32) || defined (_WIN64)
|
||||
WIN32_FIND_DATAW oFD;
|
||||
DIRECTORY_DECL_EXPORT void GetFiles2(std::wstring strDirectory, std::vector<std::wstring>& oArray, bool bIsRecursion = false);
|
||||
|
||||
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
|
||||
DIRECTORY_DECL_EXPORT std::vector<std::wstring> GetFiles(std::wstring strDirectory, bool bIsRecursion = false);
|
||||
DIRECTORY_DECL_EXPORT std::vector<std::wstring> GetDirectories(std::wstring strDirectory);
|
||||
DIRECTORY_DECL_EXPORT bool Exists(const std::wstring& strDirectory);
|
||||
DIRECTORY_DECL_EXPORT bool CreateDirectory(const std::wstring& strDirectory);
|
||||
DIRECTORY_DECL_EXPORT bool CreateDirectories(const std::wstring& strDirectory);
|
||||
DIRECTORY_DECL_EXPORT void DeleteDirectory(const std::wstring& strDirectory, bool deleteRoot = true);
|
||||
DIRECTORY_DECL_EXPORT std::wstring GetFolderPath(const std::wstring& wsFolderPath);
|
||||
DIRECTORY_DECL_EXPORT std::wstring CreateTempFileWithUniqueName (const std::wstring & strFolderPathRoot, std::wstring Prefix);
|
||||
DIRECTORY_DECL_EXPORT std::wstring CreateDirectoryWithUniqueName (const std::wstring & strFolderPathRoot);
|
||||
DIRECTORY_DECL_EXPORT std::wstring GetTempPath();
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
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;
|
||||
#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
|
||||
}
|
||||
|
||||
static std::vector<std::wstring> GetFiles(std::wstring strDirectory, bool bIsRecursion = false)
|
||||
{
|
||||
std::vector<std::wstring> oArray;
|
||||
|
||||
if (!strDirectory.empty())
|
||||
{
|
||||
GetFiles2(strDirectory, oArray, bIsRecursion);
|
||||
}
|
||||
return oArray;
|
||||
}
|
||||
static std::vector<std::wstring> GetDirectories(std::wstring strDirectory)
|
||||
{
|
||||
std::vector<std::wstring> oArray;
|
||||
|
||||
#if defined(_WIN32) || defined (_WIN64)
|
||||
WIN32_FIND_DATAW oFD;
|
||||
|
||||
std::wstring sSpec = strDirectory + L"\\*";
|
||||
HANDLE hRes = FindFirstFileW( sSpec.c_str(), &oFD );
|
||||
if( INVALID_HANDLE_VALUE == hRes )
|
||||
return oArray;
|
||||
do
|
||||
{
|
||||
sSpec = oFD.cFileName;
|
||||
if (sSpec != L"." && sSpec != L"..")
|
||||
{
|
||||
sSpec = strDirectory + L"\\" + sSpec;
|
||||
if( oFD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
|
||||
{
|
||||
oArray.push_back(sSpec);
|
||||
}
|
||||
}
|
||||
} 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);
|
||||
DIR *dp;
|
||||
struct dirent *dirp;
|
||||
if((dp = opendir((char*)pUtf8)) != NULL)
|
||||
{
|
||||
while ((dirp = readdir(dp)) != NULL)
|
||||
{
|
||||
bool bIsDir = false;
|
||||
if (DT_DIR == dirp->d_type)
|
||||
bIsDir = true;
|
||||
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_ISDIR(buff.st_mode))
|
||||
bIsDir = true;
|
||||
}
|
||||
if(bIsDir)
|
||||
{
|
||||
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;
|
||||
#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;
|
||||
#endif
|
||||
return oArray;
|
||||
}
|
||||
static 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;
|
||||
#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;
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
static bool CreateDirectory(const std::wstring& strDirectory)
|
||||
{
|
||||
if (Exists(strDirectory) == true) return true;
|
||||
|
||||
#if defined(_WIN32) || defined (_WIN64)
|
||||
return FALSE != ::CreateDirectoryW(strDirectory.c_str(), NULL);
|
||||
#else
|
||||
BYTE* pUtf8 = NULL;
|
||||
LONG lLen = 0;
|
||||
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strDirectory.c_str(), strDirectory.length(), pUtf8, lLen, false);
|
||||
struct stat st;
|
||||
int nRes = 0;
|
||||
if (stat((char*)pUtf8, &st) == -1) {
|
||||
nRes = mkdir((char*)pUtf8, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
||||
}
|
||||
delete [] pUtf8;
|
||||
return 0 == nRes;
|
||||
#endif
|
||||
}
|
||||
static bool CreateDirectories(const std::wstring& strDirectory)
|
||||
{
|
||||
if (Exists(strDirectory) == true) return true;
|
||||
|
||||
#if defined(_WIN32) || defined (_WIN64)
|
||||
DWORD dwAttrib = ::GetFileAttributesW(strDirectory.c_str());
|
||||
if (dwAttrib != INVALID_FILE_ATTRIBUTES && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) return true;
|
||||
|
||||
int codeResult = ERROR_SUCCESS;
|
||||
|
||||
SECURITY_ATTRIBUTES sa={};
|
||||
|
||||
if (strDirectory.find(L"./") == 0)
|
||||
{
|
||||
std::wstring sDir = NSFile::GetProcessDirectory() + L"/" + strDirectory;
|
||||
codeResult = SHCreateDirectoryExW(NULL, sDir.c_str(), &sa);
|
||||
}
|
||||
else
|
||||
codeResult = SHCreateDirectoryExW(NULL, strDirectory.c_str(), &sa);
|
||||
|
||||
bool created = false;
|
||||
if (codeResult == ERROR_SUCCESS)
|
||||
created = true;
|
||||
|
||||
return created;
|
||||
#else
|
||||
std::string pathUtf8 = U_TO_UTF8(strDirectory);
|
||||
return _mkdir (pathUtf8.c_str());
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
static void DeleteDirectory(const std::wstring& strDirectory, bool deleteRoot = true)
|
||||
{
|
||||
std::vector<std::wstring> aFiles = GetFiles(strDirectory);
|
||||
for(size_t i = 0; i < aFiles.size(); ++i)
|
||||
{
|
||||
NSFile::CFileBinary::Remove(aFiles[i]);
|
||||
}
|
||||
std::vector<std::wstring> aDirectories = GetDirectories(strDirectory);
|
||||
for(size_t i = 0; i < aDirectories.size(); ++i)
|
||||
{
|
||||
DeleteDirectory(aDirectories[i]);
|
||||
}
|
||||
#if defined(_WIN32) || defined (_WIN64)
|
||||
if (deleteRoot) RemoveDirectoryW(strDirectory.c_str());
|
||||
#elif __linux__
|
||||
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);
|
||||
#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);
|
||||
#endif
|
||||
}
|
||||
static std::wstring GetFolderPath(const std::wstring& wsFolderPath)
|
||||
{
|
||||
int n1 = (int)wsFolderPath.rfind('\\');
|
||||
if (n1 < 0)
|
||||
{
|
||||
n1 = (int)wsFolderPath.rfind('/');
|
||||
if (n1 < 0)
|
||||
{
|
||||
return L"";
|
||||
}
|
||||
return wsFolderPath.substr(0, n1);
|
||||
}
|
||||
return wsFolderPath.substr(0, n1);
|
||||
}
|
||||
static std::wstring CreateTempFileWithUniqueName (const std::wstring & strFolderPathRoot, std::wstring Prefix)
|
||||
{
|
||||
return NSFile::CFileBinary::CreateTempFileWithUniqueName(strFolderPathRoot, Prefix);
|
||||
}
|
||||
static 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);
|
||||
|
||||
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"";
|
||||
}
|
||||
}
|
||||
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 sRes = pcRes;
|
||||
return NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)sRes.c_str(), (LONG)sRes.length());
|
||||
#endif
|
||||
}
|
||||
static std::wstring GetTempPath()
|
||||
{
|
||||
return NSFile::CFileBinary::GetTempPath();
|
||||
}
|
||||
|
||||
static 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();
|
||||
#endif
|
||||
return (int)arrFiles.size() + 1;
|
||||
// ???
|
||||
}
|
||||
#if !defined(_WIN32) && !defined (_WIN64)
|
||||
static bool PathIsDirectory(const std::wstring& pathName)
|
||||
{
|
||||
return Exists(pathName);
|
||||
}
|
||||
#endif
|
||||
DIRECTORY_DECL_EXPORT int GetFilesCount(const std::wstring& path, const bool& recursive);
|
||||
DIRECTORY_DECL_EXPORT bool PathIsDirectory(const std::wstring& pathName);
|
||||
}
|
||||
|
||||
#endif //_BUILD_DIRECTORY_CROSSPLATFORM_H_
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
111
DesktopEditor/common/Path.cpp
Normal file
111
DesktopEditor/common/Path.cpp
Normal file
@ -0,0 +1,111 @@
|
||||
/*
|
||||
* (c) Copyright Ascensio System SIA 2010-2018
|
||||
*
|
||||
* 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 Lubanas st. 125a-25, Riga, Latvia,
|
||||
* EU, LV-1021.
|
||||
*
|
||||
* 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 "Path.h"
|
||||
#include "File.h"
|
||||
|
||||
#if defined(_WIN32) || defined (_WIN64)
|
||||
#include <tchar.h>
|
||||
#elif __linux__ || MAC
|
||||
#include <libgen.h>
|
||||
#endif
|
||||
|
||||
namespace NSSystemPath
|
||||
{
|
||||
std::wstring GetDirectoryName(const std::wstring& strFileName)
|
||||
{
|
||||
std::wstring sRes;
|
||||
//_wsplitpath return directory path, including trailing slash.
|
||||
//dirname() returns the string up to, but not including, the final '/',
|
||||
#if defined(_WIN32) || defined (_WIN64)
|
||||
wchar_t tDrive[256];
|
||||
wchar_t tFolder[256];
|
||||
_wsplitpath( strFileName.c_str(), tDrive, tFolder, NULL, NULL );
|
||||
sRes.append(tDrive);
|
||||
sRes.append(tFolder);
|
||||
if(sRes.length() > 0)
|
||||
sRes.erase(sRes.length()-1);
|
||||
#elif __linux__ || MAC
|
||||
BYTE* pUtf8 = NULL;
|
||||
LONG lLen = 0;
|
||||
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strFileName.c_str(), strFileName.length(), pUtf8, lLen, false);
|
||||
char* pDirName = dirname((char*)pUtf8);
|
||||
sRes = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)pDirName, strlen(pDirName));
|
||||
delete [] pUtf8;
|
||||
#endif
|
||||
return sRes;
|
||||
}
|
||||
std::wstring GetFileName(const std::wstring& strFileName)
|
||||
{
|
||||
std::wstring sRes;
|
||||
#if defined(_WIN32) || defined (_WIN64)
|
||||
wchar_t tFilename[256];
|
||||
wchar_t tExt[256];
|
||||
_wsplitpath( strFileName.c_str(), NULL, NULL, tFilename, tExt );
|
||||
sRes.append(tFilename);
|
||||
sRes.append(tExt);
|
||||
return sRes;
|
||||
#elif __linux__ || MAC
|
||||
BYTE* pUtf8 = NULL;
|
||||
LONG lLen = 0;
|
||||
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strFileName.c_str(), strFileName.length(), pUtf8, lLen, false);
|
||||
char* pBaseName = basename((char*)pUtf8);
|
||||
sRes = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)pBaseName, strlen(pBaseName));
|
||||
delete [] pUtf8;
|
||||
#endif
|
||||
return sRes;
|
||||
}
|
||||
std::wstring Combine(const std::wstring& strLeft, const std::wstring& strRight)
|
||||
{
|
||||
std::wstring sRes;
|
||||
bool bLeftSlash = false;
|
||||
bool bRightSlash = false;
|
||||
if(strLeft.length() > 0)
|
||||
{
|
||||
wchar_t cLeft = strLeft[strLeft.length() - 1];
|
||||
bLeftSlash = ('/' == cLeft) || ('\\' == cLeft);
|
||||
}
|
||||
if(strRight.length() > 0)
|
||||
{
|
||||
wchar_t cRight = strRight[0];
|
||||
bRightSlash = ('/' == cRight) || ('\\' == cRight);
|
||||
}
|
||||
if(bLeftSlash && bRightSlash)
|
||||
{
|
||||
sRes = strLeft + strRight.substr(1);
|
||||
}
|
||||
else if(!bLeftSlash && !bRightSlash)
|
||||
sRes = strLeft + L"/" + strRight;
|
||||
else
|
||||
sRes = strLeft + strRight;
|
||||
return sRes;
|
||||
}
|
||||
}
|
||||
@ -34,84 +34,19 @@
|
||||
|
||||
#include <string>
|
||||
#include <string.h>
|
||||
#include "File.h"
|
||||
|
||||
#if defined(_WIN32) || defined (_WIN64)
|
||||
#include <tchar.h>
|
||||
#elif __linux__ || MAC
|
||||
#include <libgen.h>
|
||||
#ifndef PATH_USE_DYNAMIC_LIBRARY
|
||||
#define PATH_DECL_EXPORT
|
||||
#else
|
||||
#include "./base_export.h"
|
||||
#define PATH_DECL_EXPORT Q_DECL_EXPORT
|
||||
#endif
|
||||
|
||||
namespace NSSystemPath
|
||||
{
|
||||
static std::wstring GetDirectoryName(const std::wstring& strFileName)
|
||||
{
|
||||
std::wstring sRes;
|
||||
//_wsplitpath return directory path, including trailing slash.
|
||||
//dirname() returns the string up to, but not including, the final '/',
|
||||
#if defined(_WIN32) || defined (_WIN64)
|
||||
wchar_t tDrive[256];
|
||||
wchar_t tFolder[256];
|
||||
_wsplitpath( strFileName.c_str(), tDrive, tFolder, NULL, NULL );
|
||||
sRes.append(tDrive);
|
||||
sRes.append(tFolder);
|
||||
if(sRes.length() > 0)
|
||||
sRes.erase(sRes.length()-1);
|
||||
#elif __linux__ || MAC
|
||||
BYTE* pUtf8 = NULL;
|
||||
LONG lLen = 0;
|
||||
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strFileName.c_str(), strFileName.length(), pUtf8, lLen, false);
|
||||
char* pDirName = dirname((char*)pUtf8);
|
||||
sRes = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)pDirName, strlen(pDirName));
|
||||
delete [] pUtf8;
|
||||
#endif
|
||||
return sRes;
|
||||
}
|
||||
static std::wstring GetFileName(const std::wstring& strFileName)
|
||||
{
|
||||
std::wstring sRes;
|
||||
#if defined(_WIN32) || defined (_WIN64)
|
||||
wchar_t tFilename[256];
|
||||
wchar_t tExt[256];
|
||||
_wsplitpath( strFileName.c_str(), NULL, NULL, tFilename, tExt );
|
||||
sRes.append(tFilename);
|
||||
sRes.append(tExt);
|
||||
return sRes;
|
||||
#elif __linux__ || MAC
|
||||
BYTE* pUtf8 = NULL;
|
||||
LONG lLen = 0;
|
||||
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strFileName.c_str(), strFileName.length(), pUtf8, lLen, false);
|
||||
char* pBaseName = basename((char*)pUtf8);
|
||||
sRes = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)pBaseName, strlen(pBaseName));
|
||||
delete [] pUtf8;
|
||||
#endif
|
||||
return sRes;
|
||||
}
|
||||
static std::wstring Combine(const std::wstring& strLeft, const std::wstring& strRight)
|
||||
{
|
||||
std::wstring sRes;
|
||||
bool bLeftSlash = false;
|
||||
bool bRightSlash = false;
|
||||
if(strLeft.length() > 0)
|
||||
{
|
||||
wchar_t cLeft = strLeft[strLeft.length() - 1];
|
||||
bLeftSlash = ('/' == cLeft) || ('\\' == cLeft);
|
||||
}
|
||||
if(strRight.length() > 0)
|
||||
{
|
||||
wchar_t cRight = strRight[0];
|
||||
bRightSlash = ('/' == cRight) || ('\\' == cRight);
|
||||
}
|
||||
if(bLeftSlash && bRightSlash)
|
||||
{
|
||||
sRes = strLeft + strRight.substr(1);
|
||||
}
|
||||
else if(!bLeftSlash && !bRightSlash)
|
||||
sRes = strLeft + L"/" + strRight;
|
||||
else
|
||||
sRes = strLeft + strRight;
|
||||
return sRes;
|
||||
}
|
||||
PATH_DECL_EXPORT std::wstring GetDirectoryName(const std::wstring& strFileName);
|
||||
PATH_DECL_EXPORT std::wstring GetFileName(const std::wstring& strFileName);
|
||||
PATH_DECL_EXPORT std::wstring Combine(const std::wstring& strLeft, const std::wstring& strRight);
|
||||
}
|
||||
|
||||
#endif //_BUILD_PATH_CROSSPLATFORM_H_
|
||||
|
||||
691
DesktopEditor/common/StringBuilder.cpp
Normal file
691
DesktopEditor/common/StringBuilder.cpp
Normal file
@ -0,0 +1,691 @@
|
||||
/*
|
||||
* (c) Copyright Ascensio System SIA 2010-2018
|
||||
*
|
||||
* 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 Lubanas st. 125a-25, Riga, Latvia,
|
||||
* EU, LV-1021.
|
||||
*
|
||||
* 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 "StringBuilder.h"
|
||||
|
||||
namespace NSStringUtils
|
||||
{
|
||||
const wchar_t g_hex_values[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
||||
|
||||
CStringBuilderA::CStringBuilderA()
|
||||
{
|
||||
m_pData = NULL;
|
||||
m_lSize = 0;
|
||||
|
||||
m_pDataCur = m_pData;
|
||||
m_lSizeCur = m_lSize;
|
||||
}
|
||||
CStringBuilderA::~CStringBuilderA()
|
||||
{
|
||||
if (NULL != m_pData)
|
||||
free(m_pData);
|
||||
m_pData = NULL;
|
||||
}
|
||||
|
||||
void CStringBuilderA::AddSize(size_t nSize)
|
||||
{
|
||||
if (NULL == m_pData)
|
||||
{
|
||||
m_lSize = (std::max)((int)nSize, 1000);
|
||||
m_pData = (char*)malloc(m_lSize * sizeof(char));
|
||||
|
||||
m_lSizeCur = 0;
|
||||
m_pDataCur = m_pData;
|
||||
return;
|
||||
}
|
||||
|
||||
if ((m_lSizeCur + nSize) > m_lSize)
|
||||
{
|
||||
while ((m_lSizeCur + nSize) > m_lSize)
|
||||
{
|
||||
m_lSize *= 2;
|
||||
}
|
||||
|
||||
char* pRealloc = (char*)realloc(m_pData, m_lSize * sizeof(char));
|
||||
if (NULL != pRealloc)
|
||||
{
|
||||
// реаллок сработал
|
||||
m_pData = pRealloc;
|
||||
m_pDataCur = m_pData + m_lSizeCur;
|
||||
}
|
||||
else
|
||||
{
|
||||
char* pMalloc = (char*)malloc(m_lSize * sizeof(char));
|
||||
memcpy(pMalloc, m_pData, m_lSizeCur * sizeof(char));
|
||||
|
||||
free(m_pData);
|
||||
m_pData = pMalloc;
|
||||
m_pDataCur = m_pData + m_lSizeCur;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CStringBuilderA::SetText(const std::string& sText)
|
||||
{
|
||||
ClearNoAttack();
|
||||
WriteString(sText);
|
||||
}
|
||||
void CStringBuilderA::WriteString(const std::string& str)
|
||||
{
|
||||
WriteString(str.c_str(), str.length());
|
||||
}
|
||||
|
||||
void CStringBuilderA::WriteStringNoSafe(const char* pString, size_t nLen)
|
||||
{
|
||||
memcpy(m_pDataCur, pString, nLen * sizeof(char));
|
||||
|
||||
m_pDataCur += nLen;
|
||||
m_lSizeCur += nLen;
|
||||
}
|
||||
void CStringBuilderA::WriteString(const char* pString, size_t nLen)
|
||||
{
|
||||
AddSize(nLen);
|
||||
WriteStringNoSafe(pString, nLen);
|
||||
}
|
||||
|
||||
void CStringBuilderA::AddCharNoSafe(const char& _c)
|
||||
{
|
||||
*m_pDataCur++ = _c;
|
||||
++m_lSizeCur;
|
||||
}
|
||||
void CStringBuilderA::AddCharSafe(const char& _c)
|
||||
{
|
||||
AddSize(1);
|
||||
*m_pDataCur++ = _c;
|
||||
++m_lSizeCur;
|
||||
}
|
||||
|
||||
size_t CStringBuilderA::GetCurSize()
|
||||
{
|
||||
return m_lSizeCur;
|
||||
}
|
||||
void CStringBuilderA::SetCurSize(size_t lCurSize)
|
||||
{
|
||||
m_lSizeCur = lCurSize;
|
||||
m_pDataCur = m_pData + m_lSizeCur;
|
||||
}
|
||||
size_t CStringBuilderA::GetSize()
|
||||
{
|
||||
return m_lSize;
|
||||
}
|
||||
|
||||
void CStringBuilderA::Clear()
|
||||
{
|
||||
if (NULL != m_pData)
|
||||
free(m_pData);
|
||||
m_pData = NULL;
|
||||
|
||||
m_pData = NULL;
|
||||
m_lSize = 0;
|
||||
|
||||
m_pDataCur = m_pData;
|
||||
m_lSizeCur = 0;
|
||||
}
|
||||
void CStringBuilderA::ClearNoAttack()
|
||||
{
|
||||
m_pDataCur = m_pData;
|
||||
m_lSizeCur = 0;
|
||||
}
|
||||
|
||||
std::string CStringBuilderA::GetData()
|
||||
{
|
||||
std::string str(m_pData, (int)m_lSizeCur);
|
||||
return str;
|
||||
}
|
||||
char* CStringBuilderA::GetBuffer()
|
||||
{
|
||||
return m_pData;
|
||||
}
|
||||
}
|
||||
|
||||
namespace NSStringUtils
|
||||
{
|
||||
CStringBuilder::CStringBuilder()
|
||||
{
|
||||
m_pData = NULL;
|
||||
m_lSize = 0;
|
||||
|
||||
m_pDataCur = m_pData;
|
||||
m_lSizeCur = m_lSize;
|
||||
}
|
||||
CStringBuilder::~CStringBuilder()
|
||||
{
|
||||
if (NULL != m_pData)
|
||||
free(m_pData);
|
||||
m_pData = NULL;
|
||||
}
|
||||
|
||||
void CStringBuilder::AddSize(size_t nSize)
|
||||
{
|
||||
if (NULL == m_pData)
|
||||
{
|
||||
m_lSize = (std::max)((int)nSize, 1000);
|
||||
m_pData = (wchar_t*)malloc(m_lSize * sizeof(wchar_t));
|
||||
|
||||
m_lSizeCur = 0;
|
||||
m_pDataCur = m_pData;
|
||||
return;
|
||||
}
|
||||
|
||||
if ((m_lSizeCur + nSize) > m_lSize)
|
||||
{
|
||||
while ((m_lSizeCur + nSize) > m_lSize)
|
||||
{
|
||||
m_lSize *= 2;
|
||||
}
|
||||
|
||||
wchar_t* pRealloc = (wchar_t*)realloc(m_pData, m_lSize * sizeof(wchar_t));
|
||||
if (NULL != pRealloc)
|
||||
{
|
||||
// реаллок сработал
|
||||
m_pData = pRealloc;
|
||||
m_pDataCur = m_pData + m_lSizeCur;
|
||||
}
|
||||
else
|
||||
{
|
||||
wchar_t* pMalloc = (wchar_t*)malloc(m_lSize * sizeof(wchar_t));
|
||||
memcpy(pMalloc, m_pData, m_lSizeCur * sizeof(wchar_t));
|
||||
|
||||
free(m_pData);
|
||||
m_pData = pMalloc;
|
||||
m_pDataCur = m_pData + m_lSizeCur;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CStringBuilder::SetText(const std::wstring& bsText)
|
||||
{
|
||||
ClearNoAttack();
|
||||
WriteString(bsText);
|
||||
|
||||
for (size_t i = 0; i < m_lSizeCur; ++i)
|
||||
{
|
||||
if (WCHAR(8233) == m_pData[i])
|
||||
m_pData[i] = WCHAR(' ');
|
||||
}
|
||||
}
|
||||
|
||||
void CStringBuilder::operator+=(const std::wstring& oTemp)
|
||||
{
|
||||
WriteString(oTemp.c_str(), oTemp.length());
|
||||
}
|
||||
|
||||
void CStringBuilder::WriteStringNoSafe(const wchar_t* pString, size_t nLen)
|
||||
{
|
||||
memcpy(m_pDataCur, pString, nLen * sizeof(wchar_t));
|
||||
|
||||
m_pDataCur += nLen;
|
||||
m_lSizeCur += nLen;
|
||||
}
|
||||
void CStringBuilder::WriteString(const wchar_t* pString, size_t nLen)
|
||||
{
|
||||
if (-1 == nLen)
|
||||
{
|
||||
nLen = wcslen(pString);
|
||||
}
|
||||
AddSize(nLen);
|
||||
WriteStringNoSafe(pString, nLen);
|
||||
}
|
||||
|
||||
void CStringBuilder::WriteString(const std::wstring& sString)
|
||||
{
|
||||
this->WriteString(sString.c_str(), sString.length());
|
||||
}
|
||||
|
||||
void CStringBuilder::AddCharNoSafe(const wchar_t& _c)
|
||||
{
|
||||
*m_pDataCur++ = _c;
|
||||
++m_lSizeCur;
|
||||
}
|
||||
void CStringBuilder::AddCharNoCheck(const wchar_t& _c)
|
||||
{
|
||||
*m_pDataCur++ = _c;
|
||||
++m_lSizeCur;
|
||||
}
|
||||
void CStringBuilder::AddSpaceNoCheck()
|
||||
{
|
||||
*m_pDataCur++ = ' ';
|
||||
++m_lSizeCur;
|
||||
}
|
||||
void CStringBuilder::AddCharSafe(const wchar_t& _c)
|
||||
{
|
||||
AddSize(1);
|
||||
*m_pDataCur++ = _c;
|
||||
++m_lSizeCur;
|
||||
}
|
||||
void CStringBuilder::AddChar2Safe(const wchar_t _c1, const wchar_t& _c2)
|
||||
{
|
||||
AddSize(2);
|
||||
*m_pDataCur++ = _c1;
|
||||
*m_pDataCur++ = _c2;
|
||||
m_lSizeCur += 2;
|
||||
}
|
||||
|
||||
void CStringBuilder::WriteEncodeXmlString(const std::wstring& sString)
|
||||
{
|
||||
WriteEncodeXmlString(sString.c_str(), (int)sString.length());
|
||||
}
|
||||
|
||||
void CStringBuilder::WriteEncodeXmlString(const wchar_t* pString, int nCount)
|
||||
{
|
||||
const wchar_t* pData = pString;
|
||||
int nCounter = 0;
|
||||
while (*pData != 0)
|
||||
{
|
||||
BYTE _code = CheckCode(*pData);
|
||||
|
||||
switch (_code)
|
||||
{
|
||||
case 1:
|
||||
AddCharSafe(*pData);
|
||||
break;
|
||||
case 0:
|
||||
AddCharSafe((wchar_t)' ');
|
||||
break;
|
||||
case 2:
|
||||
AddSize(5);
|
||||
*m_pDataCur++ = (wchar_t)('&');
|
||||
*m_pDataCur++ = (wchar_t)('a');
|
||||
*m_pDataCur++ = (wchar_t)('m');
|
||||
*m_pDataCur++ = (wchar_t)('p');
|
||||
*m_pDataCur++ = (wchar_t)(';');
|
||||
m_lSizeCur += 5;
|
||||
break;
|
||||
case 3:
|
||||
AddSize(6);
|
||||
*m_pDataCur++ = (wchar_t)('&');
|
||||
*m_pDataCur++ = (wchar_t)('a');
|
||||
*m_pDataCur++ = (wchar_t)('p');
|
||||
*m_pDataCur++ = (wchar_t)('o');
|
||||
*m_pDataCur++ = (wchar_t)('s');
|
||||
*m_pDataCur++ = (wchar_t)(';');
|
||||
m_lSizeCur += 6;
|
||||
break;
|
||||
case 4:
|
||||
AddSize(4);
|
||||
*m_pDataCur++ = (wchar_t)('&');
|
||||
*m_pDataCur++ = (wchar_t)('l');
|
||||
*m_pDataCur++ = (wchar_t)('t');
|
||||
*m_pDataCur++ = (wchar_t)(';');
|
||||
m_lSizeCur += 4;
|
||||
break;
|
||||
case 5:
|
||||
AddSize(4);
|
||||
*m_pDataCur++ = (wchar_t)('&');
|
||||
*m_pDataCur++ = (wchar_t)('g');
|
||||
*m_pDataCur++ = (wchar_t)('t');
|
||||
*m_pDataCur++ = (wchar_t)(';');
|
||||
m_lSizeCur += 4;
|
||||
break;
|
||||
case 6:
|
||||
AddSize(6);
|
||||
*m_pDataCur++ = (wchar_t)('&');
|
||||
*m_pDataCur++ = (wchar_t)('q');
|
||||
*m_pDataCur++ = (wchar_t)('u');
|
||||
*m_pDataCur++ = (wchar_t)('o');
|
||||
*m_pDataCur++ = (wchar_t)('t');
|
||||
*m_pDataCur++ = (wchar_t)(';');
|
||||
m_lSizeCur += 6;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
++pData;
|
||||
if (-1 != nCount)
|
||||
{
|
||||
++nCounter;
|
||||
if (nCounter == nCount)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t CStringBuilder::GetCurSize()
|
||||
{
|
||||
return m_lSizeCur;
|
||||
}
|
||||
void CStringBuilder::SetCurSize(size_t lCurSize)
|
||||
{
|
||||
m_lSizeCur = lCurSize;
|
||||
m_pDataCur = m_pData + m_lSizeCur;
|
||||
}
|
||||
size_t CStringBuilder::GetSize()
|
||||
{
|
||||
return m_lSize;
|
||||
}
|
||||
|
||||
void CStringBuilder::Write(CStringBuilder& oWriter, const size_t& offset)
|
||||
{
|
||||
WriteString(oWriter.m_pData + offset, oWriter.m_lSizeCur - offset);
|
||||
}
|
||||
|
||||
void CStringBuilder::Clear()
|
||||
{
|
||||
if (NULL != m_pData)
|
||||
free(m_pData);
|
||||
m_pData = NULL;
|
||||
|
||||
m_pData = NULL;
|
||||
m_lSize = 0;
|
||||
|
||||
m_pDataCur = m_pData;
|
||||
m_lSizeCur = 0;
|
||||
}
|
||||
void CStringBuilder::ClearNoAttack()
|
||||
{
|
||||
m_pDataCur = m_pData;
|
||||
m_lSizeCur = 0;
|
||||
}
|
||||
|
||||
std::wstring CStringBuilder::GetData()
|
||||
{
|
||||
std::wstring str(m_pData, (int)m_lSizeCur);
|
||||
return str;
|
||||
}
|
||||
wchar_t* CStringBuilder::GetBuffer()
|
||||
{
|
||||
return m_pData;
|
||||
}
|
||||
|
||||
void CStringBuilder::RemoveLastSpaces()
|
||||
{
|
||||
wchar_t* pMemory = m_pDataCur - 1;
|
||||
while ((pMemory > m_pData) && (wchar_t(' ') == *pMemory))
|
||||
{
|
||||
--pMemory;
|
||||
--m_lSizeCur;
|
||||
--m_pDataCur;
|
||||
}
|
||||
|
||||
}
|
||||
bool CStringBuilder::IsSpace()
|
||||
{
|
||||
if (1 != m_lSizeCur)
|
||||
return false;
|
||||
return (wchar_t(' ') == *m_pData);
|
||||
}
|
||||
|
||||
void CStringBuilder::AddInt(int val)
|
||||
{
|
||||
AddSize(10);
|
||||
AddIntNoCheck(val);
|
||||
}
|
||||
void CStringBuilder::AddIntDel10(int val)
|
||||
{
|
||||
AddSize(11);
|
||||
AddIntNoCheckDel10(val);
|
||||
}
|
||||
void CStringBuilder::AddIntDel100(int val)
|
||||
{
|
||||
AddSize(11);
|
||||
AddIntNoCheckDel100(val);
|
||||
}
|
||||
void CStringBuilder::AddInt64(__int64 val)
|
||||
{
|
||||
//todo AddIntNoCheck
|
||||
WriteString(std::to_wstring(val));
|
||||
}
|
||||
|
||||
void CStringBuilder::AddIntNoCheck(int val)
|
||||
{
|
||||
if (0 == val)
|
||||
{
|
||||
*m_pDataCur++ = (wchar_t)'0';
|
||||
++m_lSizeCur;
|
||||
return;
|
||||
}
|
||||
if (val < 0)
|
||||
{
|
||||
val = -val;
|
||||
*m_pDataCur++ = (wchar_t)'-';
|
||||
++m_lSizeCur;
|
||||
}
|
||||
|
||||
int len = 0;
|
||||
int oval = val;
|
||||
while (oval > 0)
|
||||
{
|
||||
oval /= 10;
|
||||
++len;
|
||||
}
|
||||
|
||||
oval = 1;
|
||||
while (val > 0)
|
||||
{
|
||||
m_pDataCur[len - oval] = (wchar_t)('0' + (val % 10));
|
||||
++oval;
|
||||
val /= 10;
|
||||
}
|
||||
|
||||
m_pDataCur += len;
|
||||
m_lSizeCur += len;
|
||||
}
|
||||
|
||||
void CStringBuilder::AddIntNoCheckDel10(int val)
|
||||
{
|
||||
if (0 == val)
|
||||
{
|
||||
*m_pDataCur++ = (wchar_t)'0';
|
||||
++m_lSizeCur;
|
||||
return;
|
||||
}
|
||||
if (val < 0)
|
||||
{
|
||||
val = -val;
|
||||
*m_pDataCur++ = (wchar_t)'-';
|
||||
++m_lSizeCur;
|
||||
}
|
||||
|
||||
int len = 0;
|
||||
int oval = val;
|
||||
while (oval > 0)
|
||||
{
|
||||
oval /= 10;
|
||||
++len;
|
||||
}
|
||||
|
||||
oval = 1;
|
||||
int nLastS = (val % 10);
|
||||
if (0 != nLastS)
|
||||
{
|
||||
++len;
|
||||
m_pDataCur[len - oval] = (wchar_t)('0' + nLastS);
|
||||
++oval;
|
||||
m_pDataCur[len - oval] = (wchar_t)('.');
|
||||
++oval;
|
||||
val /= 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
--len;
|
||||
val /= 10;
|
||||
}
|
||||
|
||||
while (val > 0)
|
||||
{
|
||||
m_pDataCur[len - oval] = (wchar_t)('0' + (val % 10));
|
||||
++oval;
|
||||
val /= 10;
|
||||
}
|
||||
|
||||
m_pDataCur += len;
|
||||
m_lSizeCur += len;
|
||||
}
|
||||
void CStringBuilder::AddIntNoCheckDel100(int val)
|
||||
{
|
||||
if (0 == val)
|
||||
{
|
||||
*m_pDataCur++ = (wchar_t)'0';
|
||||
++m_lSizeCur;
|
||||
return;
|
||||
}
|
||||
if (val < 0)
|
||||
{
|
||||
val = -val;
|
||||
*m_pDataCur++ = (wchar_t)'-';
|
||||
++m_lSizeCur;
|
||||
}
|
||||
|
||||
int len = 0;
|
||||
int oval = val;
|
||||
while (oval > 0)
|
||||
{
|
||||
oval /= 10;
|
||||
++len;
|
||||
}
|
||||
|
||||
oval = 1;
|
||||
int nLastS = (val % 10);
|
||||
if (0 != nLastS)
|
||||
{
|
||||
++len;
|
||||
m_pDataCur[len - oval] = (wchar_t)('0' + nLastS);
|
||||
++oval;
|
||||
m_pDataCur[len - oval] = (wchar_t)('.');
|
||||
++oval;
|
||||
val /= 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
--len;
|
||||
val /= 10;
|
||||
}
|
||||
|
||||
while (val > 0)
|
||||
{
|
||||
m_pDataCur[len - oval] = (wchar_t)('0' + (val % 10));
|
||||
++oval;
|
||||
val /= 10;
|
||||
}
|
||||
|
||||
m_pDataCur += len;
|
||||
m_lSizeCur += len;
|
||||
}
|
||||
void CStringBuilder::AddDouble(double val, int count)
|
||||
{
|
||||
std::wstring s = std::to_wstring(val);
|
||||
|
||||
if (count != -1)
|
||||
{
|
||||
size_t nSize = s.length();
|
||||
std::wstring::size_type pos1 = s.find(wchar_t('.'));
|
||||
if (pos1 != std::wstring::npos)
|
||||
{
|
||||
size_t nCountNeed = pos1 + 1 + count;
|
||||
if (nCountNeed < nSize)
|
||||
s = s.substr(0, nCountNeed);
|
||||
}
|
||||
std::wstring::size_type pos2 = s.find(wchar_t(','));
|
||||
if (pos2 != std::wstring::npos)
|
||||
{
|
||||
size_t nCountNeed = pos2 + 1 + count;
|
||||
if (nCountNeed < nSize)
|
||||
s = s.substr(0, nCountNeed);
|
||||
}
|
||||
}
|
||||
|
||||
WriteString(s);
|
||||
}
|
||||
|
||||
void CStringBuilder::WriteHexByteNoSafe(const unsigned char& value)
|
||||
{
|
||||
*m_pDataCur++ = g_hex_values[(value >> 4) & 0x0F];
|
||||
++m_lSizeCur;
|
||||
*m_pDataCur++ = g_hex_values[value & 0x0F];
|
||||
++m_lSizeCur;
|
||||
}
|
||||
void CStringBuilder::WriteHexByte(const unsigned char& value)
|
||||
{
|
||||
AddSize(2);
|
||||
WriteHexByteNoSafe(value);
|
||||
}
|
||||
void CStringBuilder::WriteHexInt3(const unsigned int& value)
|
||||
{
|
||||
AddSize(6);
|
||||
WriteHexByteNoSafe((value >> 16) & 0xFF);
|
||||
WriteHexByteNoSafe((value >> 8) & 0xFF);
|
||||
WriteHexByteNoSafe(value & 0xFF);
|
||||
}
|
||||
void CStringBuilder::WriteHexColor3(const unsigned char& r, const unsigned char& g, const unsigned char& b)
|
||||
{
|
||||
AddSize(7);
|
||||
*m_pDataCur++ = (wchar_t)'#';
|
||||
++m_lSizeCur;
|
||||
WriteHexByteNoSafe(r);
|
||||
WriteHexByteNoSafe(g);
|
||||
WriteHexByteNoSafe(b);
|
||||
}
|
||||
void CStringBuilder::WriteHexColor3(const unsigned int& value)
|
||||
{
|
||||
AddSize(7);
|
||||
*m_pDataCur++ = (wchar_t)'#';
|
||||
++m_lSizeCur;
|
||||
WriteHexByteNoSafe(value & 0xFF);
|
||||
WriteHexByteNoSafe((value >> 8) & 0xFF);
|
||||
WriteHexByteNoSafe((value >> 16) & 0xFF);
|
||||
}
|
||||
|
||||
unsigned char CStringBuilder::CheckCode(const wchar_t& c)
|
||||
{
|
||||
if ('&' == c)
|
||||
return 2;
|
||||
if ('\'' == c)
|
||||
return 3;
|
||||
if ('<' == c)
|
||||
return 4;
|
||||
if ('>' == c)
|
||||
return 5;
|
||||
if ('\"' == c)
|
||||
return 6;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void string_replace(std::wstring& text, const std::wstring& replaceFrom, const std::wstring& replaceTo)
|
||||
{
|
||||
size_t posn = 0;
|
||||
while (std::wstring::npos != (posn = text.find(replaceFrom, posn)))
|
||||
{
|
||||
text.replace(posn, replaceFrom.length(), replaceTo);
|
||||
posn += replaceTo.length();
|
||||
}
|
||||
}
|
||||
void string_replaceA(std::string& text, const std::string& replaceFrom, const std::string& replaceTo)
|
||||
{
|
||||
size_t posn = 0;
|
||||
while (std::string::npos != (posn = text.find(replaceFrom, posn)))
|
||||
{
|
||||
text.replace(posn, replaceFrom.length(), replaceTo);
|
||||
posn += replaceTo.length();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -35,14 +35,18 @@
|
||||
#include <string>
|
||||
#include <string.h>
|
||||
#include <algorithm>
|
||||
|
||||
#include "Types.h"
|
||||
|
||||
#ifndef STRINGBUILDER_USE_DYNAMIC_LIBRARY
|
||||
#define STRINGBUILDER_DECL_EXPORT
|
||||
#else
|
||||
#include "./base_export.h"
|
||||
#define STRINGBUILDER_DECL_EXPORT Q_DECL_EXPORT
|
||||
#endif
|
||||
|
||||
namespace NSStringUtils
|
||||
{
|
||||
const wchar_t g_hex_values[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
||||
|
||||
class CStringBuilderA
|
||||
class STRINGBUILDER_DECL_EXPORT CStringBuilderA
|
||||
{
|
||||
private:
|
||||
char* m_pData;
|
||||
@ -52,140 +56,32 @@ namespace NSStringUtils
|
||||
size_t m_lSizeCur;
|
||||
|
||||
public:
|
||||
CStringBuilderA()
|
||||
{
|
||||
m_pData = NULL;
|
||||
m_lSize = 0;
|
||||
|
||||
m_pDataCur = m_pData;
|
||||
m_lSizeCur = m_lSize;
|
||||
}
|
||||
~CStringBuilderA()
|
||||
{
|
||||
if (NULL != m_pData)
|
||||
free(m_pData);
|
||||
m_pData = NULL;
|
||||
}
|
||||
|
||||
inline void AddSize(size_t nSize)
|
||||
{
|
||||
if (NULL == m_pData)
|
||||
{
|
||||
m_lSize = (std::max)((int)nSize, 1000);
|
||||
m_pData = (char*)malloc(m_lSize * sizeof(char));
|
||||
|
||||
m_lSizeCur = 0;
|
||||
m_pDataCur = m_pData;
|
||||
return;
|
||||
}
|
||||
|
||||
if ((m_lSizeCur + nSize) > m_lSize)
|
||||
{
|
||||
while ((m_lSizeCur + nSize) > m_lSize)
|
||||
{
|
||||
m_lSize *= 2;
|
||||
}
|
||||
|
||||
char* pRealloc = (char*)realloc(m_pData, m_lSize * sizeof(char));
|
||||
if (NULL != pRealloc)
|
||||
{
|
||||
// реаллок сработал
|
||||
m_pData = pRealloc;
|
||||
m_pDataCur = m_pData + m_lSizeCur;
|
||||
}
|
||||
else
|
||||
{
|
||||
char* pMalloc = (char*)malloc(m_lSize * sizeof(char));
|
||||
memcpy(pMalloc, m_pData, m_lSizeCur * sizeof(char));
|
||||
|
||||
free(m_pData);
|
||||
m_pData = pMalloc;
|
||||
m_pDataCur = m_pData + m_lSizeCur;
|
||||
}
|
||||
}
|
||||
}
|
||||
CStringBuilderA();
|
||||
~CStringBuilderA();
|
||||
void AddSize(size_t nSize);
|
||||
|
||||
public:
|
||||
void SetText(const std::string& sText);
|
||||
void WriteString(const std::string& str);
|
||||
|
||||
inline void SetText(const std::string& sText)
|
||||
{
|
||||
ClearNoAttack();
|
||||
WriteString(sText);
|
||||
}
|
||||
inline void WriteString(const std::string& str)
|
||||
{
|
||||
WriteString(str.c_str(), str.length());
|
||||
}
|
||||
void WriteStringNoSafe(const char* pString, size_t nLen);
|
||||
void WriteString(const char* pString, size_t nLen);
|
||||
|
||||
inline void WriteStringNoSafe(const char* pString, size_t nLen)
|
||||
{
|
||||
memcpy(m_pDataCur, pString, nLen * sizeof(char));
|
||||
void AddCharNoSafe(const char& _c);
|
||||
void AddCharSafe(const char& _c);
|
||||
|
||||
m_pDataCur += nLen;
|
||||
m_lSizeCur += nLen;
|
||||
}
|
||||
inline void WriteString(const char* pString, size_t nLen)
|
||||
{
|
||||
AddSize(nLen);
|
||||
WriteStringNoSafe(pString, nLen);
|
||||
}
|
||||
size_t GetCurSize();
|
||||
void SetCurSize(size_t lCurSize);
|
||||
size_t GetSize();
|
||||
|
||||
inline void AddCharNoSafe(const char& _c)
|
||||
{
|
||||
*m_pDataCur++ = _c;
|
||||
++m_lSizeCur;
|
||||
}
|
||||
inline void AddCharSafe(const char& _c)
|
||||
{
|
||||
AddSize(1);
|
||||
*m_pDataCur++ = _c;
|
||||
++m_lSizeCur;
|
||||
}
|
||||
void Clear();
|
||||
void ClearNoAttack();
|
||||
|
||||
inline size_t GetCurSize()
|
||||
{
|
||||
return m_lSizeCur;
|
||||
}
|
||||
inline void SetCurSize(size_t lCurSize)
|
||||
{
|
||||
m_lSizeCur = lCurSize;
|
||||
m_pDataCur = m_pData + m_lSizeCur;
|
||||
}
|
||||
inline size_t GetSize()
|
||||
{
|
||||
return m_lSize;
|
||||
}
|
||||
|
||||
inline void Clear()
|
||||
{
|
||||
if (NULL != m_pData)
|
||||
free(m_pData);
|
||||
m_pData = NULL;
|
||||
|
||||
m_pData = NULL;
|
||||
m_lSize = 0;
|
||||
|
||||
m_pDataCur = m_pData;
|
||||
m_lSizeCur = 0;
|
||||
}
|
||||
inline void ClearNoAttack()
|
||||
{
|
||||
m_pDataCur = m_pData;
|
||||
m_lSizeCur = 0;
|
||||
}
|
||||
|
||||
std::string GetData()
|
||||
{
|
||||
std::string str(m_pData, (int)m_lSizeCur);
|
||||
return str;
|
||||
}
|
||||
char* GetBuffer()
|
||||
{
|
||||
return m_pData;
|
||||
}
|
||||
std::string GetData();
|
||||
char* GetBuffer();
|
||||
};
|
||||
|
||||
class CStringBuilder
|
||||
class STRINGBUILDER_DECL_EXPORT CStringBuilder
|
||||
{
|
||||
private:
|
||||
wchar_t* m_pData;
|
||||
@ -195,530 +91,70 @@ namespace NSStringUtils
|
||||
size_t m_lSizeCur;
|
||||
|
||||
public:
|
||||
CStringBuilder()
|
||||
{
|
||||
m_pData = NULL;
|
||||
m_lSize = 0;
|
||||
CStringBuilder();
|
||||
~CStringBuilder();
|
||||
|
||||
m_pDataCur = m_pData;
|
||||
m_lSizeCur = m_lSize;
|
||||
}
|
||||
~CStringBuilder()
|
||||
{
|
||||
if (NULL != m_pData)
|
||||
free(m_pData);
|
||||
m_pData = NULL;
|
||||
}
|
||||
|
||||
inline void AddSize(size_t nSize)
|
||||
{
|
||||
if (NULL == m_pData)
|
||||
{
|
||||
m_lSize = (std::max)((int)nSize, 1000);
|
||||
m_pData = (wchar_t*)malloc(m_lSize * sizeof(wchar_t));
|
||||
|
||||
m_lSizeCur = 0;
|
||||
m_pDataCur = m_pData;
|
||||
return;
|
||||
}
|
||||
|
||||
if ((m_lSizeCur + nSize) > m_lSize)
|
||||
{
|
||||
while ((m_lSizeCur + nSize) > m_lSize)
|
||||
{
|
||||
m_lSize *= 2;
|
||||
}
|
||||
|
||||
wchar_t* pRealloc = (wchar_t*)realloc(m_pData, m_lSize * sizeof(wchar_t));
|
||||
if (NULL != pRealloc)
|
||||
{
|
||||
// реаллок сработал
|
||||
m_pData = pRealloc;
|
||||
m_pDataCur = m_pData + m_lSizeCur;
|
||||
}
|
||||
else
|
||||
{
|
||||
wchar_t* pMalloc = (wchar_t*)malloc(m_lSize * sizeof(wchar_t));
|
||||
memcpy(pMalloc, m_pData, m_lSizeCur * sizeof(wchar_t));
|
||||
|
||||
free(m_pData);
|
||||
m_pData = pMalloc;
|
||||
m_pDataCur = m_pData + m_lSizeCur;
|
||||
}
|
||||
}
|
||||
}
|
||||
void AddSize(size_t nSize);
|
||||
|
||||
public:
|
||||
|
||||
inline void SetText(const std::wstring& bsText)
|
||||
{
|
||||
ClearNoAttack();
|
||||
WriteString(bsText);
|
||||
void SetText(const std::wstring& bsText);
|
||||
|
||||
for (size_t i = 0; i < m_lSizeCur; ++i)
|
||||
{
|
||||
if (WCHAR(8233) == m_pData[i])
|
||||
m_pData[i] = WCHAR(' ');
|
||||
}
|
||||
}
|
||||
void operator+=(const std::wstring& oTemp);
|
||||
|
||||
inline void operator+=(const std::wstring& oTemp)
|
||||
{
|
||||
WriteString(oTemp.c_str(), oTemp.length());
|
||||
}
|
||||
void WriteStringNoSafe(const wchar_t* pString, size_t nLen);
|
||||
void WriteString(const wchar_t* pString, size_t nLen = -1);
|
||||
|
||||
inline void WriteStringNoSafe(const wchar_t* pString, size_t nLen)
|
||||
{
|
||||
memcpy(m_pDataCur, pString, nLen * sizeof(wchar_t));
|
||||
void WriteString(const std::wstring& sString);
|
||||
|
||||
m_pDataCur += nLen;
|
||||
m_lSizeCur += nLen;
|
||||
}
|
||||
inline void WriteString(const wchar_t* pString, size_t nLen = -1)
|
||||
{
|
||||
if (-1 == nLen)
|
||||
{
|
||||
nLen = wcslen(pString);
|
||||
}
|
||||
AddSize(nLen);
|
||||
WriteStringNoSafe(pString, nLen);
|
||||
}
|
||||
void AddCharNoSafe(const wchar_t& _c);
|
||||
void AddCharNoCheck(const wchar_t& _c);
|
||||
void AddSpaceNoCheck();
|
||||
void AddCharSafe(const wchar_t& _c);
|
||||
void AddChar2Safe(const wchar_t _c1, const wchar_t& _c2);
|
||||
|
||||
inline void WriteString(const std::wstring& sString)
|
||||
{
|
||||
this->WriteString(sString.c_str(), sString.length());
|
||||
}
|
||||
void WriteEncodeXmlString(const std::wstring& sString);
|
||||
|
||||
inline void AddCharNoSafe(const wchar_t& _c)
|
||||
{
|
||||
*m_pDataCur++ = _c;
|
||||
++m_lSizeCur;
|
||||
}
|
||||
inline void AddCharNoCheck(const wchar_t& _c)
|
||||
{
|
||||
*m_pDataCur++ = _c;
|
||||
++m_lSizeCur;
|
||||
}
|
||||
inline void AddSpaceNoCheck()
|
||||
{
|
||||
*m_pDataCur++ = ' ';
|
||||
++m_lSizeCur;
|
||||
}
|
||||
inline void AddCharSafe(const wchar_t& _c)
|
||||
{
|
||||
AddSize(1);
|
||||
*m_pDataCur++ = _c;
|
||||
++m_lSizeCur;
|
||||
}
|
||||
inline void AddChar2Safe(const wchar_t _c1, const wchar_t& _c2)
|
||||
{
|
||||
AddSize(2);
|
||||
*m_pDataCur++ = _c1;
|
||||
*m_pDataCur++ = _c2;
|
||||
m_lSizeCur += 2;
|
||||
}
|
||||
void WriteEncodeXmlString(const wchar_t* pString, int nCount = -1);
|
||||
|
||||
inline void WriteEncodeXmlString(const std::wstring& sString)
|
||||
{
|
||||
WriteEncodeXmlString(sString.c_str(), (int)sString.length());
|
||||
}
|
||||
size_t GetCurSize();
|
||||
void SetCurSize(size_t lCurSize);
|
||||
size_t GetSize();
|
||||
|
||||
inline void WriteEncodeXmlString(const wchar_t* pString, int nCount = -1)
|
||||
{
|
||||
const wchar_t* pData = pString;
|
||||
int nCounter = 0;
|
||||
while (*pData != 0)
|
||||
{
|
||||
BYTE _code = CheckCode(*pData);
|
||||
void Write(CStringBuilder& oWriter, const size_t& offset = 0);
|
||||
|
||||
switch (_code)
|
||||
{
|
||||
case 1:
|
||||
AddCharSafe(*pData);
|
||||
break;
|
||||
case 0:
|
||||
AddCharSafe((wchar_t)' ');
|
||||
break;
|
||||
case 2:
|
||||
AddSize(5);
|
||||
*m_pDataCur++ = (wchar_t)('&');
|
||||
*m_pDataCur++ = (wchar_t)('a');
|
||||
*m_pDataCur++ = (wchar_t)('m');
|
||||
*m_pDataCur++ = (wchar_t)('p');
|
||||
*m_pDataCur++ = (wchar_t)(';');
|
||||
m_lSizeCur += 5;
|
||||
break;
|
||||
case 3:
|
||||
AddSize(6);
|
||||
*m_pDataCur++ = (wchar_t)('&');
|
||||
*m_pDataCur++ = (wchar_t)('a');
|
||||
*m_pDataCur++ = (wchar_t)('p');
|
||||
*m_pDataCur++ = (wchar_t)('o');
|
||||
*m_pDataCur++ = (wchar_t)('s');
|
||||
*m_pDataCur++ = (wchar_t)(';');
|
||||
m_lSizeCur += 6;
|
||||
break;
|
||||
case 4:
|
||||
AddSize(4);
|
||||
*m_pDataCur++ = (wchar_t)('&');
|
||||
*m_pDataCur++ = (wchar_t)('l');
|
||||
*m_pDataCur++ = (wchar_t)('t');
|
||||
*m_pDataCur++ = (wchar_t)(';');
|
||||
m_lSizeCur += 4;
|
||||
break;
|
||||
case 5:
|
||||
AddSize(4);
|
||||
*m_pDataCur++ = (wchar_t)('&');
|
||||
*m_pDataCur++ = (wchar_t)('g');
|
||||
*m_pDataCur++ = (wchar_t)('t');
|
||||
*m_pDataCur++ = (wchar_t)(';');
|
||||
m_lSizeCur += 4;
|
||||
break;
|
||||
case 6:
|
||||
AddSize(6);
|
||||
*m_pDataCur++ = (wchar_t)('&');
|
||||
*m_pDataCur++ = (wchar_t)('q');
|
||||
*m_pDataCur++ = (wchar_t)('u');
|
||||
*m_pDataCur++ = (wchar_t)('o');
|
||||
*m_pDataCur++ = (wchar_t)('t');
|
||||
*m_pDataCur++ = (wchar_t)(';');
|
||||
m_lSizeCur += 6;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
void Clear();
|
||||
void ClearNoAttack();
|
||||
|
||||
++pData;
|
||||
if (-1 != nCount)
|
||||
{
|
||||
++nCounter;
|
||||
if (nCounter == nCount)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::wstring GetData();
|
||||
wchar_t* GetBuffer();
|
||||
|
||||
inline size_t GetCurSize()
|
||||
{
|
||||
return m_lSizeCur;
|
||||
}
|
||||
inline void SetCurSize(size_t lCurSize)
|
||||
{
|
||||
m_lSizeCur = lCurSize;
|
||||
m_pDataCur = m_pData + m_lSizeCur;
|
||||
}
|
||||
inline size_t GetSize()
|
||||
{
|
||||
return m_lSize;
|
||||
}
|
||||
void RemoveLastSpaces();
|
||||
bool IsSpace();
|
||||
|
||||
inline void Write(CStringBuilder& oWriter, const size_t& offset = 0)
|
||||
{
|
||||
WriteString(oWriter.m_pData + offset, oWriter.m_lSizeCur - offset);
|
||||
}
|
||||
void AddInt(int val);
|
||||
void AddIntDel10(int val);
|
||||
void AddIntDel100(int val);
|
||||
void AddInt64(__int64 val);
|
||||
|
||||
inline void Clear()
|
||||
{
|
||||
if (NULL != m_pData)
|
||||
free(m_pData);
|
||||
m_pData = NULL;
|
||||
void AddIntNoCheck(int val);
|
||||
|
||||
m_pData = NULL;
|
||||
m_lSize = 0;
|
||||
void AddIntNoCheckDel10(int val);
|
||||
void AddIntNoCheckDel100(int val);
|
||||
void AddDouble(double val, int count);
|
||||
|
||||
m_pDataCur = m_pData;
|
||||
m_lSizeCur = 0;
|
||||
}
|
||||
inline void ClearNoAttack()
|
||||
{
|
||||
m_pDataCur = m_pData;
|
||||
m_lSizeCur = 0;
|
||||
}
|
||||
|
||||
std::wstring GetData()
|
||||
{
|
||||
std::wstring str(m_pData, (int)m_lSizeCur);
|
||||
return str;
|
||||
}
|
||||
wchar_t* GetBuffer()
|
||||
{
|
||||
return m_pData;
|
||||
}
|
||||
|
||||
inline void RemoveLastSpaces()
|
||||
{
|
||||
wchar_t* pMemory = m_pDataCur - 1;
|
||||
while ((pMemory > m_pData) && (wchar_t(' ') == *pMemory))
|
||||
{
|
||||
--pMemory;
|
||||
--m_lSizeCur;
|
||||
--m_pDataCur;
|
||||
}
|
||||
|
||||
}
|
||||
inline bool IsSpace()
|
||||
{
|
||||
if (1 != m_lSizeCur)
|
||||
return false;
|
||||
return (wchar_t(' ') == *m_pData);
|
||||
}
|
||||
|
||||
void AddInt(int val)
|
||||
{
|
||||
AddSize(10);
|
||||
AddIntNoCheck(val);
|
||||
}
|
||||
void AddIntDel10(int val)
|
||||
{
|
||||
AddSize(11);
|
||||
AddIntNoCheckDel10(val);
|
||||
}
|
||||
void AddIntDel100(int val)
|
||||
{
|
||||
AddSize(11);
|
||||
AddIntNoCheckDel100(val);
|
||||
}
|
||||
void AddInt64(__int64 val)
|
||||
{
|
||||
//todo AddIntNoCheck
|
||||
WriteString(std::to_wstring(val));
|
||||
}
|
||||
|
||||
void AddIntNoCheck(int val)
|
||||
{
|
||||
if (0 == val)
|
||||
{
|
||||
*m_pDataCur++ = (wchar_t)'0';
|
||||
++m_lSizeCur;
|
||||
return;
|
||||
}
|
||||
if (val < 0)
|
||||
{
|
||||
val = -val;
|
||||
*m_pDataCur++ = (wchar_t)'-';
|
||||
++m_lSizeCur;
|
||||
}
|
||||
|
||||
int len = 0;
|
||||
int oval = val;
|
||||
while (oval > 0)
|
||||
{
|
||||
oval /= 10;
|
||||
++len;
|
||||
}
|
||||
|
||||
oval = 1;
|
||||
while (val > 0)
|
||||
{
|
||||
m_pDataCur[len - oval] = (wchar_t)('0' + (val % 10));
|
||||
++oval;
|
||||
val /= 10;
|
||||
}
|
||||
|
||||
m_pDataCur += len;
|
||||
m_lSizeCur += len;
|
||||
}
|
||||
|
||||
void AddIntNoCheckDel10(int val)
|
||||
{
|
||||
if (0 == val)
|
||||
{
|
||||
*m_pDataCur++ = (wchar_t)'0';
|
||||
++m_lSizeCur;
|
||||
return;
|
||||
}
|
||||
if (val < 0)
|
||||
{
|
||||
val = -val;
|
||||
*m_pDataCur++ = (wchar_t)'-';
|
||||
++m_lSizeCur;
|
||||
}
|
||||
|
||||
int len = 0;
|
||||
int oval = val;
|
||||
while (oval > 0)
|
||||
{
|
||||
oval /= 10;
|
||||
++len;
|
||||
}
|
||||
|
||||
oval = 1;
|
||||
int nLastS = (val % 10);
|
||||
if (0 != nLastS)
|
||||
{
|
||||
++len;
|
||||
m_pDataCur[len - oval] = (wchar_t)('0' + nLastS);
|
||||
++oval;
|
||||
m_pDataCur[len - oval] = (wchar_t)('.');
|
||||
++oval;
|
||||
val /= 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
--len;
|
||||
val /= 10;
|
||||
}
|
||||
|
||||
while (val > 0)
|
||||
{
|
||||
m_pDataCur[len - oval] = (wchar_t)('0' + (val % 10));
|
||||
++oval;
|
||||
val /= 10;
|
||||
}
|
||||
|
||||
m_pDataCur += len;
|
||||
m_lSizeCur += len;
|
||||
}
|
||||
void AddIntNoCheckDel100(int val)
|
||||
{
|
||||
if (0 == val)
|
||||
{
|
||||
*m_pDataCur++ = (wchar_t)'0';
|
||||
++m_lSizeCur;
|
||||
return;
|
||||
}
|
||||
if (val < 0)
|
||||
{
|
||||
val = -val;
|
||||
*m_pDataCur++ = (wchar_t)'-';
|
||||
++m_lSizeCur;
|
||||
}
|
||||
|
||||
int len = 0;
|
||||
int oval = val;
|
||||
while (oval > 0)
|
||||
{
|
||||
oval /= 10;
|
||||
++len;
|
||||
}
|
||||
|
||||
oval = 1;
|
||||
int nLastS = (val % 10);
|
||||
if (0 != nLastS)
|
||||
{
|
||||
++len;
|
||||
m_pDataCur[len - oval] = (wchar_t)('0' + nLastS);
|
||||
++oval;
|
||||
m_pDataCur[len - oval] = (wchar_t)('.');
|
||||
++oval;
|
||||
val /= 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
--len;
|
||||
val /= 10;
|
||||
}
|
||||
|
||||
while (val > 0)
|
||||
{
|
||||
m_pDataCur[len - oval] = (wchar_t)('0' + (val % 10));
|
||||
++oval;
|
||||
val /= 10;
|
||||
}
|
||||
|
||||
m_pDataCur += len;
|
||||
m_lSizeCur += len;
|
||||
}
|
||||
void AddDouble(double val, int count)
|
||||
{
|
||||
std::wstring s = std::to_wstring(val);
|
||||
|
||||
if (count != -1)
|
||||
{
|
||||
size_t nSize = s.length();
|
||||
std::wstring::size_type pos1 = s.find(wchar_t('.'));
|
||||
if (pos1 != std::wstring::npos)
|
||||
{
|
||||
size_t nCountNeed = pos1 + 1 + count;
|
||||
if (nCountNeed < nSize)
|
||||
s = s.substr(0, nCountNeed);
|
||||
}
|
||||
std::wstring::size_type pos2 = s.find(wchar_t(','));
|
||||
if (pos2 != std::wstring::npos)
|
||||
{
|
||||
size_t nCountNeed = pos2 + 1 + count;
|
||||
if (nCountNeed < nSize)
|
||||
s = s.substr(0, nCountNeed);
|
||||
}
|
||||
}
|
||||
|
||||
WriteString(s);
|
||||
}
|
||||
|
||||
inline void WriteHexByteNoSafe(const unsigned char& value)
|
||||
{
|
||||
*m_pDataCur++ = g_hex_values[(value >> 4) & 0x0F];
|
||||
++m_lSizeCur;
|
||||
*m_pDataCur++ = g_hex_values[value & 0x0F];
|
||||
++m_lSizeCur;
|
||||
}
|
||||
inline void WriteHexByte(const unsigned char& value)
|
||||
{
|
||||
AddSize(2);
|
||||
WriteHexByteNoSafe(value);
|
||||
}
|
||||
inline void WriteHexInt3(const unsigned int& value)
|
||||
{
|
||||
AddSize(6);
|
||||
WriteHexByteNoSafe((value >> 16) & 0xFF);
|
||||
WriteHexByteNoSafe((value >> 8) & 0xFF);
|
||||
WriteHexByteNoSafe(value & 0xFF);
|
||||
}
|
||||
inline void WriteHexColor3(const unsigned char& r, const unsigned char& g, const unsigned char& b)
|
||||
{
|
||||
AddSize(7);
|
||||
*m_pDataCur++ = (wchar_t)'#';
|
||||
++m_lSizeCur;
|
||||
WriteHexByteNoSafe(r);
|
||||
WriteHexByteNoSafe(g);
|
||||
WriteHexByteNoSafe(b);
|
||||
}
|
||||
inline void WriteHexColor3(const unsigned int& value)
|
||||
{
|
||||
AddSize(7);
|
||||
*m_pDataCur++ = (wchar_t)'#';
|
||||
++m_lSizeCur;
|
||||
WriteHexByteNoSafe(value & 0xFF);
|
||||
WriteHexByteNoSafe((value >> 8) & 0xFF);
|
||||
WriteHexByteNoSafe((value >> 16) & 0xFF);
|
||||
}
|
||||
inline void WriteHexByteNoSafe(const unsigned char& value);
|
||||
inline void WriteHexByte(const unsigned char& value);
|
||||
inline void WriteHexInt3(const unsigned int& value);
|
||||
inline void WriteHexColor3(const unsigned char& r, const unsigned char& g, const unsigned char& b);
|
||||
inline void WriteHexColor3(const unsigned int& value);
|
||||
|
||||
protected:
|
||||
inline unsigned char CheckCode(const wchar_t& c)
|
||||
{
|
||||
if ('&' == c)
|
||||
return 2;
|
||||
if ('\'' == c)
|
||||
return 3;
|
||||
if ('<' == c)
|
||||
return 4;
|
||||
if ('>' == c)
|
||||
return 5;
|
||||
if ('\"' == c)
|
||||
return 6;
|
||||
|
||||
return 1;
|
||||
}
|
||||
unsigned char CheckCode(const wchar_t& c);
|
||||
};
|
||||
|
||||
static void string_replace(std::wstring& text, const std::wstring& replaceFrom, const std::wstring& replaceTo)
|
||||
{
|
||||
size_t posn = 0;
|
||||
while (std::wstring::npos != (posn = text.find(replaceFrom, posn)))
|
||||
{
|
||||
text.replace(posn, replaceFrom.length(), replaceTo);
|
||||
posn += replaceTo.length();
|
||||
}
|
||||
}
|
||||
static void string_replaceA(std::string& text, const std::string& replaceFrom, const std::string& replaceTo)
|
||||
{
|
||||
size_t posn = 0;
|
||||
while (std::string::npos != (posn = text.find(replaceFrom, posn)))
|
||||
{
|
||||
text.replace(posn, replaceFrom.length(), replaceTo);
|
||||
posn += replaceTo.length();
|
||||
}
|
||||
}
|
||||
STRINGBUILDER_DECL_EXPORT void string_replace(std::wstring& text, const std::wstring& replaceFrom, const std::wstring& replaceTo);
|
||||
STRINGBUILDER_DECL_EXPORT void string_replaceA(std::string& text, const std::string& replaceFrom, const std::string& replaceTo);
|
||||
}
|
||||
|
||||
#endif // _BUILD_STRING_BUILDER_CROSSPLATFORM_H_
|
||||
|
||||
@ -39,6 +39,7 @@
|
||||
|
||||
#if defined(_WIN32) || defined (_WIN64)
|
||||
#include <direct.h>
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include "OfficeUtilsCommon.h"
|
||||
|
||||
Reference in New Issue
Block a user