/*
 	HEPATH.C
 	Path manipulation functions (for Unix)

	for the Hugo Engine

 	by Kent Tessman (c) 2000-2006

 	(Note that this file hewxwin.c contains user interface functions;
	hewx.c contains the standard engine interface routines.
*/

#include "heheader.h"

/*
    FILENAME MANAGEMENT:

    Different operating systems will have their own ways of naming
    files.  The following routines are simply required to know and
    be able to dissect/build the components of a particular filename,
    storing/restoring the compenents via the specified char arrays.

    For example, in MS-DOS:

        hugo_splitpath("C:\HUGO\FILES\HUGOLIB.H", ...)
                becomes:  C:, HUGO\FILES, HUGOLIB, H

    and

        hugo_makepath(..., "C:", "HUGO\FILES", "HUGOLIB", "H")
                becomes:  C:\HUGO\FILES\HUGOLIB.H

    The appropriate equivalent nomenclature should be used for the
    operating system in question.
*/

#ifndef WIN32

void hugo_splitpath(char *path, char *drive, char *dir, char *fname, char *ext)
{
char *file;
char *extension;

        strcpy(drive,"");
        strcpy(dir,"");
        strcpy(fname,"");
        strcpy(ext,"");

        if ((file = strrchr(path,'/')) == 0)
        {
                if ((file = strrchr(path,':')) == 0) file = path;
        }
        strncpy(dir,path,strlen(path)-strlen(file));
        *(dir+strlen(path)-strlen(file)) = 0;
        extension = strrchr(file,'.');
        if (extension != 0)
        {
                strncpy(fname,file,strlen(file)-strlen(extension));
                *(fname+strlen(file)-strlen(extension)) = 0;
                strcpy(ext,extension+1);
        }
        else strcpy(fname,file);

        if (strcmp(dir, "") && fname[0]=='/') strcpy(fname, fname+1);
}

void hugo_makepath(char *path, char *drive, char *dir, char *fname, char *ext)
{
        if (*ext == '.') ext++;
        strcpy(path,drive);
        strcat(path,dir);
        switch (*(path+strlen(path)))
        {
        case '/':
        case ':':
/*        case 0: */
                break;
        default:
                if (strcmp(path, "")) strcat(path,"/");
                break;
        }
        strcat(path,fname);
        if (strcmp(ext, "")) strcat(path,".");
/* Crashes without -fwritable-strings:
        strcat(path,strlwr(ext)); */
	strcat(path, ext);
}

#else	// __WXMSW__

void hugo_splitpath(char *path, char *drive, char *dir, char *fname, char *ext)
{
	char *file;
	char *extension = NULL;

	strcpy(drive, "");
	strcpy(dir, "");
	strcpy(fname, "");
	strcpy(ext, "");

	if ((file = strrchr(path, '\\'))==0)
	{
		if ((file = strrchr(path, ':'))==0) file = path;
	}

	strncpy(dir, path, strlen(path) - strlen(file));
	*(dir + strlen(path)-strlen(file)) = 0;
	extension = strrchr(path, '.');

	if ((extension!=0) && strlen(extension) < strlen(file))
	{
		strncpy(fname, file, strlen(file)-strlen(extension));
		*(fname + strlen(file)-strlen(extension)) = 0;
		strcpy(ext, extension+1);
	}
	else strcpy(fname, file);

	if (fname[0]=='\\') strcpy(fname, fname+1);
}

void hugo_makepath(char *path, char *drive, char *dir, char *fname, char *ext)
{
	strcpy(path, drive);
	strcat(path, dir);

	if ((strlen(dir) && path[strlen(path)-1]!='\\') && fname[0]!='\\')
		strcat(path, "\\");

	strcat(path,fname);

	if (*ext!='\0')
	{
		if (*ext == '.') ext++;
		strcat(path,".");
		strcat(path,ext);
	}
}

#endif
