|
@@ -4,13 +4,26 @@
|
|
|
#include <regex>
|
|
|
#include <iosfwd>
|
|
|
|
|
|
+#include "fmtlog.h"
|
|
|
+#include "CurlFtpInfo.h"
|
|
|
+
|
|
|
+
|
|
|
+#if defined(_WIN32)
|
|
|
+
|
|
|
+#endif /* _WIN32 */
|
|
|
+
|
|
|
+/* ==================================================================================
|
|
|
+ * *********************************** 全局变量 *************************************
|
|
|
+ * ==================================================================================
|
|
|
+ */
|
|
|
|
|
|
/* 是否有初始化实例,主要用于静态函数调用curl_global_cleanup()函数前判断,有实例就不调用 */
|
|
|
static bool hasInstace = false;
|
|
|
|
|
|
/* ==================================================================================
|
|
|
* *********************************** 全局函数 *************************************
|
|
|
- * ================================================================================== */
|
|
|
+ * ==================================================================================
|
|
|
+ */
|
|
|
|
|
|
/**
|
|
|
* @brief 写入回调函数
|
|
@@ -18,25 +31,24 @@ static bool hasInstace = false;
|
|
|
* @param contents curl的数据缓冲区
|
|
|
* @param size 数据大小,单位是size_t
|
|
|
* @param nmemb size_t的单位字节数
|
|
|
- * @param s 用户提供的字符串
|
|
|
+ * @param userStr 用户提供的字符串,格式可以是任意的
|
|
|
* @return size_t 总共获取到的数据大小,单位是字节
|
|
|
*/
|
|
|
-static size_t writeStringCallback(void *contents, size_t size, size_t nmemb, std::string *s)
|
|
|
+static size_t writeStringCallback(void *contents, size_t size, size_t nmemb, std::string *userStr)
|
|
|
{
|
|
|
- size_t newLength = size*nmemb;
|
|
|
- size_t oldLength = s->size();
|
|
|
+ size_t newLength = size * nmemb;
|
|
|
+ size_t oldLength = userStr->size();
|
|
|
try
|
|
|
{
|
|
|
- s->resize(oldLength + newLength);
|
|
|
+ userStr->resize(oldLength + newLength);
|
|
|
}
|
|
|
catch(std::bad_alloc &e)
|
|
|
{
|
|
|
//handle memory problem
|
|
|
return 0;
|
|
|
}
|
|
|
-
|
|
|
- std::copy((char*)contents,(char*)contents+newLength,s->begin()+oldLength);
|
|
|
- return size*nmemb;
|
|
|
+ std::copy_n((char*)contents, newLength, userStr->begin() + oldLength);
|
|
|
+ return size * nmemb;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -58,6 +70,86 @@ static int writeStringListCallback(void* buffer, size_t size, size_t nmemb, void
|
|
|
return size * nmemb;
|
|
|
}
|
|
|
|
|
|
+/* 使用Windows API进行编码转换 */
|
|
|
+#if defined(_WIN32)
|
|
|
+static char* GBToUTF8(const char* gb2312)
|
|
|
+{
|
|
|
+ int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
|
|
|
+ wchar_t* wstr = new wchar_t[len+1];
|
|
|
+ memset(wstr, 0, len+1);
|
|
|
+ MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
|
|
|
+ len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
|
|
|
+ char* str = new char[len+1];
|
|
|
+ memset(str, 0, len+1);
|
|
|
+ WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
|
|
|
+ if(wstr) delete[] wstr;
|
|
|
+ return str;
|
|
|
+}
|
|
|
+#endif /* _WIN32 */
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief 解析字符串文件信息
|
|
|
+ *
|
|
|
+ * @param strSrc
|
|
|
+ * @param fileList
|
|
|
+ * @return true
|
|
|
+ * @return false
|
|
|
+ */
|
|
|
+static bool parseFileInfo(const std::string& strSrc, std::vector<CF_FileInfo>& fileInfo)
|
|
|
+{
|
|
|
+#if defined(_WIN32)
|
|
|
+ auto str1 = GBToUTF8(strSrc.c_str());
|
|
|
+ std::string str2 = str1;
|
|
|
+#else
|
|
|
+ std::string str2 = strSrc;
|
|
|
+#endif /* _WIN32 */
|
|
|
+
|
|
|
+ // FMTLOG_DEBUG("\n{}", str2);
|
|
|
+ /* 正则表达式,匹配多个空格 */
|
|
|
+ std::regex reg(R"(( )+)");
|
|
|
+ /* 匹配以非空格开头的字符,空格隔开,中间任意字符,空格隔开,非空格组成的结尾
|
|
|
+ * (文件类型) ... (文件大小,$5) ... (文件名)
|
|
|
+ */
|
|
|
+ std::regex reg1(R"(^([^ ]+) (\d*) (\w*) (\w*) (\d*) (.*) ([^ ]+)$)");
|
|
|
+ // FMTLOG_INFO("\n-------------------\n");
|
|
|
+ /* 匹配换行符,分割每一行 */
|
|
|
+ std::regex reg2(R"(\n)");
|
|
|
+ /* 匹配文件夹 */
|
|
|
+ std::regex reg3(R"(^d.*)");
|
|
|
+ /* 匹配文件 */
|
|
|
+ std::regex reg4(R"(^-.*$)");
|
|
|
+
|
|
|
+ /* -1 表示对正则表达式之前的子序列感兴趣
|
|
|
+ * 0 表示对正则表达式本身感兴趣,输出的就是换行符 */
|
|
|
+ std::sregex_token_iterator it2(str2.begin(), str2.end(), reg2, -1);
|
|
|
+ /* 这里取出每一行 */
|
|
|
+ for(; it2 != std::sregex_token_iterator(); ++it2)
|
|
|
+ {
|
|
|
+ /* 去掉多余的空格 */
|
|
|
+ auto line = std::regex_replace(it2->str(), reg, " ");
|
|
|
+ // FMTLOG_INFO("{}", line);
|
|
|
+ CF_FileInfo fi;
|
|
|
+ /* 取出文件类型 */
|
|
|
+ std::string strFileType = std::regex_replace(line, reg1, "$1");
|
|
|
+ if(std::regex_match(strFileType, reg3))
|
|
|
+ {
|
|
|
+ fi.type = CF_FileType::DIR;
|
|
|
+ }else if (std::regex_match(strFileType, reg4))
|
|
|
+ {
|
|
|
+ fi.type = CF_FileType::FILE;
|
|
|
+ }
|
|
|
+ /* 取出文件大小 */
|
|
|
+ std::string strFileSize = std::regex_replace(line, reg1, "$5");
|
|
|
+ fi.size = std::stoull(strFileSize);
|
|
|
+ /* 取出文件名 */
|
|
|
+ std::string strFileName = std::regex_replace(line, reg1, "$7");
|
|
|
+ fi.name = strFileName;
|
|
|
+ /* 加入队列 */
|
|
|
+ fileInfo.push_back(fi);
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
|
|
|
|
|
|
/* ==================================================================================
|
|
@@ -308,8 +400,34 @@ bool CurlFtp::setFtpUsernameAndPassword(const std::string& username, const std::
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+/* 列出文件列表 */
|
|
|
+bool CurlFtp::getFileList(std::string dir, std::vector<std::string>& fileList)
|
|
|
+{
|
|
|
+ if(m_IP.empty() || m_port.empty())
|
|
|
+ {
|
|
|
+ FMTLOG_WARN("IP or port is empty");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if(m_curl == nullptr)
|
|
|
+ {
|
|
|
+ FMTLOG_WARN("m_curl is nullptr");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ std::vector<CF_FileInfo> listInfo;
|
|
|
+ listAll(dir, listInfo);
|
|
|
+ // for(const CF_FileInfo& fi : listInfo)
|
|
|
+ // {
|
|
|
+ // FMTLOG_INFO("type = {}, size = {}, name = {}", (int)fi.type, fi.size, fi.name);
|
|
|
+ // }
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
/* 列出文件列表 */
|
|
|
-bool CurlFtp::listAll(std::string dir, std::vector<std::string>& fileList)
|
|
|
+bool CurlFtp::listAll(std::string dir, std::vector<CF_FileInfo>& fileList)
|
|
|
{
|
|
|
if(m_IP.empty() || m_port.empty())
|
|
|
{
|
|
@@ -320,8 +438,9 @@ bool CurlFtp::listAll(std::string dir, std::vector<std::string>& fileList)
|
|
|
{
|
|
|
printf("m_curl is nullptr\n");
|
|
|
return false;
|
|
|
- }
|
|
|
+ }
|
|
|
bool result = false;
|
|
|
+ std::string strSrc;
|
|
|
/* 先设置FTP地址 */
|
|
|
std::string ftpUrl = m_ftpUrl + dir;
|
|
|
curl_easy_setopt(m_curl, CURLOPT_URL, ftpUrl.c_str());
|
|
@@ -332,24 +451,21 @@ bool CurlFtp::listAll(std::string dir, std::vector<std::string>& fileList)
|
|
|
/* 设置列出文件命令,只列出文件名称,不携带信息 */
|
|
|
// curl_easy_setopt(m_curl, CURLOPT_DIRLISTONLY, 1L);
|
|
|
/* 设置回调函数 */
|
|
|
- curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, writeStringListCallback);
|
|
|
- /* 设置需要写入的容器 */
|
|
|
- curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &fileList);
|
|
|
+ curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, writeStringCallback);
|
|
|
+ /* 设置需要写入的容器,回调函数的第四个参数 */
|
|
|
+ curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &strSrc);
|
|
|
|
|
|
/* 发送请求 */
|
|
|
m_res = curl_easy_perform(m_curl);
|
|
|
if(m_res != CURLE_OK)
|
|
|
{
|
|
|
- fprintf(stderr, "Failed to get file list, error code :%d ,%s\n", m_res, curl_easy_strerror(m_res));
|
|
|
- result = false;
|
|
|
- } else
|
|
|
- {
|
|
|
- result = true;
|
|
|
+ FMTLOG_ERROR("Failed to get file list, error code: {} ,{}", (int)m_res, curl_easy_strerror(m_res));
|
|
|
+ return false;
|
|
|
}
|
|
|
+ /* 解析字符串 */
|
|
|
+ parseFileInfo(strSrc, fileList);
|
|
|
|
|
|
-
|
|
|
- // curl_easy_cleanup(m_curl);
|
|
|
- return result;
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
|