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
nImageCount = 0;
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 sNewImg = m_sMediaDir;
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));
CString sNewImg = m_sMediaDir + _T("\\") + sNewImgName;
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:
CString sImage(m_oBufferedStream.GetString3(length));
CString sFilePath;
bool bDeleteFile = false;
NSFile::CFileBinary oFile;
if(0 == sImage.Find(_T("data:")))
{
char sTempPath[MAX_PATH], sTempFile[MAX_PATH];
if ( 0 == GetTempPathA( MAX_PATH, sTempPath ) )
return S_FALSE;
if ( 0 == GetTempFileNameA( sTempPath, "CSS", 0, sTempFile ) )
return S_FALSE;
sFilePath = CString(sTempFile);
SerializeCommon::convertBase64ToImage(sFilePath, sImage);
bDeleteFile = true;
if(oFile.CreateTempFile())
SerializeCommon::convertBase64ToImage(oFile, sImage);
}
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:
}
//
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);
if(bDeleteFile)
DeleteFile(sFilePath);
NSFile::CFileBinary::Remove(string2std_string(sFilePath));
}
}
else
......
......@@ -2,6 +2,7 @@
#include "../../DesktopEditor/common/Directory.h"
#include "../../DesktopEditor/common/File.h"
#include "../../DesktopEditor/common/Path.h"
#include "../BinWriter/BinWriters.h"
#include "../BinReader/Readers.h"
#include "../../ASCOfficePPTXFile/Editor/FontPicker.h"
......@@ -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);
//папка с картинками
TCHAR tFolder[256];
TCHAR tDrive[256];
_tsplitpath( sSrcFileName, tDrive, tFolder, NULL, NULL );
CString sFolder = CString(tFolder);
CString sDrive = CString(tDrive);
CString sFileInDir = sDrive + sFolder;
std::wstring strFileInDir = NSSystemPath::GetDirectoryName(string2std_string(sSrcFileName));
CString sFileInDir = strFileInDir.c_str();
VARIANT var;
var.vt = VT_BSTR;
......
......@@ -2,6 +2,7 @@
#include "../../DesktopEditor/common/Directory.h"
#include "../../DesktopEditor/common/File.h"
#include "../../DesktopEditor/common/Path.h"
#include "../../XlsxSerializerCom/Reader/BinaryWriter.h"
#include "../../XlsxSerializerCom/Writer/BinaryReader.h"
#include "../../ASCOfficePPTXFile/Editor/FontPicker.h"
......@@ -22,18 +23,19 @@ namespace BinXlsxRW{
oOfficeDrawingConverter.SetMediaDstPath(sMediaDir);
//
TCHAR tFolder[256];
TCHAR tDrive[256];
_tsplitpath( sSrcFileName, tDrive, tFolder, NULL, NULL );
CString sFolder = CString(tFolder);
CString sDrive = CString(tDrive);
CString sFileInDir = sDrive + sFolder;
std::wstring strFileInDir = NSSystemPath::GetDirectoryName(string2std_string(sSrcFileName));
CString sFileInDir = strFileInDir.c_str();
VARIANT var;
var.vt = VT_BSTR;
#ifdef _WIN32
var.bstrVal = sFileInDir.AllocSysString();
oOfficeDrawingConverter.SetAdditionalParam(CString(L"SourceFileDir2"), var);
RELEASESYSSTRING(var.bstrVal);
#else
var.bstrVal = sFileInDir.GetString();
oOfficeDrawingConverter.SetAdditionalParam(CString(L"SourceFileDir2"), var);
#endif
BinXlsxRW::BinaryFileReader oBinaryFileReader;
oBinaryFileReader.ReadFile(sSrcFileName, sDstPath, &oOfficeDrawingConverter, sXMLOptions);
......@@ -127,17 +129,12 @@ namespace BinXlsxRW{
if(oChartSpace.isValid())
{
TCHAR tDrive[256];
TCHAR tFolder[256];
TCHAR tFilename[256];
TCHAR tExt[256];
_tsplitpath( sFilepath, tDrive, tFolder, tFilename, tExt );
CString sDrive(tDrive);
CString sFolder(tFolder);
CString sFilename(tFilename);
CString sExt(tExt);
CString sRelsDir = sDrive + sFolder;
std::wstring strFilepath = string2std_string(sFilepath);
std::wstring strDir = NSSystemPath::GetDirectoryName(strFilepath);
std::wstring strFilename = NSSystemPath::GetFileName(strFilepath);
CString sRelsDir = strDir.c_str();
CString sFilename = strFilename.c_str();
sRelsDir.Append(_T("_rels"));
if( !NSDirectory::Exists(string2std_string(sRelsDir)) )
OOX::CSystemUtility::CreateDirectories(sRelsDir);
......@@ -145,13 +142,12 @@ namespace BinXlsxRW{
oChartSpace.write2(sFilepath);
CString sRelsPath;
sRelsPath.Format(_T("%s\\%s.rels"), sRelsDir, sFilename + sExt);
sRelsPath.Format(_T("%s\\%s.rels"), sRelsDir, sFilename);
m_pExternalDrawingConverter->SaveDstContentRels(sRelsPath);
CString sContentType(sContentTypePath);
sContentType.Append(sFilename);
sContentType.Append(sExt);
(*sContentTypeElement) = new CString();
(*sContentTypeElement)->Format(_T("<Override PartName=\"%s\" ContentType=\"application/vnd.openxmlformats-officedocument.drawingml.chart+xml\"/>"), sContentType);
......
#pragma once
#include "../XML/xmlutils.h"
#include "../../../../DesktopEditor/common/File.h"
#ifdef _WIN32
// /DesktopEditor/common/File.h CString
class CFile
{
private:
HRESULT _Open(const CString& strFileName, bool bOpen = false, bool bCreate = false, bool bReadWrite = false)
{
HRESULT hRes = S_OK;
CloseFile();
#if defined(WIN32) || defined(_WIN32_WCE)
wchar_t* pModeOpen;
wchar_t* pModeCreate;
if(bReadWrite)
{
pModeOpen = L"rb+";
pModeCreate = L"wb+";
}
else
{
pModeOpen = L"rb";
pModeCreate = L"wb";
}
if(NULL == m_pFile && bOpen)
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+";
}
else
{
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;
fseek(m_pFile, 0, SEEK_END);
m_lFileSize = ftell(m_pFile);
fseek(m_pFile, 0, SEEK_SET);
m_lFilePosition = 0;
if (0 < strFileName.GetLength())
{
if (((wchar_t)'/') == strFileName[strFileName.GetLength() - 1])
m_lFileSize = 0x7FFFFFFF;
}
unsigned int err = 0x7FFFFFFF;
unsigned int cur = (unsigned int)m_lFileSize;
if (err == cur)
{
CloseFile();
return S_FALSE;
}
return hRes;
}
public:
CFile()
{
m_hFileHandle = NULL;
m_lFileSize = 0;
m_pFile = NULL;
m_lFilePosition = 0;
m_lFileSize = 0;
}
virtual ~CFile()
{
CloseFile();
}
HRESULT OpenOrCreate(CString strFileName)
HRESULT OpenOrCreate(CString strFileName, bool bOnlyOpen = false, bool bReadWrite = false)
{
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
{
ULARGE_INTEGER nTempSize;
nTempSize.LowPart = ::GetFileSize(m_hFileHandle, &nTempSize.HighPart);
m_lFileSize = nTempSize.QuadPart;
SetPosition(m_lFileSize);
}
return hRes;
return _Open(strFileName, true, true, true);
}
virtual HRESULT OpenFile(CString FileName)
{
CloseFile();
HRESULT hRes = S_OK;
DWORD AccessMode = GENERIC_READ;
DWORD ShareMode = FILE_SHARE_READ;
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)
hRes = S_FALSE;
else
{
ULARGE_INTEGER nTempSize;
nTempSize.LowPart = ::GetFileSize(m_hFileHandle, &nTempSize.HighPart);
m_lFileSize = nTempSize.QuadPart;
SetPosition(0);
}
return hRes;
return _Open(FileName, true, false, false);
}
virtual HRESULT OpenFileRW(CString FileName)
{
CloseFile();
HRESULT hRes = S_OK;
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;
}
else
{
ULARGE_INTEGER nTempSize;
nTempSize.LowPart = ::GetFileSize(m_hFileHandle, &nTempSize.HighPart);
m_lFileSize = nTempSize.QuadPart;
SetPosition(0);
}
return hRes;
return _Open(FileName, true, false, true);
}
HRESULT ReadFile(BYTE* pData, DWORD nBytesToRead)
{
DWORD nBytesRead = 0;
if(NULL == pData)
if (!m_pFile)
return S_FALSE;
if(m_hFileHandle && (pData))
{
SetPosition(m_lFilePosition);
::ReadFile(m_hFileHandle, pData, nBytesToRead, &nBytesRead, NULL);
m_lFilePosition += nBytesRead;
}
SetPosition(m_lFilePosition);
DWORD dwSizeRead = (DWORD)fread((void*)pData, 1, nBytesToRead, m_pFile);
m_lFilePosition += dwSizeRead;
return S_OK;
}
HRESULT ReadFile2(BYTE* pData, DWORD nBytesToRead)
{
DWORD nBytesRead = 0;
if(NULL == pData)
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)
{
BYTE temp = pData[index];
pData[index] = pData[nBytesToRead - index - 1];
pData[nBytesToRead - index - 1] = temp;
}
HRESULT hRes = ReadFile(pData, nBytesToRead);
//reverse bytes
for (size_t index = 0; index < nBytesToRead / 2; ++index)
{
BYTE temp = pData[index];
pData[index] = pData[nBytesToRead - index - 1];
pData[nBytesToRead - index - 1] = temp;
}
return S_OK;
}
HRESULT ReadFile3(void* pData, DWORD nBytesToRead)
{
DWORD nBytesRead = 0;
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;
return ReadFile((BYTE*)pData, nBytesToRead);
}
HRESULT WriteFile(void* pData, DWORD nBytesToWrite)
{
if(m_hFileHandle)
{
DWORD dwWritten = 0;
::WriteFile(m_hFileHandle, pData, nBytesToWrite, &dwWritten, NULL);
m_lFilePosition += nBytesToWrite;
}
if (!m_pFile)
return S_FALSE;
size_t nCountWrite = fwrite((void*)pData, 1, nBytesToWrite, m_pFile);
m_lFilePosition += nBytesToWrite;
return S_OK;
}
HRESULT WriteFile2(void* pData, DWORD nBytesToWrite)
{
if(m_hFileHandle)
{
BYTE* mem = new BYTE[nBytesToWrite];
memcpy(mem, pData, nBytesToWrite);
for (size_t index = 0; index < nBytesToWrite / 2; ++index)
{
BYTE temp = mem[index];
mem[index] = mem[nBytesToWrite - index - 1];
mem[nBytesToWrite - index - 1] = temp;
}
DWORD dwWritten = 0;
::WriteFile(m_hFileHandle, (void*)mem, nBytesToWrite, &dwWritten, NULL);
m_lFilePosition += nBytesToWrite;
RELEASEARRAYOBJECTS(mem);
if (!m_pFile)
return S_FALSE;
BYTE* mem = new BYTE[nBytesToWrite];
memcpy(mem, pData, nBytesToWrite);
for (size_t index = 0; index < nBytesToWrite / 2; ++index)
{
BYTE temp = mem[index];
mem[index] = mem[nBytesToWrite - index - 1];
mem[nBytesToWrite - index - 1] = temp;
}
return S_OK;
return WriteFile(mem, nBytesToWrite);
}
HRESULT CreateFile(CString strFileName)
{
CloseFile();
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);
return _Open(strFileName, false, true, true);
}
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;
fseek(m_pFile, m_lFilePosition, SEEK_SET);
return S_OK;
}
else
{
return (INVALID_HANDLE_VALUE == m_hFileHandle) ? S_FALSE : S_OK;
return !m_pFile ? S_FALSE : S_OK;
}
}
LONG64 GetPosition()
......@@ -209,9 +188,14 @@ public:
HRESULT CloseFile()
{
m_lFileSize = 0;
m_lFilePosition = 0;
RELEASEHANDLE(m_hFileHandle);
m_lFileSize = 0;
if (m_pFile != NULL)
{
fclose(m_pFile);
m_pFile = NULL;
}
return S_OK;
}
......@@ -267,45 +251,26 @@ public:
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;
NSFile::CUtf8Converter::GetUtf8StringFromUnicode(strXml.GetString(), strXml.GetLength(), pData, lLen, false);
// set end string
pWStr[nLength] = 0;
WriteFile(pData, lLen);
// Encoding ASCII to Unicode
MultiByteToWideChar(CP_ACP, 0, strXml, nLength, pWStr, nLength);
int nLengthW = (int)wcslen(pWStr);
// 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());
RELEASEARRAYOBJECTS(pData);
}
protected:
HANDLE m_hFileHandle;
LONG64 m_lFileSize;
LONG64 m_lFilePosition;
FILE* m_pFile;
long m_lFilePosition;
long m_lFileSize;
};
#ifdef _WIN32
namespace CDirectory
{
static CString GetFolderName(CString strFolderPath)
......
......@@ -3,8 +3,12 @@
#include <stdio.h>
#include <string>
#include <fstream>
#include "Array.h"
#include "errno.h"
#if defined(WIN32) || defined(_WIN32_WCE)
#include <wchar.h>
#endif
namespace NSFile
{
......@@ -230,7 +234,7 @@ namespace NSFile
{
if (NULL == pData)
{
pData = new BYTE[6 * lCount + 3];
pData = new BYTE[6 * lCount + 3 + 1];
}
BYTE* pCodesCur = pData;
......@@ -298,7 +302,7 @@ namespace NSFile
{
if (NULL == pData)
{
pData = new BYTE[6 * lCount + 3];
pData = new BYTE[6 * lCount + 3 + 1];
}
BYTE* pCodesCur = pData;
......@@ -363,6 +367,7 @@ namespace NSFile
}
lOutputCount = (LONG)(pCodesCur - pData);
*pCodesCur++ = 0;
}
static void GetUtf8StringFromUnicode(const wchar_t* pUnicodes, LONG lCount, BYTE*& pData, LONG& lOutputCount, bool bIsBOM = false)
......@@ -482,6 +487,15 @@ namespace NSFile
m_lFilePosition = 0;
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)
{
......@@ -537,6 +551,59 @@ namespace NSFile
else
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,43 +24,34 @@ namespace SerializeCommon
}
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);
CHAR *pUTF8String = new CHAR [nUTF8Len + 1];
memset(pUTF8String, 0, sizeof (CHAR) * (nUTF8Len + 1));
WideCharToMultiByte (CP_UTF8, 0, pBase64, -1, pUTF8String, nUTF8Len, NULL, NULL);
CStringA sUnicode; sUnicode = pUTF8String;
delete[] pUTF8String;
INT nUTF8Len = WideCharToMultiByte (CP_UTF8, 0, pBase64, pBase64.GetLength (), NULL, NULL, NULL, NULL);
CHAR *pUTF8String = new CHAR [nUTF8Len + 1];
memset(pUTF8String, 0, sizeof (CHAR) * (nUTF8Len + 1));
// "data:image/jpg;base64,"
int nShift = 0;
int nIndex = sUnicode.Find("base64,");
if(-1 != nIndex)
{
nShift = nIndex + 7;
}
//
LONG lFileSize = sUnicode.GetLength () - nShift;
INT nDstLength = lFileSize;
BYTE *pBuffer = new BYTE [lFileSize];
memset(pBuffer, 0, lFileSize);
Base64::Base64Decode ((LPCSTR)sUnicode.GetBuffer () + nShift, lFileSize, pBuffer, &nDstLength);
WideCharToMultiByte (CP_UTF8, 0, pBase64, -1, pUTF8String, nUTF8Len, NULL, NULL);
CStringA sUnicode; sUnicode = pUTF8String;
delete[] pUTF8String;
//
DWORD dwBytesWrite = 0;
::WriteFile (hFileWriteHandle, pBuffer, nDstLength, &dwBytesWrite, 0);
// "data:image/jpg;base64,"
int nShift = 0;
int nIndex = sUnicode.Find("base64,");
if(-1 != nIndex)
{
nShift = nIndex + 7;
}
//
LONG lFileSize = sUnicode.GetLength () - nShift;
INT nDstLength = lFileSize;
BYTE *pBuffer = new BYTE [lFileSize];
memset(pBuffer, 0, lFileSize);
Base64::Base64Decode ((LPCSTR)sUnicode.GetBuffer () + nShift, lFileSize, pBuffer, &nDstLength);
RELEASEARRAYOBJECTS (pBuffer);
//
oFile.WriteFile(pBuffer, nDstLength);
CloseHandle (hFileWriteHandle);
}
RELEASEARRAYOBJECTS (pBuffer);
}
long Round(double val)
......
......@@ -8,6 +8,7 @@
#include "../../Common/DocxFormat/Source/Base/ASCString.h"
#endif
#include "../../DesktopEditor/common/File.h"
#include <string>
#include <vector>
......@@ -16,7 +17,7 @@
namespace SerializeCommon
{
CString DownloadImage(const CString& strFile);
VOID convertBase64ToImage (CString sImage, CString &pBase64);
VOID convertBase64ToImage (NSFile::CFileBinary& oFile, CString &pBase64);
long Round(double val);
CString changeExtention(const CString& sSourcePath, const CString& sTargetExt);
class CommentData
......
......@@ -20,7 +20,6 @@ namespace BinXlsxRW {
CString sPath;
int nIndex;
std::map<OOX::Spreadsheet::CDrawing*, CString> mapDrawings;
bool bNeedCreate;
public:
ImageObject()
{
......@@ -29,7 +28,6 @@ namespace BinXlsxRW {
{
sPath = _sPath;
nIndex = _nIndex;
bNeedCreate = true;
}
};
class Binary_CommonReader2
......@@ -1742,15 +1740,15 @@ namespace BinXlsxRW {
OOX::Spreadsheet::CDrawing* m_pCurDrawing;
const CString& m_sDestinationDir;
const CString m_sMediaDir;
const CString& m_sMediaDir;
SaveParams& m_oSaveParams;
NSBinPptxRW::CDrawingConverter* m_pOfficeDrawingConverter;
public:
BinaryWorksheetsTableReader(NSBinPptxRW::CBinaryFileReader& oBufferedStream, OOX::Spreadsheet::CWorkbook& oWorkbook,
OOX::Spreadsheet::CSharedStrings* pSharedStrings, std::map<CString, OOX::Spreadsheet::CWorksheet*>& mapWorksheets,
std::map<long, ImageObject*>& mapMedia, const CString& sDestinationDir, SaveParams& oSaveParams,
std::map<long, ImageObject*>& mapMedia, const CString& sDestinationDir, const CString& sMediaDir, SaveParams& oSaveParams,
NSBinPptxRW::CDrawingConverter* pOfficeDrawingConverter) : Binary_CommonReader(oBufferedStream), m_oWorkbook(oWorkbook),
m_oBcr2(oBufferedStream), m_mapWorksheets(mapWorksheets), m_mapMedia(mapMedia), m_sDestinationDir(sDestinationDir), m_sMediaDir(m_sDestinationDir + _T("\\xl\\media")), m_oSaveParams(oSaveParams), m_pSharedStrings(pSharedStrings)
m_oBcr2(oBufferedStream), m_mapWorksheets(mapWorksheets), m_mapMedia(mapMedia), m_sDestinationDir(sDestinationDir), m_sMediaDir(sMediaDir), m_oSaveParams(oSaveParams), m_pSharedStrings(pSharedStrings)
{
m_pCurSheet = NULL;
m_pCurWorksheet = NULL;
......@@ -2536,19 +2534,11 @@ namespace BinXlsxRW {
std::map<OOX::Spreadsheet::CDrawing*, CString>::const_iterator pPair = pair->second->mapDrawings.find(m_pCurDrawing);
if(pair->second->mapDrawings.end() == pPair)
{
CString sNewImageName;
sNewImageName.Format(_T("image%d%s"), pair->second->nIndex, OOX::CPath(pair->second->sPath).GetExtention(true));
CString sNewImagePath = m_sMediaDir + _T("\\") + sNewImageName;
if(pair->second->bNeedCreate)
{
pair->second->bNeedCreate = false;
if( !NSDirectory::Exists(string2std_string(m_sMediaDir)) )
OOX::CSystemUtility::CreateDirectories(m_sMediaDir);
::CopyFile(pair->second->sPath, sNewImagePath, FALSE);
}
std::wstring sNewImageName = NSSystemPath::GetFileName(string2std_string(pair->second->sPath));
long rId;
CString sNewImgRel;
sNewImgRel.Format(_T("../media/%s"), sNewImageName);
sNewImgRel.Format(_T("../media/%s"), sNewImageName.c_str());
m_pOfficeDrawingConverter->WriteRels(CString(_T("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image")), sNewImgRel, CString(), &rId);
sRId.Format(_T("rId%d"), rId);
......@@ -2811,15 +2801,15 @@ namespace BinXlsxRW {
class BinaryOtherTableReader : public Binary_CommonReader<BinaryOtherTableReader>
{
std::map<long, ImageObject*>& m_mapMedia;
std::vector<CString>& m_aDeleteFiles;
const CString& m_sFileInDir;
long m_nCurId;
CString m_sCurSrc;
long m_nCurIndex;
SaveParams& m_oSaveParams;
NSBinPptxRW::CDrawingConverter* m_pOfficeDrawingConverter;
const CString& m_sMediaDir;
public:
BinaryOtherTableReader(NSBinPptxRW::CBinaryFileReader& oBufferedStream, std::map<long, ImageObject*>& mapMedia, const CString& sFileInDir, std::vector<CString>& aDeleteFiles, SaveParams& oSaveParams, NSBinPptxRW::CDrawingConverter* pOfficeDrawingConverter):Binary_CommonReader(oBufferedStream), m_mapMedia(mapMedia),m_aDeleteFiles(aDeleteFiles),m_sFileInDir(sFileInDir),m_oSaveParams(oSaveParams),m_pOfficeDrawingConverter(pOfficeDrawingConverter)
BinaryOtherTableReader(NSBinPptxRW::CBinaryFileReader& oBufferedStream, std::map<long, ImageObject*>& mapMedia, const CString& sFileInDir, SaveParams& oSaveParams, NSBinPptxRW::CDrawingConverter* pOfficeDrawingConverter, const CString& sMediaDir):Binary_CommonReader(oBufferedStream), m_mapMedia(mapMedia),m_sFileInDir(sFileInDir),m_oSaveParams(oSaveParams),m_pOfficeDrawingConverter(pOfficeDrawingConverter),m_sMediaDir(sMediaDir)
{
m_nCurId = 0;
m_sCurSrc = _T("");
......@@ -2871,26 +2861,18 @@ namespace BinXlsxRW {
CString sImage = CString(m_oBufferedStream.GetString3(length));
CString sImageSrc;
bool bAddToDelete = false;
NSFile::CFileBinary oFile;
if(0 == sImage.Find(_T("data:")))
{
wchar_t sTempPath[MAX_PATH], sTempFile[MAX_PATH];
if ( 0 == GetTempPath( MAX_PATH, sTempPath ) )
return S_FALSE;
if ( 0 == GetTempFileName( sTempPath, _T("CSS"), 0, sTempFile ) )
return S_FALSE;
CString sNewTempFile = SerializeCommon::changeExtention(CString(sTempFile), CString(_T("jpg")));
::MoveFile(sTempFile, sNewTempFile);
sImageSrc = sNewTempFile;
SerializeCommon::convertBase64ToImage(sImageSrc, sImage);
bAddToDelete = true;
if(oFile.CreateTempFile())
SerializeCommon::convertBase64ToImage(oFile, sImage);
}
else if(0 == sImage.Find(_T("http:")) || 0 == sImage.Find(_T("https:")) || 0 == sImage.Find(_T("ftp:")) || 0 == sImage.Find(_T("www")))
{
//url
sImageSrc = SerializeCommon::DownloadImage(sImage);
CString sNewTempFile = SerializeCommon::changeExtention(sImageSrc, CString(_T("jpg")));
::MoveFile(sImageSrc, sNewTempFile);
NSFile::CFileBinary::Move(string2std_string(sImageSrc), string2std_string(sNewTempFile));
sImageSrc = sNewTempFile;
bAddToDelete = true;
}
......@@ -2908,11 +2890,16 @@ namespace BinXlsxRW {
}
}
//
if(NSFile::CFileBinary::Exists(string2std_string(sImageSrc)))
FILE* pFileNative = oFile.GetFileNative();
if(NULL != pFileNative)
{
ReadMediaItemSaveFileFILE(pFileNative);
}
else if(NSFile::CFileBinary::Exists(string2std_string(sImageSrc)))
{
m_sCurSrc = sImageSrc;
ReadMediaItemSaveFilePath(sImageSrc);
if(bAddToDelete)
m_aDeleteFiles.push_back(sImageSrc);
NSFile::CFileBinary::Remove(string2std_string(sImageSrc));
}
}
else if(c_oSer_OtherType::MediaId == type)
......@@ -2923,6 +2910,43 @@ namespace BinXlsxRW {
res = c_oSerConstants::ReadUnknown;
return res;
};
CString ReadMediaItemSaveFileGetNewPath(const CString& sTempPath)
{
if( !NSDirectory::Exists(string2std_string(m_sMediaDir)) )
OOX::CSystemUtility::CreateDirectories(m_sMediaDir);
CString sNewImageName;
sNewImageName.Format(_T("image%d%s"), m_nCurIndex, OOX::CPath(sTempPath).GetExtention(true));
m_nCurIndex++;
CString sNewImagePath = m_sMediaDir + _T("\\") + sNewImageName;
return sNewImagePath;
}
void ReadMediaItemSaveFileFILE(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 = ReadMediaItemSaveFileGetNewPath(CString(_T("1.jpg")));
NSFile::CFileBinary oFile;
oFile.CreateFileW(string2std_string(sNewImagePath));
oFile.WriteFile(pData, dwSizeRead);
oFile.CloseFile();
m_sCurSrc = sNewImagePath;
}
RELEASEARRAYOBJECTS(pData);
}
}
void ReadMediaItemSaveFilePath(const CString& sTempPath)
{
CString sNewImagePath = ReadMediaItemSaveFileGetNewPath(sTempPath);
NSFile::CFileBinary::Copy(string2std_string(sTempPath), string2std_string(sNewImagePath));
m_sCurSrc = sNewImagePath;
}
};
class BinaryFileReader
{
......@@ -3010,9 +3034,8 @@ namespace BinXlsxRW {
sDstPath += _T("Temp");
OOX::Spreadsheet::CXlsx oXlsx;
std::vector<CString> aDeleteFiles;
SaveParams oSaveParams(sDstPath + _T("\\") + OOX::Spreadsheet::FileTypes::Workbook.DefaultDirectory().GetPath() + _T("\\") + OOX::FileTypes::Theme.DefaultDirectory().GetPath());
ReadMainTable(oXlsx, oBufferedStream, OOX::CPath(sSrcFileName).GetDirectory(), sDstPath, aDeleteFiles, oSaveParams, pOfficeDrawingConverter);
ReadMainTable(oXlsx, oBufferedStream, OOX::CPath(sSrcFileName).GetDirectory(), sDstPath, oSaveParams, pOfficeDrawingConverter);
CString sAdditionalContentTypes = oSaveParams.sAdditionalContentTypes;
if(NULL != pOfficeDrawingConverter)
{
......@@ -3033,17 +3056,13 @@ namespace BinXlsxRW {
oXlsx.Write(sDstPath, sAdditionalContentTypes);
break;
}
//
for(int i = 0, length = aDeleteFiles.size(); i < length; ++i)
DeleteFile(aDeleteFiles[i]);
bResultOk = true;
}
}
}
return S_OK;
}
int ReadMainTable(OOX::Spreadsheet::CXlsx& oXlsx, NSBinPptxRW::CBinaryFileReader& oBufferedStream, const CString& sFileInDir, const CString& sOutDir, std::vector<CString>& aDeleteFiles, SaveParams& oSaveParams, NSBinPptxRW::CDrawingConverter* pOfficeDrawingConverter)
int ReadMainTable(OOX::Spreadsheet::CXlsx& oXlsx, NSBinPptxRW::CBinaryFileReader& oBufferedStream, const CString& sFileInDir, const CString& sOutDir, SaveParams& oSaveParams, NSBinPptxRW::CDrawingConverter* pOfficeDrawingConverter)
{
long res = c_oSerConstants::ReadOk;
//mtLen
......@@ -3074,11 +3093,12 @@ namespace BinXlsxRW {
aOffBits.push_back(mtiOffBits);
}
}
CString sMediaDir = sOutDir + _T("\\xl\\media");
std::map<long, ImageObject*> mapMedia;
if(-1 != nOtherOffBits)
{
oBufferedStream.Seek(nOtherOffBits);
res = BinaryOtherTableReader(oBufferedStream, mapMedia, sFileInDir, aDeleteFiles, oSaveParams, pOfficeDrawingConverter).Read();
res = BinaryOtherTableReader(oBufferedStream, mapMedia, sFileInDir, oSaveParams, pOfficeDrawingConverter, sMediaDir).Read();
if(c_oSerConstants::ReadOk != res)
return res;
}
......@@ -3114,7 +3134,7 @@ namespace BinXlsxRW {
break;
case c_oSerTableTypes::Worksheets:
{
res = BinaryWorksheetsTableReader(oBufferedStream, *pWorkbook, pSharedStrings, oXlsx.GetWorksheets(), mapMedia, sOutDir, oSaveParams, pOfficeDrawingConverter).Read();
res = BinaryWorksheetsTableReader(oBufferedStream, *pWorkbook, pSharedStrings, oXlsx.GetWorksheets(), mapMedia, sOutDir, sMediaDir, oSaveParams, pOfficeDrawingConverter).Read();
}
break;
}
......
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