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

📄 data.c

📁 Dos6.0
💻 C
📖 第 1 页 / 共 4 页
字号:
          if ((c = (int) szUnreadLine[iUnread++]) == '\0' || c == '\r')
            {
              /* Nothing left in the "unread" buffer */
              FreeMemory (szUnreadLine);
              iUnread = -1;
            }
        }
      else
        {
          /* Read from the disk if there's no "unread" data */
          if ((c = DosReadChar (iFile)) == -1)
            {
              /* Error or EOF */
              szBuffer[i] = '\0';
              // If a Ctr-Z at end of file, remove it.
              if (i > 0 && szBuffer[i-1] == 26)
                 szBuffer[--i] = 0;

              // If this line had any chars, then pretend the line was cr/lf
              // terminated.  Since we're now at end of file, the next call
              // will have i=0, so will return TRUE (EOF).
              return ((i == 0) ? TRUE : FALSE);
            }
        }

      /* Newlines are ignored */
      if (c == '\n')
        {
          ++iLength;
          continue;
        }

      /* Carriage returns mark the end of the line */
      if (c == '\r')
        {
          c = '\0';

          /* Check to see if the next character is a linefeed */
          if ((iNext = DosReadChar (iFile)) != '\n')
            {
              /* Corrupt file -- simulate EOF */
              szBuffer[0] = '\0';
              return (TRUE);
            }
          else
            DosUnreadChar ((char) iNext);
        }

      /* Store the character */
      szBuffer[i++] = (char) c;
    }

  /* Put a zero byte at the end of the string, if necessary */
  if (c)
    szBuffer[i] = '\0';

  return (FALSE);
}


char chUnreadChar;        /* Stores unread character */
int fUnreadChar = FALSE;  /* TRUE when chUnreadChar stores a character */

/*********************************************************************
 * DosReadChar - Reads a single character from the disk.
 *
 * iFile - DOS file handle.
 *
 * RETURNS: -1 if EOF or error, character otherwise.
 *********************************************************************/

int DosReadChar (int iFile)
{
  char c;                     /* Character read in from disk        */
  int  iNumRead;              /* Number of characters actually read */


  /* Check the unread character */
  if (fUnreadChar)
    {
      fUnreadChar = FALSE;
      return (chUnreadChar);
    }

  /* Read the character from the disk */
  if (_dos_read (iFile, (char far *) &c, 1, &iNumRead) != 0 || iNumRead == 0)
    {
      /* Error or EOF */
      return (-1);
    }
  else
    return ((int) c);
}


/*********************************************************************
 * DosUnreadChar - Stores a single character from the disk.
 *
 * iFile - DOS file handle.
 *
 * RETURNS: -1 if EOF or error, character otherwise.
 *********************************************************************/

void DosUnreadChar (char ch)
{
  chUnreadChar = ch;
  fUnreadChar = TRUE;
}


/*********************************************************************
 * DosWriteLine - Writes a single text line from disk using _dos_write.
 *
 * szBuffer - String is stored here
 * iFile    - DOS file handle
 *
 * Returns:  ERROR if out of disk space, OK otherwise.
 *********************************************************************/

int DosWriteLine (char *szBuffer, int iFile)
{
  unsigned fReturnValue = 0;    /* Return value from _dos_write    */
  unsigned uNmbrToWrite;        /* Actual number of bytes to write */
  unsigned uNmbrWritten;        /* Actual number of bytes written  */


  /* Write the line */
  if (szBuffer)
    {
      uNmbrToWrite = strlen (szBuffer);
      fReturnValue = _dos_write (iFile, szBuffer, uNmbrToWrite,
                             &uNmbrWritten);
      if (fReturnValue)
        FatalError (FATAL_DISK_ERROR);
    }
  else
    {  // Safe simulation of write of blank line
      uNmbrToWrite = 0;
      uNmbrWritten = 0;
    }

  /* If we weren't out of disk space */
  if (uNmbrToWrite == uNmbrWritten)
    {
      /* Write the carriage return/line feed */
      uNmbrToWrite = 2;
      fReturnValue = _dos_write (iFile, "\r\n", uNmbrToWrite, &uNmbrWritten);

      if (fReturnValue)
        FatalError (FATAL_DISK_ERROR);
    }

  /* Return an error if out of disk space */
  return ((uNmbrToWrite != uNmbrWritten) ? ERROR : OK);
}


/***************************************************************************/
/* Cleanup function to free all memory allocated by any of the database 	*/
/* management functions. This is needed for debugging purposes only and 	*/
/* MEM_BUG should not be defined in the distribution build. 					*/
/* 																								*/
/* void FreeDataMemory( void )															*/
/* 																								*/
/* ARGUMENTS:	NONE																			*/
/* RETURNS: 	void																			*/
/* 																								*/
/***************************************************************************/

#ifdef	MEM_BUG

void FreeDataMemory( void )
{
	register		i;

	for ( i = 0; i < END_CLASS; i++ )
		if ( Data[ i ] != NULL )
			FreeMemory( Data[ i ] );

	if ( RelocBuf != NULL )
		FreeMemory( RelocBuf );

	if ( OemList != NULL )
		FreeMemory( OemList );

	if ( pchStringBuf != NULL )
		FreeMemory( pchStringBuf );
}
#endif

/***************************************************************************/
/* Initializes the Data[] array with arrays of pointers to strings for		*/
/* each class specifed in the dosdata.dat file associated with this OEM.	*/
/* This function is also used to load the non OEM specific data specified	*/
/* by the "UPGRADE 0.0" area in dosdata.dat. 										*/
/* 																								*/
/* void LoadOemData( struct DDR *Oem ) 												*/
/* 																								*/
/* ARGUMENTS:	Oem	- Ptr to DDR struct with name an version number 		*/
/* RETURNS: 	struct DDR *- Ptr to DDR structure for specifed OEM			*/
/* 																								*/
/***************************************************************************/

void LoadOemData( struct DDR *Oem )
{
	register 	i;
	char			**apszArray;

	ReadOemData( Oem->uDataOffset );
	for ( i = 0; i < END_CLASS; i++ )
		if ( (apszArray = GetClassData( Class[ i ] )) != NULL )
			Data[ i ] = apszArray;
}

/***************************************************************************/
/* Reads in a block of data from the DOSDATA.DAT file. First reads a 2		*/
/* byte value at the start of the block which contains the size of the		*/
/* block and then allocates a buffer the proper size and reads the data 	*/
/* into the allocated memory area. If there is an error the FatalError()	*/
/* function is called to abort the program. It is assumed that the file 	*/
/* pointer's position in the data file is already pointing at the first    */
/* 2 bytes at the start of the buffer. 												*/
/* 																								*/
/* unsigned GetNextDataBlock( int iFile, void **DataBuf )						*/
/* 																								*/
/* ARGUMENTS:	iFile 		- Open file handle to the data file 				*/
/* 				DataBuf		- Ptr to the data buffer ptr							*/
/* 																								*/
/***************************************************************************/

static unsigned GetNextDataBlock( int iFile, void **DataBuf )
{
	register		iStatus;
	unsigned 	uBufLen;
	unsigned 	uRead;

	iStatus = ERROR;							/* Assume there may be an error	*/

	if ( _dos_read( iFile, &uBufLen, USIZE, &uRead ) == OK &&
			  uRead == USIZE )
	{
		*DataBuf = GetMemory( uBufLen );
		if ( _dos_read( iFile, *DataBuf, uBufLen, &uRead ) == OK )
			if (uRead == uBufLen )
				iStatus = OK;
	}

	if ( iStatus != OK )
		FatalError( FATAL_DATA_READ_ERROR );

	return( uBufLen	);
}

/***************************************************************************/
/* Reads in a block of data into the relocation buffer. The read starts at */
/* the begining of the dosdata file data area + the specified offset.		*/
/* 																								*/
/* void ReadOemData( unsigned uDataOffset )											*/
/* 																								*/
/* ARGUMENTS:	uDataOffset - Offset of data from an OEM DDR structure		*/
/* RETURNS: 	void																			*/
/* 																								*/
/***************************************************************************/

static void ReadOemData( unsigned uDataOffset )
{
	register		iStatus; 				/* Keeps track of error conditions		*/
	int			iFile;					/* DOS file handle							*/

	unsigned 	uRead;					/* Number of bytes read by a dos_read	*/
	long			lOffset; 				/* Offset in file to start read at		*/

	lOffset = (long)uDataOffset + lDataOffset;
	iStatus = ERROR;

	if ( _dos_open( szDosDataFile, ShareAccess, &iFile	) == OK )
	{
		if ( _dos_seek( iFile, lOffset, SEEK_SET ) == lOffset )
			if ( _dos_read( iFile, RelocBuf, RELOC_LEN, &uRead ) == OK )
				if ( uRead > 0 )
					iStatus = OK;

		_dos_close( iFile );
	}

	if ( iStatus != OK )
		FatalError( FATAL_DATA_READ_ERROR );
}

/***************************************************************************/
/* Scans through the array of OEM DDR structures looking for a structure	*/
/* in which the OEM name field matchs the argument string. Returns a ptr	*/
/* to the first matching DDR structure or NULL if no match is found. 		*/
/* 																								*/
/* struct DDR *GetOemRecord( char *szOemName, int MajorVer, int MinorVer ) */
/* 																								*/
/* ARGUMENTS:	char *szOem 	-	Ptr to OEM name string							*/
/* 				MajorVer 		-	DOS major version number						*/
/* 				MinorVer 		-	DSO minor version number						*/
/* RETURNS: 	struct DDR *	-	Ptr to ddr struct for specifed OEM if the */
/* 										OEM is not found returns NULL ptr			*/
/* 																								*/
/***************************************************************************/

struct DDR *GetOemRecord( char *szOemName, int MajorVer, int MinorVer )
{
	register 	i;
	struct DDR	*Oem;

	Oem = NULL; 							/* Returns NULL if can't find match */

	for ( i = 0; i < (int)MaxOem; i++ )
	{
		if ( (int)OemList[ i ].MajorVer == MajorVer &&
			  (int)OemList[ i ].MinorVer == MinorVer &&
			  strcmpi( OemList[ i ].szOemName, szOemName ) == OK )
		{
			Oem = &(OemList[ i ]);
			break;
		}
	}

	return( Oem );
}

/***************************************************************************/
/* Scans through the relocation buffer and builds an array of pointers to	*/
/* to all of the strings in the specified class. The array is dynamically	*/
/* allocated after the total number of strings in the class is determined. */
/* An error check is done to be sure this function is only called once for */
/* any specified class of data															*/
/* 																								*/
/* char **GetClassData( char *InfoClass ) 											*/
/* 																								*/
/* ARGUMENTS:	InfoClass	- Ptr to data class string 							*/
/* RETURNS: 	char **		- Array of pointers to strings in this class 	*/
/* 								  or NULL if the class wasn't found             */
/* 																								*/
/***************************************************************************/

static char **GetClassData( char *InfoClass )
{
	register 	uFirst;					/* First entry for specified class	*/
	register 	uLast;					/* Last entry for specified class	*/
	char			**szStrings;			/* Array of ptrs to strings			*/
	int			i; 						/* Indice for array of ptrs			*/
	unsigned 	uArraySize;

	szStrings = NULL; 					/* Just in case class isn't found	*/

												/* Locate the specified class 		*/
	for ( uFirst = 0;
			uFirst < RELOC_LEN && RelocBuf[ uFirst ] != EOC_MARKER;
			uFirst++ )
		if ( strcmpi( (pchStringBuf + RelocBuf[ uFirst ]), InfoClass ) == OK )
			break;

	if ( RelocBuf[ uFirst ] != EOC_MARKER )	/* May not have found class*/
	{
		uFirst++;							/* Skip over class label */

												/* Locate end of this class */
		for ( uLast = uFirst;
				uLast < RELOC_LEN &&
				RelocBuf[ uLast ] != EOC_MARKER	&&
			  *(pchStringBuf + RelocBuf[ uLast ]) != '[';
				uLast++ )
			;
							/* Determine num class entries + end of array marker	*/
		uArraySize = (unsigned)(uLast - uFirst) + 1;

		if ( uArraySize > 1 )
		{
			szStrings = GetMemory( uArraySize * sizeof(char *) );

												/* Fill in array with ptrs to strings */
			for ( i = 0; uFirst < uLast; uFirst++, i++ )
				szStrings[ i ] = pchStringBuf + RelocBuf[ uFirst ];

			szStrings[ i ] = NULL;			/* Mark end of array with NULL ptr	*/
		}
	}

	return( szStrings );
}

/***************************************************************************/
/* Returns a ptr to a string of the specified type which is from the list	*/
/* of strings for that type. The argument Index specifies which string		*/
/* in the array for that type is returned. If the specified type is ivalid */
/* a NULL ptr will be returned but there is no error checking on the Index */
/* value but if the caller makes calls using a sequence of values the end	*/
/* end of the data for that type will be signaled when a NULL ptr is 		*/
/* returned.																					*/
/* 																								*/
/* Valid type are the enunerated values defined by "UpgradeData". 			*/
/* 																								*/
/* char *GetDataString( int Type, int Index )										*/
/* 																								*/
/* ARGUMENTS:	Type		- Specifies the data array to look in					*/
/* 				Index		- String in the list to be returned 					*/

⌨️ 快捷键说明

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