⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 convert.cpp

📁 空战游戏flacon源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Convert.cpp
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <string.h>
#include <malloc.h>
#include "tacref.h"
//#include "\falcon4\ui\include\tacref.h"

// Prototypes
char  *SkipJunk    ( char *lnptr );
char  *GetALine    ( char *lnptr, char *seps );
long  Text2Long    ( char *txt );
short Text2Short   ( char *txt );
short CheckToken   ( char *line );
void  WriteLong    ( long lnum );
void  WriteShort   ( short snum );
void  WriteText    ( char *txt );
void  DoBegEntity  ();
void  DoBegStat    ();
void  DoBegCat     ();
void  DoBegText    ();
void  DoBegRwr     ();
void  DoEndEntity  ();
void  DoEndStat    ();
void  DoEndCat     ();
void  DoEndText    ();
void  DoEndRwr     ();
void  DoDescript   ();
void  InitVar      ();
void  InitRwrKeep  ();
void  InitDescKeep ();
void  InitCatKeep  ();
void  InitCatInTxtKeep();
void  DoParse      ();
void  CreateBin    ();
//void  Conv2Bin     ();
void  Conv2Bin     (char *ifname,char *ofname);

const MAXTOKEN      = 10;
const MAXFUNC       = 10;
const MAXCATKEEPTXT = 1000;
const MAXCATKEEP    = 150;
const MAXRWRKEEP    = 150;
const MAXDESCKEEP   = 150;
const MAXINTXTKEEP  = 150;

enum {
	_ENDTEXT_=90,
	_ENDCAT_,
	_ENDSTAT_,
	_ENDENTITY_,
	_ENDRWR_
};

// Globals
HDC   gblhdc;
HWND  gblhwnd;
RECT  gblrect;

int   ifh,ofh;
long  flen;
short toktype;
short where;	// current chunk
short txthd;	// 1 means text collection header created, else 0
short ttlentity,ttlstat,ttlcat,ttltxt,ttlrwr;
short ttlcatstrhead;
char  *trdata;	// points 2 current token
char  *startentity,*startstat,*startcat,*starttxt,*startrwr;
char  *startdata,*endingdata;	// start and end of database
char  *nextdata;
char  *tokens[ MAXTOKEN ] = {
	  "BEGIN_ENTITY",
	  "BEGIN_STAT",
	  "BEGIN_CAT",
	  "BEGIN_TEXT",
	  "END_TEXT",
	  "END_CAT",
	  "END_STAT",
	  "END_ENTITY",
	  "BEGIN_RWR",
	  "END_RWR"
};

short tokmap[MAXTOKEN] = {
	  _ENTITY_,
	  _STATS_,
	  _CATEGORY_,
	  _TEXT_,
	  0,
	  0,
	  0,
	  0,
	  _RWR_DATA_,
	  0
};
char *backn = "\n";

void (*tokfunc[MAXFUNC])() = {
	 DoBegEntity,
	 DoBegStat,
	 DoBegCat,
	 DoBegText,
	 DoEndText,
	 DoEndCat,
	 DoEndStat,
	 DoEndEntity,
	 DoBegRwr,
	 DoEndRwr
};
struct Header         *Enthead,*Stathead,*Cathead,*Stringhead;
struct Header         *Rwrhead,*Deschead;
struct Entity         *Entityptr;
struct Category       *Catptr;
struct TextString     *Txtptr,*DescTxtptr;
struct Text4CatString *CatInTxtptr;
struct RWR_Data       *Rwrptr;

char   *CatKeepptr;
char   *CatKeeptxt[MAXCATKEEPTXT];		// for keeping Cat text pointers
char   *CatHeadKeep[MAXCATKEEP];		// for keeping Cat header pointers
char   *CatPtrKeep[MAXCATKEEP];			// for keeping Cat chunk pointers
char   *CatStrKeep[MAXCATKEEP];			// for keeping Cat String header pointers
char   *CatInTxtKeep[MAXCATKEEP][MAXINTXTKEEP];		// 4 keeping Cat Inside Text Str struc ptrs in 1 string header
char   *CatInChildKeep[MAXCATKEEP][MAXINTXTKEEP];	// 4 keeping Cat string data pointers in 1 string header
char   *CatInChildTxt;

char   *RwrHeadKeep[MAXRWRKEEP];	// ptrs 2 RWR heads in 1 entity
char   *RwrPtrKeep [MAXRWRKEEP];	// ptrs 2 RWR data ptrs in 1 entity
char   *DescHeadKeep;				// ptr 2 Description header in 1 ent
char   *DescPtrKeep [MAXDESCKEEP];	// ptrs 2 Description data ptrs in 1 ent
char   *DescChildKeep[MAXDESCKEEP];	// ptrs 2 Desc child texts in 1 entity
char   *DescChildTxt;
long   curtxtidx;
long   currwridx;
long   curdesidx;
long   curcatidx;
long   curintxtidx;
long   catintxtidxkeep[MAXCATKEEP];

LRESULT CALLBACK WinProc (HWND, UINT,WPARAM,LPARAM);

// Skip junks in the beginning of the file which start with double slash
char *SkipJunk( char *lnptr )
{
	char *dblslh = "//";
	char *newlnptr,*token;

	token    = strtok( lnptr, backn );	// get 1st token
	newlnptr = token;
	/* While there are tokens in "string" */
	while ( ( token ) && (!strncmp( token,dblslh,2 )) ) {
		/* Get next token: */
		token = strtok( NULL, backn );
		if ( token ) newlnptr = token;
	}
	// put back linefeed since it's replaced
	//len = strlen( token );
	//token[len] = 0x0A;
	return( newlnptr );
}

// Write a long number
void WriteLong( long lnum )
{
	long numwr;

	numwr = _write( ofh, &lnum, sizeof(long) );
}

// Write a short number
void WriteShort( short snum )
{
	long numwr;

	numwr = _write( ofh, &snum, sizeof(short) );
}

// Write a text string
void WriteText( char *txt )
{
	long numwr,len;

	len   = strlen( txt );
	numwr = _write( ofh, txt, len );
}

// Text 2 Long
long Text2Long( char *txt )
{
	long lnum;

	lnum = atol( txt );
	return( lnum );
}

// Text 2 Short
short Text2Short( char *txt )
{
	short snum;

	snum = atoi( txt);
	return( snum );
}

// Get a line to check
// returns a line ptr, else NULL
char *GetALine( char *lnptr, char *seps )
{
	char *newlnptr;

	newlnptr = strtok( lnptr,seps );
	nextdata = newlnptr+strlen( newlnptr )+1;
	return( newlnptr );
}

// Check tokens
// returns token type if it is one, else -1
short CheckToken( char *line )
{
	short i,tokentype;
	long  len;

	tokentype   = -1;
	len         = strlen( line );
	line[len-1] = '\0';	// replace a 0xD
	for ( i = 0; i < MAXTOKEN; i++ ) {
		if ( !strcmp( tokens[i],line ) ) {	// got it
			tokentype = i;
			break;
		}
	}
	line[len-1] = 0xD;	// put it back

	return( tokentype );
}

// Process Begin Entity token
void DoBegEntity()
{
	long  lGroupID,lSubGroupID,lEntityID;
	short lModelID,len,i;
	char  *tptr;

	where = _ENTITY_;
	if ( !Enthead )   Enthead   = new Header;
	if ( !Entityptr ) Entityptr = new Entity;
	Enthead->type = tokmap[toktype];
	trdata   = nextdata;
	trdata   = GetALine( trdata,backn );	// GroupID
	lGroupID = Text2Long( trdata );
	Entityptr->GroupID = lGroupID;

	trdata      = nextdata;
	trdata      = GetALine( trdata,backn );	// SubGroupID
	lSubGroupID = Text2Long( trdata );
	Entityptr->SubGroupID = lSubGroupID;

	trdata    = nextdata;
	trdata    = GetALine( trdata,backn );	// EntityID
	lEntityID = Text2Long( trdata );
	Entityptr->EntityID = lEntityID;

	ttlentity += ( 3*sizeof(long) );

	trdata   = nextdata;
	trdata   = GetALine( trdata,backn );	// ModelID
	lModelID = Text2Short( trdata );
	Entityptr->ModelID = lModelID;

	ttlentity += ( sizeof(short) );

	trdata = nextdata;
	trdata = GetALine( trdata,backn );	// EntityName
	len    = strlen( trdata );
	tptr   = (char *)&Entityptr->Name[0];
	for ( i=0;i < len-1;i++ ) {
		*tptr = trdata[i];
		tptr++;
	}
	*tptr = '\0';
	ttlentity += 32;	// fix length
}

// Process End Entity token
void DoEndEntity()
{
	short i,j;

	where         = _ENDENTITY_;
	ttlentity     += (2*sizeof(short));
	Enthead->size = ttlentity;
	// Write it here
	CreateBin();
	if ( Enthead )   delete Enthead; Enthead = NULL;
	if ( Entityptr ) delete Entityptr; Entityptr = NULL;
	if ( Stathead)   delete Stathead; Stathead = NULL;
	for ( i = 0; i < MAXRWRKEEP; i++ ) {
		delete RwrHeadKeep[i];
		delete RwrPtrKeep[i];
	}
	for ( i = 0; i < MAXDESCKEEP; i++ ) {
		delete DescPtrKeep[i];
		delete DescChildKeep[i];
	}
	delete DescHeadKeep; DescHeadKeep = NULL;

	for ( i = 0; i < MAXCATKEEP; i++ ) {
		delete CatHeadKeep[i];
		delete CatPtrKeep[i];
		delete CatStrKeep[i];
	}
	
	for ( j = 0; j < MAXCATKEEP; j++ ) {
		for ( i = 0; i < MAXINTXTKEEP; i++ ) {
			delete CatInTxtKeep[j][i];
			delete CatInChildKeep[j][i];
		}
	}

	ttlentity = ttlstat = ttlcat = ttltxt = ttlrwr = 0;
	currwridx = 0;	// reset idx
	InitRwrKeep ();	// reset ptrs
	curdesidx = 0;
	InitDescKeep();
	curcatidx = 0;
	ttlcatstrhead = 0;
	curintxtidx   = 0;
	InitCatKeep ();
	InitCatInTxtKeep();
}

// Process Begin Stat token
void DoBegStat()
{
	where     = _STATS_;
	Stathead  = new Header;
	ttlentity += (2*sizeof(short));	// for the header
	Stathead->type = tokmap[toktype];
}

// Process End Stat token
void DoEndStat()
{
	where          = _ENDSTAT_;
	Stathead->size = ttlstat;
	ttlstat = 0;
}

// Process Begin Cat token
void DoBegCat()
{
	short len,i;
	char  *tptr;

	where   = _CATEGORY_;
	Cathead = new Header;
	CatHeadKeep[curcatidx] = (char *)Cathead;
	Catptr  = new Category;
	CatPtrKeep[curcatidx]  = (char *)Catptr;
	ttlentity += (2*sizeof(short));	// for the header
	ttlstat   += (2*sizeof(short));	// for the header
	Cathead->type = tokmap[toktype];

	trdata = nextdata;
	trdata = GetALine( trdata,backn );	// CatText
	len    = strlen( trdata );
	tptr   = (char *)&Catptr->Name[0];
	for ( i=0;i < len-1;i++ ) {
		*tptr = trdata[i];
		tptr++;
	}
	*tptr = '\0';
	ttlentity += 40;	// fix length
	ttlstat += 40;
	ttlcat += 40;
}

// Process End Cat token
void DoEndCat()
{
	long i;

	where  = _ENDCAT_;
	Cathead->size = ttlcat;
	ttlcat = 0;
	if ( Cathead ) delete Cathead; Cathead = NULL;
	if ( Catptr )  delete Catptr;  Catptr  = NULL;
	for (i = 0; i < curtxtidx; i++) {
		delete CatKeeptxt[i];
		CatKeeptxt[i] = NULL;
	}
}

// Process Begin Text token
void DoBegText()
{
	short len,lcount;
	short i;
	char  *tptr;
	char  *endmark = "END_TEXT";
	char  *newlnptr;
	char  *token;
	long  lXrefGroup,lXrefSubGroup,lEntityID;

	curtxtidx  = 0;	// currently Not Used
	if ( where == _CATEGORY_ ) {	//
		Stringhead = new Header;
		CatStrKeep[curcatidx] = (char *)Stringhead;
		Stringhead->type      = tokmap[toktype];
		ttlentity  += (2*sizeof(short));
		ttlstat    += (2*sizeof(short));
		ttlcat     += (2*sizeof(short));

	trdata = nextdata;
	token  = strtok( trdata, backn );	// get 1st token
	lcount = 0;
	/* While there are tokens in "string" */
	while ( token ) {
		if (strncmp( token,endmark,strlen(endmark) )) {
			/* Not Endmark, Get next token: */
			//if ( !strncmp(token,"Propulsion: 2 Gas",17)) { // just checking
			//	len = 0;
			//}
			lcount++;	// count how many text strings are inside this block of TEXT
			CatInTxtptr = new Text4CatString;
			CatInTxtKeep[curcatidx][curintxtidx] = (char *)CatInTxtptr;

			len  = strlen( token );
			if ( len == 1 ) len++;
			CatInTxtptr->length = len;
			CatInChildTxt       = new char[len];
			CatInChildKeep[curcatidx][curintxtidx] = CatInChildTxt;
			tptr = CatInChildTxt;
			for ( i=0;i < len-1;i++ ) {
				*tptr = token[i];
				tptr++;
			}
			*tptr         = '\0';
			ttlentity     += len;
			ttlstat       += len;
			ttlcat        += len;
			ttlcatstrhead += len;
			//
			token = strtok( NULL, backn );
			if ( token ) newlnptr = token;
			if ( (strncmp(token,"END_TEXT",8)) ) {
				// more text string in this block
				curintxtidx++;
			}
		}
		else {	// End mark found, look ahead
			// get the 3 xrefs
			trdata     = token+strlen(token)+1;
			trdata     = GetALine( trdata,backn );	// XrefGroup
			lXrefGroup = Text2Long( trdata );
			CatInTxtptr->GroupID = lXrefGroup;

			trdata        = nextdata;
			trdata        = GetALine( trdata,backn );	// XrefSubGroup
			lXrefSubGroup = Text2Long( trdata );
			CatInTxtptr->SubGroupID = lXrefSubGroup;

			trdata    = nextdata;
			trdata    = GetALine( trdata,backn );	// XrefVehicle or EntityID
			lEntityID = Text2Long( trdata );
			CatInTxtptr->EntityID = lEntityID;

			ttlentity     += (lcount* (sizeof(short)+(3*sizeof(long))) );	// 11/23/98
			ttlstat       += (lcount* (sizeof(short)+(3*sizeof(long))) );
			ttlcat        += (lcount* (sizeof(short)+(3*sizeof(long))) );
			ttlcatstrhead += (lcount* (sizeof(short)+(3*sizeof(long))) );
			lcount        = 0;	// reset

			tptr = nextdata;
			if ( !(strncmp(tptr,"END_CAT",7)) ) { // no more text block, the end of a Cat
				trdata = tptr;
				curintxtidx++;
				break;
			}
			else {
				token = tptr;
				token = strtok( token, backn );
				token = strtok( NULL, backn );	// points 2 text
				curintxtidx++;
			}
		}
	}
	// Look ahead if there is more Cat
	token = trdata;
	token = strtok( token, backn );
	token = strtok( NULL, backn );
	if ( !(strncmp(token,"BEGIN_CAT",9)) ) {
		trdata           = token;	// more Cat
		nextdata         = token;
		Cathead->size    = ttlcat;	// save previous Cat chunk size
		ttlcat           = 0;	// reset 4 next cat
		Stringhead->size = ttlcatstrhead;
		ttlcatstrhead    = 0;
		catintxtidxkeep[curcatidx] = curintxtidx;
		curintxtidx      = 0;	// NEED 2 save this first
		curcatidx++;
		return;
	}

	// Advance to Description text
	/* While there are tokens in "string" */
	catintxtidxkeep[curcatidx] = curintxtidx;	// 4 the last Cat
	Cathead->size    = ttlcat;	// save previous Cat chunk size
	ttlcat           = 0;
	Stringhead->size = ttlcatstrhead;
	ttlcatstrhead    = 0;
	curcatidx++;	// 4 loop sake
	while ( token ) {
		if (strncmp( token,"BEGIN_TEXT",10 )) {
			/* Not Description Text yet, Get next token */
			token = strtok( NULL, backn );
			if ( token ) newlnptr = token;
		}
		else { // Beginning of Description text found
			Stathead->size = ttlstat;
			trdata         = token;
			nextdata       = token;
			where          = _ENDSTAT_;
			// Do the description here
			DoDescript();
			break;
		}
	}


	// put back linefeed since it's replaced
	len = strlen( token );	// does nothing, just 4 debugging
	//token[len] = 0x0A;

	} // ENDIF Category
}

// Process description text
void DoDescript()
{
	short lZoomAdjust,lVertical,lHoriz,lMissile;
	short len,i;
	char  *newlnptr;
	char  *token,*tptr;
	short ttldesctxt;

	Deschead       = new Header;
	Deschead->type = _DESCRIPTION_;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -