#include "stdafx.h" #include "FileTransformer.h" #include "SigmaDoc.h" static LPCTSTR CopyStr(LPCTSTR sourceStr) { size_t len = strlen(sourceStr); if (len <= 0) return NULL; len++; LPCTSTR dest = (LPCTSTR)malloc(len); memset((void *)dest, 0, len); memcpy((void *)dest, sourceStr, len - 1); return dest; } FileTransformationInfo * FileTransformationInfo::Copy() { FileTransformationInfo * info = new FileTransformationInfo(); info->fileNamesSelected = CopyStr(fileNamesSelected); info->kindOfoutputDirectory = kindOfoutputDirectory; info->outputDirectorySpecified = CopyStr(outputDirectorySpecified); info->format = format; info->bMark = bMark; info->bClassification = bClassification; return info; } FileTransformer::FileTransformer(CSigmaDoc * pDoc) :m_fileTransInfo(nullptr), m_stopTransforation(false), m_indexOfFileByTransformed(-1), m_bClearNoUseMark(false), m_bClearNoUseLayer(false), m_pDoc(pDoc) { } void FileTransformer::SetFileTransInfo(FileTransformationInfo * fileTransInfo) { m_fileTransInfo = fileTransInfo; //解析出文件列表 GetFileNamesByTransfored(); } FileTransformer::RESULT FileTransformer::Execute() { m_stopTransforation = false; m_indexOfFileByTransformed = -1; if (m_fileTransInfo == nullptr) { return RESULT_INNER_OBJECT_NULL; } int nOutMode = m_fileTransInfo->kindOfoutputDirectory;// pSheet->pageFileSelect.m_nOutMode; //CString strPath = pSheet->pageFileSelect.m_strPath; CString strPath; if (m_fileTransInfo->kindOfoutputDirectory == 0) { strPath = ""; } else if (m_fileTransInfo->kindOfoutputDirectory == 1){ if (m_fileTransInfo->outputDirectorySpecified == nullptr) return RESULT_OUTPUT_DIRECTORY_NULL; strPath = m_fileTransInfo->outputDirectorySpecified; } else { return RESULT_OUTPUT_VALUE_ERROR; //记录异常 } if (nOutMode == 1 && strPath.IsEmpty()) { m_indexOfFileByTransformed = 0; //::AfxMessageBox("Path is empty"); return RESULT_OUTPUT_OUTPUT_PATH_ERROR; } //int total = (int)pSheet->pageFileSelect.m_listFile.GetCount(); int total = m_fileNamesSelected.GetCount(); int count = 0; if (total == 0) { //::AfxMessageBox("Please select files"); m_indexOfFileByTransformed = -2; return RESULT_OUTPUT_INPUT_FILES_EMPTY; } //m_bConversion = TRUE; //EnableOther(FALSE); //CWnd* pWndInfo = GetDlgItem(IDC_STATIC_PROGRESS); CString str; CString name; //POSITION pos = pSheet->pageFileSelect.m_listFile.GetHeadPosition(); //m_progress.SetRange32(1, count); POSITION pos = m_fileNamesSelected.GetHeadPosition(); while (pos) { m_indexOfFileByTransformed++; name = m_fileNamesSelected.GetNext(pos); switch (m_fileTransInfo->format) { //case 0: //不用做双狐转二进制 // ConversionToBinary(strPath, name, nOutMode); // break; case 1: ConversionToAscii(strPath, name, nOutMode); break; //case 2: //不用做双狐转DML // ConversionToDML(strPath, name, nOutMode); // break; case 3: ConversionToPCG(strPath, name, nOutMode); break; default: return RESULT_FORAMT_ERROR; } //AfxGetPublicFunction()->PeekMessageLoop(); } m_indexOfFileByTransformed = 1000; //::AfxMessageBox("Finished"); return RESULT_OK; } void FileTransformer::StopTransform(bool b) { m_stopTransforation = b; } int FileTransformer::GetIndexOfFileByTransformation() { return m_indexOfFileByTransformed; } bool FileTransformer::GetFileNamesByTransfored() { CStringList strList; if (m_fileTransInfo == NULL) return false; if (m_fileTransInfo->fileNamesSelected == NULL) { return false; } CString fileNames(m_fileTransInfo->fileNamesSelected); SplitStr(fileNames, ";", m_fileNamesSelected); if (m_fileNamesSelected.IsEmpty()) { return false; } return true; } void FileTransformer::SplitStr(CString strSrc, CString strGap, CStringList &strResult) { int nPos = strSrc.Find(strGap); CString strLeft = _T(""); while (0 <= nPos) { strLeft = strSrc.Left(nPos); if (!strLeft.IsEmpty()) { strResult.AddTail(strLeft); } strSrc = strSrc.Right(strSrc.GetLength() - nPos - strGap.GetLength()); nPos = strSrc.Find(strGap); } if (!strSrc.IsEmpty()) { strResult.AddTail(strSrc); } } int FileTransformer::ConversionToAscii(CString strPath, CString strFileName, int nOutMode) { CString output = GetOutputFileName(strPath, strFileName, nOutMode, _T(".kev")); if (output.IsEmpty()) return 0; //AfxGetPublicFunction()->SetProgress(&m_one); CXy* pxy = (CXy*)ReadFile(strFileName); if (pxy == NULL) return 0; ClearNoUse(pxy); //pxy->DFD_Write(output, -1, ::GetPreferences().WorkaroundSave.m_bSaveAllLayer); pxy->DFD_Write(output, -1, true); delete pxy; //AfxGetPublicFunction()->SetProgress(NULL); return 1; } int FileTransformer::ConversionToPCG(CString strPath, CString strFileName, int nOutMode) { CString output = GetOutputFileName(strPath, strFileName, nOutMode, _T(".pcg")); if (output.IsEmpty()) return 0; //AfxGetPublicFunction()->SetProgress(&m_one); CXy* pxy = (CXy*)ReadFile(strFileName); if (pxy == NULL) return 0; ClearNoUse(pxy); //pxy->PCG_Write(output, 2, ::GetPreferences().WorkaroundSave.m_bSaveAllLayer); pxy->PCG_Write(output, 2, true); delete pxy; //AfxGetPublicFunction()->SetProgress(NULL); return 1; } CString FileTransformer::GetOutputFileName(CString strPath, CString strFileName, int nOutMode, CString strExt) { CSplitPath sp(strFileName); CString output = _T(""); switch (nOutMode) { case 0: output = sp.GetPath() + sp.GetName() + strExt; break; case 1: if (strPath[strPath.GetLength() - 1] == '\\') output = strPath + sp.GetName() + strExt; else output = strPath + _T("\\") + sp.GetName() + strExt; break; default: return _T(""); } return output; } void FileTransformer::ClearNoUse(CXy* pxy) { if (m_fileTransInfo != NULL) { m_bClearNoUseMark = m_fileTransInfo->bMark != 0; m_bClearNoUseLayer = m_fileTransInfo->bClassification != 0; } if (!m_bClearNoUseMark && !m_bClearNoUseLayer) return; if (m_bClearNoUseMark) pxy->ClearNoUseMark(NULL); if (m_bClearNoUseLayer) pxy->ClearLayerNoneData(NULL); //清除符号中的不使用的信息,使用递归调用清除所有符号中的(包括符号中的符号中的) CString str; int count = 0; void *pDraw; CMapStringToPtrNoCase* pMark = pxy->GetMark(); POSITION pos = pMark->GetStartPosition(); while (pos) { pMark->GetNextAssoc(pos, str, pDraw); if (pDraw == NULL) continue; ClearNoUse((CXy*)pDraw); } } void * FileTransformer::ReadFile(CString strFileName) { CXy* pxy = new CXy(); CSplitPath sp(strFileName); CString sext = sp.GetExtension(); sext.MakeLower(); int rt = 0; if (sext == ".dfb") rt = m_pDoc->SerializeRead(pxy, strFileName); else rt = pxy->ReadWithExtension(strFileName); if (rt > 0) return pxy; delete pxy; return NULL; }