std cpp 相对路径转绝对路径

这篇博客介绍了一个C++函数,用于将包含特殊字符如*、?的相对路径转换为绝对路径。代码首先检查输入路径是否为空或以点开头,然后规范化路径中的斜杠,并进行路径拆分。通过处理'.'和'..',最终生成合法的绝对路径。同时,代码还包含了针对Windows和Linux路径的处理逻辑。
摘要由CSDN通过智能技术生成

网上找的有bug,自己稍微改了下,简单测试了一下,实际还可以找到各种特例,例如含有特殊字符*、?等
直接上代码


std::string ToAbsolutePath(std::string input_path)
{
 if (input_path.length() == 0)
 {
  return "";
 }
 if (input_path[0] == '.')
 {
  char buff[FILENAME_MAX]; //create string buffer to hold path
  _getcwd(buff, FILENAME_MAX);
  input_path = std::string(buff) + "/" + input_path;//以相对路径
 }
 //路径字符规范化
 const std::string old_value = "\\";
 const std::string new_value = "/";
 for (std::string::size_type pos(0); pos != std::string::npos; pos += new_value.length())
 {
  if ((pos = input_path.find(old_value, pos)) != std::string::npos)
  {
   input_path.replace(pos, old_value.length(), new_value);
  }
  else
  {
   break;
  }
 }
 //路径拆分
 std::vector<std::string> input_path_vec;
 std::string str_temp;
 size_t i_size = input_path.size();
 for (size_t i = 0; i < i_size; ++i)
 {
  if (input_path[i] != '/')
  {
   str_temp += input_path[i];
  }
  else
  {
   input_path_vec.push_back(str_temp);
   str_temp.clear();
  }
 }
 //最后一个路径为目录名或文件名
 if (str_temp.length() > 0)
 {
  input_path_vec.push_back(str_temp);
  str_temp.clear();
 }
 if (input_path_vec.size() == 0)
 {
  const bool is_windows_path = input_path.find(":") != std::string::npos;
  if (is_windows_path)
  {
   return "";
  }
  else
  {
   return "/";
  }
 }
 const std::string first_path = input_path_vec[0];
 const bool is_windows_path = first_path.find(":") != std::string::npos;
 if (input_path.find("...") != std::string::npos)//错误路径
 {
  if (is_windows_path)
  {
   return first_path;
  }
  else
  {
   return "/";
  }
 }
 //路径拼接
 std::vector<std::string> output_path_vec;
 size_t i_vec_size = input_path_vec.size();
 for (size_t j = 0; j < i_vec_size; ++j)
 {
  const std::string &str = input_path_vec[j];
  if (str == ".")
  {
   //不需要拷贝
  }
  else if (str == "..")
  {
   if (output_path_vec.size() > 0)//不判断size会导致崩溃
   {
    output_path_vec.pop_back();
   }
  }
  else{
   output_path_vec.push_back(str);
  }
 }
 if (output_path_vec.size() == 0)
 {
  if (is_windows_path)
  {
   return first_path;//windows path
  }
  else
  {
   return "/";//linux path
  }
 }
 std::string ret_str = output_path_vec[0];
 for (size_t i = 1; i < output_path_vec.size(); ++i)
 {
  ret_str = ret_str + "/" + output_path_vec[i];
 }
 if (is_windows_path)//windows path
 {
  if (ret_str.find(":") == std::string::npos)
  {
   return first_path;//路径有误,返回根目录
  }
  else
  {
   return ret_str;
  }
 }
 else
 {
  return "/" + ret_str;//linux path
 }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值