Files
harbour-core/harbour/source/hbpp/harb.c
1999-05-29 20:58:42 +00:00

159 lines
4.0 KiB
C

#include <stdio.h>
#include <mem.h>
#ifndef __dj_include_stdio_h_
#include <alloc.h>
#endif
#include "harb.h"
/*
* Split given filename into path, name and extension
*/
FILENAME *SplitFilename( char *szFilename )
{
FILENAME *pName =(FILENAME *)OurMalloc( sizeof(FILENAME) );
int iLen = strlen(szFilename);
int iSlashPos, iDotPos;
int iPos;
pName->path =pName->name =pName->extension =NULL;
iSlashPos =iLen-1;
iPos =0;
while( iSlashPos >= 0 && !IS_PATH_SEP(szFilename[ iSlashPos ]) )
--iSlashPos;
if( iSlashPos == 0 )
{
/* root path -> \filename */
pName->_buffer[ 0 ] =PATH_DELIMITER[ 0 ];
pName->_buffer[ 1 ] ='\x0';
pName->path =pName->_buffer;
iPos =2; /* first free position after the slash */
}
else if( iSlashPos > 0 )
{
/* path with separator -> path\filename */
memcpy( pName->_buffer, szFilename, iSlashPos );
pName->_buffer[ iSlashPos ] ='\x0';
pName->path =pName->_buffer;
iPos =iSlashPos +1; /* first free position after the slash */
}
iDotPos =iLen-1;
while( iDotPos > iSlashPos && szFilename[ iDotPos ] != '.' )
--iDotPos;
if( (iDotPos-iSlashPos) > 1 )
{
/* the dot was found
* and there is at least one character between a slash and a dot
*/
if( iDotPos == iLen-1 )
{
/* the dot is the last character -use it as extension name */
pName->extension =pName->_buffer+iPos;
pName->_buffer[ iPos++ ] ='.';
pName->_buffer[ iPos++ ] ='\x0';
}
else
{
pName->extension =pName->_buffer+iPos;
/* copy rest of the string with terminating ZERO character */
memcpy( pName->extension, szFilename+iDotPos+1, iLen-iDotPos );
iPos +=iLen-iDotPos;
}
}
else
/* there is no dot in the filename or it is '.filename' */
iDotPos =iLen;
pName->name =pName->_buffer+iPos;
memcpy( pName->name, szFilename+iSlashPos+1, iDotPos-iSlashPos-1 );
pName->name[ iDotPos-iSlashPos-1 ] ='\x0';
return pName;
}
/*
* This function joins path, name and extension into a string with a filename
*/
char *MakeFilename( char *szFileName, FILENAME *pFileName )
{
if( pFileName->path && pFileName->path[ 0 ] )
{
/* we have not empty path specified */
int iLen =strlen(pFileName->path);
strcpy( szFileName, pFileName->path );
/* if the path is a root directory then we don't need to add path separator */
if( !(IS_PATH_SEP(pFileName->path[ 0 ]) && pFileName->path[ 0 ] == '\x0') )
{
/* add the path separator only in cases:
* when a name doesn't start with it
* when the path doesn't end with it
*/
if( !( IS_PATH_SEP(pFileName->name[ 0 ]) || IS_PATH_SEP(pFileName->path[ iLen-1 ]) ) )
{
szFileName[ iLen++ ] =PATH_DELIMITER[ 0 ];
szFileName[ iLen ] ='\x0';
}
}
strcpy( szFileName+iLen, pFileName->name );
}
else
strcpy( szFileName, pFileName->name );
if( pFileName->extension )
{
int iLen =strlen(szFileName);
if( !(pFileName->extension[ 0 ] == '.' || pFileName->name[ iLen-1 ] == '.') )
{
/* add extension separator only when extansion doesn't contain it */
szFileName[ iLen++ ] ='.';
szFileName[ iLen ] ='\x0';
}
strcpy( szFileName+iLen, pFileName->extension );
}
return szFileName;
}
void * OurMalloc( LONG lSize )
{
void * pMem = malloc( lSize );
if( ! pMem )
printf( "\nCan't allocate memory!\n" );
return pMem;
}
void * _xgrab( ULONG ulSize ) /* allocates fixed memory */
{
void * pMem = malloc( ulSize );
if( ! pMem )
{
printf( "\n_xgrab error: can't allocate memory!\n" );
exit( 1 );
}
return pMem;
}
void * _xrealloc( void * pMem, ULONG ulSize ) /* reallocates memory */
{
void * pResult = realloc( pMem, ulSize );
if( ! pResult )
{
printf( "\n_xrealloc error: can't reallocate memory!\n" );
exit( 1 );
}
return pResult;
}
void _xfree( void * pMem ) /* frees fixed memory */
{
if( pMem )
free( pMem );
}