4,436
社区成员
发帖
与我相关
我的任务
分享
bool rename(const std::string &from, const std::string &to) {
std::map<std::string, file_node>::iterator it = map_.find(from);
if(it == map_.end()) {
std::map<std::string, dir_node>::iterator dit = dir_map_.find(from);
if(dit == dir_map_.end()) {
return false;
}else {
std::map<std::string, dir_node>::iterator dit;
for(dit = dir_map_.begin(); dit != dir_map_.end(); dit++) {
std::string dtmp = dit->first;
if(!dtmp.find(from)) {/* find "path" at 0, means key is start with "path" */
dtmp.erase(0, from.length());
if(!dtmp.find("/") || dtmp.empty()) {
/* find "/" at 0, means not other dir that name start with "path" */
fprintf(stderr, "\e[1;31m\e[4m rename [%s] ", dit->first.c_str());
dtmp = to + dtmp;
fprintf(stderr, "to [%s]\e[m\n", dtmp.c_str());
dir_node dn(new dir_node_n(dtmp));
dir_map_.insert(std::make_pair(dtmp, dn));
dir_map_.erase(dit);
}
}
}
return true;
}
}else {
map_.insert(std::make_pair(to, it->second));
map_.erase(it);
return true;
}
}