txt文件形如:
转换后的xaml文件:
转换代码如下:
#include <vector> BOOL IsFileExist(LPCTSTR lpFileName) { BOOL bExist = TRUE; if(NULL == lpFileName) { return FALSE; } else { HANDLE hFind = INVALID_HANDLE_VALUE; WIN32_FIND_DATA DataFind; hFind = FindFirstFile(lpFileName, &DataFind); if(INVALID_HANDLE_VALUE == hFind) { bExist = FALSE; } else { FindClose(hFind); } } return bExist; } wchar_t* UTF8FileName(const char* utf8FilePath) { int wchars_num = MultiByteToWideChar(CP_UTF8, 0, utf8FilePath, -1, NULL, 0); wchar_t* wstr = new wchar_t[wchars_num]; MultiByteToWideChar(CP_UTF8, 0, utf8FilePath, -1, wstr, wchars_num); return wstr; } void ConvertANSIToUTF8(const char* ansiFilePath, const char* utf8FilePath) { // Open the ANSI file for reading FILE* ansiFile = fopen(ansiFilePath, "rb"); if (!ansiFile) { perror("Error opening file"); return; } // Create a file for writing in UTF-8 FILE* utf8File = _wfopen(UTF8FileName(utf8FilePath), L"wb"); if (!utf8File) { fclose(ansiFile); perror("Error opening file"); return; } // Convert ANSI to UTF-8 int ansiChar; while ((ansiChar = fgetc(ansiFile)) != EOF) { // Convert ANSI char to wide char wchar_t wideChar = MultiByteToWideChar(CP_ACP, 0, (char*)&ansiChar, 1, NULL, 0); wchar_t wbuf[2]; wbuf[0] = (wchar_t)ansiChar; wbuf[1] = 0; // Convert wide char to UTF-8 char utf8[4]; int utf8Bytes = WideCharToMultiByte(CP_UTF8, 0, wbuf, 1, utf8, sizeof(utf8), NULL, NULL); // Write UTF-8 bytes to file fwrite(utf8, 1, utf8Bytes, utf8File); } // Close files fclose(ansiFile); fclose(utf8File); } void CFileTransDlg::OnOK() { // TODO: Add extra validation here std::vector<CKeyValue> listkey; CKeyValue keyValueObject; CStdioFile file; if(!file.Open(_T("D:\\File\\polxp.txt"), CFile::modeRead)) { return; } CStringArray strArrayKey; CStringArray strArrayValue; CString szBuffer; CString groupName = ""; while(file.ReadString(szBuffer) != NULL) { int index = szBuffer.Find(_T("=")); if(index == -1) { if(szBuffer.Find("[FILE_VER]") != -1 || szBuffer.Find("[STRINGTABLE]") != -1) continue; else if(szBuffer.Find("[") != -1) { int index2 = szBuffer.Find("["); groupName = szBuffer.Mid(index2+1); index2 = groupName.Find("]"); groupName = groupName.Left(index2); } } CString key = ""; CString value = ""; if(groupName == "") { key = szBuffer.Left(index); } else { key = groupName + "_" + szBuffer.Left(index); } value = szBuffer.Mid(index+1); value.Replace(_T("\""), _T("")); strArrayKey.Add(key); strArrayValue.Add(value); } file.Close(); CStringArray strArrayKeyXml; CStringArray strArrayKeyValueAfterXml; if(!file.Open(_T("D:\\File\\jpn.xaml"), CFile::modeRead)) { return; } while(file.ReadString(szBuffer) != NULL) { int index = szBuffer.Find(_T(">")); int index2 = szBuffer.Find(_T("x:Key")); if(index == -1 || index2 == -1) continue; CString key = szBuffer.Left(index+1); strArrayKeyXml.Add(key); int index3 = szBuffer.ReverseFind('<'); CString keyValueAfter = szBuffer.Mid(index3); strArrayKeyValueAfterXml.Add(keyValueAfter); } file.Close(); for(int i = 0 ; i < strArrayKeyXml.GetSize(); i++) { CString strAllKey= strArrayKeyXml.GetAt(i); int index = strAllKey.Find(_T("x:Key=\"")); if(index != -1) { CString strKey = strAllKey.Mid(index+7); int index2 = strKey.Find(_T("\"")); if(index2 != -1) { strKey = strKey.Left(index2); strcpy(keyValueObject.allKey, strAllKey); strcpy(keyValueObject.key, strKey); strcpy(keyValueObject.keyValue, _T("")); for(int j = 0 ; j < strArrayKey.GetSize(); j++) { CString strKeyArr = strArrayKey.GetAt(j); if(strcmp(strKeyArr, strKey) == 0) { CString strValue = strArrayValue.GetAt(j); strValue.Replace("\\r\\n", "
"); strValue.Replace("\\n", "
"); strValue.Replace("<", "<"); strValue.Replace("%03d", "{0:D3}"); strValue.Replace("%f", "{0:F}"); strValue.Replace("%d", "{0:D}"); strValue.Replace("%s", "{0}"); strcpy(keyValueObject.keyValue, strValue); break; } } CString strKeyValueAfter = strArrayKeyValueAfterXml.GetAt(i); strcpy(keyValueObject.keyValueAfter, strKeyValueAfter); listkey.push_back(keyValueObject); } } } if (IsFileExist(_T("D:\\File\\polxp_local.xaml"))) { CFile::Remove(_T("D:\\File\\polxp_local.xaml")); } if (IsFileExist(_T("D:\\File\\polxp_utf8.xaml"))) { CFile::Remove(_T("D:\\File\\polxp_utf8.xaml")); } if (IsFileExist(_T("D:\\File\\polxp.xaml"))) { CFile::Remove(_T("D:\\File\\polxp.xaml")); } if(file.Open(_T("D:\\File\\polxp_local.xaml"), CFile::modeWrite | CFile::modeCreate)) { CString strTitle = "<ResourceDictionary xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"\n"; file.WriteString(strTitle); strTitle = " xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"\n"; file.WriteString(strTitle); strTitle = " xmlns:sys=\"clr-namespace:System;assembly=mscorlib\">\n"; file.WriteString(strTitle); for(int k = 0 ; k < listkey.size(); k++) { keyValueObject = listkey[k]; CString strLine = CString(keyValueObject.allKey) + CString(keyValueObject.keyValue) + CString(keyValueObject.keyValueAfter) + CString("\n"); file.WriteString(strLine); } strTitle = "</ResourceDictionary>"; file.WriteString(strTitle); file.Close(); } //ConvertANSIToUTF8(_T("D:\\File\\polxp_local.xaml"), _T("D:\\File\\polxp_utf8.xaml")); }
标签:index,vc6.0,int,xaml,CString,szBuffer,file,txt,Find From: https://www.cnblogs.com/wuguoqiang/p/18113056