AcePerl
view release on metacpan or search on metacpan
acelib/filsubs.c view on Meta::CPAN
return filopen (*nameptr, 0, spec) ;
} /* filtmpopen */
/************************************************************/
UTIL_FUNC_DEF BOOL filtmpremove (char *name) /* delete and free() */
{ BOOL result = filremove (name, 0) ;
free (name) ; /* NB free since allocated by tempnam */
assRemove (tmpFiles, name) ;
return result ;
}
/************************************************************/
UTIL_FUNC_DEF void filtmpcleanup (void)
{ char *name = 0 ;
if (tmpFiles)
while (assNext (tmpFiles, &name, 0))
{ filremove (name, 0) ;
free (name) ;
}
}
/************* filqueryopen() ****************/
static QueryOpenRoutine queryOpenFunc = 0 ;
UTIL_FUNC_DEF QueryOpenRoutine filQueryOpenRegister (QueryOpenRoutine new)
{ QueryOpenRoutine old = queryOpenFunc ; queryOpenFunc = new ; return old ; }
UTIL_FUNC_DEF FILE *filqueryopen (char *dname, char *fname, char *end, char *spec, char *title)
{
Stack s ;
FILE* fil = 0 ;
int i ;
/* use registered routine if available */
if (queryOpenFunc)
return (*queryOpenFunc)(dname, fname, end, spec, title) ;
/* otherwise do here and use messprompt() */
s = stackCreate(50);
if (dname && *dname)
{ pushText(s, dname) ; catText(s,"/") ; }
if (fname)
catText(s,fname) ;
if (end && *end)
{ catText(s,".") ; catText(s,end) ; }
lao:
if (!messPrompt("File name please", stackText(s,0), "w"))
{ stackDestroy(s) ;
return 0 ;
}
i = stackMark(s) ;
pushText(s, freepath()) ; /* freepath needed by WIN32 */
if (spec[0] == 'w' &&
(fil = fopen (stackText(s,i), "r")))
{ if ( fil != stdin && fil != stdout && fil != stderr)
fclose (fil) ;
fil = 0 ;
if (messQuery (messprintf ("Overwrite %s?",
stackText(s,i))))
{
if ((fil = fopen (stackText(s,i), spec)))
goto bravo ;
else
messout ("Sorry, can't open file %s for writing",
stackText (s,i)) ;
}
goto lao ;
}
else if (!(fil = fopen (stackText(s,i), spec)))
messout ("Sorry, can't open file %s",
stackText(s,i)) ;
bravo:
stackDestroy(s) ;
return fil ;
} /* filqueryopen */
/*********************************************/
static Associator mailFile = 0, mailAddress = 0 ;
UTIL_FUNC_DEF void filclose (FILE *fil)
{
char *address ;
char *filename ;
if (!fil || fil == stdin || fil == stdout || fil == stderr)
return ;
fclose (fil) ;
if (mailFile && assFind (mailFile, fil, &filename))
{ if (assFind (mailAddress, fil, &address))
callScript ("mail", messprintf ("%s %s", address, filename)) ;
else
messerror ("Have lost the address for mailfile %s", filename) ;
assRemove (mailFile, fil) ;
assRemove (mailAddress, fil) ;
unlink (filename) ;
free (filename) ;
}
} /* filclose */
/***********************************/
UTIL_FUNC_DEF FILE *filmail (char *address) /* requires filclose() */
{
char *filename ;
FILE *fil ;
if (!mailFile)
{ mailFile = assCreate () ;
mailAddress = assCreate () ;
}
if (!(fil = filtmpopen (&filename, "w")))
{ messout ("failed to open temporary mail file %s", filename) ;
return 0 ;
}
assInsert (mailFile, fil, filename) ;
assInsert (mailAddress, fil, address) ;
return fil ;
} /* filmail */
/******************* directory stuff *************************/
static int dirOrder(void *a, void *b)
{
char *cp1 = *(char **)a, *cp2 = *(char**)b;
return strcmp(cp1, cp2) ;
} /* dirOrder */
/* returns an Array of strings representing the filename in the
given directory according to the spec. "r" will list all files,
and "rd" will list all directories.
The behaviour of the "w" spec is undefined.
The array has to be destroyed using filDirectoryDestroy,
because the memory of the strings needs to be reclaimed as well. */
UTIL_FUNC_DEF Array filDirectoryCreate (char *dirName,
char *ending,
char *spec)
{
Array a ;
#if !defined(WIN32) && !defined(DARWIN)
DIR *dirp ;
char *dName, *dName_copy, entryPathName[MAXPATHLEN], *leaf ;
int dLen, endLen ;
MYDIRENT *dent ;
( run in 3.908 seconds using v1.01-cache-2.11-cpan-5735350b133 )