首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >从linux文件名中获取文件名前缀

从linux文件名中获取文件名前缀

原创
作者头像
tankaro
修改2024-11-19 16:51:34
修改2024-11-19 16:51:34
2.8K0
举报
代码语言:cpp
复制
int get_prefixname_from_filename(char *fileName, char *prefixName)
{

    char *tmpIn = NULL;
    char *tmpOut = NULL;
    char *tmpPointer = NULL;
    if ((NULL == fileName) || (NULL == prefixName))
    {
        printf("[%s +%d %s] (NULL == fileName) || (NULL == prefixName) failed\n", __FILE__, __LINE__, __func__);
        return -1;
    }
    //printf("[%s +%d %s] fileName=%s prefixName=%s\n", __FILE__, __LINE__, __func__, fileName, prefixName);
    tmpIn = fileName;

    if(('.' == *tmpIn) && ('/' == *(tmpIn+1)))
    {
        tmpIn = fileName + 2;
    }
    else if(('.' == *tmpIn) && ('.' == *(tmpIn+1)) && ('/' == *(tmpIn+2)))
    {
        tmpIn = fileName + 3;

    }
    else
    {
        //if(('.' == *tmpIn) && (('.' != *(tmpIn+1)) || ('/' != *(tmpIn+1))))
        if(('.' == *tmpIn) && ('.' == *(tmpIn+1)))
        {
			printf("[%s +%d %s] fileName=%s tmpIn=%s\n", __FILE__, __LINE__, __func__, fileName, tmpIn);
            printf("[%s +%d %s] ('.' == *tmpIn) && ('.' == *(tmpIn+1)) failed\n", __FILE__, __LINE__, __func__);
            return -1;
        }
        tmpIn = fileName;
    }
    tmpOut = strchr(tmpIn, '/');
    while(1)
    {
        if(NULL == tmpOut)
        {
            break;
        }
        else
        {
            tmpIn = tmpOut + 1;
            //printf("[%s +%d %s] fileName=%s tmpIn=%s\n", __FILE__, __LINE__, __func__, fileName, tmpIn);
        }        
        tmpOut = strchr(tmpIn, '/');
    }
    fileName = tmpIn;
    tmpPointer = tmpIn;
    
    while(NULL != tmpIn)
    {
        tmpOut = strchr(tmpIn, '.');
        if(NULL == tmpOut)
        {
            break;
        }
        tmpIn = tmpOut + 1;
        tmpPointer = tmpIn;
    }

    //printf("[%s +%d %s] fileName=%s tmpPointer=%s\n", __FILE__, __LINE__, __func__, fileName, tmpPointer);
    memcpy(prefixName, fileName, tmpPointer - fileName - 1);

    return 0;
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档