Exemple #1
0
bool CFile::Duplicate(const std::string& newFileName)
{
  if (IsOpen() == false)
  {
    return false;
  }

  if (m_fileName.compare(newFileName) == 0)
  {
    return false;
  }

  Flush();

  long curPos = GetPosition();

  SeekToBegin();

  long  dupLength = GetLength();

  CFile dupFile(newFileName, modeWrite);

  if( dupFile.IsOpen() == false)
  {
    return false;
  }

  char *buf = new char[dupLength];

  if (buf == NULL)
  {
    return false;
  }

  uint32_t written = 0;

  if (ReadToBuffer(buf, dupLength) != 0)
  {
    written = dupFile.WriteFromBuffer(buf, dupLength);
    dupFile.Destroy();
  }

  delete []buf;
  buf = NULL;

  SeekToBegin();
  SetPosition(curPos);

  return (written != 0);
}
Exemple #2
0
unsigned long ArchExecutor::MakeCrc(FILE* file)
{
	int nextByte;
	unsigned long crc = 0L;
	byte buffer[CRC_BUFFER_SIZE];
	int bufferLength;

	long lastPosition = GetLastFilePosition(file);

	while((bufferLength = ReadToBuffer(buffer, file)) > 0)
	{
		//TODO: если длина файла больше long, то нужна друга¤ проверка.
		if (ftell(file) == lastPosition)
		{
			bufferLength -= CRC_SIZE_BYTES + FILE_SIZE_BYTES;
		}

		crc = _crcMaker.UpdateCrc(crc, buffer, bufferLength);
	}

	rewind(file);

	return crc;
}
GDALAsyncStatusType 
ECWAsyncReader::GetNextUpdatedRegion( double dfTimeout,
                                      int* pnXBufOff, int* pnYBufOff,
                                      int* pnXBufSize, int* pnYBufSize )

{
    CPLDebug( "ECW", "GetNextUpdatedRegion()" );

/* -------------------------------------------------------------------- */
/*      We always mark the whole raster as updated since the ECW SDK    */
/*      does not have a concept of partial update notifications.        */
/* -------------------------------------------------------------------- */
    *pnXBufOff = 0;
    *pnYBufOff = 0;
    *pnXBufSize = nBufXSize;
    *pnYBufSize = nBufYSize;

    if( bComplete && !bUpdateReady )
    {
        CPLDebug( "ECW", "return GARIO_COMPLETE" );
        return GARIO_COMPLETE;
    }
        
/* -------------------------------------------------------------------- */
/*      Wait till our timeout, or until we are notified there is        */
/*      data ready.  We are trusting the CPLSleep() to be pretty        */
/*      accurate instead of keeping track of time elapsed ourselves     */
/*      - this is not necessarily a good approach.                      */
/* -------------------------------------------------------------------- */
    if( dfTimeout < 0.0 )
        dfTimeout = 100000.0;
    
    while( !bUpdateReady && dfTimeout > 0.0 )
    {
        CPLSleep( MIN(0.1, dfTimeout) );
        dfTimeout -= 0.1;
        CPLDebug( "ECW", "wait..." );
    }

    if( !bUpdateReady )
    {
        CPLDebug( "ECW", "return GARIO_PENDING" );
        return GARIO_PENDING;
    }

    bUpdateReady = FALSE;

/* -------------------------------------------------------------------- */
/*      Acquire Mutex                                                   */
/* -------------------------------------------------------------------- */
    if( !CPLAcquireMutex( hMutex, dfTimeout ) )
    {
        CPLDebug( "ECW", "return GARIO_PENDING" );
        return GARIO_PENDING;
    }

/* -------------------------------------------------------------------- */
/*      Actually decode the imagery into our buffer.                    */
/* -------------------------------------------------------------------- */
    NCSEcwReadStatus  eRStatus = ReadToBuffer();

    if( eRStatus != NCSECW_READ_OK )
    {
        CPLReleaseMutex( hMutex );
        return GARIO_ERROR;
    }

/* -------------------------------------------------------------------- */
/*      Return indication of complete or just buffer updateded.         */
/* -------------------------------------------------------------------- */

    if( bComplete && !bUpdateReady )
    {
        CPLReleaseMutex( hMutex );
        CPLDebug( "ECW", "return GARIO_COMPLETE" );
        return GARIO_COMPLETE;
    }
    else
    {
        CPLReleaseMutex( hMutex );
        CPLDebug( "ECW", "return GARIO_UPDATE" );
        return GARIO_UPDATE;
    }
}
Exemple #4
0
main (int argc, char * argv[])
{
    FILE        * File1, * File2 ;          // the input file and output file of sorting
    long        BufferRecords    = 0;       // number of records in buffer
    long        error            = 0;       // error flag
    long        RecordNum1       = 0;       // total record number in file1
    long        RecordNum2       = 0;       // total record number in file2
    unsigned long CheckSum1      = 0;       // checksum of file1
    unsigned long CheckSum2      = 0;       // checksum of file2
    char        LastRecord[RecordSize];     // a copy of the last record in the buffer
    bool        OrderSign        = true;    // sign of whether all the records in the file have the desired order 

    /************************************************************************
    *                                                                       *
    *       Put out greeting                                                *
    *                                                                       *
    ************************************************************************/
    fprintf(stderr,"ChkSort (%s) - Verify your PennySort result with its checksum and order.", VERSION);
    fprintf(stderr,"\n    Author: Peng Liu, Tsinghua University, China.  [email protected]  12/2002\n");

    /*********************************************************************
    *                                                                    *
    *       Decode parameters                                            *
    *                                                                    *
    *********************************************************************/
    if (argc != 3)
    { usage(); error = 1; goto common_exit; }
    FileName1=argv[1];
    FileName2=argv[2];

    /************************************************************************/
    /*                                                                      */
    /*      Allocate file buffer                                            */
    /*                                                                      */
    /************************************************************************/
    Buffer = (char *)malloc( BufferSize ) ;
    if( Buffer  == NULL  )
    {
        fprintf(stderr,"\nCouldn't allocate the I/O buffer\n");
        error = 1;  goto common_exit;
    }

    /************************************************************************/
    /*                                                                      */
    /*      Calculate checksum of <filename1>                               */
    /*                                                                      */
    /************************************************************************/

    fprintf(stderr,"\nAnalysing file: %s \n", FileName1);

    // open <filename1>
    File1 = fopen( FileName1, "r+b");
    if( File1 == NULL )
    {
        fprintf(stderr,"\nError opening file %s\n", FileName1) ;
        error = 1;  goto common_exit;
    }

    // check whether the record length in <filename1> is exactly 100 bytes long
    if (CheckRecordLen(File1, FileName1)==false)
    {
        error = 1;  goto common_exit;
    }

    // count the total number of records and calculate its checksum
    RecordNum1=0;
    while ((BufferRecords=ReadToBuffer(File1)) !=0)
    {
        RecordNum1 += BufferRecords;
        CheckSum1 += GetCheckSum(BufferRecords);
    }
    fclose(File1);

    // tell the user about its record number and checksum
    fprintf(stderr, "\nRecord Number: %d  Checksum: %lX\n",RecordNum1, CheckSum1);

    /************************************************************************/
    /*                                                                      */
    /*      Calculate checksum and check order of <filename2>               */
    /*                                                                      */
    /************************************************************************/

    fprintf(stderr,"\n\nAnalysing file: %s \n", FileName2);

    // open <filename2>
    File2 = fopen( FileName2, "r+b");
    if( File2 == NULL )
    {
        fprintf(stderr,"\nError opening file %s\n", FileName2) ;
        error = 1;  goto common_exit;
    }

    // check whether the record length in <filename2> is exactly 100 bytes long
    if (CheckRecordLen(File2, FileName2)==false)
    {
        error = 1;  goto common_exit;
    }

    // count the number of records and checksum of <filename2>
    // and check the order of records in <filename2> one by one
    RecordNum2=0;
    LastRecord[0]=0;
    while ((BufferRecords=ReadToBuffer(File2)) !=0)
    {
        OrderSign=VerifyOrder(LastRecord, BufferRecords);
        // Compare the last record of previous buffer content with the first record of this one
        if (!OrderSign)
            break;
        RecordNum2 += BufferRecords;
        CheckSum2 += GetCheckSum(BufferRecords);
    }
    fclose(File2);
    if (!OrderSign)     // if the order of the records is not correct
        goto common_exit;

    // tell the user about its record number and checksum
    fprintf(stderr, "\nRecord Number: %d  Checksum: %lX\n",RecordNum2, CheckSum2);

    /************************************************************************/
    /*                                                                      */
    /*      Put out result message of comparing checksum and order          */
    /*                                                                      */
    /************************************************************************/

    if (CheckSum1==CheckSum2)
        fprintf(stderr, "\n\nCongratulations: Verification Passed!\n");
    else
        fprintf(stderr, "\n\nBad News: Verification failed due to different checksums \n(although order of records are correct)!\n");

    /************************************************************************/
    /*                                                                      */
    /*      All Done                                                        */
    /*      close output file; and exit                                     */
    /*                                                                      */
    /************************************************************************/

common_exit:
    free(Buffer);                       // free record buffer
    return(error);                      // return status code
}                                       // End of ChkSort.cpp
Exemple #5
0
//----------------------------------------
int32_t CFile::ReadLine( char *lineRead, uint32_t size )
{
	//char line[CFile::m_maxBufferToRead];
	char * line = NULL;
	char * buff = new char[ CFile::m_maxBufferToRead ];
	char *current = NULL;
	uint32_t bytesRead = 0;
	char * last = NULL;
	uint32_t lenToCopy = 0;

	// if start of file (first read) then delete and reallocate m_buffer member

	do
	{

		if ( m_buffer != NULL )
		{
			memset( buff, '\0', CFile::m_maxBufferToRead );
			strcpy( buff, m_buffer );
			delete[]m_buffer;
			m_buffer = NULL;

			bytesRead = strlen( buff );

			//printf("bytesRead from m_buffer %d buff %s\n", bytesRead, buff);
		}
		else
		{
			memset( buff, '\0', CFile::m_maxBufferToRead );
			bytesRead = ReadToBuffer( buff, CFile::m_maxBufferToRead - 1 );
			//printf("bytesRead %d buff %s\n", bytesRead, buff);

		}

		if ( bytesRead == 0 )
		{
			//---------------------------------
			break;  // exit do - End of file
			//---------------------------------
		}

		last = ( buff + strlen( buff ) - 1 );

		current = strpbrk( ( buff ), "\r\n" );

		if ( current == NULL )
		{
			// Get buffer and read next
			char * linePrev = line;
			if ( linePrev != NULL )
			{
				line = new char[ strlen( linePrev ) + strlen( buff ) + 1 ];
				strcpy( line, linePrev );
				strcat( line, buff );

				delete[]linePrev;
				linePrev = NULL;
			}
			else
			{
				line = new char[ strlen( buff ) + 1 ];
				strcpy( line, buff );
			}

		}
		else  //(current != NULL)
		{
			if ( *current == '\r' )
			{
				*current = '\0';
				// if current is the last character :
				// - read next character
				// - if next is not '\n', skip it
				if ( current == last )
				{
					char c1[ 1 ];
					int32_t nRead = ReadToBuffer( c1, 1 );

					//printf(" 2nd - bytesRead \n");

					if ( nRead > 0 )
					{
						if ( *c1 != '\n' )
						{
							SetPosition( -( nRead ) ); // rewind the nRead bytes read
						}
					}
				}
				else if ( *( current + 1 ) == '\n' ) // (current != last), if the next character of current is '\n', skip it
				{
					current++;
					*current = '\0';
				}
			}
			else //(*current != '\r') ==> (*current == '\n')
			{
				*current = '\0';
			}

			//Get buffer  from start to character before current.
			lenToCopy = uint32_t( current - buff + 1 );

			char * linePrev = line;

			if ( linePrev != NULL )
			{
				line = new char[ strlen( linePrev ) + lenToCopy ];
				strcpy( line, linePrev );
				strcat( line, buff );

				delete[]linePrev;
				linePrev = NULL;
			}
			else
			{
				line = new char[ lenToCopy ];
				strcpy( line, buff );
			}

			if ( current != NULL )
			{
				if ( current != last )
				{
					current++;
				}
				else
				{
					current = NULL;
				}

			}

			if ( current != NULL )
			{
				if ( m_buffer == NULL )
				{
					m_buffer = new char[ CFile::m_maxBufferToRead ];
				}
				lenToCopy = ( strlen( current ) > m_maxBufferToRead ? m_maxBufferToRead - 1 : strlen( current ) );
				memset( m_buffer, '\0', CFile::m_maxBufferToRead );
				strncpy( m_buffer, current, lenToCopy );

			}


			//---------------------------------
			break; //a complete line is read
			//---------------------------------

		} //else (current != NULL)

	} while ( bytesRead > 0 );


	memset( lineRead, '\0', size );

	if ( line == NULL )
	{
		delete[]buff;
		buff = NULL;
		return -1;
	}

	//printf("Line read Start:%s:End\n", line);

	lenToCopy = strlen( line );


	if ( size <= lenToCopy )
	{
		std::cout << "CFile::ReadLine - line size " << lenToCopy << " - buffer size too small - line truncated";
	}

	size = strlen( line ) + 1;
	strncpy( lineRead, line, lenToCopy );

	delete[]buff;
	buff = NULL;
	delete[]line;
	line = NULL;
	//  int32_t result = (int32_t(bytesRead) == 0 ?  -1 : int32_t(size));
	//return result;

	return size;

}