You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
5.6 KiB

#include <iostream>
#include <boost/filesystem.hpp>
#include <vector>
#include <string>
#define K_VISION_DIR "/hard/k_vision"
#define AI_VISION_DIR "/hard/ai_vision"
namespace fs = boost::filesystem;
std::vector<std::string> get_file_names(const std::string& strDir)
{
std::vector<std::string> vs;
fs::path path(strDir);
if (!fs::exists(path))
{
return vs;
}
fs::directory_iterator dir_iter(path);
for (auto it:dir_iter)
{
if (fs::is_regular_file(it) && fs::exists(it))
{
vs.push_back(it.path().filename().string());
}
}
return vs;
}
std::vector<std::string> get_file_paths(const std::string& strDir)
{
std::vector<std::string> vs;
fs::path path(strDir);
if (!fs::exists(path))
{
return vs;
}
fs::directory_iterator dir_iter(path);
for (auto it:dir_iter)
{
if (fs::is_regular_file(it) && fs::exists(it))
{
vs.push_back(it.path().string());
}
}
return vs;
}
std::vector<std::string> get_search_paths(const std::string& strFileName, int nBeginTime, int nEndTime)
{
std::vector<std::string> vs;
if (strFileName.empty())
{
return vs;
}
std::string strDate = strFileName.substr(0,13);
int date[6] = {0};
sscanf(strDate.c_str(),"%02d%02d%02d_%02d%02d%02d",&date[0],&date[1],&date[2],&date[3],&date[4],&date[5]);
struct tm input_time = {0};
input_time.tm_year = date[0] + 2000 - 1900;
input_time.tm_mon = date[1] - 1;
input_time.tm_mday = date[2];
input_time.tm_hour = date[3];
input_time.tm_min = date[4];
input_time.tm_sec = date[5];
time_t tt[3] = {0};
tt[0] = mktime(&input_time);
tt[1] = tt[0] + nBeginTime;
tt[2] = tt[0] + nEndTime;
tm* tm_begin = localtime(&tt[1]);
char szPathBegin[255];
snprintf(szPathBegin, sizeof(szPathBegin),"/ssd/ok/%d%02d%02d/%d%02d%02d_%02d/%d%02d%02d_%02d%02d/",
tm_begin->tm_year + 1900, tm_begin->tm_mon + 1, tm_begin->tm_mday,
tm_begin->tm_year + 1900, tm_begin->tm_mon + 1, tm_begin->tm_mday, tm_begin->tm_hour,
tm_begin->tm_year + 1900, tm_begin->tm_mon + 1, tm_begin->tm_mday, tm_begin->tm_hour, tm_begin->tm_min);
tm* tm_end = localtime(&tt[2]);
char szPathEnd[255];
snprintf(szPathEnd, sizeof(szPathEnd),"/ssd/ok/%d%02d%02d/%d%02d%02d_%02d/%d%02d%02d_%02d%02d/",
tm_end->tm_year + 1900, tm_end->tm_mon + 1, tm_end->tm_mday,
tm_end->tm_year + 1900, tm_end->tm_mon + 1, tm_end->tm_mday, tm_end->tm_hour,
tm_end->tm_year + 1900, tm_end->tm_mon + 1, tm_end->tm_mday, tm_end->tm_hour, tm_end->tm_min);
if (std::string(szPathBegin) == std::string(szPathEnd))
{
vs.push_back(szPathBegin);
}
else
{
vs.push_back(szPathBegin);
vs.push_back(szPathEnd);
}
return vs;
}
std::string cvt_filename_to_datetime(const std::string& strFileName)
{
std::string str;
if (strFileName.empty())
{
return str;
}
std::string strDate = strFileName.substr(0,13);
int date[6] = {0};
sscanf(strDate.c_str(),"%02d%02d%02d_%02d%02d%02d",&date[0],&date[1],&date[2],&date[3],&date[4],&date[5]);
char szText[255];
snprintf(szText, sizeof (szText), "%d%02d%02d_%02d%02d%02d_",
date[0] + 2000, date[1], date[2], date[3], date[4], date[5]);
str = szText;
return str;
}
int main(int argc, char* argv[]) {
int nParam[2] = {0};
if (argc != 3)
{
std::cout << "error: input param invalid" << std::endl;
std::cout << "usage: ./ajx_find_files 8 12" << std::endl;
return -1;
}
else
{
nParam[0] = std::atoi(argv[1]);
nParam[1] = std::atoi(argv[2]);
std::cout << "command-line: " << argv[0] << " " << argv[1] << " " << argv[2] << std::endl;
}
auto tt = time(nullptr);
auto tm = localtime(&tt);
char szSaveDir[255];
snprintf(szSaveDir, sizeof (szSaveDir), "%s/%d%02d%02d/",
AI_VISION_DIR, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
fs::create_directories(szSaveDir);
auto vec_file_name = get_file_names(K_VISION_DIR);
if (vec_file_name.empty())
{
std::cout << "error: K_VISION_DIR is empty" << std::endl;
return -1;
}
std::vector<std::string> vec_matched_path;
std::cout << "info: >>> match file begin <<<" << std::endl;
int j = 0;
for (auto it:vec_file_name)
{
++j;
auto datetime = cvt_filename_to_datetime(it);
auto vec_search_path = get_search_paths(it, nParam[0], nParam[1]);
for (auto dir:vec_search_path)
{
auto vec_file_path = get_file_paths(dir);
for (auto path:vec_file_path)
{
if (path.find(datetime) != std::string::npos)
{
vec_matched_path.push_back(path);
}
}
}
std::cout << "info: match progress " << j << "/" << vec_file_name.size() << std::endl;
}
std::cout << "info: >>> match file end(" << vec_matched_path.size() << ") <<<" << std::endl;
std::cout << "info: >>> copy file begin <<<" << std::endl;
int i = 0;
for (auto it:vec_matched_path)
{
fs::path path(it);
auto strName = path.filename().string();
std::string strPath = std::string(szSaveDir) + "/" + strName;
fs::copy_file(it,strName,fs::copy_option::overwrite_if_exists);
++i;
if (i % 50 == 0)
{
std::cout << "info: copy progress " << i << "/" << vec_matched_path.size() << std::endl;
}
}
std::cout << "info: >>> copy file end(" << i << ") <<<" << std::endl;
return 0;
}