ROS2源码快捷下载的办法

一、准备工作

(1)安装git,配置git环境变量。

二、思路

通过解析ros2.repos文件,提取各模块的git下载地址,通过控制台的指令调用git进行下载。

三、C++源码

#include <fstream>
#include<iostream>
#include <string>
#include <windows.h>
#include <io.h>

using namespace std;

//检查文件是否存在
bool checkFile(const char* filePath){
    //判断文件是否存在
    if(access(filePath,0)){
        return false;
    }else{
        return true;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    system("pause");

    ifstream ifs;
    ifs.open("ros2.repos",ios::in) ;
    if (!ifs.is_open()) 
    {
        cout << "*打开文件失败!" << endl;

        return 0;
    }
    else
    {
        cout << "*打开文件成功!" << endl;
    }

    string c_string;
    string dir;

    //首行
    getline(ifs,c_string);

    bool skin;

    //循环解析ros2.repos
    while(true)
    {
        skin = false;

        //第一行-目录
        if(getline(ifs,c_string))
        {

            //去除前空格
            c_string.erase(0, c_string.find_first_not_of(" "));

            //去除尾部字符
            c_string.erase(c_string.end() - 1);

            dir = c_string;

            //判断目录是否存在
            if(checkFile(dir.c_str()))
            {
                skin = true;
            }
        }
        else
        {
            break;
        }

        //第二行-类型 跳过
        if(getline(ifs,c_string))
        {
            //跳过
        }
        else
        {
            break;
        }

        //第三行-git地址
        if(getline(ifs,c_string))
        {

            if(!skin)
            {
                //提取git地址
                int start_pos = c_string.find(":") + 1;
                string s_url = c_string.substr(start_pos, c_string.length() - start_pos + 1);
                string s_git = "git clone ";
                string s_cmd = s_git + s_url + " " + dir;

                //执行指令
                system(s_cmd.c_str());

            }


        }
        else
        {
            break;
        }

        //第四行-版本
        if(getline(ifs,c_string))
        {
            //跳过
        }
        else
        {
            break;
        }
    }

    

    ifs.close();

    system("pause");

    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大笨象、小笨熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值