Example #1
0
//---------------------------------------------------------------------------
long File::open (FilePtr _parent, unsigned long fileSize, long numChild)
{
	if (_parent && (_parent->fastFile == NULL))
	{
		parent = _parent;
		if (parent->getFileMode() != READ)
		{
			return(CANT_WRITE_TO_CHILD);
		}
		
		physicalLength = fileSize;
		parentOffset = parent->getLogicalPosition();
		logicalPosition = 0;

		//-------------------------------------------------------------
		fileName = parent->getFilename();
		fileMode = parent->getFileMode();
		
		handle = parent->getFileHandle();
		
		if (logFileTraffic)
		{
			if (!fileTrafficLog)
			{
				createTrafficLog();
			}
		
			char msg[300];
			sprintf(msg,"CHILD     Length: %010d    File: %s",fileSize,_parent->getFilename());
			fileTrafficLog->writeLine(msg);
		}

		long result = parent->addChild(this);
		if (result != NO_ERR)
			return(result);

		//------------------------------------------------------------
		// NEW FUNCTIONALITY!!!
		// 
		// Each file may have a number of files open as children which
		// use the parent's handle for reads and writes.  This would
		// allow us to open a packet file and read a packet as a fitIni
		// or allow us to write a packet as a fit ini and so forth.
		//
		// It also allows us to use the packet file extensions as tree
		// files to avoid the ten thousand file syndrome.
		//
		// There is now an open which takes a FilePtr and a size.
		// 
		// IF a numChild parameter is passed in as -1, we want this file in RAM!!
		// This means NO CHILDREN!!!!!!!!!!!!!
		if (numChild != -1)
		{
			maxChildren = numChild;
			childList = (FilePtr *)systemHeap->Malloc(sizeof(FilePtr) * maxChildren);
			
			gosASSERT(childList != NULL);

			numChildren = 0;
			for (long i=0;i<(long)maxChildren;i++)
			{
				childList[i] = NULL;
			}	
		}
		else
		{
			maxChildren = 0;
			inRAM = TRUE;
			unsigned long result = 0;

			fileImage = (MemoryPtr)malloc(fileSize);
			if (!fileImage)
				inRAM = FALSE;

			if (_parent->getFileClass() == PACKETFILE)
			{
				result = ((PacketFilePtr)_parent)->readPacket(((PacketFilePtr)_parent)->getCurrentPacket(),fileImage);
			}
			else
			{
				result = _read(handle,fileImage,fileSize);
				if (result != fileSize)
					lastError = errno;
			}
		}
	}
	else
	{
		return(PARENT_NULL);
	}
	
	return(NO_ERR);
}
Example #2
0
//******************************************************************************************
long MasterComponent::saveEXCEL (FilePtr componentFile, unsigned char masterId, float baseSensorRange) 
{
	char dataLine[512];
	char piece[512];
	char comma[2] = ",";

	if (masterID == -1)
	{
		//---------------------------------------
		// Blank undefined line in compbas here.
		sprintf(dataLine,"0,undefined,undefined,0,0,0,0,0,0,0,0,0,0,0,0,No,No,0,No,0,0,0,0,na,na,na,na,na,na,na,na,na,,,,,");
		componentFile->writeLine(dataLine);
		return(0);
	}

	//----------------------------------------------------------
	// Build the dataline piece by piece
	sprintf(piece,"%d",masterId);
	strcpy(dataLine,piece);
	strcat(dataLine,comma);

	cLoadString(COMPONENT_NAME_START+masterID,name,MAXLEN_COMPONENT_NAME);
	strcat(dataLine,name);
	strcat(dataLine,comma);

	cLoadString(COMPONENT_ABBR_START+masterID,abbreviation,MAXLEN_COMPONENT_ABBREV);
	strcat(dataLine,abbreviation);
	strcat(dataLine,comma);

	strcat(dataLine,ComponentFormString[form]);
	strcat(dataLine,comma);

	sprintf(piece,"%d",size);
	strcat(dataLine,piece);
	strcat(dataLine,comma);

	sprintf(piece,"1");
	strcat(dataLine,piece);
	strcat(dataLine,comma);

	sprintf(piece,"%3.1f",tonnage);
	strcat(dataLine,piece);
	strcat(dataLine,comma);

	sprintf(piece,"%d",resourcePoints);
	strcat(dataLine,piece);
	strcat(dataLine,comma);

	for (long location = 0; location < NUM_BODY_LOCATIONS; location++) 
	{
		if (criticalSpacesReq[location] == -1)
			sprintf(piece,"No");
		else if (criticalSpacesReq[location] == 0)
			sprintf(piece,"Yes");
		else
			Fatal(criticalSpacesReq[location],"Bad Data");

		strcat(dataLine,piece);
		strcat(dataLine,comma);
	}

	if (getCanVehicleUse())
		sprintf(piece,"Yes");
	else
		sprintf(piece,"No");

	strcat(dataLine,piece);
	strcat(dataLine,comma);

	if (getCanMechUse())
		sprintf(piece,"Yes");
	else
		sprintf(piece,"No");

	strcat(dataLine,piece);
	strcat(dataLine,comma);

	if (getClanTechBase() && getISTechBase())
	{
		sprintf(piece,"Both");
	}
	else if (getClanTechBase())
	{
		sprintf(piece,"Clan");
	}
	else if (getISTechBase())
	{
		sprintf(piece,"IS");
	}
	else
	{
		sprintf(piece,"0");
	}

	strcat(dataLine,piece);
	strcat(dataLine,comma);

	if (getCanISUse())
		sprintf(piece,"Yes");
	else
		sprintf(piece,"No");

	strcat(dataLine,piece);
	strcat(dataLine,comma);

	sprintf(piece,"0");
	strcat(dataLine,piece);
	strcat(dataLine,comma);

	sprintf(piece,"1");
	strcat(dataLine,piece);
	strcat(dataLine,comma);

	sprintf(piece,"%8.1f",CV);
	strcat(dataLine,piece);
	strcat(dataLine,comma);

	switch (form) 
	{
		case COMPONENT_FORM_COCKPIT:
		case COMPONENT_FORM_ACTUATOR:
		case COMPONENT_FORM_SIMPLE:
		case COMPONENT_FORM_CASE:
		case COMPONENT_FORM_POWER_AMPLIFIER:
		case COMPONENT_FORM_GYROSCOPE:
		case COMPONENT_FORM_LIFESUPPORT:
		case COMPONENT_FORM_BULK:
		default:
			//----------------------
			// No additional data...
			sprintf(piece,"na,na,na,na,na,na,na,na,na,,,,,");
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			break;

		case COMPONENT_FORM_SENSOR:
			sprintf(piece,"%4.1f",float(stats.sensor.range / baseSensorRange));
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"na,na,na,na,na,na,na,na,,,,,");
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			break;

		case COMPONENT_FORM_ECM:
			sprintf(piece,"%4.1f",stats.ecm.effect);
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			sprintf(piece,"%4.1f",stats.ecm.range);
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			sprintf(piece,"na,na,na,na,na,na,na,,,,,");
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			break;

		case COMPONENT_FORM_JAMMER:
			sprintf(piece,"%4.1f",stats.jammer.effect);
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			sprintf(piece,"na,na,na,na,na,na,na,na,,,,,");
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			break;

		case COMPONENT_FORM_PROBE:
			sprintf(piece,"%4.1f",stats.probe.effect);
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			sprintf(piece,"na,na,na,na,na,na,na,na,,,,,");
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			break;

		case COMPONENT_FORM_ENGINE:
			sprintf(piece,"%d",0);
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			sprintf(piece,"na,na,na,na,na,na,na,na,,,,,");
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			break;

		case COMPONENT_FORM_HEATSINK:
			sprintf(piece,"%4.1f",stats.heatsink.dissipation);
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			sprintf(piece,"na,na,na,na,na,na,na,na,,,,,");
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			break;

		case COMPONENT_FORM_WEAPON_ENERGY:
			sprintf(piece,"%4.1f",stats.weapon.damage);
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"%4.2f",stats.weapon.recycleTime);
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"%4.1f",stats.weapon.heat);
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"%d",0);
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"%d",WEAPON_AMMO_NONE);
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"%s",WeaponRangeString[stats.weapon.range]);
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			
			//sprintf(piece,"%d",stats.weapon.type);
			//strcat(dataLine,piece);
			//strcat(dataLine,comma);

			sprintf(piece,"%d",stats.weapon.specialEffect);
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"%d",0);
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"%d",stats.weapon.flags);
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			break;

		case COMPONENT_FORM_WEAPON_BALLISTIC:
			sprintf(piece,"%4.1f",stats.weapon.damage);
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"%4.2f",stats.weapon.recycleTime);
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"%4.1f",stats.weapon.heat);
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"%d",stats.weapon.ammoAmount);
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"%d",stats.weapon.ammoType);
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"%s",WeaponRangeString[stats.weapon.range]);
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			
//			sprintf(piece,"%d",stats.weapon.type);
//			strcat(dataLine,piece);
//			strcat(dataLine,comma);

			sprintf(piece,"%d",stats.weapon.specialEffect);
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"%d",stats.weapon.flags);
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			break;

		case COMPONENT_FORM_WEAPON_MISSILE:
			sprintf(piece,"%4.1f",stats.weapon.damage);
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"%4.2f",stats.weapon.recycleTime);
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"%4.1f",stats.weapon.heat);
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"%d",stats.weapon.ammoAmount);
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"%d",stats.weapon.ammoType);
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"%s",WeaponRangeString[stats.weapon.range]);
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			
//			sprintf(piece,"%d",stats.weapon.type);
//			strcat(dataLine,piece);
//			strcat(dataLine,comma);

			sprintf(piece,"%d",stats.weapon.specialEffect);
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"%d",stats.weapon.flags);
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			break;

		case COMPONENT_FORM_AMMO:
			sprintf(piece,"%d",stats.ammo.ammoPerTon);
			strcat(dataLine,piece);
			strcat(dataLine,comma);

			sprintf(piece,"%6.1f",stats.ammo.explosiveDamage);
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			sprintf(piece,"na,na,na,na,na,na,na,,,,,");
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			break;

		case COMPONENT_FORM_JUMPJET:
			sprintf(piece,"%6.2f",stats.jumpjet.rangeMod);
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			sprintf(piece,"na,na,na,na,na,na,na,na,,,,,");
			strcat(dataLine,piece);
			strcat(dataLine,comma);
			break;
	}

	return(NO_ERR);
}
Example #3
0
//---------------------------------------------------------------------------
long File::open (const char* fName, FileMode _mode, long numChild)
{
	gosASSERT( !isOpen() );
	//-------------------------------------------------------------
	long fNameLength = strlen(fName);
	
	fileName = (char *)systemHeap->Malloc(fNameLength+1);
	gosASSERT(fileName != NULL);
		
	strncpy(fileName,fName,fNameLength+1);
	fileMode = _mode;
	_fmode = _O_BINARY;

	_strlwr(fileName);

	if (fileMode == CREATE)
	{
		handle = _creat(fileName,_S_IWRITE);
		if (handle == INVALID_HANDLE_VALUE)
		{
			lastError = errno;
			return lastError;
		}
	}
	else
	{
		//----------------------------------------------------------------
		//-- First, see if file is in normal place.  Useful for patches!!
		handle = _open(fileName,_O_RDONLY);

		//------------------------------------------
		//-- Next, see if file is in fastFile.
		if (handle == INVALID_HANDLE_VALUE)
		{
			lastError = errno;

			fastFile = FastFileFind(fileName,fastFileHandle);
			if (!fastFile)
			{
				//Not in main installed directory and not in fastfile.  Look on CD.

				char actualPath[2048];
				strcpy(actualPath,CDInstallPath);
				strcat(actualPath,fileName);
				handle = _open(actualPath,_O_RDONLY);
				if (handle == INVALID_HANDLE_VALUE)
				{
					bool openFailed = false;
					bool alreadyFullScreen = (Environment.fullScreen != 0);
					while (handle == INVALID_HANDLE_VALUE)
					{
						openFailed = true;

						//OK, check to see if the CD is actually present.
						// Do this by checking for tgl.fst on the CD Path.
						// If its there, the CD is present BUT the file is missing.
						// MANY files in MechCommander 2 are LEGALLY missing!
						// Tell it to the art staff.
						char testCDPath[2048];
						strcpy(testCDPath,CDInstallPath);
						strcat(testCDPath,"tgl.fst");

						DWORD findCD = fileExists(testCDPath);
						if (findCD == 1)	//File exists. CD is in drive.  Return 2 to indicate file not found.
							return 2;

						EnterWindowMode();
		
						char data[2048];
						sprintf(data,FileMissingString,fileName,CDMissingString);
						DWORD result1 = MessageBox(NULL,data,MissingTitleString,MB_OKCANCEL | MB_ICONWARNING);
						if (result1 == IDCANCEL)
						{
							ExitGameOS();
							return (2);		//File not found.  Never returns though!
						}
		
						handle = _open(actualPath,_O_RDONLY);
					}
		
					if (openFailed && (Environment.fullScreen == 0) && alreadyFullScreen)
						EnterFullScreenMode();
				}
				else
				{
					if (logFileTraffic && (handle != INVALID_HANDLE_VALUE))
					{
						if (!fileTrafficLog)
						{
							createTrafficLog();
						}

						char msg[300];
						sprintf(msg,"CFHandle  Length: %010d    File: %s",fileSize(),fileName);
						fileTrafficLog->writeLine(msg);
					}

					setup();

					//------------------------------------------------------------
					// NEW FUNCTIONALITY!!!
					// 
					// Each file may have a number of files open as children which
					// use the parent's handle for reads and writes.  This would
					// allow us to open a packet file and read a packet as a fitIni
					// or allow us to write a packet as a fit ini and so forth.
					//
					// It also allows us to use the packet file extensions as tree
					// files to avoid the ten thousand file syndrome.
					//
					// There is now an open which takes a FilePtr and a size.
					maxChildren = numChild;
					childList = (FilePtr *)systemHeap->Malloc(sizeof(FilePtr) * maxChildren);

					if (!childList)
					{
						return(NO_RAM_FOR_CHILD_LIST);
					}

					numChildren = 0;
					for (long i=0;i<(long)maxChildren;i++)
					{
						childList[i] = NULL;
					}	

					return (NO_ERR);
				}
			}

			if (logFileTraffic)
			{
				if (!fileTrafficLog)
				{
					createTrafficLog();
				}
	
				char msg[300];
				sprintf(msg,"FASTF     Length: %010d    File: %s",fileSize(),fileName);
				fileTrafficLog->writeLine(msg);
			}

			//---------------------------------------------------------------------
			//-- FastFiles are all compressed.  Must read in entire chunk into RAM
			//-- Then close fastfile!!!!!
			inRAM = TRUE;

			fileImage = (unsigned char *)malloc(fileSize());
			if (fileImage)
			{
				fastFile->readFast(fastFileHandle,fileImage,fileSize());

				physicalLength = getLength();
				//------------------------------------
				//-- Image is in RAM.  Shut the file.
				//fastFile->closeFast(fastFileHandle);
				//fastFile = NULL;
				//fastFileHandle = -1;

				logicalPosition = 0;
			}

			return NO_ERR;
		}
		else
		{
			if (logFileTraffic && (handle != INVALID_HANDLE_VALUE))
			{
				if (!fileTrafficLog)
				{
					createTrafficLog();
				}
	
				char msg[300];
				sprintf(msg,"CFHandle  Length: %010d    File: %s",fileSize(),fileName);
				fileTrafficLog->writeLine(msg);
			}

			setup();
	
			//------------------------------------------------------------
			// NEW FUNCTIONALITY!!!
			// 
			// Each file may have a number of files open as children which
			// use the parent's handle for reads and writes.  This would
			// allow us to open a packet file and read a packet as a fitIni
			// or allow us to write a packet as a fit ini and so forth.
			//
			// It also allows us to use the packet file extensions as tree
			// files to avoid the ten thousand file syndrome.
			//
			// There is now an open which takes a FilePtr and a size.
			maxChildren = numChild;
			childList = (FilePtr *)systemHeap->Malloc(sizeof(FilePtr) * maxChildren);
			
			if (!childList)
			{
				return(NO_RAM_FOR_CHILD_LIST);
			}
		
			numChildren = 0;
			for (long i=0;i<(long)maxChildren;i++)
			{
				childList[i] = NULL;
			}	
	
			return (NO_ERR);
		}
	}
	
	return(NO_ERR);
}