home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
smapp100.zip
/
sm10.zip
/
smggenrl.c
< prev
next >
Wrap
C/C++ Source or Header
|
2000-05-14
|
35KB
|
1,336 lines
/* ------------------------------------------------------------------------
*
* File: smggenrl.c
* Project: Source Mapper.
* Created: Mai 20, 1994.
* Description: This module implements a number of general helper
* functions.
*
* Copyright (C) 2000 Leif-Erik Larsen.
* This file is part of the Source Mapper source package.
* Source Mapper is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, in version 2 as it comes in the
* "COPYING" file of the XWorkplace main distribution.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* ------------------------------------------------------------------------ */
#include "smg.h"
#define MAXFILES 16
typedef struct
{
char name[MAXPATH + 1]; /* Navn til fil */
int handle; /* Handle nr til aktuelt filnavn */
int open; /* TRUE hvis record er åpen fil */
}
FnamesTYPE;
static FnamesTYPE Fnames[MAXFILES];
static int FILEc = 0; /* Antall åpne filer for ¢yeblikket */
int ResetFNames ( FILE *stream )
/* Opprettet: Tirsdag 2. mars 1993.
Parameter: "stream" Fil som skal resettes fra struktur for filnavn.
Retur : E/O.
Beskriv : Nullstiller oppgitt element til global statisk struktur for
lagring av filnavn.
*/
{
int c1 = MAXFILES;
while (c1--) /* Test alle filnavn i struktur */
{
if (Fnames[c1].open == TRUE &&
Fnames[c1].handle == fileno (stream)) /* Hvis aktuell fil */
{
Fnames[c1].open = FALSE; /* TRUE hvis filhandle er åpen */
FILEc--;
return (OK);
}
}
return (ERROR);
} /* ResetFNames (); */
void ResetAllFNames ( void )
/* Opprettet: Tirsdag 2. mars 1993.
Parameter:
Retur :
Beskriv : Nullstiller global statisk struktur for lagring av
filnavn.
*/
{
int c1 = MAXFILES;
while (c1--) /* Alle filnavn i struktur */
Fnames[c1].open = FALSE; /* TRUE hvis filhandle er åpen */
FILEc = 0;
} /* ResetAllFNames (); */
int SetFNames ( const char *fname, FILE *stream )
/* Opprettet: Tirsdag 2. mars 1993.
Parameter: "fnames" Navn til fil.
"stream" Fil som oppgitt filnavn tilh¢rer.
Retur : E/O.
Beskriv : Lagrer oppgitt filnavn m/handle til global statisk struktur
for dette.
*/
{
int c1 = MAXFILES;
while (c1--) /* Alle filnavn i struktur */
{
if (Fnames[c1].open == FALSE) /* Hvis ledig element her */
{
strcpy (Fnames[c1].name, fname); /* Lagre filnavn */
Fnames[c1].handle = fileno (stream); /* Filhandle til åpen fil */
Fnames[c1].open = TRUE; /* Bekrefter at element er opptatt */
FILEc++;
return (OK); /* Vellykket lagring av filnavn */
}
}
/* Ikke plass til filnavn i struktur */
return (ERROR);
} /* SetFNames (); */
char *GetFNames ( FILE *stream )
/* Opprettet: Tirsdag 2. mars 1993.
Parameter: "stream" Fil som det skal finnes filnavn til.
Retur : NULL hvis feil, ellers aktuelt filnavn.
Beskriv : Finner og returnerer filnavn til oppgitt fil.
*/
{
int c1 = MAXFILES;
while (c1--) /* Sjekk alle filnavn i struktur */
{
if (Fnames[c1].open == TRUE && /* Hvis elementet tilh¢rer åpen fil */
Fnames[c1].handle == fileno (stream)) /* Hvis riktig fil */
{
return (Fnames[c1].name); /* Returner navn til aktuell handle */
}
}
return (NULL); /* Ulovlig handle */
} /* GetFNames (); */
long FLength ( FILE *stream )
/* Opprettet: Onsdag 10. juni 1992.
Parameter: "stream" Fil som funksjonen skal finne st¢rrelsen til.
Retur : Lengden (antall byte) i oppgitt fil, eller -1L ved feil.
Beskriv : Finner og returnerer lengden (antall byte) i oppgitt fil.
Aktiv filposisjon vil forbli uforandret ved retur.
*/
{
long curpos, length;
errno = 0;
if ((curpos = FTell (stream)) == -1L ||
!FSeek (stream, 0L, SEEK_END) ||
(length = FTell (stream)) == -1L ||
!FSeek (stream, curpos, SEEK_SET))
{
_iErrNo = errno; /* Remember error message from system */
return (-1L);
}
return (length);
} /* FLength (); */
FILE *FOpen ( const char *filename, char *mode )
/* Opprettet: Mandag 8. juni 1992.
Beskriv : Se standard funksjon: fopen ();
*/
{
FILE *ret;
char cMode[6];
#if __IBMC__ || __IBMCPP__
if (strcmp (mode, "rt") == 0)
strcpy (cMode, "r");
else
if (strcmp (mode, "wt") == 0)
strcpy (cMode, "w");
else
strcpy (cMode, mode);
#else
strcpy (cMode, mode);
#endif
errno = 0;
if ((ret = fopen (filename, cMode)) == NULL)
{
_iErrNo = errno; /* Remember error message from system */
return (NULL);
}
/* Lagre navn til fil som herved er åpnet: */
if (!SetFNames (filename, ret))
{
fclose (ret);
return NULL;
}
return (ret);
} /* FOpen (); */
int FClose ( FILE *stream )
/* Opprettet: Onsdag 10. juni 1992.
Beskriv : Stenger oppgitt stream.
Return : E/O.
*/
{
ResetFNames (stream); /* Fjern filnavn fra filnavn-lager */
errno = 0;
if (fclose (stream) == 0)
return (OK);
_iErrNo = errno; /* Remember error message from system */
return (ERROR);
} /* FClose (); */
int FCloseAll ( void )
/* Opprettet: Fredag 16. oktober 1992.
Beskriv : Stenger alle åpne filer, untatt stdin, stdout, stderr, stdaux.
Returnerer antall filer som ble stengt, eller -1 ved feil.
Se ellers standard funksjon: fcloseall ();
*/
{
int c1 = MAXFILES;
int ret = 0;
errno = 0;
while (c1--)
{
if (Fnames[c1].open == TRUE)
{
Fnames[c1].open = FALSE; /* TRUE hvis filhandle er åpen */
FILEc--;
if (close (Fnames[c1].handle) == -1) /* If error */
{
_iErrNo = errno; /* Remember error message from system */
ret = -1;
}
}
}
return (ret);
} /* FCloseall (); */
int FPrintF ( FILE *stream, const char *format, ... )
/* Opprettet: Onsdag 10. juni 1992.
Beskriv : Se standard funksjon: fprintf ();
Return : E/O.
*/
{
int ret = OK;
va_list argp;
va_start (argp, format);
errno = 0;
if ((vfprintf (stream, format, argp) == EOF &&
errno != 0))
{
_iErrNo = errno; /* Remember error message from system */
ret = ERROR;
}
va_end (argp);
return (ret);
} /* FPrintF (); */
size_t FWrite ( const void *ptr, size_t size, size_t n, FILE *stream )
/* Opprettet: Onsdag 10. juni 1992.
Beskriv : Se standard funksjon: fwrite ();
*/
{
size_t Ret;
errno = 0;
if ((Ret = fwrite (ptr, size, n, stream)) != n)
_iErrNo = errno; /* Remember error message from system */
return Ret;
} /* FWrite (); */
size_t FRead ( void *ptr, size_t size, size_t n, FILE *stream )
/* Opprettet: Onsdag 10. juni 1992.
Beskriv : Se standard funksjon: fread ();
*/
{
size_t Ret;
errno = 0;
if ((Ret = fread (ptr, size, n, stream)) != n)
{
if (feof (stream))
{
errno = 0;
_iErrNo = 0;
}
else
{
_iErrNo = errno; /* Remember error message from system */
}
}
return Ret;
} /* FRead (); */
int FGetS ( char *string, int maxlen, FILE *stream )
/* Opprettet: Fredag 16. oktober 1992.
Beskriv : Leser en karakterstreng fra stream.
Karakterer leses inntill en ny-linje karakter, eller inntill
maxlen antall tegn.
Return : E/O.
*/
{
errno = 0;
if (fgets (string, maxlen, stream) == NULL)
{
if (feof (stream))
{
errno = 0;
_iErrNo = 0;
}
else
{
_iErrNo = errno; /* Remember error message from system */
}
return (ERROR);
}
return (OK);
} /* FGetS (); */
int FGetS0 ( char *string, int maxlen, FILE *stream )
/*
Function: Get a null-terminated string from stream.
Created: April, 22. 1993. 02:40. By Leif-Erik Larsen.
Interface: string = Buffer of where to store the string.
maxlen = Max number of characters to read.
stream = Stream of where to do the reading.
Returns: E/O.
Comment: The string will be terminated with a null-character.
Therefore, the max number of characters actually read into
string buffer will be 'maxlen' - 1. This because
the null-character it self allso is stored in the string.
*/
{
char chr; /* To temporary store the red char */
errno = 0;
while (--maxlen) /* Read max 'maxlen' - 1 characters */
{
/* Read next character from stream */
if (fread (&chr, sizeof (char), 1, stream) != 1)
{
_iErrNo = errno; /* Remember error message from system */
return (ERROR);
}
*string++ = chr; /* Copy the red character into string */
if (chr == '\0') /* The red character a null-char? */
break; /* Yes, so stop reading string */
}
return (OK);
} /* FGetS0 (); */
int Remove ( const char *filename )
/* Opprettet: Onsdag 10. juni 1992.
Beskriv : Sletter oppgitt fil.
Return : E/O.
*/
{
errno = 0;
if (unlink (filename) == -1)
{
_iErrNo = errno; /* Remember error message from system */
return (ERROR);
}
return (OK);
} /* Remove (); */
long FTell ( FILE *stream )
/* Opprettet: Onsdag 10. juni 1992.
Beskriv : Se standard funksjon: ftell ();
Returnerer -1L ved feil, ellers aktiv filposisjon til "stream".
*/
{
long lRet;
errno = 0;
if ((lRet = ftell (stream)) == -1L)
_iErrNo = errno; /* Remember error message from system */
return lRet;
} /* FTell (); */
int FSeek ( FILE *stream, long offset, int whence )
/* Opprettet: Onsdag 10. juni 1992.
Beskriv : Se standard funksjon: fseek ();
Return : E/O.
*/
{
errno = 0;
if (fseek (stream, offset, whence) != 0)
{
_iErrNo = errno; /* Remember error message from system */
return (ERROR);
}
return (OK);
} /* FSeek (); */
int Rewind ( FILE *stream )
/* Opprettet: Onsdag 10. juni 1992.
Beskriv : Plasserer filpeker til stream tilbake til f¢rste byte.
Return : E/O.
*/
{
return FSeek (stream, 0, SEEK_SET);
} /* Rewind (); */
int FPutC ( FILE *stream, char c )
/* Opprettet: L¢rdag 12. september 1992.
Beskriv : Skriver en karakter til stream.
Return : E/O.
*/
{
errno = 0;
if (fwrite (&c, sizeof (char), 1, stream) != 1)
{
_iErrNo = errno; /* Remember error message from system */
return (ERROR);
}
return (OK);
} /* FPutC (); */
int FGetL ( FILE *stream, long *l )
/*
Function: Read a long integer from stream.
Programmed by: LEL
Date: April, 22. 1993. 00:50.
Interface: stream = Stream to read from.
l = Store red long here.
Returns: E/O.
*/
{
errno = 0;
if (fread (l, sizeof (long), 1, stream) != 1)
{
_iErrNo = errno; /* Remember error message from system */
return (ERROR);
}
return (OK);
} /* FGetL (); */
int FPutL ( FILE *stream, long l )
/*
Function: Write a long integer to stream.
Created: April 22, 1993. 00:50. By Leif-Erik Larsen.
Interface: stream = Stream to write to.
l = Write this long integer.
Returns: E/O.
*/
{
errno = 0;
if (fwrite (&l, sizeof (long), 1, stream) != 1)
{
_iErrNo = errno; /* Remember error message from system */
return (ERROR);
}
return (OK);
} /* FPutL (); */
int WritFLine ( FILE *file, int count, char chr )
/*
Function: Write 'count' number of 'chr' to open 'file'.
Created: September 12, 1992. By Leif-Erik Larsen.
Interface: file = File of where to write.
count = Number of characters to write.
char = Character to write.
Returns: Number of actual written characters (== 'count' on success).
*/
{
register int c1;
errno = 0;
for (c1 = 0; count > 0; count--, c1++)
{
if (fwrite (&chr, sizeof (char), 1, file) != 1)
{
_iErrNo = errno; /* Remember error message from system */
return (c1);
}
}
return (c1);
} /* WritFLine (); */
int WritFLineLn ( FILE *file, int count, char chr )
/* Opprettet den 12/09/92, av LEL.
Beskrivelse:
Skriver "count" antall av "chr" til den åpne filen "file", og
avslutter med linjeskift.
Returnerer antall faktiske skrivde tegn ("count" + 2). */
{
int c;
if ((c = WritFLine (file, count, chr)) != count)
return (c);
FPrintF (file, "\n");
return (c + 2);
} /* WritFLineLn (); */
int GetFileTime ( const char *filename, timeTYPE *_time )
/* Opprettet den 12/09/92, av LEL.
Beskrivelse:
Leser dato/tid-stempel til oppgitt fil. Det leste stempelet returneres
i "_time".
Return:
E/O. */
{
#if __IBMC__ || __IBMCPP__
APIRET rc;
HDIR FindHandle = HDIR_CREATE;
ULONG ulFindCount = 1; /* Don't read more than one filename at once */
FILEFINDBUF3 FFBuff; /* File Info including size of EA's */
memset (_time, 0, sizeof (*_time));
rc = DosFindFirst ((char *) filename, &FindHandle,
FILE_ARCHIVED | FILE_SYSTEM | FILE_READONLY | FILE_HIDDEN,
(PVOID) &FFBuff,
sizeof (FFBuff), &ulFindCount, FIL_STANDARD);
DosFindClose (FindHandle);
if (rc != NO_ERROR)
RETURN_ERR;
_time->hund = 0;
_time->sec = FFBuff.ftimeLastWrite.twosecs;
_time->min = FFBuff.ftimeLastWrite.minutes;
_time->hour = FFBuff.ftimeLastWrite.hours;
_time->day = FFBuff.fdateLastWrite.day;
_time->month = FFBuff.fdateLastWrite.month;
_time->year = FFBuff.fdateLastWrite.year;
#else
FILE *file;
struct ftime ftime;
memset (_time, 0, sizeof (*_time));
if ((file = FOpen (filename, "r")) == NULL)
RETURN_ERR;
getftime (fileno (file), &ftime);
FClose (file);
_time->hund = 0;
_time->sec = ftime.ft_tsec;
_time->min = ftime.ft_min;
_time->hour = ftime.ft_hour;
_time->day = ftime.ft_day;
_time->month = ftime.ft_month;
_time->year = ftime.ft_year;
#endif
return (OK);
} /* GetFileTime (); */
int PrintStrTab ( FILE *file, const char *str, int tabw )
/* Opprettet: Mandag 19. april 1993.
Parameter: "file" Fil hvor den konverterte strengen skal lagres.
"str" Streng som skal konverteres f¢r lagring.
"tabw" Tabulatorbredde.
Retur : E/O.
Beskriv : F¢r lagring konverteres alle tabulatorer i oppgitt streng til
vanlige mellomrom.
*/
{
int tabc = 1; /* Count active tab-offset (1..) */
char chr; /* Store active character */
while ((chr = *str++) != 0) /* Loop trough whole string */
{
if (chr == '\t')
{
/* Replace spaces with tabs, and write rest of tab-width */
if (!WritFLine (file, tabw - tabc + 1, ' '))
return (ERROR);
tabc = 0; /* Reset, since passed a tab-column */
}
else /* Any character but tab */
if (!FPutC (file, chr)) /* Write the character */
return (ERROR);
if (tabc++ >= tabw) /* Tab-offset passed width of tab? */
tabc = 1; /* Yes, so reset tab-offset */
}
return (OK);
} /* PrintStrTab (); */
int IsItemInFile ( FILE *file, const char *item, int itlen,
long itnr, long *res )
/*
Date: April, 22. 1993. 17:40. By LEL.
Interface: file = File to seek for item.
item = Item to se if in file.
itlen = Number of characters in each block of an item
in file (Max 80).
itnr = Number of items in file.
res = Returns result of the test. 0 if item is not in
file, else index number of where the item was
found (1..).
Returns: E/O.
Comment: Filepointer to 'file' is restored to the same position
as it was before this function was called.
*/
{
long oldpos; /* Original file position */
long index = 0; /* Count index to active item */
char str[81]; /* Buffer of where to store red item */
if ((oldpos = FTell (file)) == -1L) /* Get original file position */
return (ERROR);
*res = 0; /* Default is that item not exist */
FSeek (file, 0, SEEK_SET); /* Place file pointer at start of file*/
while (itnr--) /* Seek trough all items in file */
{
/* Get next item from file */
if (FRead (str, itlen, 1, file) != 1)
return (ERROR);
index += 1; /* Count index to active item */
if (strcmp (item, str) == 0) /* Compare to see if equal item in fil*/
{
*res = index; /* Yes, item exist in file */
break; /* So break seek loop */
}
}
FSeek (file, oldpos, SEEK_SET); /* Restore original file position */
return (OK);
} /* IsItemInFile (); */
int FAddDummy ( FILE *file, long chrnr, char value )
/*
Date: April, 22. 1993. 20:55. By LEL.
Interface: file = File to add dummy characters.
chrnr = Number of characters to add.
value = Value of characters to add.
Returns: E/O.
*/
{
while (chrnr-- > 0)
{
if (!FPutC (file, value))
return (ERROR);
}
return (OK);
} /* FAddDummy (); */
char *BigStr ( int nr, ... )
/*
Function: Join several strings into one single big string.
Created: Februar, 12. 1991. By LEL.
Interface: nr = Number of argument strings in ...
... = List of strings to join.
Returns: A pointer to a buffer holding the joined strings.
*/
{
va_list argp; /* Pointer to argument */
static char buff[200];
char *str;
int len = 0;
buff[0] = '\0';
va_start (argp, nr);
for (; nr > 0; nr--)
{
str = (char *) va_arg (argp, char*);
len += strlen (str);
if (len >= 200)
break;
strcat (buff, str);
}
va_end (argp);
return (buff);
} /* BigStr (); */
char *GetFName ( const char *path )
/*
Function: Fetch out filename part of specified path.
Created: June 11, 1992. By Leif-Erik Larsen.
Interface: path = Path to use.
Returns: A pointer to the fetched out filename.
*/
{
static char buffer [MAXFILE + MAXEXT + 1];
char file[MAXFILE + 1];
char ext [MAXEXT + 1];
FNSplit (path, NULL, NULL, file, ext);
strcpy (buffer, file);
strcat (buffer, ext);
return buffer;
} /* GetFName (); */
char *GetPath ( const char *path )
/*
Function: Fetch out directory part of specified path.
The dir will be returned with an ending backslash (if needed).
Created: June 11, 1992. By Leif-Erik Larsen.
Interface: path = Path to use.
Returns: A pointer to the fetched out directory.
*/
{
static char buffer [MAXDRIVE + MAXDIR + 1];
char drive [MAXDRIVE + 1];
char dir [MAXDIR + 1];
FNSplit (path, drive, dir, NULL, NULL);
strcpy (buffer, drive);
strcat (buffer, dir);
return buffer;
} /* GetPath (); */
int IsWild ( const char *path )
/* Opprettet den 14/09/92, av LEL.
Beskrivelse:
Returnerer sann verdi (1) hvis "path" inneholder DOS jokertegn. */
{
int flags;
flags = FNSplit (path, NULL, NULL, NULL, NULL);
return (flags & WILDCARDS) ? TRUE : FALSE;
} /* IsWild (); */
char *Slash ( char *dir )
/*
Function: Add an ending slash to dir, if neccessary.
Created: Mai, 26. 1992. By Leif-Erik Larsen.
Interface: dir = Directory to update.
Returns: A pointer to dir.
*/
{
if (!*dir)
return (dir);
if (strlen (dir) >= MAXDIR)
return (dir);
if (*(dir + strlen (dir) - 1) != DIRSPLITCHR)
strcat (dir, DIRSPLITSTR);
return (dir);
} /* Slash (); */
char *CutFName ( char *path )
/*
Function: Cut filename (w/extention), if any, from specified path.
Created: Mai, 27. 1992. By Leif-Erik Larsen.
Interface: path = Path from where we shall cut the filename and extention.
Returns: A pointer to the processed path.
*/
{
char drive [MAXDRIVE];
char dir [MAXDIR];
FNSplit (path, drive, dir, NULL, NULL);
strcpy (path, drive);
strcat (path, dir);
Slash (path);
return (path);
} /* CutFName (); */
char *CutFExt ( char *path )
/* Opprettet den 10/09/92, av LEL.
Beskrivelse:
Kutter etternavn-delen av katalogen/filnavnet "path".
Eventuell gjennværende katalog (m/filnavn) returneres uten '.'.
*/
{
char drive [MAXDRIVE];
char dir [MAXDIR];
char file [MAXFILE];
FNSplit (path, drive, dir, file, NULL);
strcpy (path, drive);
strcat (path, dir);
strcat (path, file);
return (path);
} /* CutFExt (); */
int StrPart ( int nr, int maxlen, char *dest, const char *src, const char *sep )
/*
Function: Fetch out a string part from specified string. Each part of the
string must be separated by one or more of the characters
specified by 'sep', to be identified as a part of the string.
Created: April, 7. 1992. By LEL.
Interface: nr = Index of the string part to fetch out (1..).
maxlen = Max length of the string to fetch out.
dest = Buffer of where to store the fetched string.
src = The original string, of where to fetch from.
sep = List of separating characters.
Returns: 0 if we didn't find the indexed string part, or else 1.
*/
{
*dest = 0;
if (nr < 1) /* Index must be within legal range (1..) */
return 0;
maxlen--; /* Make room for terminating '\0' */
while (*src) /* Seach for actual string-part */
{
while (nr--)
{
while (strchr (sep, *src)) /* Ignore separating characters */
{
if (*src++ == 0)
return 0;
}
if (nr) /* Active string is not requested */
{
while (!strchr (sep, *src))
{
if (*src++ == 0)
return 0;
}
}
else /* This is the requested string */
{
while (maxlen-- && /* Fetch out actual string-part */
!strchr (sep, *src))
{
if (*src == 0)
{
*dest = 0;
return 1;
}
*dest++ = *src++;
}
*dest = 0; /* String-part was too long, so cut */
return 1;
}
}
}
return 0; /* Didn't find requested string */
} /* StrPart (); */
int IsExt ( const char *path )
/*
Function: Test if filename in path has an extention.
Created: September, 14. 1992. By Leif-Erik Larsen.
Interface: path = Path to test.
Returns: TRUE if path has extention, or else FALSE.
*/
{
return (FNSplit (path, 0, 0, 0, 0) & EXTENSION) ? TRUE : FALSE;
} /* IsExt (); */
int FNSplit ( const char *path,
char *drive, char *dir, char *file, char *ext )
/*
Function: Split path into its components.
Created: Juni, 1. 1993. 17:03. By Leif-Erik Larsen.
Interface: path = Path to split.
drive = Drive part ("C:").
dir = Dir part ("\TEST\DIR\").
file = File part ("NAME").
ext = Extention part (".EXT").
Returns: Flags: DIRECTORY, DRIVE, EXTENSION, FILENAME, WILDCARDS.
*/
{
char _Drive[MAXDRIVE];
char _Dir [MAXDIR];
char _File [MAXFILE];
char _Ext [MAXEXT];
char *ptr;
char *fileptr;
int c1;
int flag = 0;
int len = strlen (path);
if (drive == NULL) drive = _Drive;
if (dir == NULL) dir = _Dir;
if (file == NULL) file = _File;
if (ext == NULL) ext = _Ext;
*drive = *dir = *file = *ext = 0; /* Empty all strings */
if (len <= 0 || /* If no path to split */
len >= MAXPATH)
{
return (0);
}
if (strchr (path, '*') || /* Test if wildchars in path */
strchr (path, '?'))
{
flag |= WILDCARDS;
}
if (*(path+len-1) != DIRSPLITCHR && /* Filename if not an ending '\' */
*(path+len-1) != ':') /* Path can be "C:" (no filename) */
{
flag |= FILENAME;
/* Seach for the last character before name of file: */
for (fileptr = (char *) path+len-1;
fileptr > path && *(fileptr-1) != DIRSPLITCHR &&
*(fileptr-1) != ':';
fileptr--);
/* Copy name of file, but do not include the extention: */
for (c1 = 0, ptr = fileptr;
c1 < MAXFILE-1 && *ptr && *ptr != '.';
c1++)
{
*file++ = *ptr++;
}
*file = 0; /* Terminate filename with 0-character*/
/* Seach to start of extesion, in case filename was to long: */
while (*ptr && *ptr != '.')
ptr++;
/* Copy extention of file, if any: */
if (*ptr == '.')
{
flag |= EXTENSION;
for (c1 = 0; c1 < MAXEXT-1 && *ptr; c1++)
*ext++ = *ptr++;
}
*ext = 0; /* Terminate extention with 0-char. */
}
else
{
fileptr = (char *) path + len;
}
if (fileptr == path) /* If path consist of only filename */
return (flag);
/* Now, we are sure that 'fileptr' points to start of name-part (if any,
else 'fileptr' point to the null-terminating character in path. */
if (*(path + 1) == ':') /* If path specify drive */
{
flag |= DRIVE;
memcpy (drive, path, 2);
*(drive + 2) = 0; /* Terminate drive with 0-character */
path += 2;
}
/* Copy dir, if any: */
for (c1 = 0; c1 < MAXDIR-1 && path < fileptr; c1++)
{
*dir++ = *path++;
flag |= DIRECTORY;
}
*dir = 0; /* Terminate dir with 0-character */
return (flag);
} /* FNSplit (); */
int StrLenSL ( long value )
/*
Function: Calculate numbers of ascii characters neccessary to display
specified long integer value.
Created: May 26, 1994. 13:50. By Leif-Erik Larsen.
Interface: value = Value to test.
radix = 2, 8, 10 or 16.
Returns: The calculated number of charactrs.
*/
{
int ret = 1;
if (value < 0)
ret += 1; /* Allso '-' take one character */
while (value /= 10)
ret++;
return (ret);
} /* StrLenSL (); */
int StrLenSI ( int value )
/*
Function: Calculate numbers of ascii characters neccessary to display
specified integer value.
Created: May 26, 1994. 13:54. By Leif-Erik Larsen.
Interface: value = Value to test.
Returns: The calculated number of charactrs.
*/
{
int ret = 1;
if (value < 0)
ret += 1; /* Allso '-' take one character */
while (value /= 10)
ret++;
return (ret);
} /* StrLenSI (); */
int CopyNBytes ( FILE *dest, FILE *source, long nr )
/*
Function: Copy specified number of characters from one file to
another.
Created: April, 21. 1993. 17:20. By Leif-Erik Larsen.
Interface: dest = Copy characters to this file.
source = Copy characters from this file.
nr = Number of characters to copy.
Returns: E/O.
*/
{
int readc;
void *buff = malloc (4096); /* Temporary read/write buffer */
if (buff == NULL) /* Not enough memory for malloc */
{
_iErrNo = errno; /* Remember error message from system */
return ERROR;
}
while (nr > 0)
{
/* Number of bytes to read next */
readc = (int) ((nr >= 4096) ? 4096 : nr);
/* Read next block from source file */
if (FRead (buff, readc, 1, source) != 1)
{
free (buff);
return ERROR;
}
/* Write read block to destination */
if (FWrite (buff, readc, 1, dest) != 1)
{
free (buff);
return ERROR;
}
nr -= readc;
}
free (buff); /* Give allocated memory back to heap */
return (OK);
} /* CopyNBytes (); */
int CopyNBytesP ( FILE *dest, FILE *source, long spos, long nr )
/*
Function: Copy specified number of characters from specified
position on source file to another file.
Programmed by: LEL
Date: April, 21. 1993. 17:20.
Interface: dest = Copy characters to this file.
source = Copy characters from this file.
spos = Position in source file to start copy from.
nr = Number of characters to copy.
Returns: E/O.
*/
{
/* Place pointer to start copy from */
if (!FSeek (source, spos, SEEK_SET))
return (ERROR);
/* Do the copy */
if (!CopyNBytes (dest, source, nr))
return (ERROR);
return (OK);
} /* CopyNBytesP (); */
int GetDisk ( void )
/*
Function: Get the current drive number
Created: August, 17. 1993. 10:48. By Leif-Erik Larsen.
Returns: The current drive number (0 = A, 1 = B, 2 = C, etc.)
*/
{
#ifdef __BORLANDC__
return getdisk ();
#elif __IBMC__ || __IBMCPP__
return (_getdrive () - 1);
#else
union REGS regs;
regs.h.ah = 0x19; /* DOS: Get current disk */
int86 (0x21, ®s, ®s); /* Call DOS */
return (regs.h.al);
#endif
} /* GetDisk (); */
void SetDisk ( int disk )
/*
Function: Set default drive number
Created: August, 17. 1993. 16:08. By Leif-Erik Larsen.
Interface: disk = Drive number (0 = A, 1 = B, 2 = C, etc.)
Returns:
Comment:
*/
{
#ifdef __BORLANDC__
setdisk (disk);
#elif __IBMC__ || __IBMCPP__
_chdrive (disk + 1);
#else
union REGS regs;
regs.h.ah = 0x0E; /* DOS: Select disk */
regs.h.dl = disk;
int86 (0x21, ®s, ®s); /* Call DOS */
#endif
} /* SetDisk (); */
void Randomize ( void )
/*
Function: Initializes random number generator.
Created: Jule, 6. 1993. 11:55. By Leif-Erik Larsen.
*/
{
time_t t;
srand ((unsigned) time (&t));
} /* Randomize (); */
int GetCurDir ( int drive, char *directory )
/*
Function: Gets current directory for specified drive.
Created: August, 17. 1993. 10:50. By Leif-Erik Larsen.
Interface: drive = Specifies a drive number (0 = default, 1 = A, etc.)
directory = Points to an area of memory (of length MAXDIR)
where the null-terminated directory name will be
placed. The name does not contain the drive
specification but it begin and end with a backslash.
Returns: On success, returns 0, else returns -1.
*/
{
#ifdef __GNUC__
int olddisk = GetDisk (); /* Remember default drive */
SetDisk (drive ? (drive - 1) : olddisk);
if (!getwd (directory)) /* Get current directory */
#elif defined (__BORLANDC__)
if (getcurdir (drive, directory) == -1)
#endif
{
strcpy (directory, DIRSPLITSTR);
#ifdef __GNUC__
SetDisk (olddisk); /* Restore default drive */
#endif
return (-1);
}
Slash (directory); /* Be sure that it is an ending backsl*/
#ifdef __GNUC__
SetDisk (olddisk); /* Restore default drive */
#endif
return (0);
} /* GetCurDir (); */