AcePerl
view release on metacpan or search on metacpan
acelib/filsubs.c view on Meta::CPAN
* Richard Durbin (MRC LMB, UK) rd@mrc-lmb.cam.ac.uk, and
* Jean Thierry-Mieg (CRBM du CNRS, France) mieg@kaa.cnrs-mop.fr
*
* Description:
* cross platform file system routines
*
* Exported functions:
* HISTORY:
* Last edited: Jan 5 16:36 1999 (fw)
* * Dec 8 10:20 1998 (fw): new function filAge to determine time since
* last modification of file
* * Oct 22 16:17 1998 (edgrif): Replace unsafe strtok with strstr.
* * Oct 15 11:47 1998 (fw): include messSysErrorText in some messges
* * Sep 30 09:37 1998 (edgrif): Replaced my strdup with acedb strnew.
* * Sep 9 14:07 1998 (edgrif): Add filGetFilename routine that will
* return the filename given a pathname
* (NOT the same as the UNIX basename).
* * DON'T KNOW WHO DID THE BELOW..assume Richard Bruskiewich (edgrif)
* - fix root path detection for default drives (in WIN32)
* * Oct 8 23:34 1996 (rd)
* filDirectory() returns a sorted Array of character
* strings of the names of files, with specified ending
* and spec's, listed in a given directory "dirName";
* If !dirName or directory is inaccessible,
* the function returns 0
* * Jun 6 17:58 1996 (rd)
* * Mar 24 02:42 1995 (mieg)
* * Feb 13 16:11 1993 (rd): allow "" endName, and call getwd if !*dname
* * Sep 14 15:57 1992 (mieg): sorted alphabetically
* * Sep 4 13:10 1992 (mieg): fixed NULL used improperly when 0 is meant
* * Jul 20 09:35 1992 (aochi): Add directory names to query file chooser
* * Jan 11 01:59 1992 (mieg): If file has no ending i suppress the point
* * Nov 29 19:15 1991 (mieg): If file had no ending, we were losing the
last character in dirDraw()
* Created: Fri Nov 29 19:15:34 1991 (mieg)
*-------------------------------------------------------------------
*/
/* $Id: filsubs.c,v 1.2 2002/11/24 19:27:20 lstein Exp $ */
#include "regular.h"
#include "mytime.h"
#include "call.h" /* for callScript (to mail stuff) */
/********************************************************************/
#include "mydirent.h"
#if !defined(WIN32)
/* UNIX */
#include <sys/file.h>
#define HOME_DIR_ENVP "HOME"
#define ABSOLUTE_PATH(path) *path == SUBDIR_DELIMITER
#else /* Utility macros for WIN32 only */
#include <tchar.h>
#include <direct.h> /* for getwcd() and _getdrive() */
/* simple, single letter logical drives assumed here */
static const char *DRIVES = "abcdefghijklmnopqrstuvwxyz";
#define DRIVE_NO(drv) ((drv)-'a'+1)
#define GET_CURRENT_DRIVE *( DRIVES + _getdrive() - 1 )
#define HOME_DIR_ENVP "HOMEPATH"
#include <ctype.h> /* for isalpha() */
#define ABSOLUTE_PATH(path) \
( isalpha( (int)*path ) ) && \
(*(path+1) == DRIVE_DELIMITER) && \
(*(path+2) == SUBDIR_DELIMITER)
#endif /* WIN32 */
/********************************************************************/
static Stack dirPath = 0 ;
UTIL_FUNC_DEF void filAddDir (char *s) /* add to dirPath */
{
char *home ;
if (!dirPath)
dirPath = stackCreate (128) ;
/* if the user directory is specified */
if (*s == '~' &&
(home = getenv (HOME_DIR_ENVP))) /* substitute */
{
#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) ;
acelib/filsubs.c view on Meta::CPAN
return FALSE ;
}
/************************************************/
static char *filDoName (char *name, char *ending, char *spec, BOOL strict)
{
static Stack part = 0, full = 0 ;
char *dir, *result ;
#if defined(WIN32)
char *cp, buf2[2] ;
static char driveStr[3] = { 'C', DRIVE_DELIMITER, '\0' },
*pDriveStr = driveStr ;
#endif
if (!name)
messcrash ("filName received a null name") ;
if (!part)
{ part = stackCreate (128) ;
full = stackCreate (MAXPATHLEN) ;
}
stackClear (part) ;
#if defined(WIN32)
/* convert '/' => '\\' in path string */
cp = name ; buf2[1] = 0 ;
while (*cp)
{
if (*cp == '/')
catText (part, "\\") ;
else
{ buf2[0] = *cp; catText (part, buf2) ; }
cp++ ;
}
#else
catText (part, name) ;
#endif
if (ending && *ending)
{ catText (part, ".") ;
catText (part, ending) ;
}
/* NB filName is reentrant in the sense that it can be called
on strings it generates, because they first get copied into
part, and then the new name is constructed in full.
*/
if (ABSOLUTE_PATH(name))
{
stackClear (full) ;
catText (full, stackText (part, 0)) ;
result = stackText (full, 0) ;
if (filCheck (result, spec))
return result ;
else
return 0 ;
}
#if defined(WIN32)
/* Check if path name is a root path
hence on the default logical drive */
if( name[0] == SUBDIR_DELIMITER )
{
stackClear (full) ;
driveStr[0] = GET_CURRENT_DRIVE ;
catText (full, pDriveStr ) ;
catText (full, stackText (part, 0)) ;
result = stackText (full, 0) ;
if (filCheck (result, spec))
return result ;
else
return 0 ;
}
#endif
if (!dirPath) /* add cwd as default to search */
filAddDir (getwd (stackText (full, 0))) ;
stackCursor (dirPath, 0) ;
while ((dir = stackNextText (dirPath)))
{
stackClear (full) ;
catText (full, dir) ;
catText (full, stackText (part, 0)) ;
result = stackText (full, 0) ;
if (filCheck (result, spec))
return result ;
if (strict)
break ;
}
return 0 ;
} /* filDoName */
/************************************************************/
UTIL_FUNC_DEF char *filName (char *name, char *ending, char *spec)
{ return filDoName(name, ending, spec, FALSE) ; }
/************************************************************/
UTIL_FUNC_DEF char *filStrictName (char *name, char *ending, char *spec)
{ return filDoName(name, ending, spec, TRUE) ; }
/************************************************************/
UTIL_FUNC_DEF BOOL filremove (char *name, char *ending)
/* TRUE if file is deleted. -HJC*/
{
char *s = filName (name, ending, "r") ;
if (s)
return unlink(s) ? FALSE : TRUE ;
else
return FALSE ;
} /* filremove */
/************************************************************/
UTIL_FUNC_DEF FILE *filopen (char *name, char *ending, char *spec)
{
char *s = filName (name, ending, spec) ;
FILE *result = 0 ;
( run in 2.084 seconds using v1.01-cache-2.11-cpan-99c4e6809bf )