📄 fileutil.c
字号:
* in Char.
*
* AUTHOR
*
* Kyoichiro Toda
*
*
* INPUTS
*
* filename File name
* fileext File extension
* pfname File name opinter
* pfext File extension opinter
*
* OUTPUTS
*
* 2 File name is long file name
* and use the upperbar file
* name.
* 1 File name is long file name
* and not use the upperbar
* file name.
* 0 File name is short file name.
* NUF_INVNAME Path or filename includes
* invalid character.
*
*************************************************************************/
INT pc_fileparse(UINT8 *filename, UINT8 *fileext, VOID *pfname, VOID *pfext)
{
INT16 i = 0;
UINT8 *fname;
UINT8 *fext;
INT upbar;
INT longfile = 0;
/* Defaults */
pc_memfill(filename, 8, ' ');
filename[8] = '\0';
pc_memfill(fileext, 3, ' ');
fileext[3] = '\0';
/* Check the file name and file extension. */
if (!pc_checkpath((UINT8 *)pfname, NO))
return(NUF_INVNAME);
if (!pc_checkpath((UINT8 *)pfext, NO))
return(NUF_INVNAME);
/* Setup pointer. */
fname = (UINT8 *)pfname;
fext = (UINT8 *)pfext;
/* Special cases of . and .. */
if (*fname == '.')
{
/* . */
*filename = '.';
/* .. */
if (*(fname+1) == '.')
*(++filename) = '.';
return(0);
}
/* Check use the upperbar.
Note: filename~xxx */
upbar = pc_use_upperbar(fname);
/* Setup the short filename. */
i = 1;
while (*fname)
{
/* Use upperbar ? */
if (upbar)
{
if ( (*fname != ' ') && (*fname != '.') )
{
if ( (*fname == '=') ||
(*fname == '[') ||
(*fname == ']') ||
(*fname == ';') ||
(*fname == '+') ||
(*fname == ',') )
{
*filename++ = '_';
}
else
{
if ( (*fname >= 'a') && (*fname <= 'z') )
*filename++ = (UINT8) ('A' + *fname - 'a');
else
*filename++ = *fname;
}
i++;
}
/* Filename end or upper 6 */
if ( ((fname+2) == pfext) || (i > 6) )
{
*filename++ = '~';
*filename = '1';
break;
}
}
else
{
if ( (*fname >= 'a') && (*fname <= 'z') )
{
*filename++ = (UINT8) ('A' + *fname - 'a');
if (!longfile)
longfile = 1;
}
else
*filename++ = *fname;
i++;
if (i > 8)
break;
}
fname++;
/* File name end ? */
if ((fname+1) == pfext)
break;
}
/* Setup the short fileext. */
if (fext)
{
i = 0;
while (*fext)
{
if ( (*fext != ' ') && (*fext != '.') )
{
if (i++ < 3)
{
if ( (*fext == '=') ||
(*fext == '[') ||
(*fext == ']') ||
(*fext == ';') ||
(*fext == '+') ||
(*fext == ',') )
{
*fileext++ = '_';
}
else if ( (*fext >= 'a') && (*fext <= 'z') )
{
*fileext++ = (UINT8) ('A' + *fext - 'a');
if (!longfile)
longfile = 1;
}
else
*fileext++ = *fext;
}
}
fext++;
}
}
if (upbar)
return(2);
return(longfile);
}
/************************************************************************
* FUNCTION
*
* pc_nibbleparse
*
* DESCRIPTION
*
* Nibble off the left most part of a pathspec
* Take a pathspec (no leading D:). and parse the left most element
* into filename and files ext. (SPACE right filled.).
*
* Parse a path. Return NULL if problems or a pointer to the ""
* If input path is NULL, return NULL and set NO to stat.
*
* AUTHOR
*
* Kyoichiro Toda
*
* INPUTS
*
* filename File name
* fileext File extension
* path Path name
*
* OUTPUTS
*
* Returns a pointer to the rest of the path specifier beyond
* file.ext
*
*************************************************************************/
UINT8 *pc_nibbleparse(UINT8 *topath)
{
UINT8 *p;
UINT8 *ppend;
ppend = 0;
p = topath;
/* Mark the next backslash */
while (*p)
{
if (*p == BACKSLASH)
{
ppend = p;
break;
}
p++;
}
/* Check the path end */
if (!ppend)
{
return(NULL);
}
/* Return next path */
return(ppend + 1);
}
/************************************************************************
* FUNCTION
*
* pc_parsepath
*
* DESCRIPTION
*
* Parse a path specifier into path,file,ext
* Take a path specifier in path and break it into three null
* terminated strings topath,filename and file ext.
*
* AUTHOR
*
* Kyoichiro Toda
*
* INPUTS
*
* topath Path name opinter
* pfname File name opinter
* pfext File extension opinter
* path Path name
*
* OUTPUTS
*
* NU_SUCCESS If service is successful.
* NUF_LONGPATH Path or filename too long.
* NUF_INVNAME Path or filename includes
* invalid character.
*
*
*************************************************************************/
STATUS pc_parsepath(VOID **topath, VOID **pfname, VOID **pfext, UINT8 *path)
{
UINT8 *pfr;
UINT8 *pslash;
UINT8 *pperiod;
UINT8 *temp;
INT colon;
UINT16 length;
/* Path is NULL */
if( ! path )
return NUF_BADPARM;
/* Move path pointer to local */
pfr = path;
/* Initialize local variable */
pslash = pperiod = 0;
*topath = *pfname = *pfext = '\0';
/* Check is there colon? */
if ( path[0] && path[1] == ':' )
{
colon = 1;
}
else
{
colon = 0;
}
/* Parse a path
Mark the last backslash and last period */
length = 1;
/* Get last slash position and last period position and check length. */
while (*pfr)
{
/* Is current pointer is slash ? */
if (*pfr == '\\')
pslash = pfr;
/* Is current pointer is period ? */
if (*pfr == '.')
{
/* Note: Not mark the next name period, backslash and NULL */
if ( (*(pfr+1) != '.') && (*(pfr+1) != '\\') && (*(pfr+1) != '\0') )
pperiod = pfr;
}
pfr++;
length++;
/* Check length */
if (length > EMAXPATH)
return(NUF_LONGPATH);
}
/**** Setup the path pointer ****/
if (pslash) /* Is the path include slash ? */
{
/* Yes, we assume pathname is specified. */
if (colon)
{
*topath = path + 2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -