readTextFile.c
上传用户:super_houu
上传日期:2008-09-21
资源大小:4099k
文件大小:20k
- /**************************************************************
- * readTextFile.c
- **************************************************************
- * Description:
- * ============
- * Functions for reading text file using caching.
- **************************************************************
- *
- * Hagayb 14.11.03
- **************************************************************/
- #include "Config.h" // Global Configuration - do not remove!
- #ifdef USE_AUX_SUBTITLES
- #ifdef _DEBUG
- #undef IFTRACE
- #define IFTRACE if (gTraceNavigator)
- #include "Debugdbgmain.h"
- #endif
- #include "PlaycoreAuxCacheAuxCache.h"
- #include "PlaycoreNav_ClipsreadTextFile.h"
- #ifdef DEBUG_AUX_SUBTITLES
- DWORD gen_timer(void);
- #endif
- extern DWORD g_dwSubtitleExtraOffset;
- BOOL textFileFillBuffer(textFilePtr tfpFile,DWORD dwOffset,WORD wReserveLast);
- /////////////////////////////////////////////////////////////////////////////////////////////
- // Function name : textFileOpen
- // Purpose : Open a text file for reading with buffering
- // Input Parameters : dwStartAddress - Address of the file on disc
- // dwFileSize - Size if the file
- // Return type : Handle to textFile on success, NULL on failure.
- // Description : The function allocates a textFile struct and sets up a buffer for reading
- /////////////////////////////////////////////////////////////////////////////////////////////
- textFilePtr textFileOpen(DWORD dwStartAddress, DWORD dwFileSize)
- {
- textFilePtr tfpFile;
- tfpFile=(textFilePtr)malloc(sizeof(textFile));
- if ( NULL == tfpFile ) {
- tr_printf(("textFileOpen - Error allocating memory for textFile structn"));
- return NULL;
- }
- tfpFile->caBuffer=(char *)malloc(sizeof(char)*TF_BUFFER_SIZE);
- if ( NULL == tfpFile->caBuffer ) {
- tr_printf(("textFileOpen - Error allocating memory for buffern"));
- free(tfpFile);
- return NULL;
- }
- tfpFile->dwStartAddress=dwStartAddress;
- tfpFile->dwFileSize=dwFileSize;
- tfpFile->dwCurrentDiscOffset = 0;
- tfpFile->pcCurrentBufferPosition = NULL;
- tfpFile->wCurrentBufferSize = 0;
- return tfpFile;
- }
- /////////////////////////////////////////////////////////////////////////////////////////////
- // Function name : textFileClose
- // Purpose : Closes a text file
- // Input Parameters : tfpFile - Handle to the file
- // Return type : void.
- // Description : The function deallocates the buffer and textFile struct for the file
- /////////////////////////////////////////////////////////////////////////////////////////////
- void textFileClose(textFilePtr tfpFile)
- {
- if ( NULL != tfpFile) {
- if ( NULL != tfpFile->caBuffer )
- free ( tfpFile->caBuffer );
- free ( tfpFile );
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////
- // Function name : textFileSeek
- // Purpose : Seeks to a position in the file
- // Input Parameters : tfpFile - Handle to the file
- // dwOffset - Offset in bytes, relative to the location specifed by bOrigin
- // bOrigin - Specifies the origin of the seek.
- // Can have the following values:
- // * TF_SEEK_SET - seek is relative to the file's beginning.
- // * TF_SEEK_CUR - seek is relative to the current position
- // * TF_SEEK_END - seek is relative to the file's end.
- // Return type : TRUE on success. FALSE on failure, or requested position isn't within the file
- // Description : The function checks is the requested position already exist in the buffer.
- // If not, it fills the buffer from that point. In both cases, it updates
- // the internal pointer pcCurrentBufferPosition to the requested position
- /////////////////////////////////////////////////////////////////////////////////////////////
- BOOL textFileSeek(textFilePtr tfpFile, long dwOffset, BYTE bOrigin)
- {
- DWORD dwRequestedPos;
- if ( NULL == tfpFile) {
- tr_printf(("textFileSeek - NULL pointer was suppliedn"));
- return FALSE;
- }
-
- // determine the requested offest in the file.
- switch (bOrigin)
- {
- case TF_SEEK_SET:
- dwRequestedPos = dwOffset;
- break;
- case TF_SEEK_CUR:
- if ( NULL != tfpFile->pcCurrentBufferPosition )
- dwRequestedPos = tfpFile->dwCurrentDiscOffset + ( tfpFile->pcCurrentBufferPosition - tfpFile->caBuffer ) + dwOffset;
- else {
- // either we were on EOF, or the buffer was empty. In both cases, it means
- // the current position is the byte after the end of the buffer.
- dwRequestedPos = tfpFile->dwCurrentDiscOffset + tfpFile->wCurrentBufferSize + dwOffset;
- }
- break;
- case TF_SEEK_END:
- // the end is considered to be the last byte of the file.
- dwRequestedPos=tfpFile->dwFileSize-1+dwOffset;
- break;
- default:
- tr_printf(("textFileSeek - unknown bOrigin valuen"));
- return FALSE;
- }
- if (dwRequestedPos < 0 || dwRequestedPos >=tfpFile->dwFileSize) {
- tr_printf(("textFileSeek - Requested postion outside of filen"));
- return FALSE;
- }
- // check if the requested position is in the buffer
- if ( 0 != tfpFile->wCurrentBufferSize && dwRequestedPos>=tfpFile->dwCurrentDiscOffset &&
- dwRequestedPos - tfpFile->dwCurrentDiscOffset < tfpFile->wCurrentBufferSize )
- {
- // the requested position is in the buffer
- tfpFile->pcCurrentBufferPosition = tfpFile->caBuffer+dwRequestedPos - tfpFile->dwCurrentDiscOffset;
- return TRUE;
- }
- else {
- // it isn't, so fill the buffer fron its beginning, starting from the requested position
- return textFileFillBuffer(tfpFile,dwRequestedPos,0);
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////
- // Function name : textFileEOF
- // Purpose : Check if end of file was reached
- // Input Parameters : tfpFile - Handle to the file
- // Return type : TRUE if end of file was reached, FALSE otherwise
- // Description : Check if pcCurrentBufferPosition is NULL, and dwCurrentDiscOffset +
- // wCurrentBufferSize points outside of the file
- /////////////////////////////////////////////////////////////////////////////////////////////
- BOOL textFileEOF(textFilePtr tfpFile)
- {
- return (tfpFile->dwCurrentDiscOffset + tfpFile->wCurrentBufferSize == tfpFile->dwFileSize
- && NULL == tfpFile->pcCurrentBufferPosition);
- }
- /////////////////////////////////////////////////////////////////////////////////////////////
- // Function name : textFileReadChar
- // Purpose : Reads a character (single byte) from the file.
- // Input Parameters : tfpFile - Handle to the file
- // wReserveLast - If rewinding is expected after this operation,
- // wReserveLast is the number of bytes for the possible rewind
- // (for buffering purposes only)
- // Output Parameters: c - The character that was read
- // Return type : TRUE if successful, FALSE if failed, or already on end of file
- // Description : Fills the buffer if necessary, and reads a character.
- /////////////////////////////////////////////////////////////////////////////////////////////
- BOOL textFileReadChar(textFilePtr tfpFile,WORD wReserveLast, char *c )
- {
- if (textFileEOF(tfpFile))
- return FALSE;
- // check if the buffer is "empty" - doesn't contain the data of the next char
- if ( NULL == tfpFile->pcCurrentBufferPosition ) {
- // fill the buffer and reserve 1 byte, in case someone wants to rewind
- if (!textFileFillBuffer(tfpFile, tfpFile->dwCurrentDiscOffset + tfpFile->wCurrentBufferSize, wReserveLast))
- {
- tr_printf(("textFileReadChar - textFileFillBuffer failedn"));
- return FALSE;
- }
- }
- *c=*(tfpFile->pcCurrentBufferPosition);
-
- // if 0x00 in the middle of the file, deal with it as space
- if (*c == 0x00)
- *c = 0x20;
-
- tfpFile->pcCurrentBufferPosition++;
- // check if the buffer becomes "empty"
- if ( tfpFile->pcCurrentBufferPosition - tfpFile->caBuffer >= tfpFile->wCurrentBufferSize )
- tfpFile->pcCurrentBufferPosition=NULL;
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////////////////////
- // Function name : textFileGetPosition
- // Purpose : Get the current poision in the file.
- // Input Parameters : tfpFile - Handle to the file
- // Return type : DWORD specifying the position. This value can be used in textFileSeek
- // together with TF_SEEK_SET to reach this position again.
- // Description : calculates the offset on disc of the current character in the buffer.
- /////////////////////////////////////////////////////////////////////////////////////////////
- DWORD textFileGetPosition(textFilePtr tfpFile)
- {
- if ( NULL == tfpFile->pcCurrentBufferPosition )
- return tfpFile->dwCurrentDiscOffset + tfpFile->wCurrentBufferSize;
- else
- return tfpFile->dwCurrentDiscOffset + ( tfpFile->pcCurrentBufferPosition - tfpFile->caBuffer );
- }
- /////////////////////////////////////////////////////////////////////////////////////////////
- // Function name : textFileFindString
- // Purpose : Searches for the specified string from het current position, in the
- // specified number of bytes.
- // Input Parameters : tfpFile - Handle to the file
- // szStr - String (null terminated, single byte) to look for.
- // dwRegionSize - a limit to the number of bytes in the file the search
- // will cover. Specify 0 for search until end of file.
- // Return type : TRUE is string was found, else otherwise.
- // The current position in the file will be the first character in the string
- // Description :
- /////////////////////////////////////////////////////////////////////////////////////////////
- BOOL textFileFindString(textFilePtr tfpFile, const char *szStr, DWORD dwRegionSize)
- {
- int len,i,j;
- char *cpWindow,*cpTmp;
- BOOL bSuccess=FALSE;
- len=strlen(szStr);
- i=0;
- cpWindow=(char *)malloc(len);
- if ( NULL == cpWindow) {
- tr_printf(("textFileFindString - error allocating memoryn"));
- return FALSE;
- }
- while ( !textFileEOF(tfpFile) && (i<dwRegionSize || dwRegionSize==0) ) {
- // shift the window, to make room for a new char in the end
- for (j=0,cpTmp=cpWindow; j<len-1 ; j++, cpTmp++)
- *cpTmp=*(cpTmp+1);
-
- // read char into the window's end
- if (FALSE == textFileReadChar(tfpFile,len,cpTmp)) {
- tr_printf(("textFileFindString - textFileReadChar failedn"));
- break;
- }
- // if the window is full, compare the window and the string
- if (i>=len-1) {
- if ( 0 == strnicmp(szStr, cpWindow, len)) {
- bSuccess=TRUE;
- free(cpWindow);
- return bSuccess;
- }
- }
- i++;
- }
- free(cpWindow);
- return bSuccess;
- }
- /////////////////////////////////////////////////////////////////////////////////////////////
- // Function name : textFileFindNumber
- // Purpose : Finds the next digit in the file, if exists in the next specified number of bytes.
- // Input Parameters : tfpFile - Handle to the file
- // dwRegionSize - a limit to the number of bytes in the file the search
- // will cover. Specify 0 for search until end of file.
- // Return type : TRUE is a digit was found within the region, else otherwise.
- // The current position in the file will be the digit.
- // Description : The function looks of the next digit, starting from the current position
- // for dwRegionSize bytes. If there was already a digit in the current position,
- // it does nothing.
- /////////////////////////////////////////////////////////////////////////////////////////////
- BOOL textFileFindNumber(textFilePtr tfpFile,DWORD dwRegionSize)
- {
- int i=0;
- BOOL bSuccess=FALSE;
- char c;
- while ( !textFileEOF(tfpFile) && ( i<dwRegionSize || dwRegionSize == 0 )) {
- // read char into the window's end
- if (FALSE == textFileReadChar(tfpFile,1,&c)) {
- tr_printf(("textFileFindNumber - textFileReadChar failedn"));
- return FALSE;
- }
- if ( c >= '0' && c <= '9' ) {
- if (FALSE == textFileSeek(tfpFile,-1,TF_SEEK_CUR)) {
- tr_printf(("textFileFindNumber - error seeking after digit foundn"));
- return FALSE;
- }
- bSuccess=TRUE;
- break;
- }
- i++;
- }
- return bSuccess;
- }
- /////////////////////////////////////////////////////////////////////////////////////////////
- // Function name : textFileReadNumber
- // Purpose : Reads a number from the current position in the file.
- // Input Parameters : tfpFile - Handle to the file
- // Output Parameters: uiNumber - The number that was read
- // Return type : TRUE if successful. FALSE if the was no number in the current position,
- // if already on EOF, or on error.
- // Description : The function checks if there's a digit in the current position. If there
- // is, it reads until (not including) the 1st non digit character and
- // converts the digit sequence to a number.
- /////////////////////////////////////////////////////////////////////////////////////////////
- BOOL textFileReadNumber(textFilePtr tfpFile, DWORD *uiNumber)
- {
- char c;
- *uiNumber=0;
- if (TRUE == textFileEOF(tfpFile))
- return FALSE;
-
- if (FALSE == textFileReadChar(tfpFile,1,&c)) {
- tr_printf(("textFileReadNumber - textFileReadChar failedn"));
- return FALSE;
- }
- // Robin_0328_2005, deal with the writing mistake
- if (c == 'O' || c == 'o')
- {
- c = '0';
- }
-
- if (!( c >= '0' && c <= '9' )) {
- if (FALSE == textFileSeek(tfpFile,-1,TF_SEEK_CUR))
- tr_printf(("textFileFindNumber - error seeking after digit not foundn"));
-
- return FALSE;
- }
- do {
- // add the previously read digit to the number
- *uiNumber= 10 * (*uiNumber) + (c - '0');
-
- // read the next digit
- if (FALSE == textFileReadChar(tfpFile,1,&c)) {
- tr_printf(("textFileReadNumber - textFileReadChar failedn"));
- return FALSE;
- }
- } while (( c >= '0' && c <= '9' ) && (FALSE == textFileEOF(tfpFile)));
- if (FALSE == textFileSeek(tfpFile,-1,TF_SEEK_CUR)) {
- tr_printf(("textFileFindNumber - error seeking after digit not foundn"));
- return FALSE;
- }
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////////////////////
- // Function name : textFileSkipLine
- // Purpose : Find the beginning of the next line
- // Input Parameters : tfpFile - Handle to the file
- // Return type : TRUE if successful. FALSE if EOF encountered before 'n',
- // or on error.
- // Description : The function searches for the next 'n' and sets the current position
- // right after it.
- /////////////////////////////////////////////////////////////////////////////////////////////
- BOOL textFileSkipLine(textFilePtr tfpFile)
- {
- char c=0;
- while ( !textFileEOF(tfpFile) && 'n' != c ) {
-
- if (FALSE == textFileReadChar(tfpFile,1,&c)) {
- tr_printf(("textFileSkipLine - textFileReadChar failedn"));
- return FALSE;
- }
- }
- // return false if EOF was reached before 'n' was found
- return ( 'n' == c );
- }
- // ******************************************************************************************
- // Private functions
- // ******************************************************************************************
- /////////////////////////////////////////////////////////////////////////////////////////////
- // Function name : textFileFillBuffer
- // Purpose : Fills the buffer
- // Input Parameters : tfpFile - Handle to the file
- // dwOffset - Position in the file, from which the data would be read.
- // wReserveLast - Number of bytes to keep from the end of the previous buffer.
- // used when it's likely that a file seek will want to access
- // these bytes. If the buffer was empty - ignore this value
- // Return type : TRUE if successful. FALSE on error.
- // Description : The function searches for the next 'n' and sets the current position
- // right after it.
- /////////////////////////////////////////////////////////////////////////////////////////////
- BOOL textFileFillBuffer(textFilePtr tfpFile,DWORD dwOffset,WORD wReserveLast)
- {
- WORD wNewBufferSize;
- BYTE bReadExtraBytes=0;
- #ifdef DEBUG_AUX_SUBTITLES
- static DWORD dwTotal=0;
- DWORD dwStart;
- DWORD dwEnd;
- #endif
- // check if requested offset is valid
- if (dwOffset >= tfpFile->dwFileSize || dwOffset < 0) {
- tr_printf(("textFileFillBuffer - requested read position outside of filen"));
- return FALSE;
- }
- /*
- // if the buffer was empty - ignore wReserveLast
- if (0 == tfpFile->wCurrentBufferSize)
- wReserveLast=0;
- // make sure that the size for reading is even
- if ( wReserveLast & 1)
- wReserveLast++;
- // check if number of reserved bytes is valid
- if (wReserveLast >= TF_BUFFER_SIZE) {
- tr_printf(("textFileFillBuffer - number of reserved bytes >= buffer sizen"));
- return FALSE;
- }
- // if specified, copy reserved bytes to the beginning of buffer
- if ( 0 != wReserveLast ) {
- //if (wReserveLast<=TF_BUFFER_SIZE_HALF) {
- // the source and destination regions don't overlap - use memcpy
- memcpy(tfpFile->caBuffer , tfpFile->caBuffer + tfpFile->wCurrentBufferSize - wReserveLast, wReserveLast);
- }
- }
- */
- // calculate new buffer size
- if ( tfpFile->dwFileSize - dwOffset < TF_BUFFER_SIZE )
- wNewBufferSize = tfpFile->dwFileSize - dwOffset;
- else
- wNewBufferSize = TF_BUFFER_SIZE;
- // the new buffer size can be odd only if it's the end of file. The extra
- // byte will still fall inside the buffer.
- if (wNewBufferSize & 1)
- bReadExtraBytes=1;
- #ifdef DEBUG_AUX_SUBTITLES
- dwStart = gen_timer();
- if (dwOffset == 0 )
- dwTotal = 0;
- #endif
- // Reading - fill only the non reserved part
- // Robin_0117_2005, support AVI internal text subtitle
- if (!AuxCache_GetBytes(tfpFile->dwStartAddress, dwOffset + g_dwSubtitleExtraOffset, wNewBufferSize /* - wReserveLast*/ + bReadExtraBytes, tfpFile->caBuffer /*+ wReserveLast*/))
- {
- tr_printf(("textFileFillBuffer - failed reading from discn"));
- return FALSE;
- }
- #ifdef DEBUG_AUX_SUBTITLES
- dwEnd = gen_timer();
- dwTotal += ((dwEnd > dwStart ) ? dwEnd - dwStart : 0xffffffffL - dwStart + dwEnd) ;
- if ( wNewBufferSize != TF_BUFFER_SIZE )
- dbg_printf(("Total time for AuxCache_GetBytes calls - %lun",dwTotal));
- #endif
- tfpFile->dwCurrentDiscOffset = dwOffset/*-wReserveLast*/;
- tfpFile->wCurrentBufferSize = wNewBufferSize;
- tfpFile->pcCurrentBufferPosition = tfpFile->caBuffer /*+ wReserveLast */;
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////////////////////
- // Function name : textFileIsCurrentChar
- // Purpose : Asserts that the current character in the file is the specified char
- // Input Parameters : tfpFile - Handle to the file
- // c - character to be tested.
- // Return type : TRUE if character is correct. FALSE otherwise, and on error
- // Description : The function reads a character from the file and compares it to the
- // specified character. This function should be used to check correctness
- // of file format.
- /////////////////////////////////////////////////////////////////////////////////////////////
- BOOL textFileIsCurrentChar(textFilePtr tfpFile,char c)
- {
- char b;
- if (FALSE == textFileReadChar(tfpFile,0,&b)) {
- //tr_printf(("textFileIsCurrentChar - can't read charn"));
- return FALSE;
- }
- c = ((c <= 'z' ) && (c >= 'a')) ? (c-32) : c;
- b = ((b <= 'z' ) && (b >= 'a')) ? (b-32) : b;
- return (b == c);
- }
- // Robin_0604_2004_C
- BOOL textFileIsCurrentChar2(textFilePtr tfpFile,char c)
- {
- char b;
- if (FALSE == textFileReadChar(tfpFile,0,&b)) {
- //tr_printf(("textFileIsCurrentChar - can't read charn"));
- return FALSE;
- }
- c = ((c <= 'z' ) && (c >= 'a')) ? (c-32) : c;
- b = ((b <= 'z' ) && (b >= 'a')) ? (b-32) : b;
- if (b == c)
- {
- return TRUE;
- }
- else
- {
- if (NULL == tfpFile->pcCurrentBufferPosition)
- tfpFile->pcCurrentBufferPosition = tfpFile->caBuffer + tfpFile->wCurrentBufferSize - 1;
- else
- tfpFile->pcCurrentBufferPosition--;
- return FALSE;
- }
- }
- #endif // USE_AUX_SUBTITLES