AcePerl
view release on metacpan or search on metacpan
acelib/filsubs.c view on Meta::CPAN
#if defined(WIN32) /* in WIN32, need to prefix homepath with home drive*/
char *drive;
drive = getenv ("HOMEDRIVE") ;
pushText(dirPath, drive) ;
catText(dirPath, home) ;
#else
pushText (dirPath, home) ;
#endif
catText (dirPath, ++s) ;
}
else
pushText (dirPath, s) ;
catText (dirPath, SUBDIR_DELIMITER_STR) ;
return;
} /* filAddDir */
/*********************************************/
UTIL_FUNC_DEF void filAddPath (char *cp)
{
char *cq = cp ;
while (TRUE)
{
while (*cq && *cq != PATH_DELIMITER)
++cq ;
if (*cq == PATH_DELIMITER)
{
*cq = 0 ;
filAddDir (cp) ;
cp = ++cq ;
}
else
{
filAddDir (cp) ;
break ;
}
}
return;
} /* filAddPath */
/*****************************************************************************/
/* This function returns the filename part of a given path, */
/* */
/* Given "/some/load/of/directories/filename" returns "filename" */
/* */
/* The function returns NULL for the following errors: */
/* */
/* 1) supplying a NULL ptr as the path */
/* 2) supplying "" as the path */
/* 3) supplying a path that ends in "/" */
/* */
/* NOTE, this function is _NOT_ the same as the UNIX basename command or the */
/* XPG4_UNIX basename() function which do different things. */
/* */
/* The function makes a copy of the supplied path on which to work, this */
/* copy is thrown away each time the function is called. */
/* */
/*****************************************************************************/
UTIL_FUNC_DEF char *filGetFilename(char *path)
{
static char *path_copy = NULL ;
const char *path_delim = SUBDIR_DELIMITER_STR ;
char *result = NULL, *tmp ;
if (path != NULL)
{
if (strcmp((path + strlen(path) - 1), path_delim) != 0) /* Last char = "/" ?? */
{
if (path_copy != NULL) messfree(path_copy) ;
path_copy = strnew(path, 0) ;
tmp = path ;
while (tmp != NULL)
{
result = tmp ;
tmp = strstr(tmp, path_delim) ;
if (tmp != NULL) tmp++ ;
}
}
}
return(result) ;
} /* filGetFilename */
/*****************************************************************************/
/* This function returns the file-extension part of a given path/filename, */
/* */
/* Given "/some/load/of/directories/filename.ext" returns "ext" */
/* */
/* The function returns NULL for the following errors: */
/* */
/* 1) supplying a NULL ptr as the path */
/* 2) supplying a path with no filename */
/* */
/* The function returns "" for a filename that has no extension */
/* */
/* The function makes a copy of the supplied path on which to work, this */
/* copy is thrown away each time the function is called. */
/* */
/*****************************************************************************/
UTIL_FUNC_DEF char *filGetExtension(char *path)
{
static char *path_copy = NULL ;
char *extension = NULL, *cp ;
if (path == NULL)
return NULL;
if (strlen(path) == 0)
return NULL;
if (path_copy != NULL) messfree(path_copy) ;
path_copy = messalloc ((strlen(path)+1) * sizeof(char));
strcpy (path_copy, path);
cp = path_copy + (strlen(path_copy) - 1);
while (cp > path_copy &&
*cp != SUBDIR_DELIMITER &&
*cp != '.')
--cp;
extension = cp+1;
return(extension) ;
} /* filGetExtension */
/**********************************************************************/
/* This function takes a directory name and does the following:
1. Returns the name if it is "complete"
(an absolute path on a given platform)
2. On WIN32 platforms, for onto rooted paths lacking a
drive specification, returns the directory name prefixed with
the default drive letter
3. Otherwise, assumes that the directory name resides within the
current working directory and thus, returns it prefixes the
directory name with the working directory path */
/**********************************************************************/
UTIL_FUNC_DEF char *filGetFullPath(char *dir)
{
static char *path_copy = NULL;
char *pwd ;
char dirbuf[MAXPATHLEN] ;
/* Return dir if absolute path already */
if (ABSOLUTE_PATH(dir))
{
if (path_copy)
messfree (path_copy);
path_copy = (char*) messalloc (strlen(dir) + 1) ;
strcpy (path_copy, dir) ;
return path_copy ;
}
#if defined(WIN32)
/* else if dir is a Win32 rooted path, then add current drive to rooted paths */
else if ( *dir == SUBDIR_DELIMITER )
{
( run in 1.581 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )