zoukankan      html  css  js  c++  java
  • One splitpath implementation (platform independent)


    #include "stdio.h"

    #include "stdlib.h"
    #include 
    "wchar.h"

    typedef wchar_t WCHAR;

    void _wsplitpath(const WCHAR* path, WCHAR* drv, WCHAR* dir, WCHAR* name, WCHAR* ext)
    {
        
    const WCHAR* end; /* end of processed string */
        
    const WCHAR* p;      /* search pointer */
        
    const WCHAR* s;      /* copy pointer */
        
        
    /* extract drive name */
        
    if (path[0&& path[1]==':') {
            
    if (drv) {
                
    *drv++ = *path++;
                
    *drv++ = *path++;
                
    *drv = '\0';
            }
        } 
    else if (drv)
            
    *drv = '\0';
        
        
    /* search for end of string or stream separator */
        
    for(end=path; *end && *end!=':'; )
            end
    ++;
        
        
    /* search for begin of file extension */
        
    for(p=end; p>path && *--p!='\\' && *p!='/'; )
            
    if (*== '.') {
                end 
    = p;
                
    break;
            }
        
        
    if (ext)
            
    for(s=end; (*ext=*s++); )
                ext
    ++;
        
        
    /* search for end of directory name */
        
    for(p=end; p>path; )
            
    if (*--p=='\\' || *p=='/') {
                p
    ++;
                
    break;
            }
        
        
    if (name) {
            
    for(s=p; s<end; )
                
    *name++ = *s++;
            
            
    *name = '\0';
        }
        
        
    if (dir) {
            
    for(s=path; s<p; )
                
    *dir++ = *s++;
            
            
    *dir = '\0';
        }
    }

    #define _MAX_DRIVE 255
    #define _MAX_DIR 255
    #define _MAX_FNAME 255
    #define _MAX_EXT 255

    int main()    // test splipath()
    {
        wchar_t drv[_MAX_DRIVE
    +1], dir[_MAX_DIR], name[_MAX_FNAME], ext[_MAX_EXT];
        
        _wsplitpath(L
    "/Perforce/sandbox/flower.jpg", drv, dir, name, ext);
        wprintf(L
    "drv = %S, dir = %S, filename = %S, ext = %S \n", drv, dir, name, ext);
        _wsplitpath(L
    "C:\\Program files\\sandbox\\flower.jpg", drv, dir, name, ext);
        wprintf(L
    "drv = %S, dir = %S, filename = %S, ext = %S \n", drv, dir, name, ext);
        _wsplitpath(L
    "\\x", drv, dir, name, ext);
        _wsplitpath(L
    "x", drv, dir, name, ext);
        _wsplitpath(L
    "", drv, dir, name, ext);
        _wsplitpath(L
    ".x", drv, dir, name, ext);
        _wsplitpath(L
    ":x", drv, dir, name, ext);
        _wsplitpath(L
    "a:x", drv, dir, name, ext);
        _wsplitpath(L
    "a.b:x", drv, dir, name, ext);
        _wsplitpath(L
    "W:\\/\\abc/Z:~", drv, dir, name, ext);
        _wsplitpath(L
    "abc.EFGH:12345", drv, dir, name, ext);
        _wsplitpath(L
    "C:/dos/command.com", drv, dir, name, ext);
        
        
    return 0;
    }

  • 相关阅读:
    react条件渲染
    js用replaceAll全部替换的方法
    批量重命名图片,去除括号
    [转]自定义alert弹框,title不显示域名
    [转]ASCII码表及扩展ASCII码表,方便查阅
    [转]PHP中file_put_contents追加和换行
    Response
    Redirect
    Request
    Paginator
  • 原文地址:https://www.cnblogs.com/SunWentao/p/1262844.html
Copyright © 2011-2022 走看看