⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fnsplit.h

📁 zip解压源码.
💻 H
字号:
/*
    filename:   fnsplit.h
    creater:    liuhaifeng
    date:       2000-07-27
    description:
*/

#include "KString.h"

#define WILDCARDS 0x01
#define EXTENSION 0x02
#define FILENAME  0x04
#define DIRECTORY 0x08
#define DRIVE     0x10

#define MAXPATH   MAX_PATH
#define MAXDRIVE  3
#define MAXDIR    MAX_PATH
#define MAXFILE   MAX_PATH
#define MAXEXT    MAX_PATH

/*
#define MAXPATH   80
#define MAXDRIVE  3
#define MAXDIR    66
#define MAXFILE   9
#define MAXEXT    5
*/

/*---------------------------------------------------------------------*

Name            CopyIt - copies a string to another

Usage           void pascal near CopyIt(char *dst, const char *src,
                                        unsigned maxlen)

Prototype in    local to this module

Description     copies string scr to string dst.

Return value    nothing

*---------------------------------------------------------------------*/
static void pascal near CopyIt(char *dst, const char *src, unsigned maxlen)
{
        if (dst) {
                if(StrLen(src) >= (int)maxlen)//seawind modified,add a int
                {
                        StrLCopy(dst, src, maxlen);
                        dst[maxlen] = 0;
                }
                else
                        StrCpy(dst, src);
        }
}

/*---------------------------------------------------------------------*

Name            DotFound - checks for special dir name cases

Usage           int pascal near DotFound(char *pB);

Prototype in    local to this module

Description     checks for special directory names

*---------------------------------------------------------------------*/
static  int pascal near DotFound(char *pB)
{
        if (*(pB-1) == '.')
                pB--;
        switch (*--pB) {
        case ':'  :
                if (*(pB-2) != '\0')
                        break;
        case '/'  :
        case '\\' :
        case '\0' :
                return 1;
        }
        return 0;
}

/*---------------------------------------------------------------------*

Name            _fnsplit - splits a full path name into its components

Usage           #include <dir.h>
                int _fnsplit(const char *path, char * drive, char * dir,
                             char * name, char * ext);

                void _splitpath(const char *path, char * drive, char * dir,
                             char * name, char * ext);

Related
functions usage void _makepath(char *path, const char *drive, const char *dir,
                            const char *name, const char *ext);

Prototype in    dir.h

Description     _fnsplit takes a file's full path name (path) as a string
                in the form

                        X:\DIR\SUBDIR\NAME.EXT

                and splits path into its four components. It then stores
                those components in the strings pointed to by drive, dir,
                name and ext. (Each component is required but can be a
                NULL, which means the corresponding component will be
                parsed but not stored.)

                The maximum sizes for these strings are given by the
                constants MAXDRIVE, MAXDIR, MAXPATH, MAXNAME and MAXEXT,
                (defined in dir.h) and each size includes space for the
                null-terminator.

                        Constant        (Max.)  String

                        MAXPATH         (80)    path
                        MAXDRIVE        (3)     drive; includes colon (:)
                        MAXDIR          (66)    dir; includes leading and
                                                trailing backslashes (\)
                        MAXFILE         (9)     name
                        MAXEXT          (5)     ext; includes leading dot (.)

                _fnsplit assumes that there is enough space to store each
                non-NULL component. fnmerge assumes that there is enough
                space for the constructed path name. The maximum constructed
                length is MAXPATH.

                When _fnsplit splits path, it treats the punctuation as
                follows:

                * drive keeps the colon attached (C:, A:, etc.)

                * dir keeps the leading and trailing backslashes
                  (\turboc\include\,\source\, etc.)

                * ext keeps the dot preceding the extension (.c, .exe, etc.)

                _splitpath is an MSC-compatible function that is identical
                to _fnsplit, except that is doesn't return a value.

                _splitpath (or _fnsplit) and _makepath are invertible; if you
                split a given path with _splitpath (or _fnsplit), then
                merge the resultant components with _makepath, you end up
                with path.

Return value    _fnsplit returns an integer (composed of five flags,
                defined in dir.h) indicating which of the full path name
                components were present in path; these flags and the components
                they represent are:

                        EXTENSION       an extension
                        FILENAME        a filename
                        DIRECTORY       a directory (and possibly
                                        sub-directories)
                        DRIVE           a drive specification (see dir.h)
                        WILDCARDS       wildcards (* or ? cards)

                _splitpath does not return a value.

*---------------------------------------------------------------------*/
int __cdecl _fnsplit(const char *pathP, char *driveP, char *dirP,
                             char *nameP, char *extP)
{
        register char   *pB;
        register int    Wrk;
        int     Ret;

        char buf[ MAXPATH+2 ];

        /*
          Set all string to default value zero
        */
        Ret = 0;
        if (driveP)
                *driveP = 0;
        if (dirP)
                *dirP = 0;
        if (nameP)
                *nameP = 0;
        if (extP)
                *extP = 0;

        /*
          Copy filename into template up to MAXPATH characters
        */
        pB = buf;
        while (*pathP == ' ')
                pathP++;
        if ((Wrk = StrLen(pathP)) > MAXPATH)
                Wrk = MAXPATH;
        *pB++ = 0;
        StrLCopy(pB, pathP, Wrk);
        *(pB += Wrk) = 0;

        /*
          Split the filename and fill corresponding nonzero pointers
        */
        Wrk = 0;
        for (; ; ) {
                switch (*--pB) {
                case '.'  :
                        if (!Wrk && (*(pB+1) == '\0'))
                                Wrk = DotFound(pB);
                        if ((!Wrk) && ((Ret & EXTENSION) == 0)) {
                                Ret |= EXTENSION;
                                CopyIt(extP, pB, MAXEXT - 1);
                                *pB = 0;
                        }
                        continue;
                case ':'  :
                        if (pB != &buf[2])
                                continue;
                case '\0' :
                        if (Wrk) {
                                if (*++pB)
                                        Ret |= DIRECTORY;
                                CopyIt(dirP, pB, MAXDIR - 1);
                                *pB-- = 0;
                                break;
                        }
                case '/'  :
                case '\\' :
                        if (!Wrk) {
                                Wrk++;
                                if (*++pB)
                                        Ret |= FILENAME;
                                CopyIt(nameP, pB, MAXFILE - 1);
                                *pB-- = 0;
                                if (*pB == 0 || (*pB == ':' && pB == &buf[2]))
                                        break;
                        }
                        continue;
                case '*'  :
                case '?'  :
                        if (!Wrk)
                                Ret |= WILDCARDS;
                default :
                        continue;
                }
                break;
        }
        if (*pB == ':') {
                if (buf[1])
                        Ret |= DRIVE;
                CopyIt(driveP, &buf[1], MAXDRIVE - 1);
        }

        return (Ret);
}

/*
void __cdecl _splitpath(const char *pathP, char *driveP, char *dirP,
                         char *nameP, char *extP)
{
        (void)_fnsplit(pathP,driveP,dirP,nameP,extP);
}
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -