#include <iostream>
#include <arpa/inet.h>
#include <string.h>
using namespace std;
void xy_log1(const char *path, bool &is_ok)
{
if (path == nullptr)
return;
if (strlen(path) == 0)
return;
// cout << path << endl;
path++;
xy_log1(path, is_ok);
path--;
if (path[0] == '/' && is_ok == false)
{
cout << path+1 << endl;
is_ok = true;
}
}
void xy_log2(const char *path, char **is_ok_p)
{
if (path == nullptr)
return;
if (strlen(path) == 0)
return;
// cout << path << endl;
path++;
xy_log2(path, is_ok_p);
path--;
if (path[0] == '/' && *is_ok_p == nullptr)
{
//cout << path << endl;
*is_ok_p = (char *)path+1;
}
}
const char *xy_log3(const char *path)
{
// 0、去除非法
if (path == nullptr)
return path;
// 1、设定递归基
if (strlen(path) == 0)
return nullptr;
// 2、完成递的过程
path++;
auto ret = xy_log3(path);
// 3、完成归的过程
path--;
if (path[0] == '/' && ret == nullptr)
{
ret = (char *)path+1;
}
return ret;
}
int main(int argc, char const *argv[])
{
{
bool is_ok = false;
xy_log1("/etc/xxx/a.cpp", is_ok);
}
{
char *is_ok_p = nullptr;
xy_log2("/etc/xxx/a.cpp", &is_ok_p);
cout << is_ok_p << endl;
}
{
cout << xy_log3("/etc/xxx/a.cpp") << endl;
}
return 0;
}
➜ ~ g++ -std=c++11 test.cpp -o test
➜ ~ ./test
a.cpp
a.cpp
a.cpp
备份地址: 【递归实现basename命令】