c++文件各种操作,包含内容:
文件读写
递归查找
复制
移动
删除
修改名称
带标志位的写入与读取
计时函数
日志函数
文件大小
文件夹是否是空
选择文件夹
弹出文件夹
计算MD5
头文件 operatingtools.hpp
//
/// These functions are C++ ports.
/// At 2020-8-13 14:04:45。
/// Version 1.0.0.2
/// Author __闹闹
/// Brief vs2015 release x64 多字节编码,链接器-优化-链接时间代码生成-使用链接时间代码生成
/// 后续再进行扩展,日志函数,建议使用spdlog库
Revision history
/// modify_author: 闹闹
/// modify_time: 2020-8-13 15:50:18
/// modify_content 总体格式修改,重载了userlog,
/// modify_version: 1.0.0.3
/// modify_author: 闹闹
/// modify_time: 2020-8-14 10:13:31
/// modify_content 修改部分注释,修改了#if defined的格式,去除成员变量,将读取文件的函数改为静态函数
/// modify_version: 1.0.0.4
/// modify_author: 闹闹
/// modify_time: 2020-8-14 11:51:17
/// modify_content 增加了函数调用失败的错误信息
/// modify_version: 1.0.0.5
/// modify_author: 闹闹
/// modify_time: 2020-8-17 16:11:10
/// modify_content 增加了获取文件与字符串的md5,增加了根据md5和文件名删除相同文件,
/// modify_version: 1.0.0.6
/// modify_author: 闹闹
/// modify_time: 2020-8-19 17:44:07
/// modify_content 修改了函数名称,作为统一标识。
/// modify_version: 1.0.0.7
/// modify_author: 闹闹
/// modify_time: 2020-8-28 11:59:01
/// modify_content 修改了函数FT_GetAllFormatFiles,FT_GetAllFiles,增加了过滤的文件夹。其他增加过滤文件夹的函数可以参考。
/// modify_version: 1.0.0.8
/// modify_author: 闹闹
/// modify_time: 2020-9-1 10:24:08
/// modify_content 去除了using namespace std;防止污染空间名,用的话,放在命名空间里面,
/// 去除了类,改为命名空间,修改了格式,命名空间的短写不要放在头文件
/// 将函数的静态属性去除
/// modify_version: 1.0.0.9
/// modify_author: 闹闹
/// modify_time: 2020-9-8 14:53:51
/// modify_content 修改了FT_GetCopyFileA函数,使用第二种方法,去除了时间慢,占用内存大的问题
/// 方法一在x86下分配大内存会失败,果断采用方法二
/// modify_version: 1.0.0.10
/// modify_author: 闹闹
/// modify_time: 2020-9-14 16:16:09
/// modify_content 修改了函数FT_GetAllFormatFiles,FT_GetAllFiles,在传入单斜线格式的路径时出现无效的BUG。
/// modify_version: 1.0.0.11
/// modify_author: 闹闹
/// modify_time: 2020-9-30 09:07:59
/// modify_content 重载FT_GetAllFormatFiles
/// modify_version: 1.0.0.13
/// modify_author: 闹闹
/// modify_time: 2020-10-14 16:53:21
/// modify_content 增加了过滤字符串的函数filterfield
/// modify_version: 1.0.0.14
/// modify_author: 闹闹
/// modify_time: 2021年1月4日14:00:11
/// modify_content 删除线程错误信息,改为全局信息,将.h与.cpp 文件合并为.hpp。
/// modify_version: 1.0.0.15
/// modify_author: 闹闹
/// modify_time: 2021年1月27日10:55:32
/// modify_content 修改代码格式,去除冗余部分,在考虑是否需要加入文件读写的内容。希望代码之间耦合性不要太高。
/// modify_version: 1.0.0.16
/// modify_author: 闹闹
/// modify_time: 2021-4-16 11:31:08
/// modify_content 修改Windows的代码,去除平台依赖
/// FT_AlterFileName 重命名修改
/// FT_FolderIsEmpty 判断文件夹非空
/// FT_GetCopyFileA 文件复制函数修改
/// FT_RemoveDir 删除目录也修改了
/// 禁用了一部分函数,此部分函数是WINDOWS平台下的函数,有需要时可以打开
/// modify_version: 1.0.0.17
//
#pragma once
#ifndef _OPERATINGTOOLS_H_
#define _OPERATINGTOOLS_H_
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif // !_CRT_SECURE_NO_WARNINGS
//文件系统头文件
#if defined(__cplusplus) && __cplusplus >= 201703L
#if defined(__has_include) || __has_include(<filesystem>)
#include <filesystem>
namespace fs = std::filesystem;
#endif
#else
#include <experimental\filesystem>
namespace fs = std::experimental::filesystem;
#endif
#include <direct.h>
#include <windows.h>
#include <time.h>
#include <tchar.h>
#include <stdlib.h>
#include <shellapi.h>
#include <thread>
#include <exception>
///正则表达式
#include <regex>
///获取绝对路径时的包含文件
#include <io.h>
///一般包含文件
#include <string>
#include <iostream>
///写文件包含文件
#include <fstream>
///集合头文件
#include <list>
#include <vector>
#include <map>
#include "md5.hpp"
//using namespace std;
namespace operatingtools
{
//仅限内部使用
using namespace std;
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: ,全局函数,函数失败信息
/// @InputParameter: const string filename
/// @OutputParameter:
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
void GetLastErrorInfo(std::string& errorinfo);
void SetLastErrorInfo(const std::string errorinfo);
std::string g_lastError;
///文件操作空间
namespace fileoperation
{
/*
非成员函数不应依赖于外部变量, 应尽量置于某个命名空间内.
This are some calsses of file-operation.
参考博客
https://blog.csdn.net/CosmopolitanMe/article/details/80629531
https://blog.csdn.net/sinat_25923849/article/details/78268984
https://www.shuzhiduo.com/A/VGzlVpvy5b/
https://www.cnblogs.com/lkpp/p/OPENFILENAME-lpstrFilter.html
http://gcc.gnu.org/onlinedocs/cpp/_005f_005fhas_005finclude.html
https://www.cctry.com/forum.php?mod=viewthread&tid=47740
https://zh-google-styleguide.readthedocs.io/en/latest/google-cpp-styleguide/scoping/
https://www.cnblogs.com/mathyk/p/11200556.html
https://blog.csdn.net/F_hawk189/article/details/100126602
*/
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 创建目录
/// @InputParameter: const string filename
/// @OutputParameter:
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
bool FT_CreatDir(const std::string filename);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 计算md5
/// @InputParameter: const string filename
/// @OutputParameter: md5value
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
int FT_GetFileMD5A(const std::string& filename, std::string& md5value);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 计算md5
/// @InputParameter: const string filename
/// @OutputParameter: md5value
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
int FT_GetFileMD5B(const std::string& filename, std::string& md5value);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 计算字符串md5
/// @InputParameter: const string filename
/// @OutputParameter: md5value
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
int FT_GetStrMD5(const std::string& stringvalue, std::string& md5value);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 递归删除目录下所有文件,返回被成功删除的文件个数
/// @InputParameter: const string filename
/// @OutputParameter:
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
int FT_RemoveDir(const std::string filename);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 根据md5删除,将dstfile中与orgfile相同的文件删除,flag是删除标识符,默认不删除
/// @InputParameter: const string orgfile,const string dstfile,
/// @OutputParameter: samefilelist
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
int FT_RemoveDuplicateFileA(const std::string orgfile, const std::string dstfile, std::vector<string>& samefilelist, int flag = 0);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 根据文件名删除,将dstfile中与orgfile相同的文件删除,flag是删除标识符,默认不删除
/// @InputParameter: const string orgfile,const string dstfile,
/// @OutputParameter: samefilelist
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
int FT_RemoveDuplicateFileB(const std::string orgfile, const std::string dstfile, std::vector<string>& samefilelist, int flag = 0);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 更改filename文件名字为newname
/// @InputParameter: string filename, string newname
/// @OutputParameter:
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
int FT_AlterFileName(std::string filename, std::string newname);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 判断目录是否存在
/// @InputParameter: const string path
/// @OutputParameter:
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
bool FT_ExistsDir(const std::string path);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 查询指定文件大小
/// @InputParameter: const string filename
/// @OutputParameter:
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
int FT_GetFileSize(const std::string filename);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 判断文件夹是否为空,有子文件夹也算非空
/// @InputParameter:
/// @OutputParameter:
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
bool FT_FolderIsEmpty(const std::string foldername);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 复制文件到指定目录,不删除原文件,使用二进制读写,路径写绝对路径加文件名带后缀
/// 修改了FT_GetCopyFileA函数, 方法一在x86下分配大内存会失败,时间慢,占用内存大。
/// 使用第二种方法,没有此种问题。 果断采用方法二,分别使用2G和120M的文件进行了测试,方法二性能均好与方法一
/// 使用方法三,丢弃windows平台代码
/// @InputParameter: const string orgPath,const string dstPath
/// @OutputParameter:
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
bool FT_GetCopyFileA(const std::string orgPath, const std::string dstPath);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 此函数已经禁用。复制文件到指定目录,不删除原文件,Window的API,路径写绝对路径加文件名带后缀
/// @InputParameter: LPCTSTR orgPath, LPCTSTR dstPath
/// @OutputParameter:
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
int FT_GetCopyFileB(const std::string orgPath, const std::string dstPath);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 此函数禁用,移动文件到指定目录,源文件会消失,Window的API,路径写绝对路径加文件名带后缀
/// @InputParameter: LPCTSTR orgPath, LPCTSTR dstPath
/// @OutputParameter:
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
int FT_GetMoveFile(const std::string orgPath, const std::string dstPath);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 计时器,获取时间毫秒级(需乘1000),程序运行时间为两个时间之差
/// @InputParameter:
/// @OutputParameter:
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
double FT_GetDoubleTime();
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 日志使用 日志在d:\\Log\\log.txt
///示例
///char info[1024];
///sprintf(info, "图像未处理,跳过");
///userlog(info);
///日志函数,建议使用spdlog库
/// @InputParameter:
/// @OutputParameter:
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
void userlog(char* info);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 此函数禁用,日志记录,日志在d:\\Log\\ 目录下
///示例: userlog("mv高视频火花检测", "MvSparkObject正在工作,第%d帧", nframe);
///日志函数,建议使用spdlog库
/// @InputParameter: 第一个参数是日志名称
/// @OutputParameter:
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
void userlog(char* logname, char* fmt, ...);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief:
///获取文件列表到当前目录的txt,以追加的方式写入文件,format是格式,lable是标志符,
///标签与文件路径之间有一个空格
/// @InputParameter:
///第一个参数是文件路径
///第二个参数是目标文件,默认为空写到第一个参数的路径,不为空,则写具体路,需带具体的文件名与后缀
///第三个参数是文件格式,默认为空,不为空可以指定过滤的格式
///第四个参数是文件标签,默认255,为空,不为空可以指定类别序号
/// @OutputParameter:
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
void FT_GetFileList(const std::string filePath, const std::string distAll = "", std::string format = "", int lable = 255);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 读取文件列表文件与标志位,读到lablefilelist中
/// @InputParameter: string filename
/// @OutputParameter: lablefilelist
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
void FT_ReadFileList(std::string filename, std::multimap<string, int>& lablefilelist);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 读取标志位,做辅助函数使用
/// @InputParameter: string strPath
/// @OutputParameter: 未有
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
int FT_GetClassFlag(std::string strPath);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 读取不含标志位的文件列表文件(一行是一个路径),读到nolablefilelist
/// @InputParameter: const string filename
/// @OutputParameter: nolablefilelist
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
bool FT_ReadNoLableFileList(const std::string filename, std::vector<string>& nolablefilelist);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief:
/// 此函数禁用。
///选择文件,并记录文件名
///使用说明,一句话的事情,就是getopenfilename这个函数是mfc函数的,
///如果你的文件头中有#define WIN32_LEAN_AND_MEAN那么你的系统会提示,
///getopenfilename未定义,顺带的OPENFILENAME的结构也会变成未定义的、
/// @InputParameter:
/// @OutputParameter:
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
string FT_SelectFile();
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 此函数禁用。弹出文件夹
/// @InputParameter: string filename
/// @OutputParameter:
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
void FT_PopUpFolder(std::string filename);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 获取所有的文件名,只有文件名(绝对路径),不带文件夹
/// @InputParameter: const string path
/// @OutputParameter: files
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
bool FT_GetAllFiles(const std::string path, std::vector<string>& files, const std::vector<string> filter_directory = std::vector<string>());
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 获取特定格式的文件名,递归目录,不区分后缀大小写
/// @InputParameter: ,第一个参数是路径,第三个参数 expression= "(.*)(.png|jpg|bmp)",第四个参数是过滤文件夹
/// @OutputParameter: 第二个参数files
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
bool FT_GetAllFormatFiles(const std::string path, std::vector<string>& files, std::string format, const std::vector<string> filter_directory = std::vector<string>());
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 重载,获取特定格式的文件名,递归目录,不区分后缀大小写
/// @InputParameter: ,第一个参数是路径,第三个参数 expression= "(.*)(.png|jpg|bmp)",第四个参数是过滤文件夹
/// @OutputParameter: 第二个参数files
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
bool FT_GetAllFormatFiles(const std::vector<string> path, std::vector<string>& files, std::string format, const std::vector<string> filter_directory = std::vector<string>());
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 从字符串列表中过滤掉包含特殊字符的字符串。
/// @InputParameter: ,第一个参数是原字符串列表,第三个参数是需要过滤掉的字符串 {expression= ".*_20200926.*"},需要匹配的字符串。
/// @OutputParameter: 第二个参数dstStringList,为接收结果
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
bool FT_FilterField(const std::vector<string> srcStringList, std::vector<string>& dstStringList, const std::vector<std::string> format);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 从字符串列表中过滤掉包含特殊字符的字符串。
/// @InputParameter: ,第一个参数是原字符串列表,第三个参数是需要过滤掉的字符串 {expression= ".*_20200926.*"},需要匹配的字符串。
/// @OutputParameter: 第二个参数dstStringList,为接收结果
/// @Returns:
/// @Others: 重载
///= = = = = = = = = = = = = = = = = = = =
bool FT_FilterField(const std::vector<string> srcStringList, std::vector<string>& dstStringList, std::string format);
//
///= = = = = = = = = = = = = = = = = = = =
/// @FuncName:
/// @Brief: 此函数禁用,根据文件名在目录下找文件,返回全路径(绝对路径)
/// @InputParameter: ,第一个参数是路径,第二个是返回的绝对路径,第三个参数 expression是查找的文件名
/// @OutputParameter: string& name
/// @Returns:
/// @Others:
///= = = = = = = = = = = = = = = = = = = =
void FT_GetAbsPathOfFile(const std::string path, std::string& name, std::string format);
}//namespace fileoperation
}//namespace operatingtools
#endif // !_OPERATINGTOOLS_H_
namespace operatingtools
{
//
///
///
void GetLastErrorInfo(std::string& errorinfo){
errorinfo = g_lastError;
}
//
///
///
static void SetLastErrorInfo(const std::string errorinfo){
g_lastError = errorinfo;
}
//文件操作空间
namespace fileoperation
{
//
///
///
bool FT_CreatDir(const std::string filename){
if (fs::exists(filename)) return true;
fs::create_directory(filename);
if (fs::exists(filename))
return true;
else
return false;
}
//
///
///
int FT_GetFileMD5A(const std::string& filename, std::string& md5value){
fs::path dir = filename;
if (!fs::exists(dir)){
SetLastErrorInfo(std::string(__FUNCTION__) + ": " + filename + "目录不存在");
return -1;
}
MD5 md5Code(filename);
md5value = md5Code.FileToMD5A(filename);
return 0;
}
//
///
///
int FT_GetFileMD5B(const std::string& filename, std::string& md5value){
fs::path dir = filename;
if (!fs::exists(dir)){
SetLastErrorInfo(std::string(__FUNCTION__) + ": " + filename + "目录不存在");
return -1;
}
md5value = MD5::FileToMD5B(filename);
return 0;
}
//
///
///
int FT_GetStrMD5(const std::string& stringvalue, std::string& md5value){
if (stringvalue == ""){
SetLastErrorInfo(std::string(__FUNCTION__) + ": " + stringvalue + "字符串为空");
return -1;
}
md5value = MD5::StringToMD5(stringvalue);
return 0;
}
//
///
///
int FT_RemoveDuplicateFileA(const std::string orgfile, const std::string dstfile, std::vector<string>& samefilelist, int flag){
std::vector<string> orgfilelist, dstfilelist;
std::vector<string> orgfilemd5, dstfilemd5;
FT_GetAllFiles(orgfile, orgfilelist);
FT_GetAllFiles(dstfile, dstfilelist);
for (size_t i = 0; i < orgfilelist.size(); i++){
std::string tmpmd5;
FT_GetFileMD5A(orgfilelist[i], tmpmd5);
orgfilemd5.push_back(tmpmd5);
}
for (size_t i = 0; i < dstfilelist.size(); i++){
std::string tempmd5;
FT_GetFileMD5A(dstfilelist[i], tempmd5);
dstfilemd5.push_back(tempmd5);
for (size_t j = 0; j < orgfilemd5.size(); j++){
if (std::strcmp(tempmd5.c_str(), orgfilemd5[j].c_str()) == 0){
samefilelist.push_back(dstfilelist[i]);
break;
}
}
}
if (flag == 1){
for (size_t i = 0; i < samefilelist.size(); i++){
FT_RemoveDir(samefilelist[i]);
}
}
return 0;
}
//
///
///
int FT_RemoveDuplicateFileB(const std::string orgfile, const std::string dstfile, std::vector<string>& samefilelist, int flag){
std::vector<string> orgfilelist, dstfilelist;
FT_GetAllFiles(orgfile, orgfilelist);
FT_GetAllFiles(dstfile, dstfilelist);
for (size_t i = 0; i < dstfilelist.size(); i++){
std::string dstfilename = fs::path(dstfilelist[i]).filename().string();
for (size_t j = 0; j < orgfilelist.size(); j++){
std::string orgfilename = fs::path(orgfilelist[j]).filename().string();
if (std::strcmp(dstfilename.c_str(), orgfilename.c_str()) == 0){
samefilelist.push_back(dstfilelist[i]);
break;
}
}
}
if (flag == 1){
for (size_t i = 0; i < samefilelist.size(); i++){
FT_RemoveDir(samefilelist[i]);
}
}
return 0;
}
//
///
///
int FT_RemoveDir(const std::string filename){
fs::path dir = filename;
if (!fs::exists(dir)){
SetLastErrorInfo(std::string(__FUNCTION__) + ": " + filename + "目录不存在");
return -1;
}
std::uintmax_t n = fs::remove_all(dir);
return (int)n;
}
//
///
///
int FT_AlterFileName(std::string filename, std::string newname){
if (!fs::exists(filename)){
SetLastErrorInfo(std::string(__FUNCTION__) + ": " + filename + "目录不存在");
return -2;
}
try{
fs::rename(filename.c_str(), newname.c_str());
return 0;
}
catch (const std::exception&)
{
SetLastErrorInfo(std::string(__FUNCTION__) + ": " + "修改名称操作失败");
return -1;
}
return 0;
}
//
///
///
bool FT_ExistsDir(const std::string path){
if (fs::exists(path)) return true;
return false;
}
//
///
///
bool FT_FolderIsEmpty(const std::string foldername){
//旧代码,遗留参考
/*
HANDLE hFind;
WIN32_FIND_DATA FindFileData;
std::string testfilename = foldername + "\\*.*";
if ((hFind = FindFirstFileA(testfilename.c_str(), &FindFileData)) != INVALID_HANDLE_VALUE){
BOOL bFind = TRUE;
BOOL EmptyDirectory = TRUE;
while (bFind){
if (strcmp(FindFileData.cFileName, ".") == 0
|| strcmp(FindFileData.cFileName, "..") == 0)
{
bFind = FindNextFile(hFind, &FindFileData);
}
else{
EmptyDirectory = FALSE; //有子文件夹也算非空
break;
}
}
if (EmptyDirectory) return 0;
if (!EmptyDirectory) return 1;
}
else{
return 0;
}
*/
fs::path tmp_name = foldername;
if (fs::is_empty(tmp_name))
return true;
return false;
}
//
///
///
bool FT_GetCopyFileA(const std::string orgPath, const std::string dstPath){
if (!fs::exists(orgPath)) return false;
//方法一
//std::fstream orgfile;
//orgfile.open(orgPath, std::ios::binary | std::ios::in | std::ios::ate);
//if (!orgfile)
//{
// return false;
//}
//std::streamoff length = orgfile.tellg();
//char* fileData = new char[static_cast<unsigned int>(length)];
memset(fileData, 0, sizeof(fileData));
//orgfile.seekg(0);
//orgfile.read(fileData, length);
//orgfile.close();
//std::fstream dstfile;
//dstfile.open(dstPath, std::ios::binary | std::ios::out);
//if (!dstfile)
//{
// delete fileData;
// return false;
//}
//dstfile.write(fileData, length);
//dstfile.close();
//delete fileData;
//
//方法二
/*
char buf[100];
ifstream fin(orgPath, std::ios::binary); //创建输入文件流
if (!fin)
{
return false;
}
ofstream fout(dstPath, std::ios::binary); //创建输入流
if (!fout)
{
return false;
}
while (!fin.eof()) { //实现文件的复制
fin.read(buf, sizeof(buf));
fout.write(buf, fin.gcount());
}
fin.close(); //关闭流
fout.close();
return true;
*/
//方法三
fs::path srcpath = orgPath;
fs::path dstpath = dstPath;
fs::copy_file(srcpath, dstpath);
return true;
}
//
///
///
int FT_GetCopyFileB(const std::string orgPath, const std::string dstPath){
/*
if (!fs::exists(orgPath))
{
SetLastErrorInfo(std::string(__FUNCTION__) + ": " + orgPath + "目录不存在");
return -1;
}
LPCTSTR org = orgPath.c_str();
LPCTSTR dst = dstPath.c_str();
return CopyFile(org, dst, false);
*/
return 1;
}
//
///此函数禁用,用处不大
///
int FT_GetMoveFile(const std::string orgPath, const std::string dstPath){
/*
if (!fs::exists(orgPath))
{
SetLastErrorInfo(std::string(__FUNCTION__) + ": " + orgPath + "目录不存在");
return -1;
}
LPCTSTR org = orgPath.c_str();
LPCTSTR dst = dstPath.c_str();
return MoveFile(org, dst);
*/
return 1;
}
//
///
///
double FT_GetDoubleTime(){
LARGE_INTEGER t, freq;
QueryPerformanceCounter(&t);
QueryPerformanceFrequency(&freq);
return t.QuadPart*1.0 / freq.QuadPart;
}
//
///
///
void userlog(char* info){
time_t t;
time(&t);
char szTime[100];
struct tm tm1 = *localtime(&t);
sprintf(szTime, "[%4.4d-%2.2d-%2.2d %2.2d:%2.2d] ",
tm1.tm_year + 1900, tm1.tm_mon + 1, tm1.tm_mday,
tm1.tm_hour, tm1.tm_min);
FILE * fp = fopen("d:\\Log\\log.txt", "a");
if (fp){
fwrite(szTime, 1, strlen(szTime), fp);
fwrite(info, 1, strlen(info), fp);
fwrite("\n", 1, 1, fp);
fclose(fp);
}
}
//
///此函数禁用
///
void userlog(char* logname, char* fmt, ...)
{
/*
char info[1024];
va_list args;
va_start(args, fmt);
vsprintf(info, fmt, args);
va_end(args);
#ifdef _WIN32
char szTime[100];
SYSTEMTIME now_time;
GetLocalTime(&now_time);
sprintf(szTime, "[%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d %3.3d] ",
now_time.wYear, now_time.wMonth, now_time.wDay,
now_time.wHour, now_time.wMinute, now_time.wSecond, now_time.wMilliseconds);
char filename[100];
sprintf(filename, "d:\\Log\\%s_%d_%d_%d.txt", logname, now_time.wYear, now_time.wMonth, now_time.wDay);
FILE * fp = fopen(filename, "a");
if (fp){
fwrite(szTime, 1, strlen(szTime), fp);
fwrite(info, 1, strlen(info), fp);
fwrite("\n", 1, 1, fp);
fclose(fp);
}
#else
char filename[100];
sprintf(filename, "/Log/%s.txt", logname);
FILE * fp = fopen(filename, "a");
if (fp){
fwrite(info, 1, strlen(info), fp);
fwrite("\n", 1, 1, fp);
fclose(fp);
}
#endif
*/
}
//
///
///
void FT_GetFileList(const std::string filePath, const std::string distAll, std::string format, int lable){
std::vector<string> files;
if (format != "") FT_GetAllFormatFiles(filePath, files, format);
if (format == "") FT_GetAllFiles(filePath, files);
if (distAll != "") std::string filename = distAll;
std::string filename = filePath + "\\filelist.txt";
//ofstream ofn(distAll, ios::app);
std::ofstream ofn(filename.c_str(), std::ios::app);
size_t size = files.size();
if (lable != 255){
for (size_t i = 0; i < size; i++){
ofn << files[i] << " " << lable << std::endl;
}
}
else{
for (size_t i = 0; i < size; i++){
ofn << files[i] << std::endl;
}
}
ofn.close();
}
//
///
///
bool FT_GetAllFiles(const std::string path, std::vector<string>& files, const std::vector<string> filter_directory){
fs::path file_path = path;
if (!fs::exists(file_path)) return false;
if (fs::is_directory(file_path)){
for (auto f : fs::recursive_directory_iterator(file_path)){
int foundFlag = 0; //过滤文件夹
for (size_t i = 0; i < filter_directory.size(); i++){
std::string::size_type idx;
fs::path filter_directory_index = filter_directory[i];
idx = f.path().string().find(filter_directory_index.string());
if (idx != string::npos){
foundFlag = 1;
continue;
}
}
if (foundFlag == 0){
if (!fs::is_directory(f)) files.push_back(f.path().string());
}
}
}
if (!fs::is_directory(file_path)) files.push_back(path);
return true;
}
//
///
///
bool FT_GetAllFormatFiles(const std::string path, std::vector<string>& files, std::string expression, const std::vector<string> filter_directory){
fs::path file_path = path;
if (!fs::exists(file_path)) return false;
std::regex Img(expression, std::regex_constants::syntax_option_type::icase);
if (fs::is_directory(file_path)){
for (auto f : fs::recursive_directory_iterator(file_path)){
int foundFlag = 0; //过滤文件夹
for (size_t i = 0; i < filter_directory.size(); i++){
std::string::size_type idx;
fs::path filter_directory_index = filter_directory[i];
idx = f.path().string().find(filter_directory_index.string());
if (idx != string::npos){
foundFlag = 1;
continue;
}
}
if (foundFlag == 0){
auto fname = f.path().filename().string();
if (std::regex_match(fname, Img)){
files.push_back(f.path().string());
}
}
}
}
else{
if (std::regex_match(fs::path(path).filename().string(), Img)){
files.push_back(path);
}
}
return true;
}
//
///
///
bool FT_GetAllFormatFiles(const std::vector<string> path, std::vector<string>& files, std::string format, const std::vector<string> filter_directory){
for (size_t i = 0; i<path.size(); i++){
FT_GetAllFormatFiles(path[i], files, format, filter_directory);
}
return true;
}
//
///
///
bool FT_FilterField(const std::vector<string> srcStringList, std::vector<string>& dstStringList, const std::vector<std::string> format){
if (srcStringList.size() <= 0) return false;
std::vector<std::regex> expression;
for (auto f : format){
std::regex e(f, std::regex_constants::syntax_option_type::icase);
expression.push_back(e);
}
for (auto s : srcStringList){
std::size_t count = 0;
for (std::size_t i = 0; i<expression.size(); i++){
if (std::regex_match(s, expression[i]))
count++;
}
if (count == 0)
dstStringList.push_back(s);
}
return true;
}
//
///
///
bool FT_FilterField(const std::vector<string> srcStringList, std::vector<string>& dstStringList, std::string format){
if (srcStringList.size() <= 0) return false;
std::regex expression(format, std::regex_constants::syntax_option_type::icase);
for (auto s : srcStringList) {
if (std::regex_match(s, expression)) {
}
else {
dstStringList.push_back(s);
}
}
return true;
}
//
///
///
void FT_ReadFileList(std::string filename, std::multimap<string, int>& lablefilelist){
lablefilelist.clear();
std::ifstream readData(filename, std::ios::in);
std::string buffer;
int nClass = 0;
while (readData){
if (getline(readData, buffer)){
if (buffer.size() > 0){
nClass = FT_GetClassFlag(buffer); //标签与文件路径之间有一个空格
std::string temp(buffer, 0, buffer.size() - 2);
lablefilelist.insert(make_pair(temp, nClass));
}
}
}
readData.close();
}
//
///
///
int FT_GetClassFlag(std::string strPath){
size_t len = strPath.size();
char drt = strPath[len - 1];
int temp = drt - '0';
return temp;
}
//
///
///
bool FT_ReadNoLableFileList(const std::string filename, std::vector<string>& nolablefilelist){
nolablefilelist.clear();
std::ifstream readData(filename);
std::string buffer;
while (readData){
if (getline(readData, buffer)){
nolablefilelist.push_back(buffer);
}
}
readData.close();
return true;
}
//
///此函数禁用
///
std::string FT_SelectFile(){
/*
std::string FileName;
#ifdef _UNICODE
TCHAR szBuffer[MAX_PATH] = { 0 };
OPENFILENAME ofn = { 0 };
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
//要选择的文件后缀
ofn.lpstrFilter = _T("All(*.*)\0*.*\0Text(*.txt)\0*.TXT\0\0");
//ofn.lpstrInitialDir = _T("D:\\Program Files");//默认的文件路径
ofn.lpstrFile = szBuffer;//存放文件的缓冲区
ofn.nMaxFile = sizeof(szBuffer) / sizeof(*szBuffer);
ofn.nFilterIndex = 0;
//标志如果是多选要加上OFN_ALLOWMULTISELECT
//BOOL bSel = GetOpenFileName(&ofn);
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER;
if (GetOpenFileName(&ofn)){
//wprintf(L"%s\n", ofn.lpstrFile);
//ofn.lpstrFile 是地址
//locale loc("chs");
//输出到控制台
//wcout.imbue(loc);
//wcout << ofn.lpstrFile << endl;
char szcConv[200];
memset(szcConv, 0, 200 * sizeof(char));
size_t wLen = wcslen(ofn.lpstrFile) + 1; // 宽字符字符长度,+1表示包含字符串结束符
int aLen = WideCharToMultiByte(CP_ACP, 0, ofn.lpstrFile, wLen, NULL, 0, NULL, NULL);
LPSTR lpa = new char[aLen];
WideCharToMultiByte(CP_ACP, 0, ofn.lpstrFile, wLen, lpa, aLen, NULL, NULL);
strcpy_s(szcConv, 200, lpa);
delete[] lpa;
lpa = NULL;
FileName = szcConv;
return FileName;
}
*/
/*
#endif
#ifdef _MBCS
char szFileName[MAX_PATH] = { 0 };
OPENFILENAME openFileName = { 0 };
openFileName.lStructSize = sizeof(OPENFILENAME);
openFileName.nMaxFile = MAX_PATH; //这个必须设置,不设置的话不会出现打开文件对话框
openFileName.lpstrFilter = "All(*.*)\0*.*\0Text(*.txt)\0*.TXT\0\0";
openFileName.lpstrInitialDir = "F:\\demo\\地铁燃弧\\测试数据\\GatherPath"; //默认的文件路径
openFileName.lpstrFile = szFileName;
openFileName.nFilterIndex = 1;
openFileName.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (::GetOpenFileName(&openFileName)){
//::MessageBoxA(NULL, openFileName.lpstrFile, "", MB_OK);
FileName = szFileName;
return FileName;
}
#endif
return FileName;
*/
return "";
}
//
///
///
int FT_GetFileSize(const std::string filename){
if (!fs::exists(filename)){
SetLastErrorInfo(std::string(__FUNCTION__) + ": " + filename + "目录不存在");
return -1;
}
uintmax_t filesize = fs::file_size(filename);
if (filesize != 0) return (int)filesize;
return 0;
}
//
///此函数禁用。
///
void FT_PopUpFolder(std::string filename){
//ShellExecute(NULL, "explore", filename.c_str(), NULL, NULL, SW_SHOWNORMAL);
}
//
/// 也可以根据,之前的查找方式进行查找,此函数禁用
///
void FT_GetAbsPathOfFile(const std::string path, std::string& name, std::string format){
/*
long long hFile = 0; //文件句柄
struct _finddata_t fileinfo; //文件信息
std::string p;
size_t fotmatlength = format.length();
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1){
do{
if ((fileinfo.attrib & _A_SUBDIR)){
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0){
FT_GetAbsPathOfFile(p.assign(path).append("\\").append(fileinfo.name), name, format);
}
}
else{
std::string strtemp = fileinfo.name;
if (strtemp == format){
name = (p.assign(path).append("\\").append(fileinfo.name));
return;
}
}
} while (_findnext(static_cast<intptr_t>(hFile), &fileinfo) == 0);
_findclose(static_cast<intptr_t>(hFile));
}
*/
}
}//namespace fileoperation
}//namespace operatingtools
#ifndef MD5_H
#define MD5_H
#include <string>
#include <fstream>
/* Type define */
typedef unsigned char byte;
typedef unsigned int uint32;
using std::string;
using std::ifstream;
using namespace std;
/* MD5 declaration. */
class MD5
{
public:
MD5();
MD5(const void *input, size_t length);
MD5(const string &str);
MD5(ifstream &in);
void update(const void *input, size_t length);
void update(const string &str);
void update(ifstream &in);
const byte* digest();
string toString();
void reset();
//
///
///自加函数
static string FileToMD5A(const string& file);
static string FileToMD5B(const string& file);
static string StringToMD5(const string& stringvalue);
private:
void update(const byte *input, size_t length);
void final();
void transform(const byte block[64]);
void encode(const uint32 *input, byte *output, size_t length);
void decode(const byte *input, uint32 *output, size_t length);
string bytesToHexString(const byte *input, size_t length);
/* class uncopyable */
MD5(const MD5&);
MD5& operator=(const MD5&);
private:
uint32 _state[4]; /* state (ABCD) */
uint32 _count[2]; /* number of bits, modulo 2^64 (low-order word first) */
byte _buffer[64]; /* input buffer */
byte _digest[16]; /* message digest */
bool _finished; /* calculate finished ? */
static const byte PADDING[64]; /* padding for calculate */
static const char HEX[16];
static const size_t BUFFER_SIZE = 1024;
};
#endif/*MD5_H*/
//********************************************************************************************************************************//
/* Constants for MD5Transform routine. */
#define S11 7
#define S12 12
#define S13 17
#define S14 22
#define S21 5
#define S22 9
#define S23 14
#define S24 20
#define S31 4
#define S32 11
#define S33 16
#define S34 23
#define S41 6
#define S42 10
#define S43 15
#define S44 21
/* F, G, H and I are basic MD5 functions.
*/
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | (~z)))
/* ROTATE_LEFT rotates x left n bits.
*/
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
Rotation is separate from addition to prevent recomputation.
*/
#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + ac; \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + ac; \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + ac; \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + ac; \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
const byte MD5::PADDING[64] = { 0x80 };
const char MD5::HEX[16] = {
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f'
};
/* Default construct. */
MD5::MD5() {
reset();
}
/* Construct a MD5 object with a input buffer. */
MD5::MD5(const void *input, size_t length) {
reset();
update(input, length);
}
/* Construct a MD5 object with a string. */
MD5::MD5(const string &str) {
reset();
update(str);
}
/* Construct a MD5 object with a file. */
MD5::MD5(ifstream &in) {
reset();
update(in);
}
/* Return the message-digest */
const byte* MD5::digest() {
if (!_finished) {
_finished = true;
final();
}
return _digest;
}
/* Reset the calculate state */
void MD5::reset() {
_finished = false;
/* reset number of bits. */
_count[0] = _count[1] = 0;
/* Load magic initialization constants. */
_state[0] = 0x67452301;
_state[1] = 0xefcdab89;
_state[2] = 0x98badcfe;
_state[3] = 0x10325476;
}
/* Updating the context with a input buffer. */
void MD5::update(const void *input, size_t length) {
update((const byte*)input, length);
}
/* Updating the context with a string. */
void MD5::update(const string &str) {
update((const byte*)str.c_str(), str.length());
}
/* Updating the context with a file. */
void MD5::update(ifstream &in) {
if (!in)
return;
std::streamsize length;
char buffer[BUFFER_SIZE];
while (!in.eof()) {
in.read(buffer, BUFFER_SIZE);
length = in.gcount();
if (length > 0)
update(buffer, static_cast<std::size_t>(length));
}
in.close();
}
/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
void MD5::update(const byte *input, size_t length) {
uint32 i, index, partLen;
_finished = false;
/* Compute number of bytes mod 64 */
index = (uint32)((_count[0] >> 3) & 0x3f);
/* update number of bits */
if ((_count[0] += ((uint32)length << 3)) < ((uint32)length << 3))
_count[1]++;
_count[1] += ((uint32)length >> 29);
partLen = 64 - index;
/* transform as many times as possible. */
if (length >= partLen) {
memcpy(&_buffer[index], input, partLen);
transform(_buffer);
for (i = partLen; i + 63 < length; i += 64)
transform(&input[i]);
index = 0;
}
else {
i = 0;
}
/* Buffer remaining input */
memcpy(&_buffer[index], &input[i], length - i);
}
/* MD5 finalization. Ends an MD5 message-_digest operation, writing the
the message _digest and zeroizing the context.
*/
void MD5::final() {
byte bits[8];
uint32 oldState[4];
uint32 oldCount[2];
uint32 index, padLen;
/* Save current state and count. */
memcpy(oldState, _state, 16);
memcpy(oldCount, _count, 8);
/* Save number of bits */
encode(_count, bits, 8);
/* Pad out to 56 mod 64. */
index = (uint32)((_count[0] >> 3) & 0x3f);
padLen = (index < 56) ? (56 - index) : (120 - index);
update(PADDING, padLen);
/* Append length (before padding) */
update(bits, 8);
/* Store state in digest */
encode(_state, _digest, 16);
/* Restore current state and count. */
memcpy(_state, oldState, 16);
memcpy(_count, oldCount, 8);
}
/* MD5 basic transformation. Transforms _state based on block. */
void MD5::transform(const byte block[64]) {
uint32 a = _state[0], b = _state[1], c = _state[2], d = _state[3], x[16];
decode(block, x, 64);
/* Round 1 */
FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */
FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
/* Round 2 */
GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
/* Round 3 */
HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */
HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
/* Round 4 */
II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
_state[0] += a;
_state[1] += b;
_state[2] += c;
_state[3] += d;
}
/* Encodes input (ulong) into output (byte). Assumes length is
a multiple of 4.
*/
void MD5::encode(const uint32 *input, byte *output, size_t length) {
for (size_t i = 0, j = 0; j < length; i++, j += 4) {
output[j] = (byte)(input[i] & 0xff);
output[j + 1] = (byte)((input[i] >> 8) & 0xff);
output[j + 2] = (byte)((input[i] >> 16) & 0xff);
output[j + 3] = (byte)((input[i] >> 24) & 0xff);
}
}
/* Decodes input (byte) into output (ulong). Assumes length is
a multiple of 4.
*/
void MD5::decode(const byte *input, uint32 *output, size_t length) {
for (size_t i = 0, j = 0; j < length; i++, j += 4) {
output[i] = ((uint32)input[j]) | (((uint32)input[j + 1]) << 8) |
(((uint32)input[j + 2]) << 16) | (((uint32)input[j + 3]) << 24);
}
}
/* Convert byte array to hex string. */
std::string MD5::bytesToHexString(const byte *input, std::size_t length) {
string str;
str.reserve(length << 1);
for (size_t i = 0; i < length; i++) {
int t = input[i];
int a = t / 16;
int b = t % 16;
str.append(1, HEX[a]);
str.append(1, HEX[b]);
}
return str;
}
/* Convert digest to string value */
std::string MD5::toString() {
return bytesToHexString(digest(), 16);
}
//
///
///
std::string MD5::FileToMD5A(const std::string &file) {
ifstream in(file.c_str(), ios::binary);
if (!in)
return "";
MD5 md5;
md5.reset();
std::streamsize length;
char buffer[1024];
while (!in.eof()) {
in.read(buffer, 1024);
length = in.gcount();
if (length > 0)
md5.update(buffer, static_cast<std::size_t>(length));
}
in.close();
return md5.toString();
}
//
///
///
std::string MD5::FileToMD5B(const std::string &file)
{
//ifstream in(file.c_str().ios::binary);
MD5 md5;
md5.reset();
md5.update(ifstream(file.c_str(), ios::binary));
return md5.toString();
}
//
///
///
std::string MD5::StringToMD5(const std::string& stringvalue)
{
MD5 md5;
md5.reset();
md5.update(stringvalue);
return md5.toString();
}