算法测试平台搭建常用代码备份

最近要在windows平台下搭建一个简单的算法测试平台进行算法的测试(又要做windows下算法的开发~

故而把一些常用的MFC代码的的代码总结如下

  1. 选择路径对话框
void CMuraDetectDlg::OnMenuChoosePath()
{
    // TODO:  在此添加命令处理程序代码
    wchar_t szPath[MAX_PATH] = {0};

    BROWSEINFO bi;
    bi.hwndOwner = m_hWnd;
    bi.pidlRoot  = NULL;
    bi.lpszTitle = _T("请选择需要打包的目录:");
    bi.ulFlags   = 0;
    bi.lpfn      = NULL;
    bi.lParam    = 0;
    bi.iImage    = 0;
    bi.pszDisplayName = szPath;

    //弹出选择目录对话框
    LPITEMIDLIST lp = SHBrowseForFolder(&bi);
    if (lp && SHGetPathFromIDList(lp, szPath))
    {
        CString str;
        str.Format(_T("选择的目录为 %s"), szPath);
        AfxMessageBox(str);
    }
    else
    {
        AfxMessageBox(_T("无效的目录,请重新选择"));
    }

    m_file_path = m_util.UnicodeToANSI(szPath);
}
  1. MFC下各种编码格式的转换
// ANSI to Unicode
wstring CUtil::ANSIToUnicode(const string& str)
{
    int  len = 0;
    len = str.length();
    int  unicodeLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
    wchar_t *  pUnicode;
    pUnicode = new  wchar_t[unicodeLen + 1];
    memset(pUnicode, 0, (unicodeLen + 1)*sizeof(wchar_t));
    ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);
    wstring  rt;
    rt = (wchar_t*)pUnicode;
    delete  pUnicode;

    return  rt;
}

//Unicode to ANSI
string CUtil::UnicodeToANSI(const wstring& str)
{
    char*     pElementText;
    int    iTextLen;
    // wide char to multi char
    iTextLen = WideCharToMultiByte(CP_ACP,
        0,
        str.c_str(),
        -1,
        NULL,
        0,
        NULL,
        NULL);
    pElementText = new char[iTextLen + 1];
    memset((void*)pElementText, 0, sizeof(char)* (iTextLen + 1));
    ::WideCharToMultiByte(CP_ACP,
        0,
        str.c_str(),
        -1,
        pElementText,
        iTextLen,
        NULL,
        NULL);
    string strText;
    strText = pElementText;
    delete[] pElementText;
    return strText;
}

// UTF - 8 to Unicode
wstring CUtil::UTF8ToUnicode(const string& str)
{
    int  len = 0;
    len = str.length();
    int  unicodeLen = ::MultiByteToWideChar(CP_UTF8,
        0,
        str.c_str(),
        -1,
        NULL,
        0);
    wchar_t *  pUnicode;
    pUnicode = new  wchar_t[unicodeLen + 1];
    memset(pUnicode, 0, (unicodeLen + 1)*sizeof(wchar_t));
    ::MultiByteToWideChar(CP_UTF8,
        0,
        str.c_str(),
        -1,
        (LPWSTR)pUnicode,
        unicodeLen);
    wstring  rt;
    rt = (wchar_t*)pUnicode;
    delete  pUnicode;

    return  rt;
}

// Unicode to UTF - 8
string CUtil::UnicodeToUTF8(const wstring& str)
{
    char*     pElementText;
    int    iTextLen;
    // wide char to multi char
    iTextLen = WideCharToMultiByte(CP_UTF8,
        0,
        str.c_str(),
        -1,
        NULL,
        0,
        NULL,
        NULL);
    pElementText = new char[iTextLen + 1];
    memset((void*)pElementText, 0, sizeof(char)* (iTextLen + 1));
    ::WideCharToMultiByte(CP_UTF8,
        0,
        str.c_str(),
        -1,
        pElementText,
        iTextLen,
        NULL,
        NULL);
    string strText;
    strText = pElementText;
    delete[] pElementText;
    return strText;
}
  1. 获取指定文件夹下所有的指定格式的文件
    这里用到了boost,自从做服务器开发用到了boost,就离不开了,什么功能都想直接用boost,果然有毒呀!!
int CUtil::get_path_allfiles(const std::string& dir, const std::string fmt, bool mult_layer, std::vector<std::string>& filenames)
{
    filesystem::path path(dir);
    if (!filesystem::exists(path))
    {
        return -1;
    }

    filesystem::directory_iterator end_iter;
    for (filesystem::directory_iterator iter(path); iter != end_iter; ++iter)
    {
        if (filesystem::is_regular_file(iter->status()))
        {
            string tmp_name = iter->path().string();
            if (tmp_name.size() < 4)
            {
                continue;
            }
            tmp_name = tmp_name.substr(tmp_name.size() - 4, 4);
            if (tmp_name.compare(fmt) == 0)
            {
                filenames.push_back(iter->path().string());
            }
        }

        if (filesystem::is_directory(iter->status()))
        {
            if (mult_layer)
            {
                get_path_allfiles(iter->path().string(), fmt, mult_layer, filenames);
            }
        }
    }

    return (int)filenames.size();
}
    4.

你可能感兴趣的:(C/C++/MFC学习)