pathmac.c #4

  • //
  • guest/
  • miklos_fazekas/
  • jam/
  • src/
  • pathmac.c
  • View
  • Commits
  • Open Download .zip Download (7 KB)
/*
 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
 *
 * This file is part of Jam - see jam.c for Copyright information.
 */

# include "jam.h"
# include "pathsys.h"

# ifdef OS_MAC

# define DELIM ':'

/*
 * pathmac.c - manipulate file names on MacOS (MPW)
 *
 * External routines:
 *
 *	path_parse() - split a file name into dir/base/suffix/member
 *	path_build() - build a filename given dir/base/suffix/member
 *	path_parent() - make a PATHNAME point to its parent dir
 *
 * File_parse() and path_build() just manipuate a string and a structure;
 * they do not make system calls.
 */

/*
 * On Macintosh we follow the rules:
 *
 *	We end directory names to ':'. Except if we generate a filename from it, ie. 
 *	file_parse (':Dir') = (':','Dir')
 *	file_parse (':Dir:File.c') = (':Dir:','File.c')
 *
 */

/*
 * file_parse() - split a file name into dir/base/suffix/member
 *
 *	Case 1: file_parse ('Macintosh HD:') = ('Macintosh HD:','')
 *  Case 2: file_parse (':') = (':','')
 *	Case 3: file_parse ('::') = (':','::')
 *  Case 4: file_parse (':::') = ('::','::')
 *	Case 5: file_parse (':Directory:') = (':','Directory')
 *  Case 6: file_parse (':Direcotry') = (':','Directory')
 *  Case 7: file_parse (':Direcotry:File.c') = (':Directory','File.c')
 *	Case 7: file_parse (':Direcotry::File.c') = (':Directory::','File.c')
 *
 *	So dir is not terminated by a ':' if possible. (Ie. Not ':', not ':...:' and not '<volumename>:'
 *	Base or extension is never terminated by ':' expect if base is '::'
 *
 * 	Algorithm: 
 *		p := the last ':' in the string (if possible not the last character.)
 *		dir is the text till p, cutting the trialing ':' if possible.
 */

void
path_parse( 
	char	*file,
	PATHNAME *f )
{
	char *p, *q;
	char *end;
	
	memset( (char *)f, 0, sizeof( *f ) );

	/* Look for <grist> */
	if( file[0] == '<' && ( p = strchr( file, '>' ) ) )
	{
	    f->f_grist.ptr = file;
	    f->f_grist.len = p - file;
	    file = p + 1;
	}

	/* Look for Dirname: */
	p = strrchr( file, ':' );

	if( p )
	{
		/* p := the last ':' in the string unless it's the last character in the file */
		if (*(p+1) == 0)
		{
			char *q = p-1;
			
			while ((q >= file) && (*q != ':'))
				q--;
			
			if (q >= file)
			{
				p = q;
			}
		}
		
		/* if the ':' was the first ':' in the dir or it's the last of an '::' leave it */
		if ((memchr(file,':',p-file)==0) || (p == file) || (*(p-1) == ':'))
		{
			f->f_dir.ptr = file;
			f->f_dir.len = p - file + 1;
		}
		else
		{
			f->f_dir.ptr = file;
			f->f_dir.len = p - file;
		}
		
		if (*(p+1) != ':')
			file = p+1;
		else
			file = p;
	}

	end = file + strlen( file );

	/* Look for (member) */

	if( ( p = strchr( file, '(' ) ) && end[-1] == ')' )
	{
	    f->f_member.ptr = p + 1;
	    f->f_member.len = end - p - 2;
	    end = p;
	} 

	/* Look for .suffix */
	/* This would be memrchr() */

	p = 0;
	q = file;

	while( q = (char*)memchr( q, '.', end - q ) )
	    p = q++;

	if( p )
	{
	    f->f_suffix.ptr = p;
	    f->f_suffix.len = end - p;
		if ((f->f_suffix.len > 0) && (f->f_suffix.ptr[f->f_suffix.len-1] == ':')) /* Cut the trailing ':' */
			f->f_suffix.len--;
	    end = p;
	}

	/* Leaves base */

	f->f_base.ptr = file;
	f->f_base.len = end - file;
	if ((f->f_base.len > 0) && (f->f_base.ptr[f->f_base.len-1] == ':') && !((f->f_base.len == 2) && (f->f_base.ptr[0] == ':')) ) /* Cut the trailing ':' if the base is not '::' */
		f->f_base.len--;
}

/*
 * path_build() - build a filename given dir/base/suffix/member
 */
 
# define DIR_EMPTY	0	/* "" */
# define DIR_DOT	1	/* : */
# define DIR_DOTDOT	2	/* :: */
# define DIR_ABS	3	/* vol:dira: */
# define DIR_REL	4	/* :dira:dirb: */

# define G_DIR		0	/* take dir */
# define G_ROOT		1	/* take root */
# define G_CAT		2	/* prepend root to dir */
# define G_DTDR		3	/* :: of rel dir */
# define G_DDDD		4	/* make it ::: (../..) */
# define G_MT		5	/* leave it empty */

char grid[5][5] = {
/* Root:  Dir:	EMPTY	DOT		DOTDOT	ABS		REL */
/* EMPTY */   {	G_MT,	G_DIR,	G_DIR,	G_DIR,	G_DIR },
/* DOT */     {	G_ROOT,	G_DIR,	G_DIR,	G_DIR,	G_DIR },
/* DOTDOT */  {	G_ROOT,	G_ROOT,	G_DDDD,	G_DIR,	G_DTDR },
/* ABS */     {	G_ROOT,	G_ROOT, G_ROOT,	G_DIR,	G_CAT },
/* REL */     {	G_ROOT,	G_ROOT,	G_ROOT,	G_DIR,	G_CAT }
} ;

static int
file_flags( 
	char	*ptr,
	int	len )
{
	if( !len )
	    return DIR_EMPTY;
	if( len == 1 && ptr[0] == DELIM )
	    return DIR_DOT;
	if( len == 2 && ptr[0] == DELIM && ptr[1] == DELIM )
	    return DIR_DOTDOT;
	if( ptr[0] == DELIM )
	    return DIR_REL;
	return DIR_ABS;
}

void
path_build(
	PATHNAME *f,
	char	*file,
	int	binding )
{
	char *ofile = file;
	int dflag, rflag, act;
	
	if( DEBUG_SEARCH )
	{
	printf("build file: ");
	if( f->f_root.len )
		printf( "root = '%.*s' ", f->f_root.len, f->f_root.ptr );
	if( f->f_dir.len )
		printf( "dir = '%.*s' ", f->f_dir.len, f->f_dir.ptr );
	if( f->f_base.len )
		printf( "base = '%.*s' ", f->f_base.len, f->f_base.ptr );
	}
	
	/* Start with the grist.  If the current grist isn't */
	/* surrounded by <>'s, add them. */

	if( f->f_grist.len )
	{
	    if( f->f_grist.ptr[0] != '<' ) *file++ = '<';
	    memcpy( file, f->f_grist.ptr, f->f_grist.len );
	    file += f->f_grist.len;
	    if( file[-1] != '>' ) *file++ = '>';
	}
	
	/* Combine root & directory, according to the grid. */
	
	dflag = file_flags( f->f_dir.ptr, f->f_dir.len );
	rflag = file_flags( f->f_root.ptr, f->f_root.len );
	
	switch( act = grid[ rflag ][ dflag ] )
	{
	case G_DTDR:
		/* :: of rel dir */
		*file++ = DELIM;
		/* fall through */
		
	case G_DIR: 	
		/* take dir */
		memcpy( file, f->f_dir.ptr, f->f_dir.len );
	    	file += f->f_dir.len;
		break;
		
	case G_ROOT:	
		/* take root */
		memcpy( file, f->f_root.ptr, f->f_root.len );
	    	file += f->f_root.len;
		break;
	    
	case G_CAT:	
		/* prepend root to dir */
		memcpy( file, f->f_root.ptr, f->f_root.len );
	    	file += f->f_root.len;
		if( file[-1] == DELIM ) --file;
		memcpy( file, f->f_dir.ptr, f->f_dir.len );
	    	file += f->f_dir.len;
		break;
	
	case G_DDDD:	
		/* make it ::: (../..) */
		strcpy( file, ":::" );
		file += 3;
		break;
	}

	/* Put : between dir and file (if none already) */
	
	if( act != G_MT && 
	    file[-1] != DELIM && 
	    ( f->f_base.len || f->f_suffix.len ) )
	{
	    *file++ = DELIM;
	}

	if( f->f_base.len )
	{
		if ((f->f_base.len == 2) && (f->f_base.ptr[0] == ':') && (f->f_base.ptr[1] == ':') && (file[-1] == DELIM))
		{
			memcpy (file, f->f_base.ptr, f->f_base.len-1);
			file += f->f_base.len-1;
		}
		else
		{
	    	memcpy( file, f->f_base.ptr, f->f_base.len );
	    	file += f->f_base.len;
		}
	}

	if( f->f_suffix.len )
	{
	    memcpy( file, f->f_suffix.ptr, f->f_suffix.len );
	    file += f->f_suffix.len;
	}

	if( f->f_member.len )
	{
	    *file++ = '(';
	    memcpy( file, f->f_member.ptr, f->f_member.len );
	    file += f->f_member.len;
	    *file++ = ')';
	}
	*file = 0;	
	
	if( DEBUG_SEARCH )
		printf(" -> '%s'\n", ofile);
}

/*
 *	path_parent() - make a PATHNAME point to its parent dir
 */

void
path_parent( PATHNAME *f )
{
	/* just set everything else to nothing */

	f->f_base.ptr =
	f->f_suffix.ptr =
	f->f_member.ptr = "";

	f->f_base.len = 
	f->f_suffix.len = 
	f->f_member.len = 0;
}

# endif /* OS_MAC */
# Change User Description Committed
#8 2984 Miklos Fazekas Sync to 2.5rc2
#7 2521 Miklos Fazekas Compile error fixes after merge
#6 2520 Miklos Fazekas Integration error fixes
#5 2519 Miklos Fazekas Sync to 2.5rc1
#4 1573 Miklos Fazekas Merge to jam mainline.
#3 1395 Miklos Fazekas Merge with main jam
#2 1219 Miklos Fazekas MPW related fixes:
- support for longer than 255 pahtnames
- fixed problems with handling of paths
- fix: Mac file system is case insensitive
- code is compatible with MrC and MWCCP
#1 1212 Miklos Fazekas Created a Jam branch
//guest/perforce_software/jam/src/pathmac.c
#2 486 Perforce staff Jam 2.3.
 See RELNOTES for a list of changes from 2.2.x.

Just about every source file was touched when jam got ANSI-fied.
#1 2 laura Add Jam/MR 2.2 source