C++常用的工具类源码

来源:互联网 发布:php二手交易网站源码 编辑:程序博客网 时间:2024/06/07 09:41
#pragma once#include <string> #include <iostream>  #include <vector> using namespace std;using std::string; /* * 说明:综合工具类 * 提示:仅支持多子节项目 * 作者:旋律 *  Q Q:535770212 */class FileUtil{public:FileUtil(void);virtual ~FileUtil(void);/* * 创建路径(仅单级路径)  * 参数一:路径 * 返回:true创建成功(路径存在)/false创建失败 */BOOL createDir(CString path);/* * 校验路径是否存在  * 参数一:路径 * 返回:true存在/false不存在 */BOOL checkDir(CString path);/* * 校验文件是否存在  * 参数一:路径 * 返回:true存在/false不存在 */BOOL checkDoc(CString docPath);//校验文件/* * 获得文件内容 * 参数一:路径 * 返回:文件内容 */char* finFileData(CString filePath);/* * 写入文件内容 不存在则创建  覆盖写入 * 参数一:文件路径 * 参数二:文件内容 * 返回:true成功/false失败 */BOOL saveFileData(CString filePath,char* fileData);/** 写入文件内容 不存在则创建  追加写入* 参数一:文件路径* 参数二:文件内容* 返回:true成功/false失败*/BOOL saveFileDataZJ(CString filePath, char* fileData);/** * 说明:UTF8转化为GBK格式 * 参数:utf8字符格式的字符串 * 返回: */void ConvertUtf8ToGBK(CString &strUtf8);/** * 说明:GBK转化为UTF8格式 * 参数:GBK字符格式的字符串 * 返回: */void ConvertGBKToUtf8(CString &strGBK);/** * 说明:取路径的文件名或最后一级目录名称 * 参数:完整的路径 * 返回:最后一级名称(文件夹名称或文件名称) */CString getPathFileName(CString path);/** * 说明:取完整路径带文件名的路径(过滤文件名或最后一级目录) * 参数:完整的路径 * 返回:过滤最后一级后的路径 */CString getPathDirPathName(CString path);/** * 说明:去除文件后缀 只留文件名 * 参数:完整的路径 * 返回:纯文件名称不带后缀 */CString getFileName(CString fileName); /** * 说明:递归遍历文件夹,找到其中包含的所有文件  * 参数:1需遍历的文件夹目录/2以文件名称的形式存储遍历后的文件/3以路径+文件名称的形式存储遍历后的文件  * 例子: //定义存放结果文件名称的链表   std::vector<const std::string> fileList; //定义存放路径的链表 std::vector<const std::string> dirList; find((char*)(LPCSTR)(CStringA)docPath,fileList,dirList); * 返回: */ void find(char* lpPath,std::vector<const std::string> &fileList,std::vector<const std::string> &dirList);  /** * 说明:遍历文件夹(单级)只遍历出文件夹 * 参数:1路径/2遍历到的文件夹名称 * 例子:vector<string> dirList;getJustCurrentDir((LPCSTR)(CStringA)strPathName,dirList); * 返回: */void getJustCurrentDir( string path, vector<string>& files);  /** * 说明:复制文件A-B(可选择是否覆盖原有的) * 参数:1原路径/2目标路径/3是否覆盖   - TRUE 不覆盖  false覆盖 * 返回:成功true /false失败 */BOOL CopyFileAtoB(CString fileA,CString fileB,BOOL checkFile); /** * 说明:删除文件 * 参数:1路径 * 返回:成功true /false失败 */BOOL DeleteFileUtil(CString SecondColumn); /** * 说明:选择文件 * 参数:指定后缀  可以为*  代表所有 * 返回:路径 */    CString getDlgFile(CString fileHouZhui); /** * 说明:选择文件夹 * 参数:直接传入m_hWnd * 返回:路径 */    CString getDlgPath(HWND m_hWnd); /** * 说明:删除文件夹(所有文件包括子目录)  * 参数:路径 * 返回:成功true /false失败 */    BOOL DeleteDirectory(CString strDir); /** * 说明:获得当前程序的路径 * 参数: * 返回:路径 */CString getexePathLB();/** * 说明:字符串替换(不循环重复替换) * 参数:1总字符串 2要替换的字符 3替换成的字符 * 例子:CString webBodyHtml = _T("abcxxoofg"); //xxoo替换成dereplace_all_distinct((string)(LPCSTR)(CStringA)webBodyHtml,"xxoo","de").c_str(); * 返回:替换后的字符串 */string& replace_all_distinct(string& str,const string& old_value,const string& new_value);/*** 说明:多字符串替换(不循环重复替换)* 参数:str总字符串  old_vlaue_Arr要替换的字符(string[]类型)num要替换字符串的个数  new_value替换成的字符* 返回:替换后的字符串*/string& FileUtil::replace_all_distinct_list(string& str, string old_vlaue_Arr[], int num, const string& new_value);/** * 说明:字符串替换(循环重复替换) * 参数:1总字符串 2要替换的字符 3替换成的字符 * 返回:替换后的字符串 */string& replace_all(string& str, const string& old_value, const string& new_value);/*** 说明:判断是否包含子串(A中是否包含B)* 参数:A总字符串  B子串* 返回:true包含 /false不包含*/BOOL checkIsStr(string strA, string strB);/*** 说明:截取字符串(从A到B中间的字符串)* 参数:all总字符串  a开始字符串  b结束字符串* 返回:处理后的字符串*/CString subCString(CString all, CString a, CString b);/*** 说明:将字符串中的html标签转义* 参数:需要转义的字符串* 返回:处理后的字符串*/CString replaceHTML(CString str);/*** 说明:根据数组规则抓取网页内容* 参数:url网页地址/arr抓取规则/length数组长度/编码CP_UTF8 CP_ACP* 返回:处理后的字符串  例子:string 测试url = "http://zhannei.baidu.com/cse/search?q=%E7%B3%BB%E7%BB%9F&p=1&s=7845455592055299828&nsid=";const int 测试规则Length = 11;string 测试规则[测试规则Length][4] = {{ "home", "class=\"result-list\"" },//确定开始标识{ "end", "id=\"footer\"" },//确定结束标识{ "itmeHome", "class=\"result-item result-game-item\"" },//确定list行开始{ "itmeEnd", "preBold\">更新时间" },//确定list行结束(为空代表同一行){ "截取", "cpos=\"title\" href=\"", "\" title=\"", "0" },//url截取{ "截取", "title=\"", "\" class=\"result-game-item-title-link\"", "0" }, //名称截取{ "截取", "class=\"result-game-item-desc\">", "<div class=\"result-game-item-info\"", "1" },//介绍{ "转义", "", "", "0" },//介绍{ "截取", "preBold\">作者:</span>", "<span class=\"result-game-item-info-tag-title preBold\">类型:", "1" },{ "截取", "<span>", "</span>", "1" },{ "转义", "", "", "0" }//作者};vector<vector<CString>> veArr = util->html(测试url, 测试规则, 测试规则Length, CP_UTF8);*/vector<vector<CString>> FileUtil::html(CString url, string arr[][4], int length, UINT BM);/*** 说明:中文转为浏览器识别的特殊编码* 参数:带转换字符串* 返回:处理后的字符串*/CString  URLEncode(const char *sIn);/*** 说明:浏览器识别的特殊编码转为中文* 参数:带转换字符串* 返回:处理后的字符串*/CString URLDecode(const char *sIn);/*** 说明:获得指定字符串左边的字符串* 参数:all总字符串  a指定字符串* 返回:处理后的字符串*/CString findZ(CString all, CString a);/*** 说明:获得指定字符串右边的字符串* 参数:all总字符串  a指定字符串* 返回:处理后的字符串*/CString findY(CString all, CString a);};

注:部分功能代码整理自网络

工具类下载链接:http://download.csdn.net/download/jkl012789/9986841



原创粉丝点击