Commit cf3cfcef authored by Sergey.Konovalov's avatar Sergey.Konovalov Committed by Alexander Trofimov

Отказ от WinApi функций CopyFile, MoveFile, DeleteFile, GetTempPath,...

Отказ от WinApi функций CopyFile, MoveFile, DeleteFile, GetTempPath, _tsplitpath; SystemUtility/File.h единый класс для всех платформ.


git-svn-id: svn://fileserver/activex/AVS/Sources/TeamlabOffice/trunk/ServerComponents@58969 954022d7-b5bf-4e40-9824-e11837661b57
parent 58633555
...@@ -19,26 +19,44 @@ namespace Writers ...@@ -19,26 +19,44 @@ namespace Writers
nImageCount = 0; nImageCount = 0;
m_sMediaDir = m_sDir + _T("\\word\\media"); m_sMediaDir = m_sDir + _T("\\word\\media");
} }
void AddImage(const CString& sImg) CString AddImageGetNewPath()
{ {
NSDirectory::CreateDirectory(string2std_string(m_sMediaDir)); if( !NSDirectory::Exists(string2std_string(m_sMediaDir)) )
OOX::CSystemUtility::CreateDirectories(m_sMediaDir);
//TCHAR tExt[256];
//TCHAR tFilename[256];
//_tsplitpath( sImg, NULL, NULL, tFilename, tExt );
//CString sExt = CString(tExt);
//CString sFilename = CString(tFilename);
CString sNewImgName;sNewImgName.Format(_T("image%d.jpg"), (nImageCount + 1)); CString sNewImgName;sNewImgName.Format(_T("image%d.jpg"), (nImageCount + 1));
CString sNewImg = m_sMediaDir; CString sNewImg = m_sMediaDir + _T("\\") + sNewImgName;
sNewImg += _T("\\") + sNewImgName;
CopyFile(sImg, sNewImg, FALSE);
m_aImageNames.push_back(sNewImgName);
//CString sNewImgRel;sNewImgRel = _T("media\\") + sNewImgName;
//CorrectString(sNewImgRel);
//m_aImageRels.Add(m_poDocumentRelsWriter->AddRels(_T("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"), sNewImgRel, false));
nImageCount++; nImageCount++;
return sNewImg;
}
void AddImage2(FILE* pFile)
{
long size = ftell(pFile);
if(size > 0)
{
rewind(pFile);
BYTE* pData = new BYTE[size];
DWORD dwSizeRead = (DWORD)fread((void*)pData, 1, size, pFile);
if(dwSizeRead > 0)
{
CString sNewImagePath = AddImageGetNewPath();
NSFile::CFileBinary oFile;
oFile.CreateFileW(string2std_string(sNewImagePath));
oFile.WriteFile(pData, dwSizeRead);
oFile.CloseFile();
CString sFilename = NSSystemPath::GetFileName(string2std_string(sNewImagePath)).c_str();
m_aImageNames.push_back(sFilename);
}
RELEASEARRAYOBJECTS(pData);
}
}
void AddImage(const CString& sImg)
{
CString sNewImg = AddImageGetNewPath();
NSFile::CFileBinary::Copy(string2std_string(sImg), string2std_string(sNewImg));
CString sFilename = NSSystemPath::GetFileName(string2std_string(sNewImg)).c_str();
m_aImageNames.push_back(sFilename);
} }
}; };
} }
......
...@@ -2309,17 +2309,11 @@ public: ...@@ -2309,17 +2309,11 @@ public:
CString sImage(m_oBufferedStream.GetString3(length)); CString sImage(m_oBufferedStream.GetString3(length));
CString sFilePath; CString sFilePath;
bool bDeleteFile = false; bool bDeleteFile = false;
NSFile::CFileBinary oFile;
if(0 == sImage.Find(_T("data:"))) if(0 == sImage.Find(_T("data:")))
{ {
char sTempPath[MAX_PATH], sTempFile[MAX_PATH]; if(oFile.CreateTempFile())
if ( 0 == GetTempPathA( MAX_PATH, sTempPath ) ) SerializeCommon::convertBase64ToImage(oFile, sImage);
return S_FALSE;
if ( 0 == GetTempFileNameA( sTempPath, "CSS", 0, sTempFile ) )
return S_FALSE;
sFilePath = CString(sTempFile);
SerializeCommon::convertBase64ToImage(sFilePath, sImage);
bDeleteFile = true;
} }
else if(0 == sImage.Find(_T("http:")) || 0 == sImage.Find(_T("https:")) || 0 == sImage.Find(_T("ftp:")) || 0 == sImage.Find(_T("www"))) else if(0 == sImage.Find(_T("http:")) || 0 == sImage.Find(_T("https:")) || 0 == sImage.Find(_T("ftp:")) || 0 == sImage.Find(_T("www")))
{ {
...@@ -2334,11 +2328,16 @@ public: ...@@ -2334,11 +2328,16 @@ public:
} }
// //
if(NSFile::CFileBinary::Exists(string2std_string(sFilePath))) FILE* pFileNative = oFile.GetFileNative();
if(NULL != pFileNative)
{
m_oFileWriter.m_oMediaWriter.AddImage2(pFileNative);
}
else if(NSFile::CFileBinary::Exists(string2std_string(sFilePath)))
{ {
m_oFileWriter.m_oMediaWriter.AddImage(sFilePath); m_oFileWriter.m_oMediaWriter.AddImage(sFilePath);
if(bDeleteFile) if(bDeleteFile)
DeleteFile(sFilePath); NSFile::CFileBinary::Remove(string2std_string(sFilePath));
} }
} }
else else
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "../../DesktopEditor/common/Directory.h" #include "../../DesktopEditor/common/Directory.h"
#include "../../DesktopEditor/common/File.h" #include "../../DesktopEditor/common/File.h"
#include "../../DesktopEditor/common/Path.h"
#include "../BinWriter/BinWriters.h" #include "../BinWriter/BinWriters.h"
#include "../BinReader/Readers.h" #include "../BinReader/Readers.h"
#include "../../ASCOfficePPTXFile/Editor/FontPicker.h" #include "../../ASCOfficePPTXFile/Editor/FontPicker.h"
...@@ -166,12 +167,8 @@ bool BinDocxRW::CDocxSerializer::loadFromFile(CString& sSrcFileName, CString& sD ...@@ -166,12 +167,8 @@ bool BinDocxRW::CDocxSerializer::loadFromFile(CString& sSrcFileName, CString& sD
m_pCurFileWriter = new Writers::FileWriter(sDstPath, m_sFontDir, nVersion, m_bSaveChartAsImg, &oDrawingConverter, sThemePath); m_pCurFileWriter = new Writers::FileWriter(sDstPath, m_sFontDir, nVersion, m_bSaveChartAsImg, &oDrawingConverter, sThemePath);
//папка с картинками //папка с картинками
TCHAR tFolder[256]; std::wstring strFileInDir = NSSystemPath::GetDirectoryName(string2std_string(sSrcFileName));
TCHAR tDrive[256]; CString sFileInDir = strFileInDir.c_str();
_tsplitpath( sSrcFileName, tDrive, tFolder, NULL, NULL );
CString sFolder = CString(tFolder);
CString sDrive = CString(tDrive);
CString sFileInDir = sDrive + sFolder;
VARIANT var; VARIANT var;
var.vt = VT_BSTR; var.vt = VT_BSTR;
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "../../DesktopEditor/common/Directory.h" #include "../../DesktopEditor/common/Directory.h"
#include "../../DesktopEditor/common/File.h" #include "../../DesktopEditor/common/File.h"
#include "../../DesktopEditor/common/Path.h"
#include "../../XlsxSerializerCom/Reader/BinaryWriter.h" #include "../../XlsxSerializerCom/Reader/BinaryWriter.h"
#include "../../XlsxSerializerCom/Writer/BinaryReader.h" #include "../../XlsxSerializerCom/Writer/BinaryReader.h"
#include "../../ASCOfficePPTXFile/Editor/FontPicker.h" #include "../../ASCOfficePPTXFile/Editor/FontPicker.h"
...@@ -22,18 +23,19 @@ namespace BinXlsxRW{ ...@@ -22,18 +23,19 @@ namespace BinXlsxRW{
oOfficeDrawingConverter.SetMediaDstPath(sMediaDir); oOfficeDrawingConverter.SetMediaDstPath(sMediaDir);
// //
TCHAR tFolder[256]; std::wstring strFileInDir = NSSystemPath::GetDirectoryName(string2std_string(sSrcFileName));
TCHAR tDrive[256]; CString sFileInDir = strFileInDir.c_str();
_tsplitpath( sSrcFileName, tDrive, tFolder, NULL, NULL );
CString sFolder = CString(tFolder);
CString sDrive = CString(tDrive);
CString sFileInDir = sDrive + sFolder;
VARIANT var; VARIANT var;
var.vt = VT_BSTR; var.vt = VT_BSTR;
#ifdef _WIN32
var.bstrVal = sFileInDir.AllocSysString(); var.bstrVal = sFileInDir.AllocSysString();
oOfficeDrawingConverter.SetAdditionalParam(CString(L"SourceFileDir2"), var); oOfficeDrawingConverter.SetAdditionalParam(CString(L"SourceFileDir2"), var);
RELEASESYSSTRING(var.bstrVal); RELEASESYSSTRING(var.bstrVal);
#else
var.bstrVal = sFileInDir.GetString();
oOfficeDrawingConverter.SetAdditionalParam(CString(L"SourceFileDir2"), var);
#endif
BinXlsxRW::BinaryFileReader oBinaryFileReader; BinXlsxRW::BinaryFileReader oBinaryFileReader;
oBinaryFileReader.ReadFile(sSrcFileName, sDstPath, &oOfficeDrawingConverter, sXMLOptions); oBinaryFileReader.ReadFile(sSrcFileName, sDstPath, &oOfficeDrawingConverter, sXMLOptions);
...@@ -127,17 +129,12 @@ namespace BinXlsxRW{ ...@@ -127,17 +129,12 @@ namespace BinXlsxRW{
if(oChartSpace.isValid()) if(oChartSpace.isValid())
{ {
TCHAR tDrive[256]; std::wstring strFilepath = string2std_string(sFilepath);
TCHAR tFolder[256]; std::wstring strDir = NSSystemPath::GetDirectoryName(strFilepath);
TCHAR tFilename[256]; std::wstring strFilename = NSSystemPath::GetFileName(strFilepath);
TCHAR tExt[256];
_tsplitpath( sFilepath, tDrive, tFolder, tFilename, tExt ); CString sRelsDir = strDir.c_str();
CString sDrive(tDrive); CString sFilename = strFilename.c_str();
CString sFolder(tFolder);
CString sFilename(tFilename);
CString sExt(tExt);
CString sRelsDir = sDrive + sFolder;
sRelsDir.Append(_T("_rels")); sRelsDir.Append(_T("_rels"));
if( !NSDirectory::Exists(string2std_string(sRelsDir)) ) if( !NSDirectory::Exists(string2std_string(sRelsDir)) )
OOX::CSystemUtility::CreateDirectories(sRelsDir); OOX::CSystemUtility::CreateDirectories(sRelsDir);
...@@ -145,13 +142,12 @@ namespace BinXlsxRW{ ...@@ -145,13 +142,12 @@ namespace BinXlsxRW{
oChartSpace.write2(sFilepath); oChartSpace.write2(sFilepath);
CString sRelsPath; CString sRelsPath;
sRelsPath.Format(_T("%s\\%s.rels"), sRelsDir, sFilename + sExt); sRelsPath.Format(_T("%s\\%s.rels"), sRelsDir, sFilename);
m_pExternalDrawingConverter->SaveDstContentRels(sRelsPath); m_pExternalDrawingConverter->SaveDstContentRels(sRelsPath);
CString sContentType(sContentTypePath); CString sContentType(sContentTypePath);
sContentType.Append(sFilename); sContentType.Append(sFilename);
sContentType.Append(sExt);
(*sContentTypeElement) = new CString(); (*sContentTypeElement) = new CString();
(*sContentTypeElement)->Format(_T("<Override PartName=\"%s\" ContentType=\"application/vnd.openxmlformats-officedocument.drawingml.chart+xml\"/>"), sContentType); (*sContentTypeElement)->Format(_T("<Override PartName=\"%s\" ContentType=\"application/vnd.openxmlformats-officedocument.drawingml.chart+xml\"/>"), sContentType);
......
#pragma once #pragma once
#include "../XML/xmlutils.h" #include "../XML/xmlutils.h"
#include "../../../../DesktopEditor/common/File.h"
#ifdef _WIN32
// /DesktopEditor/common/File.h CString
class CFile class CFile
{ {
public: private:
CFile() HRESULT _Open(const CString& strFileName, bool bOpen = false, bool bCreate = false, bool bReadWrite = false)
{ {
m_hFileHandle = NULL; HRESULT hRes = S_OK;
m_lFileSize = 0; CloseFile();
m_lFilePosition = 0;
}
virtual ~CFile() #if defined(WIN32) || defined(_WIN32_WCE)
wchar_t* pModeOpen;
wchar_t* pModeCreate;
if(bReadWrite)
{ {
CloseFile(); pModeOpen = L"rb+";
pModeCreate = L"wb+";
} }
HRESULT OpenOrCreate(CString strFileName)
{
CloseFile();
HRESULT hRes = S_OK;
DWORD AccessMode = GENERIC_READ | GENERIC_WRITE;
DWORD ShareMode = FILE_SHARE_WRITE;
DWORD Disposition = OPEN_ALWAYS;
m_hFileHandle = ::CreateFile(strFileName, AccessMode, ShareMode, NULL, Disposition, FILE_ATTRIBUTE_NORMAL, NULL);
if (NULL == m_hFileHandle || INVALID_HANDLE_VALUE == m_hFileHandle)
hRes = S_FALSE;
else else
{ {
ULARGE_INTEGER nTempSize; pModeOpen = L"rb";
nTempSize.LowPart = ::GetFileSize(m_hFileHandle, &nTempSize.HighPart); pModeCreate = L"wb";
m_lFileSize = nTempSize.QuadPart;
SetPosition(m_lFileSize);
} }
if(NULL == m_pFile && bOpen)
return hRes; m_pFile = _wfopen(strFileName, pModeOpen);
if(NULL == m_pFile && bCreate)
m_pFile = _wfopen(strFileName, pModeCreate);
#else
BYTE* pUtf8 = NULL;
LONG lLen = 0;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strFileName.c_str(), strFileName.length(), pUtf8, lLen, false);
char* pModeOpen;
char* pModeCreate;
if(bReadWrite)
{
pModeOpen = "rb+";
pModeCreate = "wb+";
} }
virtual HRESULT OpenFile(CString FileName) else
{ {
CloseFile(); pModeOpen = "rb";
pModeCreate = "wb";
}
if(NULL == m_pFile && bOpen)
m_pFile = fopen((char*)pUtf8, pModeOpen);
if(NULL == m_pFile && bCreate)
m_pFile = fopen((char*)pUtf8, pModeCreate);
RELEASEARRAYOBJECTS(pUtf8);
#endif
if (NULL == m_pFile)
return S_FALSE;
HRESULT hRes = S_OK; fseek(m_pFile, 0, SEEK_END);
DWORD AccessMode = GENERIC_READ; m_lFileSize = ftell(m_pFile);
DWORD ShareMode = FILE_SHARE_READ; fseek(m_pFile, 0, SEEK_SET);
DWORD Disposition = OPEN_EXISTING;
m_hFileHandle = ::CreateFile(FileName, AccessMode, ShareMode, NULL, Disposition, FILE_ATTRIBUTE_NORMAL, NULL);
if (NULL == m_hFileHandle || INVALID_HANDLE_VALUE == m_hFileHandle) m_lFilePosition = 0;
hRes = S_FALSE;
else if (0 < strFileName.GetLength())
{ {
ULARGE_INTEGER nTempSize; if (((wchar_t)'/') == strFileName[strFileName.GetLength() - 1])
nTempSize.LowPart = ::GetFileSize(m_hFileHandle, &nTempSize.HighPart); m_lFileSize = 0x7FFFFFFF;
m_lFileSize = nTempSize.QuadPart; }
SetPosition(0); unsigned int err = 0x7FFFFFFF;
unsigned int cur = (unsigned int)m_lFileSize;
if (err == cur)
{
CloseFile();
return S_FALSE;
} }
return hRes; return hRes;
} }
public:
CFile()
{
m_pFile = NULL;
m_lFilePosition = 0;
m_lFileSize = 0;
}
virtual HRESULT OpenFileRW(CString FileName) virtual ~CFile()
{ {
CloseFile(); CloseFile();
}
HRESULT hRes = S_OK; HRESULT OpenOrCreate(CString strFileName, bool bOnlyOpen = false, bool bReadWrite = false)
DWORD AccessMode = GENERIC_READ | GENERIC_WRITE;
DWORD ShareMode = FILE_SHARE_READ;
DWORD Disposition = OPEN_EXISTING;
m_hFileHandle = ::CreateFile(FileName, AccessMode, ShareMode, NULL, Disposition, 0, 0);
if (NULL == m_hFileHandle || INVALID_HANDLE_VALUE == m_hFileHandle)
{ {
hRes = S_FALSE; return _Open(strFileName, true, true, true);
} }
else virtual HRESULT OpenFile(CString FileName)
{ {
ULARGE_INTEGER nTempSize; return _Open(FileName, true, false, false);
nTempSize.LowPart = ::GetFileSize(m_hFileHandle, &nTempSize.HighPart);
m_lFileSize = nTempSize.QuadPart;
SetPosition(0);
} }
return hRes; virtual HRESULT OpenFileRW(CString FileName)
{
return _Open(FileName, true, false, true);
} }
HRESULT ReadFile(BYTE* pData, DWORD nBytesToRead) HRESULT ReadFile(BYTE* pData, DWORD nBytesToRead)
{ {
DWORD nBytesRead = 0; if (!m_pFile)
if(NULL == pData)
return S_FALSE; return S_FALSE;
if(m_hFileHandle && (pData))
{
SetPosition(m_lFilePosition); SetPosition(m_lFilePosition);
::ReadFile(m_hFileHandle, pData, nBytesToRead, &nBytesRead, NULL); DWORD dwSizeRead = (DWORD)fread((void*)pData, 1, nBytesToRead, m_pFile);
m_lFilePosition += nBytesRead; m_lFilePosition += dwSizeRead;
}
return S_OK; return S_OK;
} }
HRESULT ReadFile2(BYTE* pData, DWORD nBytesToRead) HRESULT ReadFile2(BYTE* pData, DWORD nBytesToRead)
{ {
DWORD nBytesRead = 0; HRESULT hRes = ReadFile(pData, nBytesToRead);
if(NULL == pData) //reverse bytes
return S_FALSE;
if(m_hFileHandle && (pData))
{
SetPosition(m_lFilePosition);
::ReadFile(m_hFileHandle, pData, nBytesToRead, &nBytesRead, NULL);
m_lFilePosition += nBytesRead;
for (size_t index = 0; index < nBytesToRead / 2; ++index) for (size_t index = 0; index < nBytesToRead / 2; ++index)
{ {
BYTE temp = pData[index]; BYTE temp = pData[index];
pData[index] = pData[nBytesToRead - index - 1]; pData[index] = pData[nBytesToRead - index - 1];
pData[nBytesToRead - index - 1] = temp; pData[nBytesToRead - index - 1] = temp;
} }
}
return S_OK; return S_OK;
} }
HRESULT ReadFile3(void* pData, DWORD nBytesToRead) HRESULT ReadFile3(void* pData, DWORD nBytesToRead)
{ {
DWORD nBytesRead = 0; return ReadFile((BYTE*)pData, nBytesToRead);
if(NULL == pData)
return S_FALSE;
if(m_hFileHandle && (pData))
{
SetPosition(m_lFilePosition);
::ReadFile(m_hFileHandle, pData, nBytesToRead, &nBytesRead, NULL);
m_lFilePosition += nBytesRead;
}
return S_OK;
} }
HRESULT WriteFile(void* pData, DWORD nBytesToWrite) HRESULT WriteFile(void* pData, DWORD nBytesToWrite)
{ {
if(m_hFileHandle) if (!m_pFile)
{ return S_FALSE;
DWORD dwWritten = 0;
::WriteFile(m_hFileHandle, pData, nBytesToWrite, &dwWritten, NULL); size_t nCountWrite = fwrite((void*)pData, 1, nBytesToWrite, m_pFile);
m_lFilePosition += nBytesToWrite; m_lFilePosition += nBytesToWrite;
}
return S_OK; return S_OK;
} }
HRESULT WriteFile2(void* pData, DWORD nBytesToWrite) HRESULT WriteFile2(void* pData, DWORD nBytesToWrite)
{ {
if(m_hFileHandle) if (!m_pFile)
{ return S_FALSE;
BYTE* mem = new BYTE[nBytesToWrite]; BYTE* mem = new BYTE[nBytesToWrite];
memcpy(mem, pData, nBytesToWrite); memcpy(mem, pData, nBytesToWrite);
...@@ -166,36 +157,24 @@ public: ...@@ -166,36 +157,24 @@ public:
mem[nBytesToWrite - index - 1] = temp; mem[nBytesToWrite - index - 1] = temp;
} }
DWORD dwWritten = 0; return WriteFile(mem, nBytesToWrite);
::WriteFile(m_hFileHandle, (void*)mem, nBytesToWrite, &dwWritten, NULL);
m_lFilePosition += nBytesToWrite;
RELEASEARRAYOBJECTS(mem);
}
return S_OK;
} }
HRESULT CreateFile(CString strFileName) HRESULT CreateFile(CString strFileName)
{ {
CloseFile(); return _Open(strFileName, false, true, true);
DWORD AccessMode = GENERIC_WRITE;
DWORD ShareMode = FILE_SHARE_WRITE;
DWORD Disposition = CREATE_ALWAYS;
m_hFileHandle = ::CreateFile(strFileName, AccessMode, ShareMode, NULL, Disposition, FILE_ATTRIBUTE_NORMAL, NULL);
return SetPosition(0);
} }
HRESULT SetPosition( ULONG64 nPos ) HRESULT SetPosition( ULONG64 nPos )
{ {
if (m_hFileHandle && nPos <= (ULONG)m_lFileSize) if (m_pFile && nPos <= (ULONG)m_lFileSize)
{ {
LARGE_INTEGER nTempPos;
nTempPos.QuadPart = nPos;
::SetFilePointer(m_hFileHandle, nTempPos.LowPart, &nTempPos.HighPart, FILE_BEGIN);
m_lFilePosition = nPos; m_lFilePosition = nPos;
fseek(m_pFile, m_lFilePosition, SEEK_SET);
return S_OK; return S_OK;
} }
else else
{ {
return (INVALID_HANDLE_VALUE == m_hFileHandle) ? S_FALSE : S_OK; return !m_pFile ? S_FALSE : S_OK;
} }
} }
LONG64 GetPosition() LONG64 GetPosition()
...@@ -209,9 +188,14 @@ public: ...@@ -209,9 +188,14 @@ public:
HRESULT CloseFile() HRESULT CloseFile()
{ {
m_lFileSize = 0;
m_lFilePosition = 0; m_lFilePosition = 0;
RELEASEHANDLE(m_hFileHandle); m_lFileSize = 0;
if (m_pFile != NULL)
{
fclose(m_pFile);
m_pFile = NULL;
}
return S_OK; return S_OK;
} }
...@@ -267,45 +251,26 @@ public: ...@@ -267,45 +251,26 @@ public:
return lProgress; return lProgress;
} }
void WriteStringUTF8(CString& strXml) void WriteStringUTF8(const CString& strXml)
{ {
int nLength = strXml.GetLength(); BYTE* pData = NULL;
LONG lLen = 0;
CStringA saStr;
#ifdef UNICODE
// Encoding Unicode to UTF-8
WideCharToMultiByte(CP_UTF8, 0, strXml.GetBuffer(), nLength + 1, saStr.GetBuffer(nLength*3 + 1), nLength*3, NULL, NULL);
saStr.ReleaseBuffer();
#else
wchar_t* pWStr = new wchar_t[nLength + 1];
if (!pWStr)
return;
// set end string NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strXml.GetString(), strXml.GetLength(), pData, lLen, false);
pWStr[nLength] = 0;
// Encoding ASCII to Unicode WriteFile(pData, lLen);
MultiByteToWideChar(CP_ACP, 0, strXml, nLength, pWStr, nLength);
int nLengthW = (int)wcslen(pWStr); RELEASEARRAYOBJECTS(pData);
// Encoding Unicode to UTF-8
WideCharToMultiByte(CP_UTF8, 0, pWStr, nLengthW + 1, saStr.GetBuffer(nLengthW*3 + 1), nLengthW*3, NULL, NULL);
saStr.ReleaseBuffer();
delete[] pWStr;
#endif
WriteFile((void*)saStr.GetBuffer(), saStr.GetLength());
} }
protected: protected:
HANDLE m_hFileHandle; FILE* m_pFile;
LONG64 m_lFileSize;
LONG64 m_lFilePosition; long m_lFilePosition;
long m_lFileSize;
}; };
#ifdef _WIN32
namespace CDirectory namespace CDirectory
{ {
static CString GetFolderName(CString strFolderPath) static CString GetFolderName(CString strFolderPath)
......
...@@ -3,8 +3,12 @@ ...@@ -3,8 +3,12 @@
#include <stdio.h> #include <stdio.h>
#include <string> #include <string>
#include <fstream>
#include "Array.h" #include "Array.h"
#include "errno.h" #include "errno.h"
#if defined(WIN32) || defined(_WIN32_WCE)
#include <wchar.h>
#endif
namespace NSFile namespace NSFile
{ {
...@@ -230,7 +234,7 @@ namespace NSFile ...@@ -230,7 +234,7 @@ namespace NSFile
{ {
if (NULL == pData) if (NULL == pData)
{ {
pData = new BYTE[6 * lCount + 3]; pData = new BYTE[6 * lCount + 3 + 1];
} }
BYTE* pCodesCur = pData; BYTE* pCodesCur = pData;
...@@ -298,7 +302,7 @@ namespace NSFile ...@@ -298,7 +302,7 @@ namespace NSFile
{ {
if (NULL == pData) if (NULL == pData)
{ {
pData = new BYTE[6 * lCount + 3]; pData = new BYTE[6 * lCount + 3 + 1];
} }
BYTE* pCodesCur = pData; BYTE* pCodesCur = pData;
...@@ -363,6 +367,7 @@ namespace NSFile ...@@ -363,6 +367,7 @@ namespace NSFile
} }
lOutputCount = (LONG)(pCodesCur - pData); lOutputCount = (LONG)(pCodesCur - pData);
*pCodesCur++ = 0;
} }
static void GetUtf8StringFromUnicode(const wchar_t* pUnicodes, LONG lCount, BYTE*& pData, LONG& lOutputCount, bool bIsBOM = false) static void GetUtf8StringFromUnicode(const wchar_t* pUnicodes, LONG lCount, BYTE*& pData, LONG& lOutputCount, bool bIsBOM = false)
...@@ -482,6 +487,15 @@ namespace NSFile ...@@ -482,6 +487,15 @@ namespace NSFile
m_lFilePosition = 0; m_lFilePosition = 0;
return true; return true;
} }
bool CreateTempFile()
{
m_pFile = tmpfile ();
if (NULL == m_pFile)
return false;
m_lFilePosition = 0;
return true;
}
bool ReadFile(BYTE* pData, DWORD nBytesToRead, DWORD& dwSizeRead) bool ReadFile(BYTE* pData, DWORD nBytesToRead, DWORD& dwSizeRead)
{ {
...@@ -537,6 +551,59 @@ namespace NSFile ...@@ -537,6 +551,59 @@ namespace NSFile
else else
return false; return false;
} }
static bool Copy(const std::wstring& strSrc, const std::wstring& strDst)
{
if(strSrc == strDst)
return true;
#if defined(WIN32) || defined(_WIN32_WCE)
std::wifstream src(strSrc.c_str(), std::ios::binary);
std::wofstream dst(strDst.c_str(), std::ios::binary);
#else
BYTE* pUtf8Src = NULL;
LONG lLenSrc = 0;
CUtf8Converter::GetUtf8StringFromUnicode(strSrc.c_str(), strSrc.length(), pUtf8Src, lLenSrc, false);
BYTE* pUtf8Dst = NULL;
LONG lLenDst = 0;
CUtf8Converter::GetUtf8StringFromUnicode(strDst.c_str(), strDst.length(), pUtf8Dst, lLenDst, false);
std::ifstream src((char*)pUtf8Src, std::ios::binary);
std::ofstream dst((char*)pUtf8Dst, std::ios::binary);
delete [] pUtf8Src;
delete [] pUtf8Dst;
#endif
if(src.is_open() && dst.is_open())
{
dst << src.rdbuf();
src.close();
dst.close();
return true;
}
else
return false;
}
static bool Remove(const std::wstring& strFileName)
{
#if defined(WIN32) || defined(_WIN32_WCE)
int nRes = _wremove(strFileName.c_str());
#else
BYTE* pUtf8 = NULL;
LONG lLen = 0;
CUtf8Converter::GetUtf8StringFromUnicode(strFileName.c_str(), strFileName.length(), pUtf8, lLen, false);
int nRes = std::remove((char*)pUtf8);
delete [] pUtf8;
#endif
return 0 == nRes;
}
static bool Move(const std::wstring& strSrc, const std::wstring& strDst)
{
if(strSrc == strDst)
return true;
if(Copy(strSrc, strDst))
if(Remove(strSrc))
return true;
return false;
}
}; };
} }
......
#ifndef _BUILD_PATH_CROSSPLATFORM_H_
#define _BUILD_PATH_CROSSPLATFORM_H_
#include <string>
#include <string.h>
#include "File.h"
#ifdef WIN32
#include <wchar.h>
#elif LINUX
#include <libgen.h>
#elif MAC
#endif
namespace NSSystemPath
{
static std::wstring GetDirectoryName(const std::wstring& strFileName)
{
std::wstring sRes;
#ifdef WIN32
TCHAR tDrive[256];
TCHAR tFolder[256];
_tsplitpath( strFileName.c_str(), tDrive, tFolder, NULL, NULL );
sRes.append(tDrive);
sRes.append(tFolder);
return sRes;
#elif LINUX
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;
#elif MAC
#endif
}
static std::wstring GetFileName(const std::wstring& strFileName)
{
std::wstring sRes;
#ifdef WIN32
TCHAR tFilename[256];
TCHAR tExt[256];
_tsplitpath( strFileName.c_str(), NULL, NULL, tFilename, tExt );
sRes.append(tFilename);
sRes.append(tExt);
return sRes;
#elif LINUX
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;
#elif MAC
#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 + _T("/") + strRight;
else
sRes = strLeft + strRight;
return sRes;
}
}
#endif //_BUILD_PATH_CROSSPLATFORM_H_
...@@ -24,12 +24,7 @@ namespace SerializeCommon ...@@ -24,12 +24,7 @@ namespace SerializeCommon
} }
return strFileName; return strFileName;
} }
VOID convertBase64ToImage (CString sImage, CString &pBase64) VOID convertBase64ToImage (NSFile::CFileBinary& oFile, CString &pBase64)
{
HANDLE hFileWriteHandle;
//
hFileWriteHandle = ::CreateFile (sImage, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (INVALID_HANDLE_VALUE != hFileWriteHandle)
{ {
INT nUTF8Len = WideCharToMultiByte (CP_UTF8, 0, pBase64, pBase64.GetLength (), NULL, NULL, NULL, NULL); INT nUTF8Len = WideCharToMultiByte (CP_UTF8, 0, pBase64, pBase64.GetLength (), NULL, NULL, NULL, NULL);
CHAR *pUTF8String = new CHAR [nUTF8Len + 1]; CHAR *pUTF8String = new CHAR [nUTF8Len + 1];
...@@ -54,13 +49,9 @@ namespace SerializeCommon ...@@ -54,13 +49,9 @@ namespace SerializeCommon
Base64::Base64Decode ((LPCSTR)sUnicode.GetBuffer () + nShift, lFileSize, pBuffer, &nDstLength); Base64::Base64Decode ((LPCSTR)sUnicode.GetBuffer () + nShift, lFileSize, pBuffer, &nDstLength);
// //
DWORD dwBytesWrite = 0; oFile.WriteFile(pBuffer, nDstLength);
::WriteFile (hFileWriteHandle, pBuffer, nDstLength, &dwBytesWrite, 0);
RELEASEARRAYOBJECTS (pBuffer); RELEASEARRAYOBJECTS (pBuffer);
CloseHandle (hFileWriteHandle);
}
} }
long Round(double val) long Round(double val)
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "../../Common/DocxFormat/Source/Base/ASCString.h" #include "../../Common/DocxFormat/Source/Base/ASCString.h"
#endif #endif
#include "../../DesktopEditor/common/File.h"
#include <string> #include <string>
#include <vector> #include <vector>
...@@ -16,7 +17,7 @@ ...@@ -16,7 +17,7 @@
namespace SerializeCommon namespace SerializeCommon
{ {
CString DownloadImage(const CString& strFile); CString DownloadImage(const CString& strFile);
VOID convertBase64ToImage (CString sImage, CString &pBase64); VOID convertBase64ToImage (NSFile::CFileBinary& oFile, CString &pBase64);
long Round(double val); long Round(double val);
CString changeExtention(const CString& sSourcePath, const CString& sTargetExt); CString changeExtention(const CString& sSourcePath, const CString& sTargetExt);
class CommentData class CommentData
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment