// **************************************************************************
// **************************************************************************
// **************************************************************************
// **                                                                      **
// ** XVTNBA.C                                                     PROGRAM **
// **                                                                      **
// ** Convert Microsoft Excel NBA roster information (in CVS format)       **
// **                                                                      **
// ** Last modified : 26 Feb 1997 by John Brandwood                        **
// **                                                                      **
// ** XVTNBA.EXE NBATEAMS.CSV NBA9697.CSV NBAPLYRS.CSV NBADATES.CSV        **
// **                                                                      **
// **************************************************************************
// **************************************************************************
// **************************************************************************

#define  WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <direct.h>
#include <io.h>
#include <time.h>

#include <ctype.h>

#include "lfptypes.h"
#include "\hockey\inc\teamdata.h"

#include "aiffio.h"
#include "xvtnba.h"
#include "xvtpxl.h"

#include "Types.h"


//
// DEFINITIONS
//

#define	VERSION_STR         "XvtNBA v1.01 - Kobe 2 (" __DATE__ ")"

#define	Swap16(n) \
			((((n)>>8)&0x00FFu)|(((n)<<8)&0xFF00u))

#define	Swap32(n) \
			((((n)>>24)&0x000000FFu)|(((n)>>8)&0x0000FF00u)|(((n)<<8)&0x00FF0000u)|(((n)<<24)&0xFF000000u))

#if BYTE_ORDER_LO_HI
 #define XvertD16LOHI(n) (n)
 #define XvertD32LOHI(n) (n)
 #define XvertD16HILO(n) Swap16(n)
 #define XvertD32HILO(n) Swap32(n)
#else
 #define XvertD16LOHI(n) Swap16((n))
 #define XvertD32LOHI(n) Swap32((n))
 #define XvertD16HILO(n) (n)
 #define XvertD32HILO(n) (n)
#endif


// Structure for loading one of Ben's resources.

typedef	struct RESOURCE_S
	{
	char                acz__Name[36];
	UB *                pub__Buf;
	UB *                pub__End;
	} RESOURCE_T;


// Externs...

extern s32
	HeadTexToN64( const char *folder, FILE *fp );


extern void
	HeadGeom_Init(),
	HeadGeom_ToN64( const char *filename );

extern s32
	HeadGeom_Process( const char *folder );


extern void
	GSM2_ToN64( const char *bankname, const char *bitname );

extern s32
	GSM2_Process( const char *folder, const char *player, FILE *log );


//
// GLOBAL VARIABLES
//

// Fatal error flag.
//
// Used to signal that the error returned by ErrorCode (see below) is fatal
// and that the program should terminate immediately.

global	BOOL                fl___FatalError     = FALSE;

global	ERRORCODE           ErrorCode           = ERROR_NONE;
global	char                ErrorMessage [256];

global	char                acz__FileInp [_MAX_PATH + 4];
global	char                acz__FileOut [_MAX_PATH + 4];

global	char                acz__FileDrv [_MAX_DRIVE];
global	char                acz__FileDir [_MAX_DIR];
global	char                acz__FileNam [_MAX_FNAME];
global	char                acz__FileExt [_MAX_EXT];

global	BOOL                fl___BinaryPhoto = FALSE;

global	HMMIO               pcl__gOutputFile;

global	UB                  aub__gLfpVers [4] =
	{
	0,1,0,1
	};

//
// STATIC VARIABLES
//

static	RESOURCE_T *        apcl_sResources [64];

static	int                 si___sNumber;

// String table.

typedef	struct STRNODE_S
	{
	struct STRNODE_S *  pcl__parent;
	struct STRNODE_S *  pcl__lesser;
	struct STRNODE_S *  pcl__bigger;
	int                 si___number;
	char *              pcz__string;
	} STRNODE_T;

global	int                 si___gStrCount;
global	STRNODE_T           cl___gStrRoot;

#define	STRING_MEM          65536

global	char *              pcz__gStrData;
global	char                acz__gStrData [STRING_MEM];

// Player table.

typedef	struct PLYRNODE_S
	{
	struct PLYRNODE_S * pcl__parent;
	struct PLYRNODE_S * pcl__lesser;
	struct PLYRNODE_S * pcl__bigger;
	int                 si___number;
#if 0 //phil
	char                acz__filename [64];
#else
	char *acz__filename;
#endif
	char HeadFolder[32];
	PlyrRomData_T       r;
	} PLYRNODE_T;

global	int                 si___gRepeatPlyr;
global	int                 si___gPlyrCount;
global	PLYRNODE_T          cl___gPlyrRoot;


#define ROSTER_SIZE	1024

//global	PLYRNODE_T *        apcl_gPlyr [768];
global	PLYRNODE_T *        apcl_gPlyr [ROSTER_SIZE];

global	int                 si___gPlyrsSeason;
global	int                 si___gPlyrsTotal;

// Team table.

typedef	struct TEAMNODE_S
	{
	struct TEAMNODE_S * pcl__parent;
	struct TEAMNODE_S * pcl__lesser;
	struct TEAMNODE_S * pcl__bigger;
	int                 si___offset;
	int                 si___number;
	int                 si___players;
	PLYRNODE_T *        apcl_players [16];
	TeamRomData_T       r;
	} TEAMNODE_T;

global	int                 si___gTeamCount;
global	TEAMNODE_T          cl___gTeamRoot;

global	TEAMNODE_T *        apcl_gTeam [64];

// Team roster.11

global	UW *                puw__gRoster;
//global	UW                  auw__gRoster [768];	// 2 * 32 * 12
global	UW                  auw__gRoster [ROSTER_SIZE];	 // v 1.01 - need a bunch of room - mjk

// Schedule.

global	UW                  auw__gSchedule [3072];
global	UW *                puw__gSchedule = auw__gSchedule;

//
//
//

typedef	struct STR_TO_NUM_S
	{
	int                 si___N;
	char *              pcz__S;
	} STR_TO_NUM_T;


static	STR_TO_NUM_T        acl__gTeamString [] =
	{
	{ TEAM_NONE, "NUL" },

	{ TEAM_ATL,  "ATL" },
	{ TEAM_BOS,  "BOS" },
	{ TEAM_CHA,  "CHA" },
	{ TEAM_CHI,  "CHI" },
	{ TEAM_CLE,  "CLE" },
	{ TEAM_DAL,  "DAL" },
	{ TEAM_DEN,  "DEN" },
	{ TEAM_DET,  "DET" },
	{ TEAM_GS ,  "GS"  },
	{ TEAM_HOU,  "HOU" },
	{ TEAM_IND,  "IND" },
	{ TEAM_LAC,  "LAC" },
	{ TEAM_LAL,  "LAL" },
	{ TEAM_MIA,  "MIA" },
	{ TEAM_MIL,  "MIL" },
	{ TEAM_MIN,  "MIN" },
	{ TEAM_NJ ,  "NJ"  },
	{ TEAM_NY ,  "NY"  },
	{ TEAM_ORL,  "ORL" },
	{ TEAM_PHI,  "PHI" },
	{ TEAM_PHO,  "PHO" },
	{ TEAM_POR,  "POR" },
	{ TEAM_SAC,  "SAC" },
	{ TEAM_SA ,  "SA"  },
	{ TEAM_SEA,  "SEA" },
	{ TEAM_TOR,  "TOR" },
	{ TEAM_UTA,  "UTA" },
	{ TEAM_VAN,  "VAN" },
	{ TEAM_WAS,  "WAS" },

	{ TEAM_EAS,  "EAS" },
	{ TEAM_WES,  "WES" },

	{ TEAM_LFP,  "LFP" },
	{ TEAM_NIN,  "NIN" },
	{ TEAM_NOA,  "NOA" },

	{ TEAM_3PT,  "3PT" },

	{ 0, NULL }
	};

static	STR_TO_NUM_T        acl__gScheduleCity [] =
	{
	{ TEAM_ATL, "ATLANTA"      },
	{ TEAM_BOS, "BOSTON"       },
	{ TEAM_CHA, "CHARLOTTE"    },
	{ TEAM_CHI, "CHICAGO"      },
	{ TEAM_CLE, "CLEVELAND"    },
	{ TEAM_DAL, "DALLAS"       },
	{ TEAM_DEN, "DENVER"       },
	{ TEAM_DET, "DETROIT"      },
	{ TEAM_GS , "GOLDEN STATE" },
	{ TEAM_HOU, "HOUSTON"      },
	{ TEAM_IND, "INDIANA"      },
	{ TEAM_LAC, "CLIPPERS"     },
	{ TEAM_LAL, "LAKERS"       },
	{ TEAM_MIA, "MIAMI"        },
	{ TEAM_MIL, "MILWAUKEE"    },
	{ TEAM_MIN, "MINNESOTA"    },
	{ TEAM_NJ , "NEW JERSEY"   },
	{ TEAM_NY , "NEW YORK"     },
	{ TEAM_ORL, "ORLANDO"      },
	{ TEAM_PHI, "PHILADELPHIA" },
	{ TEAM_PHO, "PHOENIX"      },
	{ TEAM_POR, "PORTLAND"     },
	{ TEAM_SAC, "SACRAMENTO"   },
	{ TEAM_SA , "SAN ANTONIO"  },
	{ TEAM_SEA, "SEATTLE"      },
	{ TEAM_TOR, "TORONTO"      },
	{ TEAM_UTA, "UTAH"         },
	{ TEAM_VAN, "VANCOUVER"    },
	{ TEAM_WAS, "WASHINGTON"   },
	{ TEAM_EAS, "EASTERN"      },
	{ TEAM_WES, "WESTERN"      },
	{ TEAM_LFP, "LEFT FIELD"   },
	{ TEAM_NIN, "NINTENDO"     },
	{ TEAM_NOA, "NINTENDO 2"   },
	{ TEAM_3PT,  "THREE POINT" },

	{ 0, NULL }
	};

static	STR_TO_NUM_T        acl__gScheduleDay [] =
	{
	{ 1, "SUN" },
	{ 2, "MON" },
	{ 3, "TUE" },
	{ 4, "WED" },
	{ 5, "THU" },
	{ 6, "FRI" },
	{ 7, "SAT" },
	{ 8, "SUN" },

	{ 0, NULL }
	};

static	STR_TO_NUM_T        acl__gScheduleMonth [] =
	{
	{  1, "JAN" },
	{  2, "FEB" },
	{  3, "MAR" },
	{  4, "APR" },
	{  5, "MAY" },
	{  6, "JUN" },
	{  7, "JUL" },
	{  8, "AUG" },
	{  9, "SEP" },
	{ 10, "OCT" },
	{ 11, "NOV" },
	{ 12, "DEC" },

	{ 0, NULL }
	};

static	STR_TO_NUM_T        acl__gDivString [] =
	{
	{ DIV_ATLANTIC, "ATLANTIC" },
	{ DIV_CENTRAL,  "CENTRAL"  },
	{ DIV_MIDWEST,  "MIDWEST"  },
	{ DIV_PACIFIC,  "PACIFIC"  },
	{ DIV_ALLSTAR,  "ALLSTAR"  },
	{ DIV_SPECIAL,  "SPECIAL"  },
	{ DIV_NONE,     "NONE"     },
	{ 0, NULL }
	};

static	STR_TO_NUM_T        acl__gConString [] =
	{
	{ CNF_EASTERN, "EASTERN" },
	{ CNF_WESTERN, "WESTERN" },
	{ CNF_ALLSTAR, "ALLSTAR" },
	{ CNF_SPECIAL, "SPECIAL" },
	{ CNF_NONE,    "NONE"    },
	{ 0, NULL }
	};

static	STR_TO_NUM_T        acl__gPosString [] =
	{
	{ POS_C , "C" },
	{ POS_F , "F" },
	{ POS_G , "G" },
	{ POS_PF, "PF" },
	{ POS_SF, "SF" },
	{ POS_PG, "PG" },
	{ POS_SG, "SG" },
	{ POS_CF, "C/F" },
	{ POS_FC, "F/C" },
	{ POS_GF, "G/F" },
	{ POS_FG, "F/G" },
	{ 0, NULL }
	};

static	STR_TO_NUM_T        acl__gHandString [] =
	{
	{ L_HANDED , "L" },
	{ R_HANDED , "R" },
	{ 0, NULL }
	};

static	STR_TO_NUM_T        acl__gArmPads [] =
	{
	{ 0,              "0" },
	{ 0,              "N" },
	{ PLYR_HAS_LAPAD, "L" },
	{ PLYR_HAS_RAPAD, "R" },
	{ PLYR_HAS_BAPAD, "B" },
	{ 0, NULL }
	};

static	STR_TO_NUM_T        acl__gKneePads [] =
	{
	{ 0,              "0" },
	{ 0,              "N" },
	{ PLYR_HAS_LKPAD, "L" },
	{ PLYR_HAS_RKPAD, "R" },
	{ PLYR_HAS_BKPAD, "B" },
	{ 0, NULL }
	};

static	STR_TO_NUM_T        acl__gWristPads [] =
	{
	{ 0,              "0" },
	{ 0,              "N" },
	{ PLYR_HAS_LWPAD, "L" },
	{ PLYR_HAS_RWPAD, "R" },
	{ PLYR_HAS_BWPAD, "B" },
	{ 0, NULL }
	};

static	STR_TO_NUM_T        acl__gSocks [] =
	{
	{ 0,              "0" },
	{ 0,              "N" },
	{ PLYR_HAS_SOCKS, "Y" },
	{ PLYR_HAS_SOCKS, "1" },
	{ 0, NULL }
	};

static	STR_TO_NUM_T        acl__gPadColour [] =
	{
	{ 0,               "0"     },
	{ PLYR_PADS_TEAM , "TEAM"  },
	{ PLYR_PADS_WHITE, "WHT"   },
	{ PLYR_PADS_BLACK, "BLK"   },
	{ PLYR_PADS_RED  , "RED"   },
	{ PLYR_PADS_BLUE , "BLU"   },
	{ PLYR_PADS_WHITE, "WHITE" },
	{ PLYR_PADS_BLACK, "BLACK" },
	{ PLYR_PADS_RED  , "RED"   },
	{ PLYR_PADS_BLUE , "BLUE"  },
	{ 0, NULL }
	};

static	STR_TO_NUM_T        acl__gBodyType [] =
	{
	{ 0,                "0"      },
	{ PLYR_BODY_THIN  , "T"      },
	{ PLYR_BODY_NORMAL, "N"      },
	{ PLYR_BODY_BUFF,   "F"      },
	{ PLYR_BODY_BUFF,   "B"      },
	{ PLYR_BODY_THIN  , "THIN"   },
	{ PLYR_BODY_NORMAL, "NORMAL" },
	{ PLYR_BODY_BUFF,   "FAT"    },
	{ PLYR_BODY_BUFF,   "BUFF"   },
	{ 0, NULL }
	};

static	STR_TO_NUM_T        acl__gHeadType [] =
	{
	{ 0,                "0"      },
	{ PLYR_HEAD_NORMAL, "N"      },
	{ PLYR_HEAD_BLOCK , "B"      },
	{ PLYR_HEAD_FLAT  , "F"      },
	{ PLYR_HEAD_HAIR  , "H"      },
	{ PLYR_HEAD_THIN  , "T"      },
	{ PLYR_HEAD_NORMAL, "NORM"   },
	{ PLYR_HEAD_BLOCK , "BLOC"   },
	{ PLYR_HEAD_FLAT  , "FLAT"   },
	{ PLYR_HEAD_HAIR  , "HAIR"   },
	{ PLYR_HEAD_THIN  , "THIN"   },
	{ PLYR_HEAD_NORMAL, "NORMAL" },
	{ PLYR_HEAD_BLOCK , "BLOCK"  },
	{ 0, NULL }
	};

//
// STATIC FUNCTION PROTOTYPES
//

static	int                 LoadTeamFile            (char *);
static	int                 LoadStatFile            (char *);
static	int                 LoadPlyrFile            (char *);
static	int                 LoadDateFile            (char *);

static	void                MakeHSeason             (void);
static	void                MakeQSeason             (void);
static	void                MakeESeason             (void);
static	void                ShowSeason              (UI);

static	void                WriteDates              (void);

static	int                 LoadResourceFile        (char *);
static	void                FreeResourceFile        (void);

static	int                 SaveInfoFile            (char *);
static	int                 SaveDataFile            (char *);
static	int                 SaveTalkFile            (char *);

static	void                InitTeamTree            (void);
static	TEAMNODE_T *        AddTeam                 (TEAMNODE_T *);
static	void                OrderTeams              (void);
static	void                CalcTeamNumber          (TEAMNODE_T *, int, int);
static	void                WriteTeams              (void);

static	void                MakeRosters             (void);
static	void                WriteRoster             (void);

static	void                InitPlyrTree            (void);
static	PLYRNODE_T *        AddPlyr                 (PLYRNODE_T *);
static	void                OrderPlyrs              (void);
static	void                CalcPlyrNumber          (PLYRNODE_T *, int, int);
static	void                WritePlyrs              (void);
static	void                WritePlyrsTex           (void);
static	void                WritePlyrsImg           (void);
static	void                WritePlyrsNam           (void);
static	void                WriteSuperPalette       (void);

static	void                WriteTalk               (void);

static	UB *                GrabPlyrTex             (UB *, IMG_HDR_T *);

static	void                InitStringTree          (void);
static	STRNODE_T *         AddString               (char *);
static	void                OrderStrings            (void);
static	void                CalcStringNumber        (STRNODE_T *);

static	char *              strcsv                  (char *);

static void SaveHeadsTex( const char *filename );
static void SaveHeadsGeom( const char *filename );

static void SavePlayerIDs( const char *filename );
static void SavePlayerAudio( const char *bankname, const char *bitname );

//
//
//

global	int                 IdentifyScheduleDay     (
								char *              pcz__S);
global	int                 IdentifyScheduleMonth   (
								char *              pcz__S);
global	int                 IdentifyScheduleCity    (
								char *              pcz__S);

global	int                 IdentifyTeam            (
								char *              pcz__S);
global	int                 IdentifyDiv             (
								char *              pcz__S);
global	int                 IdentifyCon             (
								char *              pcz__S);
global	int                 IdentifyPos             (
								char *              pcz__S);
global	int                 IdentifyHand            (
								char *              pcz__S);

global	int                 IdentifyString          (
								char *              pcz__S,
								STR_TO_NUM_T *      pcl__T);

static	char *              strrmvspace             (
								char *              pcz__S);

//
//
//

extern	long                LoadWholeFile           (
								char *              pcz__Name,
								unsigned char **    ppbf_Addr,
								long *              psl__Size);

extern	long                SaveWholeFile           (
								char *              pcz__Name,
								unsigned char *     pbf__Addr,
								long                sl___Size);

extern	long                GetFileLength           (
								FILE *              file);

extern	void                ErrorReset              (void);
extern	void                ErrorQualify            (void);

//
//
//

extern	int                 ProcessOption           (
								char *              pcz__Option);

extern	int                 ProcessFileSpec         (
								char *              pcz__File);

extern	int                 ProcessFile             (
								char *              pcz__File);

//
//
//

static	char *              GetLong                 (
								SL *                l,
								char *              s);

static	char *              GetHeight               (
								SL *                l,
								char *              s);

static	char *              GetFloat                (
								double *            f,
								char *              s);



// **************************************************************************
// **************************************************************************
// **************************************************************************
//	GLOBAL FUNCTIONS
// **************************************************************************
// **************************************************************************
// **************************************************************************

static FILE
	*LogFile = NULL;


void output( char *args, ... )
{
	char
		text[256];

	vsprintf( text, args, (char *)(&args+1) );

	printf( text );

	if (LogFile)
		fprintf( LogFile, text );
}


// **************************************************************************
// * main ()                                                                *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  int      argument count                                        *
// *         char **  argument strings                                      *
// *                                                                        *
// * Output  int      Returns an exit code for the whole program.           *
// **************************************************************************

global	int __cdecl         main                    (
								int                 argc,
								char **             argv)

	{

	// Local variables.

	int                 i;

	// Sign on.

	printf("\n%s by J.C.Brandwood\n\n", VERSION_STR);

	// Check the command line arguments.

	if (argc < 2)
		{
		ProcessOption("-?");
		goto exit;
		}

	// Read through and process the arguments.

	for (i = 1; i < argc; i++)
		{
		if ((*argv[i] == '-') || (*argv[i] == '/'))
			{
			if (ProcessOption(argv[i]) != ERROR_NONE) goto exit;
			}
		else
			{
			break;
//			if (ProcessFileSpec(argv[i]) != ERROR_NONE) goto exit;
			}
		}

	//

	if (argc < (i+4))
		{
		ErrorCode = ERROR_ILLEGAL;
		sprintf(ErrorMessage,
			"XvtNBA - Missing file argument !\n");
		goto exit;
		}

	//

	HeadGeom_Init();

	InitStringTree();

	InitPlyrTree();


	LogFile = fopen( "XvtNBA.LOG", "w" );

	// Process the teams.

	LoadTeamFile(argv[i+0]);

	OrderTeams();

	// Process the teams last season stats.

	LoadStatFile(argv[i+1]);

	// Process the players.

	LoadPlyrFile(argv[i+2]);

	OrderPlyrs();

	printf("XvtNBA - Found %d repeat plyrs !\n", si___gRepeatPlyr);

	// Create the rosters.

	MakeRosters();

	// Order the string table.

	OrderStrings();

	// Process the schedule.

	LoadDateFile(argv[i+3]);

	// Construct shorter seasons.

	MakeHSeason();
	MakeQSeason();
	MakeESeason();

	// Display the schedule.

	ShowSeason(IS_IN_F_SEASON);
	ShowSeason(IS_IN_H_SEASON);
	ShowSeason(IS_IN_Q_SEASON);
	ShowSeason(IS_IN_C_SEASON);

	// Write out the processed info.

	SaveInfoFile("TEAMINFO.IFF");

	// Write out the processed data.

	SaveDataFile("TEAMDATA.IFF");

	SaveHeadsTex( "HEADS.GFX" );
	SaveHeadsGeom( "HEADS.DIR" );

	SavePlayerAudio( "PLAYERS.BNK", "NICKNAME.BIT" );

	fclose( LogFile );

	SavePlayerIDs( "PlayerID.H" );

	// Print success message.

	printf("XvtN64 - Operation Completed OK !\n\n");

	// Program exit.
	//
	// This will either be dropped through to if everything is OK, or 'goto'ed
	// if there was an error.

	exit:

	ErrorQualify();

	if (ErrorCode != ERROR_NONE)
		{
		puts(ErrorMessage);
		}

	// All done.

	return ((ErrorCode != ERROR_NONE));
	}



// **************************************************************************
// * ProcessOption ()                                                       *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  char *   option                                                *
// *                                                                        *
// * Output  int      Returns an exit code for the whole program.           *
// **************************************************************************

global	int                 ProcessOption           (
								char *              pcz__Option)

	{
	// Local variables.

//	int                 i;
//	long                l;
//	char *              p;

	// Process option string.

	strupr(pcz__Option);

	switch(pcz__Option[1])
		{
		// Display help.

		case '?':
		case 'H':
			{
			printf
				(
				"Purpose    : NBA roster converter\n"
				"\n"
				"Usage      : XvtNBA [<option>] <teams> <stats> <plyrs> <schedule>\n"
				"\n"
				"Example    : XvtNBA nbateams.csv nba9697.csv nbaplyrs.csv nbadates.csv\n"
				"\n"
				"<option>   : Option.....Description...................................\n"
				"\n"
				"             -bp        Read binary photographs\n"
				"\n"
				"\n"
				);

			ErrorCode = ERROR_NONE;

			return (ERROR_DIAGNOSTIC);
			}

		// Select binary input.

		case 'B':
			{
			switch (pcz__Option[2])
				{
				case 'P':
					fl___BinaryPhoto = TRUE;
					break;
				default:
					sprintf(ErrorMessage,
						"XvtNBA - Illegal binary-mode option !\n");
					return (ErrorCode = ERROR_ILLEGAL);
				}

			break;
			}

		// Unknown option.

		default:
			{
			sprintf(ErrorMessage,
				"XvtNBA - Unknown option !\n");
			return (ErrorCode = ERROR_ILLEGAL);
			}
		}

	// All done.

	return (ERROR_NONE);
	}



// **************************************************************************
// * GetLong ()                                                             *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	char *              GetLong                 (
								SL *                l,
								char *              s)

	{
	char * string;
	char * p;

	string = strtok(s, " \t\n");

	if (string == NULL) return (NULL);

	*l = strtol(string, &p, 0);

	if (*p != '\0') return (NULL);

	return (p);
	}



// **************************************************************************
// * GetHeight ()                                                           *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	char *              GetHeight               (
								SL *                l,
								char *              s)

	{
	char * string;
	char * p;
	SL     f;
	SL     i;

	string = strtok(s, "-");

	if (string == NULL) return (NULL);

	f = strtol(string, &p, 0);

	if (*p != '\0') return (NULL);

	string = strtok(s, " \t\n");

	if (string == NULL) return (NULL);

	i = strtol(string, &p, 0);

	if (*p != '\0') return (NULL);

	*l = (f << 4) + i;

	return (p);
	}



// **************************************************************************
// * GetFloat ()                                                            *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	char *              GetDouble               (
								double *            f,
								char *              s)

	{
	char * string;
	char * p;

	string = strtok(s, " \t\n");

	if (string == NULL) return (NULL);

	*f = strtod(string, &p);

	if (*p != '\0') return (NULL);

	return (p);
	}



// **************************************************************************
// * ProcessFileSpec ()                                                     *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  char *   option                                                *
// *                                                                        *
// * Output  int      Returns an exit code for the whole program.           *
// **************************************************************************

global	int                 ProcessFileSpec         (
								char *              pcz__File)

	{
	// Local variables.

	int                 i;

    long                h____FileSpec;
    struct _finddata_t  cl___FileSpec;

	char                acz__All [_MAX_PATH];
	char                acz__Drv [_MAX_DRIVE];
	char                acz__Dir [_MAX_DIR];
	char                acz__Nam [_MAX_FNAME];
	char                acz__Ext [_MAX_EXT];

	// File name too long ?

	if (strlen(pcz__File) > _MAX_PATH)
		{
		sprintf(ErrorMessage,
			"XvtNBA - File name too long !\n");
		return (ErrorCode = ERROR_ILLEGAL);
		}

	// Copy the filespec to our buffer.

	#if 0
		if (_fullpath(acz__All, pcz__File, _MAX_PATH) == NULL)
			{
			sprintf(ErrorMessage,
				"XvtNBA - Illegal filename argument !\n");
			return (ErrorCode = ERROR_NO_FILE);
			}
	#else
		strcpy(acz__All, pcz__File);
	#endif

	// Split the filespec into its components.

	_splitpath(acz__All, acz__Drv, acz__Dir, acz__Nam, acz__Ext);

	// Now search the given path for files matching the filespec.

	if ((h____FileSpec = _findfirst(acz__All, &cl___FileSpec)) == -1L)
		{
		sprintf(ErrorMessage,
			"XvtNBA - File not found !\n");
		return (ErrorCode = ERROR_NO_FILE);
		}
	else
		{
		do	{
			// Have we found a directory or a file ?

			if ((cl___FileSpec.attrib & (_A_SUBDIR)) != 0)
				{
				// Process subdirectory.

				}
			else
			if ((cl___FileSpec.attrib & (_A_HIDDEN | _A_SYSTEM)) == 0)
				{
				// Process normal file.

				strcpy(acz__All, acz__Drv);
				strcat(acz__All, acz__Dir);
				strcat(acz__All, cl___FileSpec.name);

				// Process the file.

				if ((i = ProcessFile(acz__All)) != ERROR_NONE)
					{
					_findclose(h____FileSpec);

					return (i);
					}
				}

			} while (_findnext(h____FileSpec, &cl___FileSpec) == 0);

		_findclose(h____FileSpec);
		}

	// All done, return success.

	return (ERROR_NONE);
	}



// **************************************************************************
// * ProcessFile ()                                                         *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  char *   option                                                *
// *                                                                        *
// * Output  int      Returns an exit code for the whole program.           *
// **************************************************************************

global	int                 ProcessFile             (
								char *              pcz__File)

	{
	// Local variables.

	FILE *              pcl__File;

	// Initialize the WAV file info block.

	ErrorCode = 0;

	// Inform user.

	printf("Processing \"%s\"\n", pcz__File);

	// File name too long ?

	if (strlen(pcz__File) > _MAX_PATH)
		{
		sprintf(ErrorMessage,
			"XvtNBA - File name too long !\n");
		return (ErrorCode = ERROR_ILLEGAL);
		}

	// Split the filename into its components.

	_splitpath(pcz__File, acz__FileDrv, acz__FileDir, acz__FileNam, acz__FileExt);

	// Create input filename.

	strcpy(acz__FileInp, pcz__File);

	// Open the file.

	if ((pcl__File = fopen(acz__FileInp, "rb")) == NULL)
		{
		sprintf(ErrorMessage,
			"XvtNBA - Unable to open input file \"%s\" !\n",
			acz__FileInp);
		return (ErrorCode = ERROR_NO_FILE);
		}

	// Determine the file type and read the information that it contains.

	// Close the file.

	fclose(pcl__File);

	// Write out the result ?

/*
	if (fl___Output)
		{
		// Construct the output filename.

//		strcpy(acz__FileOut, acz__FileDrv);
//		strcat(acz__FileOut, acz__FileDir);
//		strcat(acz__FileOut, acz__FileNam);

		strcpy(acz__FileOut, acz__FileNam);

		strcat(acz__FileOut, ".RES");

		if (XvertToRES(pcl__Bitmap) < 0)
			{
			goto errorExit;
			}
		}
*/

	// Free up resources and return error code.

	ErrorCode = 0;

//	errorExit:

		return (ErrorCode);
	}





// **************************************************************************
// * LoadTeamFile ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	int                 LoadTeamFile            (
								char *              pcz__File)

	{
	// Local variables.

	int                 errorcode = -1;

	long                l;

	TEAMNODE_T *        pcl__T;

	char *              pcz__S;

	FILE *              pcl__File;
	int                 si___Line;
	char                acz__Line [1024];

	// Open the file.

	if ((pcl__File = fopen(pcz__File, "rt")) == NULL)
		{
		ErrorCode = ERROR_NO_FILE;
		sprintf(ErrorMessage,
			"XvtNBA - Unable to open input file \"%s\" !\n",
			pcz__File);
		goto errorExit;
		}

	// Add the free-agent dummy team.

	if ((pcl__T = calloc(sizeof(TEAMNODE_T), 1)) == NULL)
		{
		printf("XvtNBA - Not enough memory to add a team !\n");
		exit(1);
		}

	pcl__T->r.pcz__tCity       = (char *) AddString("Free Agents");
	pcl__T->r.pcz__tName       = (char *) AddString("");
	pcl__T->r.pcz__tAbbr       = (char *) AddString("NUL");
	pcl__T->r.pcz__tCityName   = (char *) AddString("Free Agents");
	pcl__T->r.ub___tTeam       = TEAM_NONE;
	pcl__T->r.ub___tConference = CNF_SPECIAL;
	pcl__T->r.ub___tDivision   = DIV_SPECIAL;

	AddTeam(pcl__T);

	// Process each line.

	si___Line = 0;

	while (fgets(acz__Line, 1024, pcl__File) != NULL)
		{
		// Increment line count.

		si___Line += 1;

		// Skip first 1 lines which are headings.

		if (si___Line < 2) continue;

		// Get team city (skipping blank lines and lines with no name).

		if ((pcz__S = strcsv(acz__Line)) == NULL)
			{
			continue;
			}

		pcz__S = strrmvspace(pcz__S);

		if (*pcz__S == 0)
			{
			continue;
			}

		// Allocate a structure to fill.

		if ((pcl__T = calloc(sizeof(TEAMNODE_T), 1)) == NULL)
			{
			printf("XvtNBA - Not enough memory to add a team !\n");
			exit(1);
			}

		// Save last name.

		pcl__T->r.pcz__tCity = (char *) AddString(pcz__S);

		// Process team name.

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team name at line %d !\n", si___Line);
			continue;
			}

		pcz__S = strrmvspace(pcz__S);

		pcl__T->r.pcz__tName = (char *) AddString(pcz__S);

		// Get abbr and team.

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team abbr at line %d !\n", si___Line);
			continue;
			}

		pcz__S = strrmvspace(pcz__S);

		pcl__T->r.pcz__tAbbr = (char *) AddString(pcz__S);

		l = IdentifyTeam(pcz__S);

		if (l < 0)
			{
			output("XvtNBA - Illegal team abbr at line %d !\n", si___Line);
			exit(1);
			}

		pcl__T->r.ub___tTeam = (UB) l;

		// Process team cityname.

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team cityname at line %d !\n", si___Line);
			continue;
			}

		pcz__S = strrmvspace(pcz__S);

		pcl__T->r.pcz__tCityName = (char *) AddString(pcz__S);

		// Get conference.

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team conference at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = IdentifyCon(pcz__S);
		if (l < 0)
			{
			output("XvtNBA - Illegal team conference at line %d !\n", si___Line);
			exit(1);
			}
		pcl__T->r.ub___tConference = (UB) l;

		// Get division.

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team division at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = IdentifyDiv(pcz__S);
		if (l < 0)
			{
			output("XvtNBA - Illegal team division at line %d !\n", si___Line);
			exit(1);
			}
		pcl__T->r.ub___tDivision = (UB) l;

		// Get rating - scoring.

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team scoring at line %d !\n", si___Line);
			continue;
			}

		pcz__S = strrmvspace(pcz__S);

		l = strtol(pcz__S, &pcz__S, 0);

		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal team scoring at line %d !\n", si___Line);
			exit(1);
			}

		pcl__T->r.ub___tRatScoring = (UB) l;

		// Get rating - rebound.

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team rebound at line %d !\n", si___Line);
			continue;
			}

		pcz__S = strrmvspace(pcz__S);

		l = strtol(pcz__S, &pcz__S, 0);

		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal team rebound at line %d !\n", si___Line);
			exit(1);
			}

		pcl__T->r.ub___tRatRebound = (UB) l;

		// Get rating - control.

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team control at line %d !\n", si___Line);
			continue;
			}

		pcz__S = strrmvspace(pcz__S);

		l = strtol(pcz__S, &pcz__S, 0);

		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal team control at line %d !\n", si___Line);
			exit(1);
			}

		pcl__T->r.ub___tRatControl = (UB) l;

		// Get rating - defense.

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team defense at line %d !\n", si___Line);
			continue;
			}

		pcz__S = strrmvspace(pcz__S);

		l = strtol(pcz__S, &pcz__S, 0);

		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal team defense at line %d !\n", si___Line);
			exit(1);
			}

		pcl__T->r.ub___tRatDefense = (UB) l;

		// Get rating - overall.

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team overall at line %d !\n", si___Line);
			continue;
			}

		pcz__S = strrmvspace(pcz__S);

		l = strtol(pcz__S, &pcz__S, 0);

		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal team overall at line %d !\n", si___Line);
			exit(1);
			}

		pcl__T->r.ub___tRatOverall = (UB) l;

		// Get default team set - #1 of 2.

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing default team set 1 at line %d !\n", si___Line);
			continue;
			}

		pcz__S = strrmvspace(pcz__S);

		l = strtol(pcz__S, &pcz__S, 0);

		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal defatult team set 1 at line %d !\n", si___Line);
			exit(1);
			}

		pcl__T->r.ub___tDefaultSet0 = (UB) l;

		// Get default team set - #2 of 2.

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing default team set 2 at line %d !\n", si___Line);
			continue;
			}

		pcz__S = strrmvspace(pcz__S);

		l = strtol(pcz__S, &pcz__S, 0);

		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal defatult team set 2 at line %d !\n", si___Line);
			exit(1);
			}

		pcl__T->r.ub___tDefaultSet1 = (UB) l;

		// Get Give'N'Go.

		if ((pcz__S = strcsv(NULL)) == NULL)
		{
			output("XvtNBA - Missing Give'N'Go at line %d !\n", si___Line);
			continue;
		}

		pcz__S = strrmvspace(pcz__S);

		l = strtol(pcz__S, &pcz__S, 0);

		if (*pcz__S != '\0')
		{
			output("XvtNBA - Illegal Give'N'Go at line %d !\n", si___Line);
//			exit(1);
			l = 0;
		}

		pcl__T->r.GnG = (UB)l;

		// Get Offensive Rotation.

		if ((pcz__S = strcsv(NULL)) == NULL)
		{
			output("XvtNBA - Missing Offensive Rotation at line %d !\n", si___Line);
			continue;
		}

		pcz__S = strrmvspace(pcz__S);

		l = strtol(pcz__S, &pcz__S, 0);

		if (*pcz__S != '\0')
		{
			output("XvtNBA - Illegal Offensive Rotation at line %d !\n", si___Line);
//			exit(1);
			l = 0;
		}

		pcl__T->r.OffRo = (UB)l;

		// Get Set Picks.

		if ((pcz__S = strcsv(NULL)) == NULL)
		{
			output("XvtNBA - Missing Set Picks at line %d !\n", si___Line);
			continue;
		}

		pcz__S = strrmvspace(pcz__S);

		l = strtol(pcz__S, &pcz__S, 0);

		if (*pcz__S != '\0')
		{
			output("XvtNBA - Illegal Set Picks at line %d !\n", si___Line);
//			exit(1);
			l = 0;
		}

		pcl__T->r.SetPicks = (UB)l;

		//
		//
		//

		// Finished processing line, add the player structure to the list.

		AddTeam(pcl__T);

		// Loop around and get the next line.
		}

	// Normal finish, or error ?

	if (!feof(pcl__File))
		{
		ErrorCode = ERROR_IO_READ;
		sprintf(ErrorMessage,
			"XvtNBA - File error while reading file.\n");
		goto errorExit;
		}

	// All done OK.

	ErrorCode = 0;

	// Close the file and return the errorcode.

	errorExit:

	if (pcl__File)
		{
		fclose(pcl__File);
		}

	pcl__File = NULL;

	return (ErrorCode);
	}



// **************************************************************************
// * LoadStatFile ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	int                 LoadStatFile            (
								char *              pcz__File)

	{
	// Local variables.

	int                 errorcode = -1;
	int                 i;

	long                l;

	TEAMNODE_T *        pcl__T;

	char *              pcz__S;

	FILE *              pcl__File;
	int                 si___Line;
	char                acz__Line [1024];

	// Open the file.

	if ((pcl__File = fopen(pcz__File, "rt")) == NULL)
		{
		ErrorCode = ERROR_NO_FILE;
		sprintf(ErrorMessage,
			"XvtNBA - Unable to open input file \"%s\" !\n",
			pcz__File);
		goto errorExit;
		}

	// Process each line.

	si___Line = 0;

	while (fgets(acz__Line, 1024, pcl__File) != NULL)
		{
		// Increment line count.

		si___Line += 1;

		// Skip first 1 lines which are headings.

		if (si___Line < 2) continue;

		// Get team abbr (skipping blank lines and lines with no name).

		if ((pcz__S = strcsv(acz__Line)) == NULL)
			{
			continue;
			}

		pcz__S = strrmvspace(pcz__S);

		if (*pcz__S == 0)
			{
			continue;
			}

		// Locate team structure.

		for (i = 0; i <= si___gTeamCount; i++)
			{
			pcl__T = apcl_gTeam[i];

			if (pcl__T != NULL)
				{
				if (strcmpi(pcz__S,
					((STRNODE_T *) pcl__T->r.pcz__tAbbr)->pcz__string) == 0) break;
				}
			}

		if (i > si___gTeamCount)
			{
			output("XvtNBA - Can't match team to stats at line %d !\n", si___Line);
			exit(1);
			}

		// Get team GP

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team GP at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal team GP at line %d !\n", si___Line);
			exit(1);
			}
		pcl__T->r.cl___tStatNBA.uw___tGP = (UW) l;

		// Get team GW

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team GW at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal team GW at line %d !\n", si___Line);
			exit(1);
			}
		pcl__T->r.cl___tStatNBA.uw___tGW = (UW) l;

		// Get team FGM

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team FGM at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal team FGM at line %d !\n", si___Line);
			exit(1);
			}
		pcl__T->r.cl___tStatNBA.uw___tFGM = (UW) l;

		// Get team FGA

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team FGA at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal team FGA at line %d !\n", si___Line);
			exit(1);
			}
		pcl__T->r.cl___tStatNBA.uw___tFGA = (UW) l;

		// Skip team FG%

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team FG% at line %d !\n", si___Line);
			continue;
			}

		// Get team 3PM

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team 3PM at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal team 3PM at line %d !\n", si___Line);
			exit(1);
			}
		pcl__T->r.cl___tStatNBA.uw___t3PM  = (UW) l;
		pcl__T->r.cl___tStatNBA.uw___tFGM -= (UW) l; // Seperate 3pt shots from field goals

		// Get team 3PA

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team 3PA at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal team 3PA at line %d !\n", si___Line);
			exit(1);
			}
		pcl__T->r.cl___tStatNBA.uw___t3PA  = (UW) l;
		pcl__T->r.cl___tStatNBA.uw___tFGA -= (UW) l; // Seperate 3pt shots from field goals

		// Skip team 3P%

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team 3P% at line %d !\n", si___Line);
			continue;
			}

		// Get team FTM

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team FTM at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal team FTM at line %d !\n", si___Line);
			exit(1);
			}
		pcl__T->r.cl___tStatNBA.uw___tFTM = (UW) l;

		// Get team FTA

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team FTA at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal team FTA at line %d !\n", si___Line);
			exit(1);
			}
		pcl__T->r.cl___tStatNBA.uw___tFTA = (UW) l;

		// Skip team FT%

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team FT% at line %d !\n", si___Line);
			continue;
			}

		// Get team OFF rebounds

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team OFF at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal team OFF at line %d !\n", si___Line);
			exit(1);
			}
		pcl__T->r.cl___tStatNBA.uw___tOFF = (UW) l;

		// Get team DEF rebounds

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team DEF at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal team DEF at line %d !\n", si___Line);
			exit(1);
			}
		pcl__T->r.cl___tStatNBA.uw___tDEF = (UW) l;

		// Skip team TOT rebounds

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team TOT at line %d !\n", si___Line);
			continue;
			}

		// Get team AST

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team AST at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal team AST at line %d !\n", si___Line);
			exit(1);
			}
		pcl__T->r.cl___tStatNBA.uw___tAST = (UW) l;

		// Get team PF

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team PF at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal team PF at line %d !\n", si___Line);
			exit(1);
			}
		pcl__T->r.cl___tStatNBA.uw___tPF = (UW) l;

		// Get team DQ

		/*
		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			printf("XvtNBA - Missing team DQ at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			printf("XvtNBA - Illegal team DQ at line %d !\n", si___Line);
			exit(1);
			}
		pcl__T->r.cl___tStatNBA.ub___tDQ = (UB) l;
		*/

		// Skip team DQ

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team DQ at line %d !\n", si___Line);
			continue;
			}

		// Get team STL

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team STL at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal team STL at line %d !\n", si___Line);
			exit(1);
			}
		pcl__T->r.cl___tStatNBA.uw___tSTL = (UW) l;

		// Get team TO

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team TO at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal team TO at line %d !\n", si___Line);
			exit(1);
			}
		pcl__T->r.cl___tStatNBA.uw___tTO = (UW) l;

		// Get team BLK

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing team BLK at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal team BLK at line %d !\n", si___Line);
			exit(1);
			}
		pcl__T->r.cl___tStatNBA.uw___tBLK = (UW) l;

		//
		//
		//

		// Loop around and get the next line.
		}

	// Normal finish, or error ?

	if (!feof(pcl__File))
		{
		ErrorCode = ERROR_IO_READ;
		sprintf(ErrorMessage,
			"XvtNBA - File error while reading file.\n");
		goto errorExit;
		}

	// All done OK.

	ErrorCode = 0;

	// Close the file and return the errorcode.

	errorExit:

	if (pcl__File)
		{
		fclose(pcl__File);
		}

	pcl__File = NULL;

	return (ErrorCode);
	}



// **************************************************************************
// * LoadPlyrFile ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	int                 LoadPlyrFile            (
								char *              pcz__File)

	{
	// Local variables.

	int                 errorcode = -1;

	long                l;

	PLYRNODE_T *        pcl__P;
	TEAMNODE_T *        pcl__T;

	char *              pcz__S;

	FILE *              pcl__File;
	int                 si___Line;
	char                acz__Line [1024];

	// Open the file.

	if ((pcl__File = fopen(pcz__File, "rt")) == NULL)
		{
		ErrorCode = ERROR_NO_FILE;
		sprintf(ErrorMessage,
			"XvtNBA - Unable to open input file \"%s\" !\n",
			pcz__File);
		goto errorExit;
		}

	// Process each line.

	si___Line = 0;

	while (fgets(acz__Line, 1024, pcl__File) != NULL)
		{
		// Increment line count.

		si___Line += 1;

		// Skip first 2 lines which are headings.

		if (si___Line < 3) continue;

		// Skip nintendo plyr id number (and skip blank lines).

		if (((pcz__S = strcsv(acz__Line)) == NULL) || (*pcz__S == 0))
			{
			continue;
			}

		// Skip nintendo team id number

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			continue;
			}

		// Get last name (skipping blank lines and lines with no name).

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr sname at line %d !\n", si___Line);
			continue;
			}

		pcz__S = strrmvspace(pcz__S);

		if (*pcz__S == 0)
			{
			continue;
			}

		// Allocate a structure to fill.

		if ((pcl__P = calloc(sizeof(PLYRNODE_T), 1)) == NULL)
			{
			output("XvtNBA - Not enough memory to add a player !\n");
			exit(1);
			}

		// Save last name.

		pcl__P->r.pcz__pSName = (char *) AddString(pcz__S);

		// Process first name(s).

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			break;
			}

		pcz__S = strrmvspace(pcz__S);

		pcl__P->r.pcz__pCName = (char *) AddString(pcz__S);

		// Get plyr team

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr team at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = IdentifyTeam(pcz__S);
		if (l < 0)
			{
			output("XvtNBA - Illegal plyr team at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pTeam = (UB) l;

		// Get plyr position

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr POS at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = IdentifyPos(pcz__S);
		if (l < 0)
			{
			output("XvtNBA - Illegal plyr POS at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pPos = (UB) l;

		// Get wrist pads

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr wrist pads at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); pcz__S[1] = 0; l = IdentifyString(pcz__S, acl__gWristPads);
		if (l < 0)
			{
			output("XvtNBA - Illegal plyr wrist pads at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAcc0 |= (UB) l;

		// Get arm pads

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr arm pads at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); pcz__S[1] = 0; l = IdentifyString(pcz__S, acl__gArmPads);
		if (l < 0)
			{
			output("XvtNBA - Illegal plyr arm pads at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAcc0 |= (UB) l;

		// Get knee pads

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr knee pads at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); pcz__S[1] = 0; l = IdentifyString(pcz__S, acl__gKneePads);
		if (l < 0)
			{
			output("XvtNBA - Illegal plyr knee pads at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAcc0 |= (UB) l;

		// Get socks

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr socks at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); pcz__S[1] = 0; l = IdentifyString(pcz__S, acl__gSocks);
		if (l < 0)
			{
			output("XvtNBA - Illegal plyr socks at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAcc0 |= (UB) l;

		// Get pads colour

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr pad colour at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = IdentifyString(pcz__S, acl__gPadColour);
		if (l < 0)
			{
			output("XvtNBA - Illegal plyr pad colour at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAcc1 |= (UB) l;

		// Get head type

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr head type at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = IdentifyString(pcz__S, acl__gHeadType);
		if (l < 0)
			{
			output("XvtNBA - Illegal plyr head type at line %d !\n", si___Line);
//phil			exit(1);
l = 0;
			}
		pcl__P->r.ub___pAcc1 |= (UB) l;

		// Get body type (and store whether we're using the default)

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr body type at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S);

		if (strcmp(pcz__S, "0") != 0)
			{
//			pcl__P->r.ub___pAcc1 |= (UB) 0x80;

			l = IdentifyString(pcz__S, acl__gBodyType);
			if (l < 0)
				{
				output("XvtNBA - Illegal plyr body type at line %d !\n", si___Line);
//phil				exit(1);
l = 0;
				}
			pcl__P->r.ub___pAcc1 |= (UB) l;
			}

		// Get plyr skin

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr skin at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr skin at line %d !\n", si___Line);
//phil			exit(1);
l = 0;
			}
		pcl__P->r.ub___pSkin = (UB) l;

#if 0 //phil
		// Get plyr base filename

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr filename at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S);

		strcpy(pcl__P->acz__filename, pcz__S);
#endif

		// Get plyr uniform number

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr NUM at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr NUM at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pNum = (UB) l;

		// Skip plyr college

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr college at line %d !\n", si___Line);
			continue;
			}

		// Skip plyr draft number

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr draft number at line %d !\n", si___Line);
			continue;
			}

		// Skip plyr draft team

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr draft team at line %d !\n", si___Line);
			continue;
			}

		// Get plyr handed

/*
		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			printf("XvtNBA - Missing plyr handed at line %d !\n", si___Line);
			continue;
			}

		pcz__S = strrmvspace(pcz__S);

		if (*pcz__S == 0)
			{
			l = 0;
			}
		else
			{
			l = IdentifyHand(pcz__S);
			}

		if (l < 0)
			{
			printf("XvtNBA - Illegal plyr handed at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pHand = (UB) l;
*/

		// Get plyr dunk code

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr DUNKS at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr DUNKS at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pDunks = (UB) l;

		// Get plyr postup code

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr POSTUPS at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr POSTUPS at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pPostUps = (UB) l;

		// Get plyr height

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr HGT at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S);

		if (*pcz__S == 0)
			{
			pcl__P->r.ub___pHgt = (UB) 0x6Au;
			}
		else
			{
			l = strtol(pcz__S,     &pcz__S, 0);
			if (*pcz__S != '-')
				{
				output("XvtNBA - Illegal plyr HGT at line %d !\n", si___Line);
				exit(1);
				}
			l = strtol(pcz__S + 1, &pcz__S, 0) + (l << 4);
			if (*pcz__S != '\0')
				{
				output("XvtNBA - Illegal plyr HGT at line %d !\n", si___Line);
				exit(1);
				}
			pcl__P->r.ub___pHgt = (UB) l;
			}

		// Get plyr weight

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr WGT at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr WGT at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pWgt = (UB) l - 100;

		// Get plyr age

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr AGE at line %d !\n", si___Line);
			continue;
			}
//		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
//		if (*pcz__S != '\0')
//			{
//			printf("XvtNBA - Illegal plyr AGE at line %d !\n", si___Line);
//			exit(1);
//			}

		l = 0;

		pcl__P->r.ub___pAge = (UB) l;

		// Get plyr experience

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr EXP at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr EXP at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pExp = (UB) l;

		// Get plyr rating range

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr attribute (range) at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr attribute (range) at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAttrRange = (UB) l;

		// Get plyr rating FG

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr attribute (FG) at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr attribute (FG) at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAttrFG = (UB) l;

		// Get plyr rating 3P

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr attribute (3P) at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr attribute (3P) at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAttr3P = (UB) l;

		// Get plyr rating FT

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr attribute (FT) at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr attribute (FT) at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAttrFT = (UB) l;

		// Get plyr rating REB

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr attribute (REB) at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr attribute (REB) at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAttrRebOff = (UB) l;
		pcl__P->r.ub___pAttrRebDef = (UB) l;

		// Get plyr rating STL

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr attribute (steal) at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr attribute (steal) at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAttrSteal = (UB) l;

		// Get plyr rating Pass

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr attribute (pass) at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr attribute (pass) at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAttrPass = (UB) l;

		// Get plyr rating Block

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr attribute (block) at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr attribute (block) at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAttrBlock = (UB) l;

		// Get plyr rating Foul

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr attribute (foul) at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr attribute (foul) at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAttrFoul = (UB) l;

		// Get plyr rating Dribble

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr attribute (dribble) at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr attribute (dribble) at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAttrDribble = (UB) l;

		// Get plyr rating Speed

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr attribute (speed) at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr attribute (speed) at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAttrSpeed = (UB) l;

		// Get plyr rating Jump

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr attribute (jump) at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr attribute (jump) at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAttrJump = (UB) l;

		// Get plyr rating Dunk

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr attribute (dunk) at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr attribute (dunk) at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAttrDunk = (UB) l;

		// Get plyr rating Goto

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr attribute (goto) at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr attribute (goto) at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAttrGoto = (UB) l;

		// Get plyr rating Strength

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr attribute (strength) at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr attribute (strength) at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAttrStrength = (UB) l;

		// Get plyr rating Stamina

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr attribute (stamina) at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr attribute (stamina) at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAttrStamina = (UB) l;

		// Get plyr rating Injury

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr attribute (injury) at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr attribute (injury) at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.ub___pAttrInjury = (UB) l;

		// Get plyr rating Defense

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr attribute (defense) at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr attribute (defense) at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.pAttrDefense = (UB) l;

		// Get plyr GP

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr GP at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr GP at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.cl___pStatNBA.uw___pGP = (UW) l ;

		// Skip plyr GP%

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr GP%% at line %d !\n", si___Line);
			continue;
			}

		// Skip plyr GS

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr GS at line %d !\n", si___Line);
			continue;
			}

		// Skip plyr GS%

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr GS%% at line %d !\n", si___Line);
			continue;
			}

		// Get plyr MIN (and store with 1/4 minute resolution)

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr MIN at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr MIN at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.cl___pStatNBA.uw___pMIN = (UW) (l << 2);

		// Skip plyr MIN-per-G

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr MIN-per-G at line %d !\n", si___Line);
			continue;
			}

		// Get plyr FGM

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr FGM at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr FGM at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.cl___pStatNBA.uw___pFGM = (UW) l;

		// Get plyr FGA

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr FGA at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr FGA at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.cl___pStatNBA.uw___pFGA = (UW) l;

		// Skip plyr FG%

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr FG%% at line %d !\n", si___Line);
			continue;
			}

		// Get plyr 3PM

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr 3PM at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr 3PM at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.cl___pStatNBA.uw___p3PM  = (UW) l;
		pcl__P->r.cl___pStatNBA.uw___pFGM -= (UW) l; // Seperate 3pt shots from field goals

		// Get plyr 3PA

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr 3PA at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr 3PA at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.cl___pStatNBA.uw___p3PA  = (UW) l;
		pcl__P->r.cl___pStatNBA.uw___pFGA -= (UW) l; // Seperate 3pt shots from field goals

		// Skip plyr 3P%

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr 3P%% at line %d !\n", si___Line);
			continue;
			}

		// Get plyr FTM

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr FTM at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr FTM at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.cl___pStatNBA.uw___pFTM = (UW) l;

		// Get plyr FTA

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr FTA at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr FTA at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.cl___pStatNBA.uw___pFTA = (UW) l;

		// Skip plyr FT%

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr FT%% at line %d !\n", si___Line);
			continue;
			}

		// Get plyr OFF

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr OFF at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr OFF at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.cl___pStatNBA.uw___pOFF = (UW) l;

		// Skip plyr OFF-per-G

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr OFF-per-G at line %d !\n", si___Line);
			continue;
			}

		// Get plyr DEF and convert to total rebounds

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr DEF at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr DEF at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.cl___pStatNBA.uw___pDEF = (UW) l;

		// Skip plyr DEF-per-G

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr DEF-per-G at line %d !\n", si___Line);
			continue;
			}

		// Skip plyr TOT

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr TOT at line %d !\n", si___Line);
			continue;
			}

		// Skip plyr TOT-per-G

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr TOT-per-G at line %d !\n", si___Line);
			continue;
			}

		// Get plyr AST

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr AST at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr AST at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.cl___pStatNBA.uw___pAST = (UW) l;

		// Skip plyr AST-per-G

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr AST-per-G at line %d !\n", si___Line);
			continue;
			}

		// Get plyr PF

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr PF at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr PF at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.cl___pStatNBA.uw___pPF = (UW) l;

		// Skip plyr PF-per-G

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr PF-per-G at line %d !\n", si___Line);
			continue;
			}

		// Get plyr DQ

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr DQ at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr DQ at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.cl___pStatNBA.uw___pDQ = (UW) l ;

		// Get plyr STL

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr STL at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr STL at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.cl___pStatNBA.uw___pSTL = (UW) l;

		// Skip plyr STL-per-G

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr STL-per-G at line %d !\n", si___Line);
			continue;
			}

		// Get plyr TO

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr TO at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr TO at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.cl___pStatNBA.uw___pTO = (UW) l;

		// Skip plyr TO-per-G

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr TO-per-G at line %d !\n", si___Line);
			continue;
			}

		// Get plyr BLK

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr BLK at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr BLK at line %d !\n", si___Line);
			exit(1);
			}
		pcl__P->r.cl___pStatNBA.uw___pBLK = (UW) l;

		// Skip plyr BLK-per-G

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr BLK-per-G at line %d !\n", si___Line);
			continue;
			}

		// Skip plyr PTS

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr PTS at line %d !\n", si___Line);
			continue;
			}

		// Skip plyr PPG

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr PPG at line %d !\n", si___Line);
			continue;
			}

		// Skip plyr PHI

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr PHI at line %d !\n", si___Line);
			continue;
			}

		// Get head folder name

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing head folder name at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S);

		strcpy(pcl__P->HeadFolder, pcz__S);
		pcl__P->acz__filename = pcl__P->HeadFolder;

		// Get plyr rating Overall

		if ((pcz__S = strcsv(NULL)) == NULL)
		{
			output("XvtNBA - Missing plyr attribute (overall) at line %d !\n", si___Line);
			continue;
		}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
		{
			output("XvtNBA - Illegal plyr attribute (overall) at line %d !\n", si___Line);
//phil			exit(1);
			l = 0;
		}
		pcl__P->r.pAttrOverall = (UW)Swap16( l );

#if 0 //phil
		// Get plyr rookie stats flag

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing plyr ROOKIE-STATS-FLAG at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);
		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal plyr ROOKIE-STATS-FLAG at line %d !\n", si___Line);
			exit(1);
			}

		if (l != 0)
			{
			pcl__P->r.cl___pStatNBA.ub___pDQ |= (UB) 0x80u;
			}
#endif


		//
		// Fill in default plyr body type
		//

#if 0 //phil
		if ((pcl__P->r.ub___pAcc1 & 0x80u) == 0)
			{
			l = (((pcl__P->r.ub___pHgt >> 4) & 15) * 12) + (pcl__P->r.ub___pHgt & 15);

			l = (long) (3.1 * l);

			if (l > (pcl__P->r.ub___pWgt + 100))
				{
				pcl__P->r.ub___pAcc1 |= (PLYR_BODY_THIN);
				}
			}
#endif

		//
		// N.B. The following is written to allow the same plyr to be on more than
		//      1 team, to handle the east and west all-star team.
		//

		// Locate which team this plyr is on.

		pcl__T = apcl_gTeam[pcl__P->r.ub___pTeam];

		if (pcl__T == NULL)
			{
			output("XvtNBA - Can't add player to a team that doesn't exist at line %d!\n",si___Line);
			exit(1);
			}

		// Finished processing line, add the player structure to the list.

		pcl__P = AddPlyr(pcl__P);

		// Finished processing plyr, add the player structure to the team.

		pcl__T->apcl_players[pcl__T->si___players++] = pcl__P;

		// Loop around and get the next line.
		}

	// Normal finish, or error ?

	if (!feof(pcl__File))
		{
		ErrorCode = ERROR_IO_READ;
		sprintf(ErrorMessage,
			"XvtNBA - File error while reading file.\n");
		goto errorExit;
		}

	// All done OK.

	ErrorCode = 0;

	// Close the file and return the errorcode.

	errorExit:

	if (pcl__File)
		{
		fclose(pcl__File);
		}

	pcl__File = NULL;

	return (ErrorCode);
	}



// **************************************************************************
// * LoadDateFile ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	int                 LoadDateFile            (
								char *              pcz__File)

	{
	// Local variables.

	int                 errorcode = -1;

	long                l;

	UW                  cur_game;
	UW                  cur_date;
	UW                  old_date;

	char *              pcz__S;

	FILE *              pcl__File;
	int                 si___Line;
	char                acz__Line [1024];

	// Open the file.

	if ((pcl__File = fopen(pcz__File, "rt")) == NULL)
		{
		ErrorCode = ERROR_NO_FILE;
		sprintf(ErrorMessage,
			"XvtNBA - Unable to open input file \"%s\" !\n",
			pcz__File);
		goto errorExit;
		}

	// Initialize the schedule.

	puw__gSchedule = auw__gSchedule;

	*puw__gSchedule++ = (UW) SCHEDULE_END;

	// Process each line.

	old_date  = 0;

	si___Line = 0;

	while (fgets(acz__Line, 1024, pcl__File) != NULL)
		{
		// Increment line count.

		si___Line += 1;

		// Skip first 1 lines which are headings.

		if (si___Line < 2) continue;

		// Convert the line to upper case.

		strupr(acz__Line);

		// Get the game number (skipping blank lines and lines with no number).

		if ((pcz__S = strcsv(acz__Line)) == NULL)
			{
			continue;
			}

		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);

		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal schedule number at line %d !\n", si___Line);
			exit(1);
			}

		// Get schedule day

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing schedule day at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S);

		if (strlen(pcz__S) < 3)
			{
			output("XvtNBA - Illegal schedule day at line %d !\n", si___Line);
			exit(1);
			}

		pcz__S[3] = 0; l = IdentifyScheduleDay(pcz__S);

		if (l < 0)
			{
			output("XvtNBA - Illegal schedule day at line %d !\n", si___Line);
			exit(1);
			}

		cur_date = (((l - 0) & 7) << 0);				// day = 1-7

		// Get schedule date

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing schedule date at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);

		if (*pcz__S++ != '-')
			{
			output("XvtNBA - Illegal schedule date at line %d !\n", si___Line);
			exit(1);
			}

		cur_date = (((l - 0) & 31) << 3) + cur_date;	// date = 1-31

		if (strlen(pcz__S) < 3)
			{
			output("XvtNBA - Illegal schedule date at line %d !\n", si___Line);
			exit(1);
			}

		pcz__S[3] = 0; l = IdentifyScheduleMonth(pcz__S);

		if (l < 0)
			{
			output("XvtNBA - Illegal schedule date at line %d !\n", si___Line);
			exit(1);
			}

		cur_date = (((l - 0) & 15) << 8) + cur_date;	// month = 1-12

		pcz__S += 4;

		if ((*pcz__S < '0') || (*pcz__S > '9'))
			{
			while (*pcz__S != 0)
				{
				if (*pcz__S++ == '-') break;
				}
			}

		pcz__S = strrmvspace(pcz__S); l = strtol(pcz__S, &pcz__S, 0);

		if (*pcz__S != '\0')
			{
			output("XvtNBA - Illegal schedule date at line %d !\n", si___Line);
			exit(1);
			}

		if (l == 00)
			l = 1;
		else
			l = 0;

		cur_date = (l << 12) + cur_date;	// year = 0-7

		// Get schedule away city

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing schedule away team at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = IdentifyScheduleCity(pcz__S);

		if (l < 0)
			{
			output("XvtNBA - Illegal schedule away team at line %d !\n", si___Line);
			exit(1);
			}

		cur_game = (((l - 0) & 63) << 6) + IS_A_GAME;	// team = 1-63

		// Skip schedule @

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing schedule \'@\' at line %d !\n", si___Line);
			continue;
			}

		// Get schedule home city

		if ((pcz__S = strcsv(NULL)) == NULL)
			{
			output("XvtNBA - Missing schedule away team at line %d !\n", si___Line);
			continue;
			}
		pcz__S = strrmvspace(pcz__S); l = IdentifyScheduleCity(pcz__S);

		if (l < 0)
			{
			output("XvtNBA - Illegal schedule away team at line %d !\n", si___Line);
			exit(1);
			}

		cur_game = (((l - 0) & 63) << 0) + cur_game;	// team = 1-63

		// Save match information.

		if (old_date != cur_date)
			{
			old_date  = cur_date;

			*puw__gSchedule++ = (UW) cur_date;
			*puw__gSchedule++ = (UW) cur_game;
			}
		else
			{
			*puw__gSchedule++ = (UW) cur_game;
			}

		// Loop around and get the next line.
		}

	// Terminate the schedule.

	*puw__gSchedule++ = (UW) SCHEDULE_END;

	// Normal finish, or error ?

	if (!feof(pcl__File))
		{
		ErrorCode = ERROR_IO_READ;
		sprintf(ErrorMessage,
			"XvtNBA - File error while reading file.\n");
		goto errorExit;
		}

	// All done OK.

	ErrorCode = 0;

	// Close the file and return the errorcode.

	errorExit:

	if (pcl__File)
		{
		fclose(pcl__File);
		}

	pcl__File = NULL;

	return (ErrorCode);
	}



// **************************************************************************
// * WriteDates ()                                                          *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                WriteDates              (void)

	{
	// Local Variables.

	int                  i;

	UW *                 p;

	// Reorder the data.

	p = auw__gSchedule;

	while (p != puw__gSchedule)
		{
		*p = (Swap16(*p)); p += 1;
		}

	// Pad out to a 4-byte boundary.

	i = (p - auw__gSchedule) << 1;

	if (i & 2)
		{
		i = i + 2;

		*p++ = 0;
		}

	// Write out the schedule.

	if (aiffWrite(pcl__gOutputFile, (char *) auw__gSchedule, i) != (LONG) i)
		{
		printf("XvtNBA - Can't write schedule to output !\n");
		exit(1);
		}

	// All done.

	return;
	}



// **************************************************************************
// * ShowSeason ()                                                          *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	char *          TotalName [] =
	{
	"HOM",
	"AWA",
	"DIV",
	"CNF",
	"OTH",
	"TOT"
	};

global	UB                  aub__sResults [64][64];

#define	ATHOME              59
#define	ONROAD              60
#define	INDIV               61
#define	INCNF               62
#define	OUTCNF              63

static	void                ShowSeason              (
								UI                  ui___Flag)

	{
	// Local variables.

	UW *            puw__Match;

	UI              ui___Home;
	UI              ui___Away;
	UI              ui___Mask;

	char *          pcz__S;

	int             i;

	int             line;
	int             divn;
	int             team;
	int             divn2;
	int             team2;

	// Initialize global data.

	memset(aub__sResults, 0, sizeof(aub__sResults));

	puw__Match = &auw__gSchedule[1];

	while ((ui___Mask = *puw__Match++) != SCHEDULE_END)
		{
		// Skip date entries.

		if ((ui___Mask & IS_A_GAME) == 0) continue;

		// Skip matches that we aren't interested in.

		if ((ui___Mask & ui___Flag) != ui___Flag) continue;

		// Skip special matches.

		ui___Away = SCHEDULE_AWAY(ui___Mask);
		ui___Home = SCHEDULE_HOME(ui___Mask);

		if ((ui___Home > TEAM_SSN) || (ui___Away > TEAM_SSN)) continue;

		// Tally up the results.

		aub__sResults[ui___Home][ATHOME]    += 1;
		aub__sResults[ui___Home][ui___Away] += 1;
		aub__sResults[ui___Away][ONROAD]    += 1;
		aub__sResults[ui___Away][ui___Home] += 1;

		if (apcl_gTeam[ui___Home]->r.ub___tDivision ==
			apcl_gTeam[ui___Away]->r.ub___tDivision)
			{
			aub__sResults[ui___Home][INDIV] += 1;
			aub__sResults[ui___Away][INDIV] += 1;

			aub__sResults[ui___Home][INCNF] += 1;
			aub__sResults[ui___Away][INCNF] += 1;
			}
		else
			{
			if (apcl_gTeam[ui___Home]->r.ub___tConference ==
				apcl_gTeam[ui___Away]->r.ub___tConference)
				{
				aub__sResults[ui___Home][INCNF] += 1;
				aub__sResults[ui___Away][INCNF] += 1;
				}
			else
				{
				aub__sResults[ui___Home][OUTCNF] += 1;
				aub__sResults[ui___Away][OUTCNF] += 1;
				}
			}
		}

	// Print top titles.

	printf("\n");

	for (line = 0; line < 3; line += 1)
		{
		printf("     ");

		for (divn = 1; divn <= 4; divn += 1)
			{
			for (team = 1; team <= TEAM_SSN; team += 1)
				{
				if (apcl_gTeam[team]->r.ub___tDivision == divn)
					{
					pcz__S = acl__gTeamString[team].pcz__S + line;

					if (*pcz__S == 0)
						putchar(' ');
					else
						putchar(*pcz__S);
					}
				}
			}

		putchar(' ');

		for (divn = 0; divn < 6; divn += 1)
			{
			putchar(*(TotalName[divn] + line));
			putchar(' ');
			putchar(' ');
			}

		printf("\n");

		}

	printf("\n");

	//

	for (divn = 1; divn <= 4; divn += 1)
		{
		for (team = 1; team <= TEAM_SSN; team += 1)
			{
			if (apcl_gTeam[team]->r.ub___tDivision == divn)
				{
				pcz__S = acl__gTeamString[team].pcz__S;

				for (i = 0; i < 3; i += 1)
					{
					if (*pcz__S == 0)
						putchar(' ');
					else
						putchar(*pcz__S);

					pcz__S += 1;
					}

				putchar(' ');
				putchar(' ');

				for (divn2 = 1; divn2 <= 4; divn2 += 1)
					{
					for (team2 = 1; team2 <= TEAM_SSN; team2 += 1)
						{
						if (apcl_gTeam[team2]->r.ub___tDivision == divn2)
							{
							printf("%d", (UI) aub__sResults[team][team2]);
							}
						}
					}

				printf(" %02d %02d %02d %02d %02d %02d\n",
					(UI) aub__sResults[team][ATHOME],
					(UI) aub__sResults[team][ONROAD],
					(UI) aub__sResults[team][INDIV],
					(UI) aub__sResults[team][INCNF],
					(UI) aub__sResults[team][OUTCNF],
					(UI) aub__sResults[team][INCNF] +
					(UI) aub__sResults[team][OUTCNF]);
				}
			}
		}

	// All Done.

	return;
	}



// **************************************************************************
// * MakeHSeason ()                                                         *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                MakeHSeason             (void)

	{
	// Local variables.

	UW *            puw__Match;

	UI              ui___Where;
	UI              ui___Home;
	UI              ui___Away;
	UI              ui___Mask;

	UI              ui___i;
	UI              ui___j;

	UI              ui___HomeCount;
	UI              ui___AwayCount;

	UW *            apuw_MatchHome [8];
	UW *            apuw_MatchAway [8];

	// Initialize global data.

	memset(aub__sResults, 0, sizeof(aub__sResults));

	for (ui___i = 1; ui___i <= TEAM_SSN; ui___i += 1)
		{
		for (ui___j = 1; ui___j <= TEAM_SSN; ui___j += 1)
			{
			aub__sResults[ui___i][ui___j] = 2;
			}
		}

	//

	ui___Where = 0;

	for (ui___i = 1; ui___i <= TEAM_SSN; ui___i += 1)
		{
		for (ui___j = 1; ui___j <= TEAM_SSN; ui___j += 1)
			{
			// Repeat for each match this team has yet to play.

			if ((ui___i != ui___j) && (aub__sResults[ui___i][ui___j] != 0))
				{
				// Create a list of pointers to all the matches between these
				// two teams.

				ui___HomeCount = 0;
				ui___AwayCount = 0;

				puw__Match = &auw__gSchedule[1];

				while ((ui___Mask = *puw__Match++) != SCHEDULE_END)
					{
					if ((ui___Mask & IS_A_GAME) == 0) continue;

					ui___Away = SCHEDULE_AWAY(ui___Mask);
					ui___Home = SCHEDULE_HOME(ui___Mask);

					if ((ui___Home > TEAM_SSN) || (ui___Away > TEAM_SSN)) continue;

					if ((ui___Mask & IS_IN_H_SEASON) == 0)
						{
						if ((ui___Away == ui___i) && (ui___Home == ui___j))
							{
							apuw_MatchAway[ui___AwayCount++] = puw__Match - 1;
							}
						else if ((ui___Home == ui___i) && (ui___Away == ui___j))
							{
							apuw_MatchHome[ui___HomeCount++] = puw__Match - 1;
							}
						}
					}

				// printf("Home %02d Away %02d\n", ui___HomeCount, ui___AwayCount);

				// Now pick the 2 matches we want.

				if (ui___Where)
					{
					*apuw_MatchHome[0]                  |= IS_IN_H_SEASON;
					*apuw_MatchAway[ui___AwayCount - 1] |= IS_IN_H_SEASON;
					}
				else
					{
					*apuw_MatchAway[0]                  |= IS_IN_H_SEASON;
					*apuw_MatchHome[ui___HomeCount - 1] |= IS_IN_H_SEASON;
					}

				ui___Where ^= 1;

				// Now show that we've already done this combo.

				aub__sResults[ui___i][ui___j] = 0;
				aub__sResults[ui___j][ui___i] = 0;

				} // end while

			} // next ui___j

		ui___Where ^= 1;

		} // next ui___i

	// All Done.

	return;

	}



// **************************************************************************
// * MakeQSeason ()                                                         *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                MakeQSeason             (void)

	{
	// Local variables.

	UW *            puw__Match;

	UI              ui___Flip;

	UI              ui___Sum0;
	UI              ui___Sum1;
	SI              si___Dif0;
	SI              si___Dif1;

	UI              ui___Home0Already;
	UI              ui___Away0Already;
	UI              ui___Home1Already;
	UI              ui___Away1Already;

	UI              ui___Where;
	UI              ui___Home;
	UI              ui___Away;
	UI              ui___Mask;

	UI              ui___i;
	UI              ui___j;

	UI              ui___HomeCount;
	UI              ui___AwayCount;

	UW *            apuw_MatchHome [8];
	UW *            apuw_MatchAway [8];

	// Initialize global data.

	memset(aub__sResults, 0, sizeof(aub__sResults));

	for (ui___i = 1; ui___i <= TEAM_SSN; ui___i += 1)
		{
		for (ui___j = 1; ui___j <= TEAM_SSN; ui___j += 1)
			{
			aub__sResults[ui___i][ui___j] = 1;
			}
		}

	//

	ui___Flip = 0;

	for (ui___i = 1; ui___i <= TEAM_SSN; ui___i += 1)
		{
		for (ui___j = 1; ui___j <= TEAM_SSN; ui___j += 1)
			{
			// Repeat for each match this team has yet to play.

			if ((ui___i != ui___j) && (aub__sResults[ui___i][ui___j] != 0))
				{
				// Create a list of pointers to all the matches between these
				// two teams.

				ui___Home0Already = 0;
				ui___Away0Already = 0;
				ui___Home1Already = 0;
				ui___Away1Already = 0;
				ui___HomeCount    = 0;
				ui___AwayCount    = 0;

				puw__Match = &auw__gSchedule[1];

				while ((ui___Mask = *puw__Match++) != SCHEDULE_END)
					{
					if ((ui___Mask & IS_A_GAME) == 0) continue;

					ui___Away = SCHEDULE_AWAY(ui___Mask);
					ui___Home = SCHEDULE_HOME(ui___Mask);

					if ((ui___Home > TEAM_SSN) || (ui___Away > TEAM_SSN)) continue;

					if ((ui___Mask & IS_IN_Q_SEASON) == 0)
						{
						if ((ui___Away == ui___i) && (ui___Home == ui___j))
							{
							apuw_MatchAway[ui___AwayCount++] = puw__Match - 1;
							}
						else if ((ui___Home == ui___i) && (ui___Away == ui___j))
							{
							apuw_MatchHome[ui___HomeCount++] = puw__Match - 1;
							}
						}
					else
						{
						if (ui___Away == ui___i)
							{
							ui___Away0Already += 1;
							}
						if (ui___Home == ui___i)
							{
							ui___Home0Already += 1;
							}
						if (ui___Away == ui___j)
							{
							ui___Away1Already += 1;
							}
						if (ui___Home == ui___j)
							{
							ui___Home1Already += 1;
							}
						}
					}

				//printf("Home0 %02d Away0 %02d  Home1 %02d Away1 %02d  ",
				//	ui___Home0Already, ui___Away0Already,
				//	ui___Home1Already, ui___Away1Already);

				// Now pick the match we want.

				ui___Where = 0;

				ui___Sum0 = ui___Home0Already + ui___Away0Already;
				si___Dif0 = ui___Home0Already - ui___Away0Already;
				ui___Sum1 = ui___Home1Already + ui___Away1Already;
				si___Dif1 = ui___Home1Already - ui___Away1Already;

				if (abs(si___Dif0) > abs(si___Dif1)) {
					if (si___Dif0 > 0) ui___Where = 1;
					}
				else if (abs(si___Dif0) < abs(si___Dif1)) {
					if (si___Dif1 < 0) ui___Where = 1;
					}
				else
					{
					if (ui___Sum0 >= ui___Sum1) {
						if (si___Dif0 > 0) ui___Where = 1;
						} else {
						if (si___Dif1 < 0) ui___Where = 1;
						}
					}

				if (ui___Where == 0)
					{
					if (ui___Flip) {
						*apuw_MatchHome[ui___HomeCount - 1] |= IS_IN_Q_SEASON;
						} else {
						*apuw_MatchHome[ui___HomeCount / 2] |= IS_IN_Q_SEASON;
						}
					}
				else
					{
					if (ui___Flip) {
						*apuw_MatchAway[ui___AwayCount - 1] |= IS_IN_Q_SEASON;
						} else {
						*apuw_MatchAway[ui___AwayCount / 2] |= IS_IN_Q_SEASON;
						}
					}

				// Now show that we've already done this combo.

				aub__sResults[ui___i][ui___j] = 0;
				aub__sResults[ui___j][ui___i] = 0;

				} // end while

			} // next ui___j

		ui___Flip ^= 1;

		} // next ui___i

	//

	return;

	}



// **************************************************************************
// * MakeESeason ()                                                         *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                MakeESeason             (void)

	{
	// Local variables.

	UW *            puw__Match;

	UI              ui___Flip;

	UI              ui___Sum0;
	UI              ui___Sum1;
	SI              si___Dif0;
	SI              si___Dif1;

	UI              ui___Home0Already;
	UI              ui___Away0Already;
	UI              ui___Home1Already;
	UI              ui___Away1Already;

	UI              ui___Where;
	UI              ui___Home;
	UI              ui___Away;
	UI              ui___Mask;

	UI              ui___i;
	UI              ui___j;

	UI              ui___HomeCount;
	UI              ui___AwayCount;

	UW *            apuw_MatchHome [8];
	UW *            apuw_MatchAway [8];

	// Initialize global data.

	memset(aub__sResults, 0, sizeof(aub__sResults));

	for (ui___i = 1; ui___i <= TEAM_SSN; ui___i += 1)
		{
		for (ui___j = 1; ui___j <= TEAM_SSN; ui___j += 1)
			{
			 if (apcl_gTeam[ui___i]->r.ub___tConference ==
			 	 apcl_gTeam[ui___j]->r.ub___tConference)
				{
				aub__sResults[ui___i][ui___j] = 1;
				}
			}
		}

	//

	ui___Flip = 0;

	for (ui___i = 1; ui___i <= TEAM_SSN; ui___i += 1)
		{
		for (ui___j = 1; ui___j <= TEAM_SSN; ui___j += 1)
			{
			// Repeat for each match this team has yet to play.

			if ((ui___i != ui___j) && (aub__sResults[ui___i][ui___j] != 0))
				{
				// Create a list of pointers to all the matches between these
				// two teams.

				ui___Home0Already = 0;
				ui___Away0Already = 0;
				ui___Home1Already = 0;
				ui___Away1Already = 0;
				ui___HomeCount    = 0;
				ui___AwayCount    = 0;

				puw__Match = &auw__gSchedule[1];

				while ((ui___Mask = *puw__Match++) != SCHEDULE_END)
					{
					if ((ui___Mask & IS_A_GAME) == 0) continue;

					ui___Away = SCHEDULE_AWAY(ui___Mask);
					ui___Home = SCHEDULE_HOME(ui___Mask);

					if ((ui___Home > TEAM_SSN) || (ui___Away > TEAM_SSN)) continue;

					if ((ui___Mask & IS_IN_C_SEASON) == 0)
						{
						if ((ui___Away == ui___i) && (ui___Home == ui___j))
							{
							apuw_MatchAway[ui___AwayCount++] = puw__Match - 1;
							}
						else if ((ui___Home == ui___i) && (ui___Away == ui___j))
							{
							apuw_MatchHome[ui___HomeCount++] = puw__Match - 1;
							}
						}
					else
						{
						if (ui___Away == ui___i)
							{
							ui___Away0Already += 1;
							}
						if (ui___Home == ui___i)
							{
							ui___Home0Already += 1;
							}
						if (ui___Away == ui___j)
							{
							ui___Away1Already += 1;
							}
						if (ui___Home == ui___j)
							{
							ui___Home1Already += 1;
							}
						}
					}

				//printf("Home0 %02d Away0 %02d  Home1 %02d Away1 %02d  ",
				//	ui___Home0Already, ui___Away0Already,
				//	ui___Home1Already, ui___Away1Already);

				// Now pick the match we want.

				ui___Where = 0;

				ui___Sum0 = ui___Home0Already + ui___Away0Already;
				si___Dif0 = ui___Home0Already - ui___Away0Already;
				ui___Sum1 = ui___Home1Already + ui___Away1Already;
				si___Dif1 = ui___Home1Already - ui___Away1Already;

				if (abs(si___Dif0) > abs(si___Dif1)) {
					if (si___Dif0 > 0) ui___Where = 1;
					}
				else if (abs(si___Dif0) < abs(si___Dif1)) {
					if (si___Dif1 < 0) ui___Where = 1;
					}
				else
					{
					if (ui___Sum0 >= ui___Sum1) {
						if (si___Dif0 > 0) ui___Where = 1;
						} else {
						if (si___Dif1 < 0) ui___Where = 1;
						}
					}

				if (ui___Where == 0)
					{
					if (ui___Flip) {
						*apuw_MatchHome[ui___HomeCount - 1] |= IS_IN_C_SEASON;
						} else {
						*apuw_MatchHome[ui___HomeCount / 2] |= IS_IN_C_SEASON;
						}
					}
				else
					{
					if (ui___Flip) {
						*apuw_MatchAway[ui___AwayCount - 1] |= IS_IN_C_SEASON;
						} else {
						*apuw_MatchAway[ui___AwayCount / 2] |= IS_IN_C_SEASON;
						}
					}

				// Now show that we've already done this combo.

				aub__sResults[ui___i][ui___j] = 0;
				aub__sResults[ui___j][ui___i] = 0;

				} // end while

			} // next ui___j

//		ui___Flip ^= 1;

		} // next ui___i

	//

	return;

	}






// **************************************************************************
// **************************************************************************
// **************************************************************************
// **************************************************************************



// **************************************************************************
// * SaveInfoFile ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	int                 SaveInfoFile            (
								char *              s)

	{
	// Local variables.

	int                 errorcode = -1;

	int                 i;

	MMCKINFO            cl___Riff;
	MMCKINFO            cl___Temp;

	HMMIO               pcl__File;

	UD                  ud___Temp;

	// Open the file.

	pcl__File = aiffOpen(s, NULL, MMIO_EXCLUSIVE | MMIO_ALLOCBUF | MMIO_CREATE | MMIO_WRITE);

	if (pcl__File == NULL)
		{
		sprintf(ErrorMessage,
			"XvtNBA - Unable to open file \"%s\" !\n", s);
		return (ErrorCode = ERROR_IO_WRITE);
		}

	pcl__gOutputFile = pcl__File;

	// Write the 'AIFF' header.

	cl___Riff.ckid    = mmioFOURCC('A', 'I', 'F', 'F');
	cl___Riff.cksize  = 0;
	cl___Riff.fccType = mmioFOURCC('L', 'F', 'P', 'I');

	if (aiffCreateChunk(pcl__File, &cl___Riff, MMIO_CREATERIFF) != 0)
		{
		goto errorExit;
		}

	// Write the 'VERS' chunk.

	cl___Temp.ckid    = mmioFOURCC('I', 'V', 'E', 'R');
	cl___Temp.cksize  = 0;

	if (aiffCreateChunk(pcl__File, &cl___Temp, 0) != 0)
		{
		goto errorExit;
		}

	if (aiffWrite(pcl__File, (char *) aub__gLfpVers, 4) != (LONG) 4)
		{
		goto errorExit;
		}

	if (aiffAscend(pcl__File, &cl___Temp, 0) != 0)
		{
		goto errorExit;
		}

	// Write the 'CSTR' chunk.

	if (si___gStrCount != 0)
		{
		cl___Temp.ckid    = mmioFOURCC('C', 'S', 'T', 'R');
		cl___Temp.cksize  = 0;

		if (aiffCreateChunk(pcl__File, &cl___Temp, 0) != 0)
			{
			goto errorExit;
			}

		i = pcz__gStrData - acz__gStrData;

		if (aiffWrite(pcl__File, (char *) acz__gStrData, i) != (LONG) i)
			{
			goto errorExit;
			}

		if (aiffAscend(pcl__File, &cl___Temp, 0) != 0)
			{
			goto errorExit;
			}
		}

	// Write the 'TEAM' chunk.

	if (si___gTeamCount != 0)
		{
		cl___Temp.ckid    = mmioFOURCC('T', 'E', 'A', 'M');
		cl___Temp.cksize  = 0;

		if (aiffCreateChunk(pcl__File, &cl___Temp, 0) != 0)
			{
			goto errorExit;
			}

		ud___Temp = (UD) si___gTeamCount;

		ud___Temp =
			((ud___Temp >> 24) & 0x000000FFu)|
			((ud___Temp >>  8) & 0x0000FF00u)|
			((ud___Temp <<  8) & 0x00FF0000u)|
			((ud___Temp << 24) & 0xFF000000u);

		if (aiffWrite(pcl__File, (char *) &ud___Temp, 4) != (LONG) 4)
			{
			goto errorExit;
			}

		if (si___gTeamCount != 0)
			{
			WriteTeams();
			}

		if (aiffAscend(pcl__File, &cl___Temp, 0) != 0)
			{
			goto errorExit;
			}
		}

	// Write the 'ROST' chunk.

	if (si___gStrCount != 0)
		{
		cl___Temp.ckid    = mmioFOURCC('R', 'O', 'S', 'T');
		cl___Temp.cksize  = 0;

		if (aiffCreateChunk(pcl__File, &cl___Temp, 0) != 0)
			{
			goto errorExit;
			}

		WriteRoster();

		if (aiffAscend(pcl__File, &cl___Temp, 0) != 0)
			{
			goto errorExit;
			}
		}

	// Write the 'PLYR' chunk.

	if (si___gPlyrCount != 0)
		{
		cl___Temp.ckid    = mmioFOURCC('P', 'L', 'Y', 'R');
		cl___Temp.cksize  = 0;

		if (aiffCreateChunk(pcl__File, &cl___Temp, 0) != 0)
			{
			goto errorExit;
			}

		ud___Temp = (UD) si___gPlyrCount;

		ud___Temp =
			((ud___Temp >> 24) & 0x000000FFu)|
			((ud___Temp >>  8) & 0x0000FF00u)|
			((ud___Temp <<  8) & 0x00FF0000u)|
			((ud___Temp << 24) & 0xFF000000u);

		if (aiffWrite(pcl__File, (char *) &ud___Temp, 4) != (LONG) 4)
			{
			goto errorExit;
			}

		ud___Temp = (UD) si___gPlyrsSeason;

		ud___Temp =
			((ud___Temp >> 24) & 0x000000FFu)|
			((ud___Temp >>  8) & 0x0000FF00u)|
			((ud___Temp <<  8) & 0x00FF0000u)|
			((ud___Temp << 24) & 0xFF000000u);

		if (aiffWrite(pcl__File, (char *) &ud___Temp, 4) != (LONG) 4)
			{
			goto errorExit;
			}

		if (si___gPlyrCount != 0)
			{
			WritePlyrs();
			}

		if (aiffAscend(pcl__File, &cl___Temp, 0) != 0)
			{
			goto errorExit;
			}
		}

	// Write the 'DATE' chunk.

	cl___Temp.ckid    = mmioFOURCC('D', 'A', 'T', 'E');
	cl___Temp.cksize  = 0;

	if (aiffCreateChunk(pcl__File, &cl___Temp, 0) != 0)
		{
		goto errorExit;
		}

	if (puw__gSchedule != auw__gSchedule)
		{
		WriteDates();
		}

	if (aiffAscend(pcl__File, &cl___Temp, 0) != 0)
		{
		goto errorExit;
		}

	// Close the file.

	if (aiffAscend(pcl__File, &cl___Riff, 0) != 0)
		{
		goto errorExit;
		}

	if (aiffClose(pcl__File, 0) != 0)
		{
		goto errorExit;
		}

	pcl__File = NULL;

	// All done OK.

	errorcode = 0;

	// Close the file and return the errorcode.

	errorExit:

	if (pcl__File)
		{
		aiffClose(pcl__File, 0);
		}

	pcl__File = NULL;

	if (errorcode)
		{
		sprintf(ErrorMessage,
			"XvtNBA - Unable the write chunk !\n");
		ErrorCode = ERROR_IO_WRITE;
		}

	return (errorcode);
	}



// **************************************************************************
// * SaveDataFile ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	int                 SaveDataFile            (
								char *              s)

	{
	// Local variables.

	int                 errorcode = -1;

//	int                 i;

	MMCKINFO            cl___Riff;
	MMCKINFO            cl___Temp;

	HMMIO               pcl__File;

	// Open the file.

	pcl__File = aiffOpen(s, NULL, MMIO_EXCLUSIVE | MMIO_ALLOCBUF | MMIO_CREATE | MMIO_WRITE);

	if (pcl__File == NULL)
		{
		sprintf(ErrorMessage,
			"XvtNBA - Unable to open file \"%s\" !\n", s);
		return (ErrorCode = ERROR_IO_WRITE);
 		}

	pcl__gOutputFile = pcl__File;

	// Write the 'AIFF' header.

	cl___Riff.ckid    = mmioFOURCC('A', 'I', 'F', 'F');
	cl___Riff.cksize  = 0;
	cl___Riff.fccType = mmioFOURCC('L', 'F', 'P', 'D');

	if (aiffCreateChunk(pcl__File, &cl___Riff, MMIO_CREATERIFF) != 0)
		{
		goto errorExit;
		}

	// Write the 'VERS' chunk.

	cl___Temp.ckid    = mmioFOURCC('D', 'V', 'E', 'R');
	cl___Temp.cksize  = 0;

	if (aiffCreateChunk(pcl__File, &cl___Temp, 0) != 0)
		{
		goto errorExit;
		}

	if (aiffWrite(pcl__File, (char *) aub__gLfpVers, 4) != (LONG) 4)
		{
		goto errorExit;
		}

	if (aiffAscend(pcl__File, &cl___Temp, 0) != 0)
		{
		goto errorExit;
		}

#if 0 // phil
	// Write the 'PTEX' chunk.

	if (si___gPlyrCount != 0)
		{
		cl___Temp.ckid    = mmioFOURCC('P', 'T', 'E', 'X');
		cl___Temp.cksize  = 0;

		if (aiffCreateChunk(pcl__File, &cl___Temp, 0) != 0)
			{
			goto errorExit;
			}

		if (si___gPlyrCount != 0)
			{
			WritePlyrsTex();
			}

		if (aiffAscend(pcl__File, &cl___Temp, 0) != 0)
			{
			goto errorExit;
			}
		}
#endif

	// Write the 'PIMG' chunk.

	if (si___gPlyrCount != 0)
		{
		cl___Temp.ckid    = mmioFOURCC('P', 'I', 'M', 'G');
		cl___Temp.cksize  = 0;

		if (aiffCreateChunk(pcl__File, &cl___Temp, 0) != 0)
			{
			goto errorExit;
			}

		if (si___gPlyrCount != 0)
			{
			WritePlyrsImg();
			}

		if (aiffAscend(pcl__File, &cl___Temp, 0) != 0)
			{
			goto errorExit;
			}
		}

	// Write the 'SPAL' chunk.

	cl___Temp.ckid    = mmioFOURCC('S', 'P', 'A', 'L');
	cl___Temp.cksize  = 0;

	if (aiffCreateChunk(pcl__File, &cl___Temp, 0) != 0)
		{
		goto errorExit;
		}

	WriteSuperPalette();

	if (aiffAscend(pcl__File, &cl___Temp, 0) != 0)
		{
		goto errorExit;
		}

	// Close the file.

	if (aiffAscend(pcl__File, &cl___Riff, 0) != 0)
		{
		goto errorExit;
		}

	if (aiffClose(pcl__File, 0) != 0)
		{
		goto errorExit;
		}

	pcl__File = NULL;

	// All done OK.

	errorcode = 0;

	// Close the file and return the errorcode.

	errorExit:

	if (pcl__File)
		{
		aiffClose(pcl__File, 0);
		}

	pcl__File = NULL;

	if (errorcode)
		{
		sprintf(ErrorMessage,
			"XvtNBA - Unable the write chunk !\n");
		ErrorCode = ERROR_IO_WRITE;
		}

	return (errorcode);
	}



// **************************************************************************
// * SaveHeadsTex()                                                         *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static void SaveHeadsTex( const char *filename )
{
	// Local Variables.

	int                 i;

	PLYRNODE_T *        pcl__Plyr;
	PLYRNODE_T **       ppcl_Plyr;

	char                acz__Str [256];

	FILE
		*fp;


	puts( "Head Textures:" );

	fp = fopen( filename, "wb" );
	if (!fp)
	{
		printf( "XvtNBA - Can't create: '%s'\n", filename );
		getchar();
		return;
	}


	// Write out the default head texture.

	HeadTexToN64( "RES/LAL/BRYANT", fp );

	// Write out the head textures for the plyrs in the list.

	ppcl_Plyr = &apcl_gPlyr[1];

	i = si___gPlyrCount;

	while (i--)
	{
		// Get the plyr.

		pcl__Plyr = *ppcl_Plyr++;

		// Blank plyr ?

		if (pcl__Plyr == NULL)
		{
			// Write out the default head texture.

			HeadTexToN64( "RES/LAL/BRYANT", fp );
			putchar( '.' );
		}
		else
		{
			// Write out the players's head texture.

			strcpy(acz__Str, "RES");
			strcat(acz__Str, "\\");
			strcat(acz__Str,
				((STRNODE_T *) apcl_gTeam[pcl__Plyr->r.ub___pTeam]->r.pcz__tAbbr)->pcz__string);
			strcat(acz__Str, "\\");
			strcat(acz__Str, pcl__Plyr->HeadFolder);

			if (HeadTexToN64( acz__Str, fp ))
			{
				putchar( '.' );
			}
			else
			{
				output("Using default texture for plyr - (%s)\t%s, %s\n",
					((STRNODE_T *) apcl_gTeam[pcl__Plyr->r.ub___pTeam]->r.pcz__tAbbr)->pcz__string,
					((STRNODE_T *) pcl__Plyr->r.pcz__pSName)->pcz__string,
					((STRNODE_T *) pcl__Plyr->r.pcz__pCName)->pcz__string);

				HeadTexToN64( "RES/LAL/BRYANT", fp );
			}
		}

		// Loop around and process the next plyr.
	}

	// Custom heads...

	HeadTexToN64( "RES/MugMaker/Skin0Mug01", fp );
	HeadTexToN64( "RES/MugMaker/Skin0Mug02", fp );
	HeadTexToN64( "RES/MugMaker/Skin1Mug03", fp );
	HeadTexToN64( "RES/MugMaker/Skin1Mug04", fp );
	HeadTexToN64( "RES/MugMaker/Skin1Mug05", fp );
	HeadTexToN64( "RES/MugMaker/Skin2Mug06", fp );
	HeadTexToN64( "RES/MugMaker/Skin2Mug07", fp );
	HeadTexToN64( "RES/MugMaker/Skin2Mug08", fp );
	HeadTexToN64( "RES/MugMaker/Skin3Mug09", fp );
	HeadTexToN64( "RES/MugMaker/Skin3Mug10", fp );
	HeadTexToN64( "RES/MugMaker/Skin4Mug11", fp );
	HeadTexToN64( "RES/MugMaker/Skin4Mug12", fp );
	HeadTexToN64( "RES/MugMaker/Skin5Mug13", fp );
	HeadTexToN64( "RES/MugMaker/Skin5Mug14", fp );
	HeadTexToN64( "RES/MugMaker/Skin5Mug15", fp );
	HeadTexToN64( "RES/MugMaker/Skin6Mug16", fp );
	HeadTexToN64( "RES/MugMaker/Skin6Mug17", fp );
	HeadTexToN64( "RES/MugMaker/Skin6Mug18", fp );
	HeadTexToN64( "RES/MugMaker/Skin7Mug19", fp );
	HeadTexToN64( "RES/MugMaker/Skin7Mug20", fp );

	fclose( fp );

	// All done.

	printf("\n\n");
}


// **************************************************************************
// * SaveHeadsGeom()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static void SaveHeadsGeom( const char *filename )
{
	// Local Variables.

	int                 i;

	PLYRNODE_T *        pcl__Plyr;
	PLYRNODE_T **       ppcl_Plyr;

	char                acz__Str [256];


	puts( "Head Geometries:" );

	// Write out the default head geom.

	HeadGeom_Process( "RES/LAL/BRYANT" );

	// Write out the head geom for the plyrs in the list.

	ppcl_Plyr = &apcl_gPlyr[1];

	i = si___gPlyrCount;

	while (i--)
	{
		// Get the plyr.

		pcl__Plyr = *ppcl_Plyr++;

		// Blank plyr ?

		if (pcl__Plyr == NULL)
		{
			// Write out the default head geom.

			HeadGeom_Process( "RES/LAL/BRYANT" );
			putchar( '.' );
		}
		else
		{
			// Write out the players's head geom.

			strcpy(acz__Str, "RES");
			strcat(acz__Str, "\\");
			strcat(acz__Str,
				((STRNODE_T *) apcl_gTeam[pcl__Plyr->r.ub___pTeam]->r.pcz__tAbbr)->pcz__string);
			strcat(acz__Str, "\\");
			strcat(acz__Str, pcl__Plyr->HeadFolder);

			if (HeadGeom_Process( acz__Str ))
			{
				putchar( '.' );
			}
			else
			{
				output("Using default head geom for plyr - (%s)\t%s, %s\n",
					((STRNODE_T *) apcl_gTeam[pcl__Plyr->r.ub___pTeam]->r.pcz__tAbbr)->pcz__string,
					((STRNODE_T *) pcl__Plyr->r.pcz__pSName)->pcz__string,
					((STRNODE_T *) pcl__Plyr->r.pcz__pCName)->pcz__string);

				HeadGeom_Process( "RES/LAL/BRYANT" );
			}
		}

		// Loop around and process the next plyr.
	}

	// Custom heads...

	HeadGeom_Process( "RES/MugMaker/Skin0Mug01" );
	HeadGeom_Process( "RES/MugMaker/Skin0Mug02" );
	HeadGeom_Process( "RES/MugMaker/Skin1Mug03" );
	HeadGeom_Process( "RES/MugMaker/Skin1Mug04" );
	HeadGeom_Process( "RES/MugMaker/Skin1Mug05" );
	HeadGeom_Process( "RES/MugMaker/Skin2Mug06" );
	HeadGeom_Process( "RES/MugMaker/Skin2Mug07" );
	HeadGeom_Process( "RES/MugMaker/Skin2Mug08" );
	HeadGeom_Process( "RES/MugMaker/Skin3Mug09" );
	HeadGeom_Process( "RES/MugMaker/Skin3Mug10" );
	HeadGeom_Process( "RES/MugMaker/Skin4Mug11" );
	HeadGeom_Process( "RES/MugMaker/Skin4Mug12" );
	HeadGeom_Process( "RES/MugMaker/Skin5Mug13" );
	HeadGeom_Process( "RES/MugMaker/Skin5Mug14" );
	HeadGeom_Process( "RES/MugMaker/Skin5Mug15" );
	HeadGeom_Process( "RES/MugMaker/Skin6Mug16" );
	HeadGeom_Process( "RES/MugMaker/Skin6Mug17" );
	HeadGeom_Process( "RES/MugMaker/Skin6Mug18" );
	HeadGeom_Process( "RES/MugMaker/Skin7Mug19" );
	HeadGeom_Process( "RES/MugMaker/Skin7Mug20" );

	HeadGeom_ToN64( filename );

	// All done.

	printf("\n\n");
}


static void FixName( char *name )
{
	strupr( name );

	while (*name)
	{
		if (*name < 'A' || *name > 'Z')
			strcpy( name, &name[1] );
		else
			name++;
	}
}


// **************************************************************************
// * SavePlayerIDs()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static void SavePlayerIDs( const char *filename )
{
	// Local Variables.

	int                 i, id;

	PLYRNODE_T *        pcl__Plyr;
	PLYRNODE_T **       ppcl_Plyr;

	char                name[64];

	FILE *fp;


	fp = fopen( filename, "w" );
	if (!fp)
	{
		sprintf( ErrorMessage, "XvtNBA - Unable to create file \"%s\" !\n", filename );
		return;
	}

	fprintf( fp, "// PlayerID.h - Generated by XvtBNA tool.\n\n" );
	fprintf( fp, "#ifndef _PLAYERID_H_\n" );
	fprintf( fp, "#define _PLAYERID_H_\n\n" );


	// Write out the IDs for the plyrs in the list.

	id = 1;
	ppcl_Plyr = &apcl_gPlyr[id];

	i = si___gPlyrCount;

	while (i--)
	{
		// Get the plyr.

		pcl__Plyr = *ppcl_Plyr++;

		// Blank plyr ?

		if (pcl__Plyr == NULL)
		{
			// Write out a blank line.

			fprintf( fp, "\n" );
		}
		else
		{
			// Write out the player's first name...

			strcpy( name, ((STRNODE_T *) pcl__Plyr->r.pcz__pCName)->pcz__string );
			FixName( name );
			fprintf( fp, "#define PID_%s", name );

			// Write out the player's first last...

			strcpy( name, ((STRNODE_T *) pcl__Plyr->r.pcz__pSName)->pcz__string );
			FixName( name );
			fprintf( fp, "_%s", name );

			// Write out the player's team...

			strcpy( name, ((STRNODE_T *) apcl_gTeam[pcl__Plyr->r.ub___pTeam]->r.pcz__tAbbr)->pcz__string );
			FixName( name );
			fprintf( fp, "_%s", name );

			// Write out the player's ID...

			fprintf( fp, "\t%d\n", id );
		}

		// Loop around and process the next plyr.

		id++;
	}

	fprintf( fp, "\n#endif\n" );

	// All done.

	fclose( fp );
}


// **************************************************************************
// * SavePlayerAudio()                                                      *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static void SavePlayerAudio( const char *bankname, const char *bitname )
{
	// Local Variables.

	int                 i;

	PLYRNODE_T *        pcl__Plyr;
	PLYRNODE_T **       ppcl_Plyr;

	char                acz__Str [256];

	FILE *log;


	puts( "Player Audio:" );


	log = fopen( "Audio.LOG", "w" );

	fprintf( log, "Player Audio Log File\n" );
	fprintf( log, "=====================\n" );
	fprintf( log, "\n" );
	fprintf( log, "<Chick> <Stu> audio\n" );
	fprintf( log, ". - Normal\n" );
	fprintf( log, "X - Excited\n" );
	fprintf( log, "T - To\n" );
	fprintf( log, "N - Nickname\n" );
	fprintf( log, "! - <none>\n" );
	fprintf( log, "\n\n" );


	// Write out the default audio.

	GSM2_Process( "RES/LAL", "BRYANT", NULL );

	// Write out the audio for the plyrs in the list.

	ppcl_Plyr = &apcl_gPlyr[1];

	i = si___gPlyrCount;

	while (i--)
	{
		// Get the plyr.

		pcl__Plyr = *ppcl_Plyr++;

		// Blank plyr ?

		if (pcl__Plyr == NULL)
		{
			// Write out the default audio.

			GSM2_Process( "RES/LAL", "BRYANT", NULL );
			fprintf( log, "-\n" );
			putchar( '.' );
		}
		else
		{
			// Write out the player's audio.

			strcpy(acz__Str, "RES");
			strcat(acz__Str, "\\");
			strcat(acz__Str,
				((STRNODE_T *) apcl_gTeam[pcl__Plyr->r.ub___pTeam]->r.pcz__tAbbr)->pcz__string);

			if (GSM2_Process( acz__Str, pcl__Plyr->HeadFolder, log ))
			{
				fprintf( log, "\t (%s)\t%s, %s\n",
					((STRNODE_T *) apcl_gTeam[pcl__Plyr->r.ub___pTeam]->r.pcz__tAbbr)->pcz__string,
					((STRNODE_T *) pcl__Plyr->r.pcz__pSName)->pcz__string,
					((STRNODE_T *) pcl__Plyr->r.pcz__pCName)->pcz__string );

				putchar( '.' );
			}
			else
			{
				fprintf( log, "\tUsing default audio for plyr - (%s)\t%s, %s\n",
					((STRNODE_T *) apcl_gTeam[pcl__Plyr->r.ub___pTeam]->r.pcz__tAbbr)->pcz__string,
					((STRNODE_T *) pcl__Plyr->r.pcz__pSName)->pcz__string,
					((STRNODE_T *) pcl__Plyr->r.pcz__pCName)->pcz__string);

				printf("Using default audio for plyr - (%s)\t%s, %s\n",
					((STRNODE_T *) apcl_gTeam[pcl__Plyr->r.ub___pTeam]->r.pcz__tAbbr)->pcz__string,
					((STRNODE_T *) pcl__Plyr->r.pcz__pSName)->pcz__string,
					((STRNODE_T *) pcl__Plyr->r.pcz__pCName)->pcz__string);

				GSM2_Process( "RES/LAL", "BRYANT", NULL );
			}
		}

		// Loop around and process the next plyr.
	}

	GSM2_ToN64( bankname, bitname );

	// All done.

	fclose( log );

	printf("\n\n");
}


// **************************************************************************
// * SaveTalkFile ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	int                 SaveTalkFile            (
								char *              s)

	{
	// Local variables.

	int                 errorcode = -1;

//	int                 i;

	MMCKINFO            cl___Riff;
	MMCKINFO            cl___Temp;

	HMMIO               pcl__File;

	// Open the file.

	pcl__File = aiffOpen(s, NULL, MMIO_EXCLUSIVE | MMIO_ALLOCBUF | MMIO_CREATE | MMIO_WRITE);

	if (pcl__File == NULL)
		{
		sprintf(ErrorMessage,
			"XvtNBA - Unable to open file \"%s\" !\n", s);
		return (ErrorCode = ERROR_IO_WRITE);
		}

	pcl__gOutputFile = pcl__File;

	// Write the 'AIFF' header.

	cl___Riff.ckid    = mmioFOURCC('A', 'I', 'F', 'F');
	cl___Riff.cksize  = 0;
	cl___Riff.fccType = mmioFOURCC('L', 'F', 'P', 'T');

	if (aiffCreateChunk(pcl__File, &cl___Riff, MMIO_CREATERIFF) != 0)
		{
		goto errorExit;
		}

	// Write the 'VERS' chunk.

	cl___Temp.ckid    = mmioFOURCC('T', 'V', 'E', 'R');
	cl___Temp.cksize  = 0;

	if (aiffCreateChunk(pcl__File, &cl___Temp, 0) != 0)
		{
		goto errorExit;
		}

	if (aiffWrite(pcl__File, (char *) aub__gLfpVers, 4) != (LONG) 4)
		{
		goto errorExit;
		}

	if (aiffAscend(pcl__File, &cl___Temp, 0) != 0)
		{
		goto errorExit;
		}

	// Write the 'PNAM' chunk.

	if (si___gPlyrCount != 0)
		{
		cl___Temp.ckid    = mmioFOURCC('P', 'N', 'A', 'M');
		cl___Temp.cksize  = 0;

		if (aiffCreateChunk(pcl__File, &cl___Temp, 0) != 0)
			{
			goto errorExit;
			}

		if (si___gPlyrCount != 0)
			{
			WritePlyrsNam();
			}

		if (aiffAscend(pcl__File, &cl___Temp, 0) != 0)
			{
			goto errorExit;
			}
		}

	// Write the 'TALK' chunk.

	if (si___gPlyrCount != 0)
		{
		cl___Temp.ckid    = mmioFOURCC('T', 'A', 'L', 'K');
		cl___Temp.cksize  = 0;

		if (aiffCreateChunk(pcl__File, &cl___Temp, 0) != 0)
			{
			goto errorExit;
			}

		WriteTalk();

		if (aiffAscend(pcl__File, &cl___Temp, 0) != 0)
			{
			goto errorExit;
			}
		}

	// Close the file.

	if (aiffAscend(pcl__File, &cl___Riff, 0) != 0)
		{
		goto errorExit;
		}

	if (aiffClose(pcl__File, 0) != 0)
		{
		goto errorExit;
		}

	pcl__File = NULL;

	// All done OK.

	errorcode = 0;

	// Close the file and return the errorcode.

	errorExit:

	if (pcl__File)
		{
		aiffClose(pcl__File, 0);
		}

	pcl__File = NULL;

	if (errorcode)
		{
		sprintf(ErrorMessage,
			"XvtNBA - Unable the write chunk !\n");
		ErrorCode = ERROR_IO_WRITE;
		}

	return (errorcode);
	}



// **************************************************************************
// * LoadResourceFile ()                                                    *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// *                                                                        *
// * N.B.    This will only process a subset of the possible resource files *
// *         that are legal with Ben's syntax.                              *
// **************************************************************************

static	int                 LoadResourceFile        (
								char *              pcz__File)

	{
	// Local variables.

	int                 errorcode = -1;

	int                 i;

	UB                  b;
	char                c;

	long                l;

	int                 si___DefItem;
	int                 si___CurItem;

	RESOURCE_T *        pcl__R;

	UB *                pub__D;
	UB *                pub__T;
	char *              pcz__S;

	FILE *              pcl__File;
	int                 si___Line;

	char *              pcz__Line;
	char                acz__Line [1024];

	// Clear resource list.

	apcl_sResources[0] = NULL;

	// Initialize defaults.

	si___DefItem = 0;
	si___CurItem = 0;

	pcl__R = NULL;
	pub__D = NULL;

	// Open the file.

	if ((pcl__File = fopen(pcz__File, "rt")) == NULL)
		{
		ErrorCode = ERROR_NO_FILE;
		sprintf(ErrorMessage,
			"XvtNBA - Unable to open input file \"%s\" !\n",
			pcz__File);
		goto errorExit;
		}

	// Process each line.

	si___Line = 0;

	while (fgets(acz__Line, 1024, pcl__File) != NULL)
		{
		// Increment line count.

		si___Line += 1;

		// Now read each word/token on the line.

		pcz__Line = acz__Line;

		while ((pcz__S = strtok(pcz__Line, " \t\n")) != NULL)

			{
			// Clear start-of-line marker.

			pcz__Line = NULL;

			// Convert to upper case.

			strupr(pcz__S);

			c = *pcz__S;

			// Is it a comment ?

			if (c == '#') break;

			// Is it a hexdump ?

			if (c == '[')
				{
				pcz__S += 1;

				while ((*pcz__S != ']') && (*pcz__S != 0))
					{
					if ((c = *pcz__S++ - '0') > 9) c = c - ('A' - ('0' + 10));

					b = c;

					if ((c = *pcz__S++ - '0') > 9) c = c - ('A' - ('0' + 10));

					b = (b << 4) + c;

					*pub__T++ = b;
					}

				continue;
				}

			// Is it a reference ?

			if (c == '@')
				{
				// Blank out the reference.

				pcz__S = "0!d";
				}

			// Is it a label ?

			c = pcz__S[strlen(pcz__S) - 1];

			if (c == ':') continue;

			// Is it a file command ?

			if (strcmp(pcz__S, "FILE") == 0)
				{
				break;
				}

			if (strcmp(pcz__S, "ENDFILE") == 0)
				{
				break;
				}

			// Is it a resource command ?

			if (strcmp(pcz__S, "RESOURCE") == 0)
				{
				// Allocate a bufffer for the resource.

				pcl__R = (RESOURCE_T *) malloc(0x10000 + sizeof(RESOURCE_T));

				if (pcl__R == NULL)
					{
					printf("XvtNBA - Can\'t allocate space for resource !\n");
					exit(1);
					}

				pub__T =
				pub__D = (UB *) (pcl__R + 1);

				pcl__R->acz__Name[0] = 0;
				pcl__R->pub__Buf     = pub__D;
				pcl__R->pub__End     = pub__D + 0x10000;

				// Get the resource name.

				if ((pcz__S = strtok(NULL, " \t\n")) == NULL)
					{
					printf("XvtNBA - Illegal resource name in file %s at line %d !\n", pcz__File, si___Line);
					exit(1);
					}

				if (*pcz__S++ != '\"')
					{
					printf("XvtNBA - Illgeal resource name in file %s at line %d !\n", pcz__File, si___Line);
					exit(1);
					}

				pcz__S[strlen(pcz__S) - 1] = 0;

				strupr(pcz__S);

				strcpy(pcl__R->acz__Name, pcz__S);

				// Add resource to the list.

				for (i = 0; i < 63; i++)
					{
					if (apcl_sResources[i] == NULL)
						{
						apcl_sResources[i+0] = pcl__R;
						apcl_sResources[i+1] = NULL;
						break;
						}
					}

				// All Done.

				break;
				}

			if (strcmp(pcz__S, "ENDRESOURCE") == 0)
				{
				// Illegal end ?

				if (pcl__R == NULL)
					{
					printf("XvtNBA - Illegal ENDRESOURCE in file %s at line %d !\n", pcz__File, si___Line);
					exit(1);
					}

				// Pad out data to a multiple of 4 bytes.

				while (((pub__T - pub__D) & 3) != 0)
					{
					*pub__T++ = 0;
					}

				// Update resource end point.

				pcl__R->pub__End = pub__T;

				pcl__R = NULL;

				break;
				}

			if (strcmp(pcz__S, "BYTES") == 0)
				{
				si___DefItem = 1;
				continue;
				}

			if (strcmp(pcz__S, "WORDS") == 0)
				{
				si___DefItem = 2;
				continue;
				}

			if (strcmp(pcz__S, "DWORDS") == 0)
				{
				si___DefItem = 4;
				continue;
				}

			// Well then, its got to be a number.

			si___CurItem = si___DefItem;

			l = strtol(pcz__S, &pcz__S, 0);

			// Did the number have a size override ?

			if (*pcz__S == '!')
				{
				c = pcz__S[1];

				pcz__S += 2;

				if (c == 'B') si___CurItem = 1;
				else
				if (c == 'W') si___CurItem = 2;
				else
				if (c == 'D') si___CurItem = 4;
				else
					{
					printf("XvtNBA - Illegal size override in file %s at line %d !\n", pcz__File, si___Line);
					exit(1);
					}
				}

			// Do we have illegal rubbish at the end of the number ?

			if (*pcz__S != '\0')
				{
				printf("XvtNBA - Illegal number in file %s at line %d !\n", pcz__File, si___Line);
				exit(1);
				}

			// Write out the number (in Motorola/MIPS format).

			while (si___CurItem--)
				{
				b = (l >> (si___CurItem << 3)) & 0xFFu;

				*pub__T++ = b;
				}

			// Write out the number (in Intel format).

//			while (si___CurItem--)
//				{
//				b = l & 0xFFu; l = l >> 8;
//
//				*pub__T++ = b;
//				}

			// Loop around and get the next token to process on this line.
			}

		// Loop around and get the next line to process.
		}

	// Normal finish, or error ?

	if (!feof(pcl__File))
		{
		ErrorCode = ERROR_IO_READ;
		sprintf(ErrorMessage,
			"XvtNBA - File error while reading file.\n");
		goto errorExit;
		}

	// All done OK.

	ErrorCode = 0;

	// Close the file and return the errorcode.

	errorExit:

	if (pcl__File)
		{
		fclose(pcl__File);
		}

	pcl__File = NULL;

	return (ErrorCode);
	}



// **************************************************************************
// * FreeResourceFile ()                                                    *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// *                                                                        *
// * N.B.    This will only process a subset of the possible resource files *
// *         that are legal with Ben's syntax.                              *
// **************************************************************************

static	void                FreeResourceFile        (void)

	{
	// Local variables.

	RESOURCE_T **       ppcl_Resources;

	// Free resource list.

	ppcl_Resources = &apcl_sResources[0];

	while (*ppcl_Resources != NULL)
		{
		free(*ppcl_Resources);

		ppcl_Resources += 1;
		}

	// Clear resource list.

	apcl_sResources[0] = NULL;

	// All Done.

	return;
	}



// **************************************************************************
// **************************************************************************
// **************************************************************************
// **************************************************************************



// **************************************************************************
// **************************************************************************
// **************************************************************************
//	STATIC FUNCTIONS
// **************************************************************************
// **************************************************************************
// **************************************************************************



// **************************************************************************
// * InitTeamTree ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                InitTeamTree            (void)

	{
	// Initialize string tree.

	si___gTeamCount = 0;

	memset(&cl___gTeamRoot, 0, sizeof(TEAMNODE_T));

	// All done OK.

	return;
	}



// **************************************************************************
// * AddTeam ()                                                             *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	TEAMNODE_T *        AddTeam                 (
								TEAMNODE_T *        pcl__new)

	{

	// Local Variables.

	int                 si___cmp;

	TEAMNODE_T *        pcl__old;
	TEAMNODE_T **       ppcl_nxt;

	// Search the tree for a matching string.

	pcl__old = &cl___gTeamRoot;
	si___cmp = 1;

	for (;;)
		{
		if (si___cmp >= 0)
			{
			ppcl_nxt = &pcl__old->pcl__bigger;
			}
		else
		if (si___cmp <  0)
			{
			ppcl_nxt = &pcl__old->pcl__lesser;
			}

		if (*ppcl_nxt == NULL)
			{
			*ppcl_nxt = pcl__new;

			pcl__new->pcl__parent = pcl__old;
			pcl__new->pcl__bigger = NULL;
			pcl__new->pcl__lesser = NULL;
			pcl__new->si___number = 0;

			pcl__new->si___offset = si___gTeamCount++;

			return (pcl__new);
			}

		pcl__old = *ppcl_nxt;

		si___cmp = strcmp(
			((STRNODE_T *) pcl__new->r.pcz__tCity)->pcz__string,
			((STRNODE_T *) pcl__old->r.pcz__tCity)->pcz__string);

		if (si___cmp == 0)
			{
			si___cmp = strcmp(
				((STRNODE_T *) pcl__new->r.pcz__tName)->pcz__string,
				((STRNODE_T *) pcl__old->r.pcz__tName)->pcz__string);
			}
		}

	// We never actually get here.
	}



// **************************************************************************
// * OrderTeams ()                                                          *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                OrderTeams              (void)

	{
	// Work our way down from the root.

	si___sNumber = 0;

	if (cl___gTeamRoot.pcl__bigger != NULL)
	{
/********	ver 1.00
		CalcTeamNumber(cl___gTeamRoot.pcl__bigger,  0,  0);
		CalcTeamNumber(cl___gTeamRoot.pcl__bigger,  1, 29);
		CalcTeamNumber(cl___gTeamRoot.pcl__bigger, 30, 31);
		CalcTeamNumber(cl___gTeamRoot.pcl__bigger, 32, 33);
		CalcTeamNumber(cl___gTeamRoot.pcl__bigger, 34, 99);
**********/
//  ver 1.01
		CalcTeamNumber(cl___gTeamRoot.pcl__bigger,  0,  0);	
		CalcTeamNumber(cl___gTeamRoot.pcl__bigger,  1, 29);	// NBA teams
		CalcTeamNumber(cl___gTeamRoot.pcl__bigger, 30, 31); // All star
		CalcTeamNumber(cl___gTeamRoot.pcl__bigger, 32, 34); // LF, Nin1, Nin2
		CalcTeamNumber(cl___gTeamRoot.pcl__bigger, 35, 99);	// 3pt
	}

	// All done.

	return;
	}



// **************************************************************************
// * CalcTeamNumber ()                                                      *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                CalcTeamNumber          (
								TEAMNODE_T *        pcl__T,
								int                 si___MinTeam,
								int                 si___MaxTeam)

	{
	// Local Variables.

//	char *              pcz__S;
//	char *              pcz__T;

	// Calculate numbers for lesser strings.

	if (pcl__T->pcl__lesser != NULL)
		{
		CalcTeamNumber(pcl__T->pcl__lesser, si___MinTeam, si___MaxTeam);
		}

	// Calculate number for this team.

	if (pcl__T->si___number == 0)
		{
		if ((((int) pcl__T->si___offset) >= si___MinTeam) &&
			(((int) pcl__T->si___offset) <= si___MaxTeam))
			{
			pcl__T->si___number  = (SI) si___sNumber;
			pcl__T->r.ub___tTeam = (UB) si___sNumber;

			apcl_gTeam[si___sNumber] = pcl__T;

			si___sNumber += 1;
			}
		}

	// Calculate numbers for bigger strings.

	if (pcl__T->pcl__bigger != NULL)
		{
		CalcTeamNumber(pcl__T->pcl__bigger, si___MinTeam, si___MaxTeam);
		}

	// All done.

	return;
	}



// **************************************************************************
// * WriteTeams ()                                                          *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                WriteTeams              (void)

	{
	// Local Variables.

	int                  i;

	TeamRomData_T        cl___T;
	UD                   ud___t;

	// Go through the team list calculating the roster.

	for (i = 0; i <= si___gTeamCount; i++)
		{
		// Blank team ?

		if (apcl_gTeam[i] == NULL)
			{
			continue;
			}

		// Make a copy of the team.

		memcpy(&cl___T, &apcl_gTeam[i]->r, sizeof(TeamRomData_T));

		// Display team.

		printf("Team - %s %s\n",
			((STRNODE_T *) cl___T.pcz__tCity)->pcz__string,
			((STRNODE_T *) cl___T.pcz__tName)->pcz__string);

		// Convert the current team to output format.

		ud___t = (((STRNODE_T *) cl___T.pcz__tCity)->pcz__string - acz__gStrData);

		cl___T.pcz__tCity = (char *) (Swap32(ud___t));

		ud___t = (((STRNODE_T *) cl___T.pcz__tName)->pcz__string - acz__gStrData);

		cl___T.pcz__tName = (char *) (Swap32(ud___t));

		ud___t = (((STRNODE_T *) cl___T.pcz__tAbbr)->pcz__string - acz__gStrData);

		cl___T.pcz__tAbbr = (char *) (Swap32(ud___t));

		ud___t = (((STRNODE_T *) cl___T.pcz__tCityName)->pcz__string - acz__gStrData);

		cl___T.pcz__tCityName = (char *) (Swap32(ud___t));

		cl___T.cl___tStatNBA.uw___tGP  = Swap16(cl___T.cl___tStatNBA.uw___tGP );
		cl___T.cl___tStatNBA.uw___tGW  = Swap16(cl___T.cl___tStatNBA.uw___tGW );
		cl___T.cl___tStatNBA.uw___tFGM = Swap16(cl___T.cl___tStatNBA.uw___tFGM);
		cl___T.cl___tStatNBA.uw___tFGA = Swap16(cl___T.cl___tStatNBA.uw___tFGA);
		cl___T.cl___tStatNBA.uw___t3PM = Swap16(cl___T.cl___tStatNBA.uw___t3PM);
		cl___T.cl___tStatNBA.uw___t3PA = Swap16(cl___T.cl___tStatNBA.uw___t3PA);
		cl___T.cl___tStatNBA.uw___tFTM = Swap16(cl___T.cl___tStatNBA.uw___tFTM);
		cl___T.cl___tStatNBA.uw___tFTA = Swap16(cl___T.cl___tStatNBA.uw___tFTA);
		cl___T.cl___tStatNBA.uw___tAST = Swap16(cl___T.cl___tStatNBA.uw___tAST);
		cl___T.cl___tStatNBA.uw___tSTL = Swap16(cl___T.cl___tStatNBA.uw___tSTL);
		cl___T.cl___tStatNBA.uw___tBLK = Swap16(cl___T.cl___tStatNBA.uw___tBLK);
		cl___T.cl___tStatNBA.uw___tDEF = Swap16(cl___T.cl___tStatNBA.uw___tDEF);
		cl___T.cl___tStatNBA.uw___tOFF = Swap16(cl___T.cl___tStatNBA.uw___tOFF);
		cl___T.cl___tStatNBA.uw___tPF  = Swap16(cl___T.cl___tStatNBA.uw___tPF );
		cl___T.cl___tStatNBA.uw___tTO  = Swap16(cl___T.cl___tStatNBA.uw___tTO );

		// Write out current team.

		if (aiffWrite(pcl__gOutputFile, (char *) &cl___T, sizeof(TeamRomData_T))
			!= (LONG) sizeof(TeamRomData_T))
			{
			printf("XvtNBA - Can't write team to output !\n");
			exit(1);
			}
		}

	// All done.

	printf("\n");

	return;
	}



// **************************************************************************
// **************************************************************************
// **************************************************************************
// **************************************************************************



// **************************************************************************
// * MakeRoster ()                                                          *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	UB                  aub__sExcelOrder [] =
	{
	COURT_PG,
	COURT_SG,
	COURT_SF,
	COURT_PF,
	COURT_C,
	0,
	0,
	0
	};

static	void                MakeRosters             (void)

	{
	// Local Variables.

	int                 i;
	int                 j;
	int                 k;

	TEAMNODE_T *        pcl__T;

	// Work our way down from the root.

	puw__gRoster = auw__gRoster;

	// Save number of team rosters stored.

	*puw__gRoster++ = (UW) si___gTeamCount + 2;

	// Go through the team list calculating the roster.

	for (i = 0; i < si___gTeamCount; i++)

		{
		// Calculate the roster for this team.

		pcl__T = apcl_gTeam[i];

		// Blank team ?

		if (pcl__T == NULL)
			{
			*puw__gRoster++ = 0;
			continue;
			}

		// Save number of players in this team.

		*puw__gRoster++ = pcl__T->si___players;

		if (pcl__T->si___players != 0)
			{
			// Create the list of player numbers.

			for (j = 0; j < pcl__T->si___players; j++)
				{
				// Get player number.

				k = pcl__T->apcl_players[j]->si___number;

				// Add starter info into player number.
				//
				// Convert excel order to court order as well.

				if (j < 5)
					{
					k |= (aub__sExcelOrder[j]+1) * 512;
					}

				// Save it.

				puw__gRoster[j] = k;
				}

			// Order the roster.

			puw__gRoster -= 1;

			while (1)
				{
				k = 0;

				for (j = 1; j < pcl__T->si___players; j++)
					{
					if ((puw__gRoster[j] & 511) > (puw__gRoster[j+1] & 511))
						{
						k                 = puw__gRoster[j];
						puw__gRoster[j]   = puw__gRoster[j+1];
						puw__gRoster[j+1] = k;
						}
					}

				if (k == 0) break;
				}

			// Update roster pointer.

			puw__gRoster += 1 + pcl__T->si___players;
			}
		}

	// Write out entries for the custom plyrs.

	*puw__gRoster++ = CUSTOM_PLYRS;

	for (i = 1; i <= CUSTOM_PLYRS; i += 1)
		{
		*puw__gRoster++ = si___gPlyrCount + i;
		}

	// Pad out to a 4-byte boundary.

	if ((puw__gRoster - auw__gRoster) & 1)
		{
		*puw__gRoster++ = 0;
		}

	// All done.

	return;
	}



// **************************************************************************
// * WriteRoster ()                                                         *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                WriteRoster             (void)

	{
	// Local Variables.

	UW *                 puw__R;
	UW                   uw___t;

	int                  i;

	// Byte swap the data.

	puw__R = auw__gRoster;

	while (puw__R != puw__gRoster)
		{
		uw___t = *puw__R;

		*puw__R++ = Swap16(uw___t);
		}

	// Write out the roster.

	i = (puw__gRoster - auw__gRoster) * sizeof(UW);

	if (aiffWrite(pcl__gOutputFile, (char *) auw__gRoster, i) != (LONG) i)
		{
		printf("XvtNBA - Can't write roster to output !\n");
		exit(1);
		}

	// All done.

	return;
	}



// **************************************************************************
// **************************************************************************
// **************************************************************************
// **************************************************************************



// **************************************************************************
// * InitPlyrTree ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                InitPlyrTree            (void)

	{
	// Initialize string tree.

	si___gPlyrCount = 0;

	memset(&cl___gPlyrRoot, 0, sizeof(PLYRNODE_T));

	// All done OK.

	return;
	}



// **************************************************************************
// * AddPlyr ()                                                             *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	PLYRNODE_T *        AddPlyr                 (
								PLYRNODE_T *        pcl__new)

	{

	// Local Variables.

	int                 si___cmp;

	PLYRNODE_T *        pcl__old;

	PLYRNODE_T **       ppcl_nxt;

	// Search the tree for a matching string.

	pcl__old = &cl___gPlyrRoot;
	si___cmp = 1;

	for (;;)
		{
		if (si___cmp  > 0)
			{
			ppcl_nxt = &pcl__old->pcl__bigger;
			}
		else
		if (si___cmp  < 0)
			{
			ppcl_nxt = &pcl__old->pcl__lesser;
			}
		else
		if (si___cmp == 0)
			{
			si___gRepeatPlyr += 1;

			return (pcl__old);
			}

		if (*ppcl_nxt == NULL)
			{
			*ppcl_nxt = pcl__new;

			pcl__new->pcl__parent = pcl__old;
			pcl__new->pcl__bigger = NULL;
			pcl__new->pcl__lesser = NULL;
			pcl__new->si___number = 0;

			si___gPlyrCount += 1;

			return (pcl__new);
			}

		pcl__old = *ppcl_nxt;

		si___cmp = strcmp(
			((STRNODE_T *) pcl__new->r.pcz__pSName)->pcz__string,
			((STRNODE_T *) pcl__old->r.pcz__pSName)->pcz__string);

		if (si___cmp == 0)
			{
			si___cmp = strcmp(
				((STRNODE_T *) pcl__new->r.pcz__pCName)->pcz__string,
				((STRNODE_T *) pcl__old->r.pcz__pCName)->pcz__string);
			}
		}

	// We never actually get here.
	}



// **************************************************************************
// * OrderPlyrs ()                                                          *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                OrderPlyrs              (void)

	{
	// Work our way down from the root.

	si___sNumber = 0;

	if (cl___gPlyrRoot.pcl__bigger != NULL)
		{
		CalcPlyrNumber(cl___gPlyrRoot.pcl__bigger, 0, TEAM_SSN);

		si___gPlyrsSeason = si___sNumber;

		CalcPlyrNumber(cl___gPlyrRoot.pcl__bigger, TEAM_SSN+1, TEAM_END);

		si___gPlyrsTotal  = si___sNumber;

		if (si___gPlyrsTotal != si___gPlyrCount)
			{
			printf("XvtNBA - Where did those players go to ?\n");
			exit(1);
			}
		}

	// All done.

	return;
	}



// **************************************************************************
// * CalcPlyrNumber ()                                                      *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                CalcPlyrNumber          (
								PLYRNODE_T *        pcl__P,
								int                 si___MinTeam,
								int                 si___MaxTeam)

	{
	// Local Variables.

//	char *              pcz__S;
//	char *              pcz__T;

	// Calculate numbers for lesser strings.

	if (pcl__P->pcl__lesser != NULL)
		{
		CalcPlyrNumber(pcl__P->pcl__lesser, si___MinTeam, si___MaxTeam);
		}

	// Calculate number for this player.

	if (pcl__P->si___number == 0)
		{
		if ((((int) pcl__P->r.ub___pTeam) >= si___MinTeam) &&
			(((int) pcl__P->r.ub___pTeam) <= si___MaxTeam))
			{
			si___sNumber += 1;

			pcl__P->si___number  = si___sNumber;
			pcl__P->r.uw___pIndx = si___sNumber;

			apcl_gPlyr[si___sNumber] = pcl__P;
			}
		}

	// Calculate numbers for bigger strings.

	if (pcl__P->pcl__bigger != NULL)
		{
		CalcPlyrNumber(pcl__P->pcl__bigger, si___MinTeam, si___MaxTeam);
		}

	// All done.

	return;
	}



// **************************************************************************
// * WritePlyrs ()                                                          *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                WritePlyrs              (void)

	{
	// Local Variables.

	int                  i;

	PlyrRomData_T        cl___P;
	UD                   ud___t;

	// Go through the team list calculating the roster.

	for (i = 1; i <= si___gPlyrCount; i++)
		{
		// Blank plyr ?

		if (apcl_gPlyr[i] == NULL)
			{
			continue;
			}

		// Make a copy of the plyr.

		memcpy(&cl___P, &apcl_gPlyr[i]->r, sizeof(PlyrRomData_T));

		// Display plyr.

		printf("Plyr - (%s)\t%s, %s\n",
			((STRNODE_T *) apcl_gTeam[cl___P.ub___pTeam]->r.pcz__tAbbr)->pcz__string,
			((STRNODE_T *) cl___P.pcz__pSName)->pcz__string,
			((STRNODE_T *) cl___P.pcz__pCName)->pcz__string);

		// Convert the current plyr to output format.

		ud___t = (((STRNODE_T *) cl___P.pcz__pSName)->pcz__string - acz__gStrData);

		cl___P.pcz__pSName = (char *) (Swap32(ud___t));

		ud___t = (((STRNODE_T *) cl___P.pcz__pCName)->pcz__string - acz__gStrData);

		cl___P.pcz__pCName = (char *) (Swap32(ud___t));

//		ud___t = (((STRNODE_T *) cl___P.pcz__pInitials)->pcz__string - acz__gStrData);
//
//		cl___P.pcz__pInitials = (char *) (Swap32(ud___t));

		cl___P.uw___pIndx    = Swap16(cl___P.uw___pIndx   );

		cl___P.cl___pStatNBA.uw___pGP  = Swap16(cl___P.cl___pStatNBA.uw___pGP );
		cl___P.cl___pStatNBA.uw___pDQ  = Swap16(cl___P.cl___pStatNBA.uw___pDQ );
		cl___P.cl___pStatNBA.uw___pPF  = Swap16(cl___P.cl___pStatNBA.uw___pPF );
		cl___P.cl___pStatNBA.uw___pMIN = Swap16(cl___P.cl___pStatNBA.uw___pMIN);
		cl___P.cl___pStatNBA.uw___pFGM = Swap16(cl___P.cl___pStatNBA.uw___pFGM);
		cl___P.cl___pStatNBA.uw___pFGA = Swap16(cl___P.cl___pStatNBA.uw___pFGA);
		cl___P.cl___pStatNBA.uw___p3PM = Swap16(cl___P.cl___pStatNBA.uw___p3PM);
		cl___P.cl___pStatNBA.uw___p3PA = Swap16(cl___P.cl___pStatNBA.uw___p3PA);
		cl___P.cl___pStatNBA.uw___pFTM = Swap16(cl___P.cl___pStatNBA.uw___pFTM);
		cl___P.cl___pStatNBA.uw___pFTA = Swap16(cl___P.cl___pStatNBA.uw___pFTA);
		cl___P.cl___pStatNBA.uw___pAST = Swap16(cl___P.cl___pStatNBA.uw___pAST);
		cl___P.cl___pStatNBA.uw___pSTL = Swap16(cl___P.cl___pStatNBA.uw___pSTL);
		cl___P.cl___pStatNBA.uw___pBLK = Swap16(cl___P.cl___pStatNBA.uw___pBLK);
		cl___P.cl___pStatNBA.uw___pDEF = Swap16(cl___P.cl___pStatNBA.uw___pDEF);
		cl___P.cl___pStatNBA.uw___pOFF = Swap16(cl___P.cl___pStatNBA.uw___pOFF);
		cl___P.cl___pStatNBA.uw___pTO  = Swap16(cl___P.cl___pStatNBA.uw___pTO );

		// Write out current plyr.

		if (aiffWrite(pcl__gOutputFile, (char *) &cl___P, sizeof(PlyrRomData_T))
			!= (LONG) sizeof(PlyrRomData_T))
			{
			printf("XvtNBA - Can't write plyr to output !\n");
			exit(1);
			}
		}

	// All done.

	printf("\n");

	return;
	}



// **************************************************************************
// * OutputPlyrTex ()                                                       *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	int                 OutputPlyrTex           (
								char *              pcz__Str)

	{
	// Local Variables.

	IMG_HDR_T *         pcl__Img;

	long                sl___Buf;
	UB *                pub__Buf;
	UB                  aub__Buf [8192];

	// Complete the filename.

	strcat(pcz__Str, ".PCX");

	// Load the file.

	pcl__Img = LoadPcx(pcz__Str);

	if (pcl__Img == NULL)
		{
		return (-1);
		}

	// Grab the textures out of the bitmap.

	pub__Buf = GrabPlyrTex(aub__Buf, pcl__Img);

	if (pub__Buf == NULL)
		{
		printf("XvtNBA - Can't grab plyrtex from pcx file %s !\n", pcz__Str);
		exit(1);
		}

	// Write out the resource (padded to a 4 byte boundary).

	while (((sl___Buf = pub__Buf - aub__Buf) & 3) != 0)
		{
		*pub__Buf++ = 0;
		}

	if (sl___Buf != ((64*32)+(8*8)))
		{
		printf("XvtNBA - Illegal plyrtex in file %s !\n", pcz__Str);
		exit(1);
		}

	if (sl___Buf != 0)
		{
		if (aiffWrite(pcl__gOutputFile, aub__Buf, sl___Buf) != sl___Buf)
			{
			printf("XvtNBA - Can't write plyrtex to output !\n");
			exit(1);
			}
		}

	// Free up the texture.

	free(pcl__Img);

	// All done.


	return (0);
	}



// **************************************************************************
// * WritePlyrsTex ()                                                       *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                WritePlyrsTex           (void)

	{
	// Local Variables.

	int                 i;

	UD                  ud___t;

	PLYRNODE_T *        pcl__Plyr;
	PLYRNODE_T **       ppcl_Plyr;

	long                sl___Idx;
	long                sl___End;

	UD *                pud__Idx;
	UD                  aud__Idx [1024];

	char                acz__Str [256];

	// Create room for the index table.

	sl___Idx = aiffSeek(pcl__gOutputFile, 0, SEEK_CUR);

	i = si___gPlyrCount + 1 + CUSTOM_HEADS;

	ud___t = XvertD32HILO(i);

	pud__Idx = &aud__Idx[0];

	*pud__Idx++ = ud___t;

	ud___t = (UD) (i + 2) << 2;

	if (aiffWrite(pcl__gOutputFile, (char *) &aud__Idx[0], ud___t) != (LONG) ud___t)
		{
		printf("XvtNBA - Can't write plyrtex index to output !\n");
		exit(1);
		}

	// Write out the default head texture.

	sl___End = aiffSeek(pcl__gOutputFile, 0, SEEK_CUR);
	ud___t   = sl___End - sl___Idx;

	*pud__Idx++ = XvertD32HILO(ud___t);

	strcpy(acz__Str, "RES\\DEFAULT");

	if (OutputPlyrTex(acz__Str) != 0)
		{
		printf("XvtNBA - Can't load %s file !\n", acz__Str);
		exit(1);
		}

	// Write out the head textures for the plyrs in the list.

	ppcl_Plyr = &apcl_gPlyr[1];

	i = si___gPlyrCount;

	while (i--)

		{
		// Get the plyr.

		pcl__Plyr = *ppcl_Plyr++;

		// Save the data index.

		sl___End = aiffSeek(pcl__gOutputFile, 0, SEEK_CUR);
		ud___t   = sl___End - sl___Idx;

		*pud__Idx++ = XvertD32HILO(ud___t);

		// Blank plyr ?

		if (pcl__Plyr == NULL)
			{
			continue;
			}

		// Load up the PCX file.

		strcpy(acz__Str, "RES");
		strcat(acz__Str, "\\");
		strcat(acz__Str,
			((STRNODE_T *) apcl_gTeam[pcl__Plyr->r.ub___pTeam]->r.pcz__tAbbr)->pcz__string);
		strcat(acz__Str, "\\");
		strcat(acz__Str, pcl__Plyr->acz__filename);

		if ((pcl__Plyr->acz__filename[0] == 0) || (OutputPlyrTex(acz__Str) != 0))
			{
			printf("Using default texture for plyr - (%s)\t%s, %s\n",
				((STRNODE_T *) apcl_gTeam[pcl__Plyr->r.ub___pTeam]->r.pcz__tAbbr)->pcz__string,
				((STRNODE_T *) pcl__Plyr->r.pcz__pSName)->pcz__string,
				((STRNODE_T *) pcl__Plyr->r.pcz__pCName)->pcz__string);
			}

		// Loop around and process the next plyr.
		}

	// Write out the head textures for the custom plyrs.

	for (i = 0; i < CUSTOM_HEADS; i += 1)
		{
		// Save the data index.

		sl___End = aiffSeek(pcl__gOutputFile, 0, SEEK_CUR);
		ud___t   = sl___End - sl___Idx;

		*pud__Idx++ = XvertD32HILO(ud___t);

		// Load up the PCX file.

		sprintf(acz__Str, "RES\\CUS\\CUSTOM%02d", i);

		if (OutputPlyrTex(acz__Str) != 0)
			{
			printf("Using default.pcx instead of custom texture %s\n", acz__Str);
			}

		// Loop around and process the next head.
		}

	// Fix up the index table.

	sl___End = aiffSeek(pcl__gOutputFile, 0, SEEK_CUR);
	ud___t   = sl___End - sl___Idx;

	*pud__Idx++ = XvertD32HILO(ud___t);

	aiffSeek(pcl__gOutputFile, sl___Idx, SEEK_SET);

	i = (pud__Idx - aud__Idx) << 2;

	if (aiffWrite(pcl__gOutputFile, (char *) &aud__Idx[0], i) != (LONG) i)
		{
		printf("XvtNBA - Can't write plyrtex index to output !\n");
		exit(1);
		}

	aiffSeek(pcl__gOutputFile, sl___End, SEEK_SET);

	// All done.

	printf("\n");

	return;
	}



// **************************************************************************
// * OutputPlyrImg ()                                                       *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	int                 OutputPlyrImg           (
								char *              pcz__Str)

	{
	// Local Variables.

	RESOURCE_T *        pcl__Res;

	unsigned char *     pub__File;
	long                sl___File;

	long                sl___Buf;
	UB *                pub__Buf;

	// Load up the photo.

	if (fl___BinaryPhoto)
		{
		// Complete the filename.

		strcat(pcz__Str, ".SWD");

		// Load up the binary file.

		pub__File = NULL;
		sl___File = 0;

		LoadWholeFile(pcz__Str, &pub__File, &sl___File);

		if (pub__File == NULL)
			{
			return (-1);
			}

		// Write out the binary file.

		sl___File = (sl___File + 3) & ~3;

		if (aiffWrite(pcl__gOutputFile, pub__File, sl___File) != sl___File)
			{
			printf("XvtNBA - Can't write plyrimg to output !\n");
			exit(1);
			}

		// Free up the binary file.

		free(pub__File);
		}
	else
		{
		// Complete the filename.

		strcat(pcz__Str, ".STF");

		// Load up the resource.

		LoadResourceFile(pcz__Str);

		if (apcl_sResources[0] == NULL)
			{
			return (-1);
			}

		// Write out the resource.

		pcl__Res = apcl_sResources[0];

		pub__Buf = pcl__Res->pub__Buf;
		sl___Buf = pcl__Res->pub__End - pub__Buf;

		if (aiffWrite(pcl__gOutputFile, pub__Buf, sl___Buf) != sl___Buf)
			{
			printf("XvtNBA - Can't write plyrimg to output !\n");
			exit(1);
			}

		// Free up the resource file.

		FreeResourceFile();
		}

	// All done.

	return (0);
	}



// **************************************************************************
// * WritePlyrsImg ()                                                       *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                WritePlyrsImg           (void)

	{
	// Local Variables.

	int                 i;

	UD                  ud___t;

	PLYRNODE_T *        pcl__Plyr;
	PLYRNODE_T **       ppcl_Plyr;

	long                sl___Idx;
	long                sl___End;

	UD *                pud__Idx;
	UD                  aud__Idx [1024];

	char                acz__Str [256];

	// Create room for the index table.

	sl___Idx = aiffSeek(pcl__gOutputFile, 0, SEEK_CUR);

	i = si___gPlyrCount + 1;

	ud___t = XvertD32HILO(i);

	pud__Idx = &aud__Idx[0];

	*pud__Idx++ = ud___t;

	ud___t = (UD) (i + 2) << 2;

	if (aiffWrite(pcl__gOutputFile, (char *) &aud__Idx[0], ud___t) != (LONG) ud___t)
		{
		printf("XvtNBA - Can't write plyrimg index to output !\n");
		exit(1);
		}

	// Write out the default plyr photo.

	sl___End = aiffSeek(pcl__gOutputFile, 0, SEEK_CUR);
	ud___t   = sl___End - sl___Idx;

	*pud__Idx++ = XvertD32HILO(ud___t);

	strcpy(acz__Str, "RES\\DEFAULT");

	if (OutputPlyrImg(acz__Str) != 0)
		{
		printf("XvtNBA - Can't load %s file !\n", acz__Str);
		exit(1);
		}

	// Write out the photos for the plyrs in the list.

	ppcl_Plyr = &apcl_gPlyr[1];

	i = si___gPlyrCount;

	while (i--)

		{
		// Get the plyr.

		pcl__Plyr = *ppcl_Plyr++;

		// Save the data index.

		sl___End = aiffSeek(pcl__gOutputFile, 0, SEEK_CUR);
		ud___t   = sl___End - sl___Idx;

		*pud__Idx++ = XvertD32HILO(ud___t);

		// Blank plyr ?

		if (pcl__Plyr == NULL)
			{
			continue;
			}

		// Load up the photo.

		strcpy(acz__Str, "RES");
		strcat(acz__Str, "\\");
		strcat(acz__Str,
			((STRNODE_T *) apcl_gTeam[pcl__Plyr->r.ub___pTeam]->r.pcz__tAbbr)->pcz__string);
		strcat(acz__Str, "\\");
		strcat(acz__Str, pcl__Plyr->acz__filename);

		if ((pcl__Plyr->acz__filename[0] == 0) || (OutputPlyrImg(acz__Str) != 0))
			{
			output("Using default photo for plyr - (%s)\t%s, %s\n",
				((STRNODE_T *) apcl_gTeam[pcl__Plyr->r.ub___pTeam]->r.pcz__tAbbr)->pcz__string,
				((STRNODE_T *) pcl__Plyr->r.pcz__pSName)->pcz__string,
				((STRNODE_T *) pcl__Plyr->r.pcz__pCName)->pcz__string);
			}

		// Loop around and process the next plyr.
		}

	// Fix up the index table.

	sl___End = aiffSeek(pcl__gOutputFile, 0, SEEK_CUR);
	ud___t   = sl___End - sl___Idx;

	*pud__Idx++ = XvertD32HILO(ud___t);

	aiffSeek(pcl__gOutputFile, sl___Idx, SEEK_SET);

	i = (pud__Idx - aud__Idx) << 2;

	if (aiffWrite(pcl__gOutputFile, (char *) &aud__Idx[0], i) != (LONG) i)
		{
		printf("XvtNBA - Can't write plyrimg index to output !\n");
		exit(1);
		}

	aiffSeek(pcl__gOutputFile, sl___End, SEEK_SET);

	// All done.

	printf("\n");

	return;
	}



// **************************************************************************
// * WriteSuperPalette ()                                                   *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                WriteSuperPalette       (void)

	{
	// Local Variables.

	unsigned char *     pub__File;
	long                sl___File;

	// Only if we're using binary photos.

	if (fl___BinaryPhoto)
		{
		// Load up the binary file.

		pub__File = NULL;
		sl___File = 0;

		LoadWholeFile("RES\\SUPERPAL\\SUPERPAL.BIN", &pub__File, &sl___File);

		if (pub__File != NULL)
			{
			// Write out the binary file.

			sl___File = (sl___File + 3) & ~3;

			if (aiffWrite(pcl__gOutputFile, pub__File, sl___File) != sl___File)
				{
				printf("XvtNBA - Can't write super-palette to output !\n");
				exit(1);
				}

			// Free up the binary file.

			if (pub__File != NULL) free(pub__File);
			}
		}

	// All done.

	printf("\n");

	return;
	}



// **************************************************************************
// * OutputPlyrNam ()                                                       *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	int                 OutputPlyrNam           (
								char *              pcz__Str)

	{
	// Local Variables.

	RESOURCE_T *        pcl__Res;

	long                sl___Buf;
	UB *                pub__Buf;

	// Complete the filename.

	strcat(pcz__Str, ".SAM");

	// Load the file.

	LoadResourceFile(pcz__Str);

	if (apcl_sResources[0] == NULL)
		{
		return (-1);
		}

	// Write out the resource (normal voice).

	pcl__Res = apcl_sResources[0];

	pub__Buf = pcl__Res->pub__Buf;
	sl___Buf = pcl__Res->pub__End - pub__Buf;

	if (aiffWrite(pcl__gOutputFile, pub__Buf, sl___Buf) != sl___Buf)
		{
		printf("XvtNBA - Can't write plyrnam to output !\n");
		exit(1);
		}

	// Free up the resource file.

	FreeResourceFile();

	// All done.

	return (0);
	}



// **************************************************************************
// * WritePlyrsNam ()                                                       *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                WritePlyrsNam           (void)

	{
	// Local Variables.

	int                 i;

	UD                  ud___t;

	PLYRNODE_T *        pcl__Plyr;
	PLYRNODE_T **       ppcl_Plyr;

	long                sl___Idx;
	long                sl___End;

	UD *                pud__Idx;
	UD                  aud__Idx [1024*2];

	char                acz__Str [256];

	// Create room for the index table.

	sl___Idx = aiffSeek(pcl__gOutputFile, 0, SEEK_CUR);

	i = (si___gPlyrCount * 2) + 1;

	ud___t = XvertD32HILO((i));

	pud__Idx = &aud__Idx[0];

	*pud__Idx++ = ud___t;

	ud___t = (UD) (i + 2) << 2;

	if (aiffWrite(pcl__gOutputFile, (char *) &aud__Idx[0], ud___t) != (LONG) ud___t)
		{
		printf("XvtNBA - Can't write plyrnam index to output !\n");
		exit(1);
		}

	// Write out the default plyr name (there isn't one).

	sl___End = aiffSeek(pcl__gOutputFile, 0, SEEK_CUR);
	ud___t   = sl___End - sl___Idx;

	*pud__Idx++ = XvertD32HILO(ud___t);

//	strcpy(acz__Str, "RES\\DEFAULT");
//
//	if (OutputPlyrNam(acz__Str) != 0)
//		{
//		printf("XvtNBA - Can't load %s file !\n", acz__Str);
//		exit(1);
//		}

	// Go through the plyr list writing out the voc.

	ppcl_Plyr = &apcl_gPlyr[1];

	i = si___gPlyrCount;

	while (i--)

		{
		// Get the plyr.

		pcl__Plyr = *ppcl_Plyr++;

		if (pcl__Plyr == NULL)
			{
			continue;
			}

		// Save the data index (normal voice).

		sl___End = aiffSeek(pcl__gOutputFile, 0, SEEK_CUR);
		ud___t   = sl___End - sl___Idx;

		*pud__Idx++ = XvertD32HILO(ud___t);

		// Load up the resource (normal voice).

		strcpy(acz__Str, "RES");
		strcat(acz__Str, "\\");
		strcat(acz__Str,
			((STRNODE_T *) apcl_gTeam[pcl__Plyr->r.ub___pTeam]->r.pcz__tAbbr)->pcz__string);
		strcat(acz__Str, "\\");
		strcat(acz__Str, pcl__Plyr->acz__filename);

		if ((pcl__Plyr->acz__filename[0] == 0) || (OutputPlyrNam(acz__Str) != 0))
			{
			printf("Using default announcer for plyr away - (%s)\t%s, %s\n",
				((STRNODE_T *) apcl_gTeam[pcl__Plyr->r.ub___pTeam]->r.pcz__tAbbr)->pcz__string,
				((STRNODE_T *) pcl__Plyr->r.pcz__pSName)->pcz__string,
				((STRNODE_T *) pcl__Plyr->r.pcz__pCName)->pcz__string);
			}

		// Save the data index (excited voice).

		sl___End = aiffSeek(pcl__gOutputFile, 0, SEEK_CUR);
		ud___t   = sl___End - sl___Idx;

		*pud__Idx++ = XvertD32HILO(ud___t);

		// Load up the resource (excited voice).

		strcpy(acz__Str, "RES");
		strcat(acz__Str, "\\");
		strcat(acz__Str,
			((STRNODE_T *) apcl_gTeam[pcl__Plyr->r.ub___pTeam]->r.pcz__tAbbr)->pcz__string);
		strcat(acz__Str, "\\");
		strcat(acz__Str, pcl__Plyr->acz__filename);
		strcat(acz__Str, "X");

		if ((pcl__Plyr->acz__filename[0] == 0) || (OutputPlyrNam(acz__Str) != 0))
			{
			printf("Using default announcer for plyr home - (%s)\t%s, %s\n",
				((STRNODE_T *) apcl_gTeam[pcl__Plyr->r.ub___pTeam]->r.pcz__tAbbr)->pcz__string,
				((STRNODE_T *) pcl__Plyr->r.pcz__pSName)->pcz__string,
				((STRNODE_T *) pcl__Plyr->r.pcz__pCName)->pcz__string);
			}

		// Loop around and process the next plyr.
		}

	// Fix up the index table.

	sl___End = aiffSeek(pcl__gOutputFile, 0, SEEK_CUR);
	ud___t   = sl___End - sl___Idx;

	*pud__Idx++ = XvertD32HILO(ud___t);

	aiffSeek(pcl__gOutputFile, sl___Idx, SEEK_SET);

	i = (pud__Idx - aud__Idx) << 2;

	if (aiffWrite(pcl__gOutputFile, (char *) &aud__Idx[0], i) != (LONG) i)
		{
		printf("XvtNBA - Can't write plyrnam index to output !\n");
		exit(1);
		}

	aiffSeek(pcl__gOutputFile, sl___End, SEEK_SET);

	// All done.

	printf("\n");

	return;
	}



// **************************************************************************
// * WriteTalk ()                                                           *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                WriteTalk               (void)

	{
	// Local Variables.

	int                 i;

	UD                  ud___t;

	RESOURCE_T *        pcl__Res;

	FILE *              pcl__Fil;
	FILE *              pcl__Fil1;
	FILE *              pcl__Fil2;
	char *              pcz__1st;
	char *              pcz__Cur;
	char *              pcz__New;
	char *              pcz__Str;

	UB *                pub__Buf;
	long                sl___Buf;

	long                sl___Idx;
	long                sl___End;

	UD *                pud__Idx;
	UD                  aud__Idx [1024*2];

	char                acz__Str [256];

	// Read in the filelist.

	if ((pcl__Fil = fopen("res\\talk\\talklist.txt", "rt")) == NULL)
		{
		ErrorCode = ERROR_NO_FILE;
		sprintf(ErrorMessage,
			"(XS) Unable to open talklist.txt !\n");
		exit(1);
		}

	i = 0;

	pcz__1st = NULL;
	pcz__Cur = NULL;

	while (fgets(acz__Str, 251, pcl__Fil) != NULL)
		{
		pcz__New = acz__Str + strlen(acz__Str) - 1;

		if (*pcz__New == '\n') *pcz__New = 0;

		pcz__Str = strrmvspace(acz__Str);

		if (pcz__Str == NULL) continue;

		if (*pcz__Str == 0) continue;

		pcz__Str = strupr(pcz__Str);

		pcz__New = (char *) malloc(256);

		strcpy(pcz__New + 4, pcz__Str);

		if (pcz__1st == NULL)
			{
			pcz__1st              = pcz__New;
			pcz__Cur              = pcz__New;
			}
		else
			{
			*((char **) pcz__Cur) = pcz__New;
			pcz__Cur              = pcz__New;
			}

		i += 1;
		}

	*((char **) pcz__New) = NULL;

	fclose(pcl__Fil);

	// Create room for the index table.

	sl___Idx = aiffSeek(pcl__gOutputFile, 0, SEEK_CUR);

	ud___t = XvertD32HILO((i));

	pud__Idx = &aud__Idx[0];

	*pud__Idx++ = ud___t;

	ud___t = (UD) (i + 2) << 2;

	if (aiffWrite(pcl__gOutputFile, (char *) &aud__Idx[0], ud___t) != (LONG) ud___t)
		{
		printf("XvtNBA - Can't write talk index to output !\n");
		exit(1);
		}

	// Go through the file list writing out the phrase.

	pcz__Cur = pcz__1st;

	while (i--)

		{
		// Save the data index.

		sl___End = aiffSeek(pcl__gOutputFile, 0, SEEK_CUR);
		ud___t   = sl___End - sl___Idx;

		*pud__Idx++ = XvertD32HILO(ud___t);

		// Load up the resource.

		strcpy(acz__Str, "RES");
		strcat(acz__Str, "\\TALK\\");
		strcat(acz__Str, pcz__Cur + 4);

		LoadResourceFile(acz__Str);

		if (apcl_sResources[0] == NULL)
			{
			printf("XvtNBA - Can't load talk resource %s !\n", acz__Str);
			exit(1);
			}

		// Write out the resource.

		pcl__Res = apcl_sResources[0];

		pub__Buf = pcl__Res->pub__Buf;
		sl___Buf = pcl__Res->pub__End - pub__Buf;

		if (aiffWrite(pcl__gOutputFile, pub__Buf, sl___Buf) != sl___Buf)
			{
			printf("XvtNBA - Can't write plyrnam to output !\n");
			exit(1);
			}

		// Free up the resource file.

		FreeResourceFile();

		// Loop around and process the next phrase.

		pcz__Cur = *((char **) pcz__Cur);
		}

	// Fix up the index table.

	sl___End = aiffSeek(pcl__gOutputFile, 0, SEEK_CUR);
	ud___t   = sl___End - sl___Idx;

	*pud__Idx++ = XvertD32HILO(ud___t);

	aiffSeek(pcl__gOutputFile, sl___Idx, SEEK_SET);

	i = (pud__Idx - aud__Idx) << 2;

	if (aiffWrite(pcl__gOutputFile, (char *) &aud__Idx[0], i) != (LONG) i)
		{
		printf("XvtNBA - Can't write talk index to output !\n");
		exit(1);
		}

	aiffSeek(pcl__gOutputFile, sl___End, SEEK_SET);

	// Write out the phrase ID list.

	if ((pcl__Fil1 = fopen("TEAMTALK.ID", "wt")) == NULL)
		{
		ErrorCode = ERROR_NO_FILE;
		sprintf(ErrorMessage,
			"(XS) Unable to open TEAMTALK.ID !\n");
		exit(1);
		}

	if ((pcl__Fil2 = fopen("TEAMTALK.HID", "wt")) == NULL)
		{
		ErrorCode = ERROR_NO_FILE;
		sprintf(ErrorMessage,
			"(XS) Unable to open TEAMTALK.HID !\n");
		exit(1);
		}

	pcz__Cur = pcz__1st;

	i = 0;

	while (pcz__Cur != NULL)
		{
		i += 1;

		pcz__New = pcz__Cur + 4;

		pcz__New[strlen(pcz__New) - 4] = 0;

		fprintf(pcl__Fil1, "unsigned long PHRASE_%s = %d;\n", pcz__New, i);
		fprintf(pcl__Fil2, "#define PHRASE_%s %d\n", pcz__New, i);

		pcz__New = *((char **) pcz__Cur);

		free(pcz__Cur);

		pcz__Cur = pcz__New;
		}

	fprintf(pcl__Fil1, "unsigned long TOTAL_PHRASES = %d;\n", i);
	fprintf(pcl__Fil2, "#define TOTAL_PHRASES %d\n", i);

	fclose(pcl__Fil1);
	fclose(pcl__Fil2);

	// All done.

	printf("\n");

	return;
	}



// **************************************************************************
// **************************************************************************
// **************************************************************************
// **************************************************************************



// **************************************************************************
// * InitStringTree ()                                                      *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                InitStringTree          (void)

	{
	// Initialize string memory.

	pcz__gStrData  = &acz__gStrData[4];

	// Initialize string tree.

	si___gStrCount = 0;

	memset(&cl___gStrRoot, 0, sizeof(STRNODE_T));

	// Add a blank string.

	AddString("");

	// All done OK.

	return;
	}



// **************************************************************************
// * AddString ()                                                           *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	STRNODE_T *         AddString               (
								char *              pcz__S)

	{

	// Local Variables.

	int                 si___cmp;

	STRNODE_T *         pcl__old;
	STRNODE_T *         pcl__new;

	STRNODE_T **        ppcl_nxt;

	// Search the tree for a matching string.

	pcl__old = &cl___gStrRoot;
	si___cmp = 1;

	for (;;)
		{
		if (si___cmp > 0)
			{
			ppcl_nxt = &pcl__old->pcl__bigger;
			}
		else
		if (si___cmp < 0)
			{
			ppcl_nxt = &pcl__old->pcl__lesser;
			}

		if (*ppcl_nxt == NULL)
			{
			pcl__new = malloc(sizeof(STRNODE_T) + 1 + strlen(pcz__S));

			if (pcl__new == NULL)
				{
				printf("XvtNBA - Not enough memory to add a string !\n");
				exit(1);
				}

			*ppcl_nxt = pcl__new;

			pcl__new->pcl__parent = pcl__old;
			pcl__new->pcl__bigger = NULL;
			pcl__new->pcl__lesser = NULL;
			pcl__new->si___number = 0;
			pcl__new->pcz__string = (char *) (pcl__new + 1);

			strcpy(pcl__new->pcz__string, pcz__S);

			si___gStrCount += 1;

			return (pcl__new);
			}

		pcl__old = *ppcl_nxt;
		si___cmp = strcmp(pcz__S, pcl__old->pcz__string);

		if (si___cmp == 0)
			{
			return (pcl__old);
			}
		}

	// We never actually get here.
	}



// **************************************************************************
// * OrderStrings ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                OrderStrings            (void)

	{
	// Work our way down from the root.

	si___sNumber = 0;

	if (cl___gStrRoot.pcl__bigger != NULL)
		{
		CalcStringNumber(cl___gStrRoot.pcl__bigger);
		}

	// Terminate the string table.

	*pcz__gStrData++ = 0;

	while (((pcz__gStrData - acz__gStrData) & 3) != 0)
		{
		*pcz__gStrData++ = 0;
		}

	acz__gStrData[0] = (char) ((si___gStrCount >> 24) & 0xFFu);
	acz__gStrData[1] = (char) ((si___gStrCount >> 16) & 0xFFu);
	acz__gStrData[2] = (char) ((si___gStrCount >>  8) & 0xFFu);
	acz__gStrData[3] = (char) ((si___gStrCount >>  0) & 0xFFu);

	// All done.

	return;
	}



// **************************************************************************
// * CalcStringNumber ()                                                    *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                CalcStringNumber        (
								STRNODE_T *         pcl__S)

	{
	// Local Variables.

	char *              pcz__S;
	char *              pcz__T;

	// Calculate numbers for lesser strings.

	if (pcl__S->pcl__lesser != NULL)
		{
		CalcStringNumber(pcl__S->pcl__lesser);
		}

	// Calculate number for this string.

	pcl__S->si___number = ++si___sNumber;

	pcz__S = pcl__S->pcz__string;
	pcz__T = pcz__gStrData;

	pcl__S->pcz__string = pcz__T;

	while (*pcz__S != 0)
		{
		*pcz__T++ = *pcz__S++;
		}

	*pcz__T++ = 0;

	pcz__gStrData = pcz__T;

	// Calculate numbers for bigger strings.

	if (pcl__S->pcl__bigger != NULL)
		{
		CalcStringNumber(pcl__S->pcl__bigger);
		}

	// All done.

	return;
	}



// **************************************************************************
// * GrabPlyrTex ()                                                         *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	UB *                GrabPlyrTex             (
								UB *                pub__Buf,
								IMG_HDR_T *         pcl__Img)

	{
	// Local Variables.

	SI                  si___Col;
	SI                  si___Row;

	SI                  si___Cols;
	SI                  si___Rows;

	IMG_BOX_T           cl___Box;

	SI                  asi__ColX [64];
	SI                  asi__ColW [64];

	SI                  asi__RowY [64];
	SI                  asi__RowH [64];

	// Scan the bitmap to locate the rows and columns.

	si___Cols = FindImgCols(pcl__Img, &asi__ColX[0], &asi__ColW[0]);
	si___Rows = FindImgRows(pcl__Img, &asi__RowY[0], &asi__RowH[0]);

	if (si___Cols < 2)
		{
		return (NULL);
		}

	// Now scan the rows and columns for texture sprites.

	for (si___Row = 0; si___Row < si___Rows; si___Row++)
		{
		for (si___Col = 0; si___Col < si___Cols; si___Col++)
			{
			// Find the bounding box for the sprite.

			cl___Box.pcl__h = pcl__Img;
			cl___Box.si___x = asi__ColX[si___Col];
			cl___Box.si___w = asi__ColW[si___Col];
			cl___Box.si___y = asi__RowY[si___Row];
			cl___Box.si___h = asi__RowH[si___Row];

			FindImgEdge(&cl___Box);

			// Grab the sprite.

			if (cl___Box.si___w != 0)
				{
				if ((cl___Box.si___w == 64) && (cl___Box.si___h == 32))
					{
					pub__Buf = GrabSpriteBtmToTop(&cl___Box, pub__Buf);
					}
				else
				if ((cl___Box.si___w ==  8) && (cl___Box.si___h ==  8))
					{
					pub__Buf = GrabSpriteBtmToTop(&cl___Box, pub__Buf);
					}
				else
					{
					return (NULL);
					}
				}
			}
		}

	// All done.

	return (pub__Buf);

	}



// **************************************************************************
// **************************************************************************
// **************************************************************************
// **************************************************************************


// **************************************************************************
// * IdentifyScheduleDay ()                                                 *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	int                 IdentifyScheduleDay     (
								char *              pcz__S)

	{
	//

	return (IdentifyString(pcz__S, acl__gScheduleDay));
	}



// **************************************************************************
// * IdentifyScheduleMonth ()                                               *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	int                 IdentifyScheduleMonth   (
								char *              pcz__S)

	{
	//

	return (IdentifyString(pcz__S, acl__gScheduleMonth));
	}



// **************************************************************************
// * IdentifyScheduleTeam ()                                                *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	int                 IdentifyScheduleCity    (
								char *              pcz__S)

	{
	//

	return (IdentifyString(pcz__S, acl__gScheduleCity));
	}



// **************************************************************************
// * IdentifyTeam ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	int                 IdentifyTeam            (
								char *              pcz__S)

	{
	// Local Variables.

//	char                acz__T [128];

	//

//	strcpy(acz__T, pcz__S);
//	strupr(acz__T);
//	return (IdentifyString(acz__T, acl__gTeamString));

	return (IdentifyString(pcz__S, acl__gTeamString));
	}



// **************************************************************************
// * IdentifyDiv ()                                                         *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	int                 IdentifyDiv             (
								char *              pcz__S)

	{
	return (IdentifyString(pcz__S, acl__gDivString));
	}



// **************************************************************************
// * IdentifyCon ()                                                         *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	int                 IdentifyCon             (
								char *              pcz__S)

	{
	return (IdentifyString(pcz__S, acl__gConString));
	}



// **************************************************************************
// * IdentifyPos ()                                                         *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	int                 IdentifyPos             (
								char *              pcz__S)

	{
	return (IdentifyString(pcz__S, acl__gPosString));
	}



// **************************************************************************
// * IdentifyHand ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	int                 IdentifyHand            (
								char *              pcz__S)

	{
	return (IdentifyString(pcz__S, acl__gHandString));
	}



// **************************************************************************
// * IdentifyString ()                                                      *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	int                 IdentifyString          (
								char *              pcz__S,
								STR_TO_NUM_T *      pcl__T)

	{
	// Locate string in table.

	while (pcl__T->pcz__S != NULL)
		{
		if (strcmpi(pcz__S, pcl__T->pcz__S) == 0)
			{
			return (pcl__T->si___N);
			}
		pcl__T += 1;
		}

	// String not found.

	return (-1);
	}



// **************************************************************************
// * strrmvspace ()                                                         *
// **************************************************************************
// * Remove leading and trailing spaces                                     *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	char *              strrmvspace             (
								char *              pcz__S)

	{

	// Local Variables.

	char                c;
	char *              pcz__T;

	// Remove leading spaces.

	c = *pcz__S;

	while ((c == ' ') || (c == '\t')) c = *(++pcz__S);

	// Find the last character in the string.

	pcz__T = pcz__S;

	while (*pcz__T++ != 0);

	pcz__T -= 2;

	// Remove trailing space;

	while (pcz__T >= pcz__S)
		{
		c = *pcz__T;

		if ((c != ' ') && (c != '\t')) break;

		*pcz__T = 0;

		pcz__T -= 1;
		}

	// Return new string pointer.

	return (pcz__S);
	}



// **************************************************************************
// **************************************************************************
// **************************************************************************
// **************************************************************************



// **************************************************************************
// * ErrorReset ()                                                          *
// **************************************************************************
// * Reset the error condition flags                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                ErrorReset              (void)

	{
	ErrorCode = ERROR_NONE;

	ErrorMessage[0] = '\0';
	}



// **************************************************************************
// * ErrorQualify ()                                                        *
// **************************************************************************
// * If ErrorMessage is blank, then fill it with a generic message          *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                ErrorQualify            (void)

	{
	if (*ErrorMessage == '\0')

		{
		if (ErrorCode == ERROR_NONE)
			{
			}

		else if (ErrorCode == ERROR_DIAGNOSTIC)
			{
			sprintf(ErrorMessage,
				"Error : Error during diagnostic printout.\n");
			}

		else if (ErrorCode == ERROR_NO_MEMORY)
			{
			sprintf(ErrorMessage,
				"Error : Not enough memory to complete this operation.\n");
			}

		else if (ErrorCode == ERROR_NO_FILE)
			{
			sprintf(ErrorMessage,
				"Error : File not found.\n");
			}

		else if (ErrorCode == ERROR_IO_EOF)
			{
			sprintf(ErrorMessage,
				"Error : Unexpected end-of-file.\n");
			}

		else if (ErrorCode == ERROR_IO_READ)
			{
			sprintf(ErrorMessage,
				"Error : I/O read failure (file corrupted ?).\n");
			}

		else if (ErrorCode == ERROR_IO_WRITE)
			{
			sprintf(ErrorMessage,
				"Error : I/O write failure (disk full ?).\n");
			}

		else if (ErrorCode == ERROR_IO_SEEK)
			{
			sprintf(ErrorMessage,
				"Error : I/O seek failure (file corrupted ?).\n");
			}

		else if (ErrorCode == ERROR_PROGRAM)
			{
			sprintf(ErrorMessage,
				"Error : A program error has occurred.\n");
			}

		else
			{
			sprintf(ErrorMessage,
				"Error : Unknown error number.\n");
			}
		}
	}



// **************************************************************************
// **************************************************************************
// **************************************************************************
// **************************************************************************



// **************************************************************************
// * LoadWholeFile ()                                                       *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  char *   File name                                             *
// *         UB **    Ptr to variable holding file address                  *
// *         long *   Ptr to variable holding file size                     *
// *                                                                        *
// * Output  long     Bytes read, or -ve if an error                        *
// **************************************************************************

global	long                LoadWholeFile           (
								char *              pcz__Name,
								unsigned char **    ppbf_Addr,
								long *              psl__Size)

	{
	// Local Variables.

	FILE *              file;
	unsigned char *     addr;
	long                size;

	//

	if ((pcz__Name == NULL) || (ppbf_Addr == NULL) || (psl__Size == NULL))
		{
		return (-1);
		}

	if ((file = fopen(pcz__Name, "rb")) == NULL)
		{
		return (-1);
		}

	size = GetFileLength(file);

	if ((*psl__Size != 0) && (*psl__Size < size))
		{
		size = *psl__Size;
		}

	addr = *ppbf_Addr;

	if (addr == NULL)
		{
		addr = (unsigned char *) malloc(size);
		}

	if (addr == NULL)
		{
		size = -1;
		}
	else
		{
		if (fread(addr, 1, size, file) != (size_t) size)
			{
			size = -1;
			}
		}

	fclose(file);

	*psl__Size = size;

	if (*ppbf_Addr == NULL)
		{
		if (size < 0)
			{
			free(addr);
			}
		else
			{
			*ppbf_Addr = addr;
			}
		}

	// All done, return size or -ve if error.

	return (size);
	}



// **************************************************************************
// * SaveWholeFile ()                                                       *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  char *   File name                                             *
// *         UB *     File address                                          *
// *         long     File size                                             *
// *                                                                        *
// * Output  long     Bytes written, or -ve if an error                     *
// **************************************************************************

global	long                SaveWholeFile           (
								char *              pcz__Name,
								unsigned char *     pbf__Addr,
								long                sl___Size)

	{
	// Local Variables.

	FILE *              file;

	//

	if ((pcz__Name == NULL) || (pbf__Addr == NULL) || (sl___Size < 0))
		{
		return (-1);
		}

	if ((file = fopen(pcz__Name, "wb")) == NULL)
		{
		return (-1);
		}

	if (sl___Size != 0)
		{
		if (fwrite(pbf__Addr, 1, sl___Size, file) != (size_t) sl___Size)
			{
			sl___Size = -1;
			}
		}

	fclose(file);

	// All done, return size or -ve if error.

	return (sl___Size);
	}



// **************************************************************************
// * GetFileLength ()                                                       *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  FILE *                                                         *
// *                                                                        *
// * Output  long                                                           *
// **************************************************************************

global	long                GetFileLength           (
								FILE *              file)

	{
	// Local Variables.

	long                CurrentPos;
	long                FileLength;

	//

	CurrentPos = ftell(file);

	fseek(file, 0, SEEK_END);

	FileLength = ftell(file);

	fseek(file, CurrentPos, SEEK_SET);

	return (FileLength);
	}



// **************************************************************************
// * strcsv ()                                                              *
// **************************************************************************
// * Find next token in an Excel CSV string (like strtok)                   *
// **************************************************************************
// * Inputs  char *                                                         *
// *                                                                        *
// * Output  char *                                                         *
// **************************************************************************

static	char *              strcsv                  (
								char *              pcz__Str)

	{
	// Local variables.

	static char *       pcz__Nxt;

	// New string, or continue with old ?

	if (pcz__Str == NULL)
		{
		pcz__Str = pcz__Nxt;
		}

	pcz__Nxt = pcz__Str;

	// Search for end point.

	if (pcz__Str != NULL)
		{
		for (;;)
			{
			if (*pcz__Nxt == 0)
				{
				if (pcz__Str == pcz__Nxt)
					{
					pcz__Str = NULL;
					}
				pcz__Nxt = NULL;
				break;
				}
			else
			if (*pcz__Nxt == '\n')
				{
				*pcz__Nxt++ = 0;
				break;
				}
			else
			if ((*pcz__Nxt == ',') || (*pcz__Nxt == ';'))
				{
				*pcz__Nxt++ = 0;
				break;
				}
			pcz__Nxt += 1;
			}
		}

	// Return pointer to token.

	return (pcz__Str);
	}



// **************************************************************************
// **************************************************************************
// **************************************************************************
//	END OF XVTNBA.C
// **************************************************************************
// **************************************************************************
// **************************************************************************