首页 > 其他分享 >vc6.0 txt文件资源转为xaml资源

vc6.0 txt文件资源转为xaml资源

时间:2024-04-03 16:56:42浏览次数:12  
标签:index vc6.0 int xaml CString szBuffer file txt Find

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", "&#x0A;");
                        strValue.Replace("\\n", "&#x0A;");
                        strValue.Replace("<", "&lt;");
                        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

相关文章

  • 【python】txt文件读取
    1open()函数file=open('example.txt','r')print(file.read())file.close()(1)整体读入,可以直接read()(2)一定要记得关闭2with语句withopen('example.txt','r')asf:forlineinf:print(line.strip())(1)line.strip的意思是去......
  • 程序运行要求,三角形三边的值来自于本地一个文本文件input.txt,三角形类型的值最终存储
    本周完成如下2个实验:面向对象数据持久化编程,使用java编写程序,完成三角形的类型判断,程序模块要求如下:创建三角形对象triangle,该对象属性有三边a,b,c,该对象有:方法1:isOutOfRange(intI,intmax),用于判断一个整数是否在(0,max)区间内(max值请各人自行设定),返回值:true-否;fal......
  • 从安装python开始教你利用python将excel中的数据导出到txt文件中,并且有如何安装python
    制作方法想到制作这个程序的原因开始写程序前的准备工作安装python第一步:下载python第二步:运行python安装包测试python用python输出你好python安装python库开始编写程序编写前的准备开始编写想到制作这个程序的原因工作的时候,领导说让把表格里的数据导出到txt文......
  • [20240325]FORCE_MATCHING_SIGNATURE与DML.txt
    [20240325]FORCE_MATCHING_SIGNATURE与DML.txt--//生产系统遇到1个FORCE_MATCHING_SIGNATURE重合的奇怪现象,一般情况都是相似的sql语句(没有使用绑定变量的sql语句),--//FORCE_MATCHING_SIGNATURE相同。--//实际上insert语句真实FORCE_MATCHING_SIGNATURE=0,但是在v$active_session......
  • [20240325]expand_sql_text dba_hist_sysstat(12c).txt
    [20240325]expand_sql_textdba_hist_sysstat(12c).txt--//前几天测试dba_hist_sysdate的底层视图定义里面包含提示.--//测试一条sql语句包含dba_hist_sysstat使用expand_sql_text的展开情况.1.环境:SYS@test>@ver1PORT_STRING                   VERSION ......
  • [20240321]分析FORCE_MATCHING_SIGNATURE重合的奇怪情况.txt
    [20240321]分析FORCE_MATCHING_SIGNATURE重合的奇怪情况.txt--//生产系统遇到1个FORCE_MATCHING_SIGNATURE重合的奇怪现象,一般情况都是相似的sql语句(没有使用绑定变量的sql语句),--//FORCE_MATCHING_SIGNATURE相同。--//注:11g之前如果绑定变量与常量混合,会出现EXACT_MATCHING_SIGN......
  • [20240320]空格与sqlpus的sql语句.txt
    [20240320]空格与sqlpus的sql语句.txt--//优化sql语句时遇到的问题,自己上我发现我手工执行获得的sql_id与程序里面的sql_id不一致,原因很多sqlplus下如果是dos文本格--//式,计算的文本会将\r\n字符变成\n,如果多行,文本结尾的空格会删除等等,通过例子说明:1.环境:SCOTT@book>@ver1......
  • WPF Add ResourceDictionary file and declared in app.xaml
    //AddresourcedictionaryfilenamedBrushes.xaml<ResourceDictionaryxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><LinearG......
  • WPF中阴影效果和模糊效果的使用【Xaml】
    原文:https://blog.csdn.net/qq_39847278/article/details/129707074前言WPF中的控件效果主要通过Effect来实现,而Effect有DropShadowEffect(投影效果)和BlurEffect(模糊效果)两个派生类,本文将主要介绍Effect的运用!一、DropShadowEffect1、DropShadowEffect各属性效果图 另外还有......
  • windows txt怎么转成unix 格式
    将Windows格式的txt文件转换为Unix格式,你可以采取以下几种方法:方法一:使用Notepad++打开Notepad++软件。在菜单栏中,点击“编辑”选项。在下拉菜单中,选择“文档格式转换”。在弹出的子菜单中,选择“转换为UNIX格式”。方法二:使用sed命令打开命令提示符(CMD)或PowerShell。使......