Example #1
0
	CString MakeIvec() {
		CString sRet;
		time_t t;
		time(&t);
		int r = rand();
		sRet.append((char*) &t, 4);
		sRet.append((char*) &r, 4);

		return sRet;
	}
Example #2
0
bool CFile::ReadFile(CString& sData, size_t iMaxSize) {
	char buff[4096];
	size_t iBytesRead = 0;

	sData.clear();

	while (iBytesRead < iMaxSize) {
		ssize_t iBytes = Read(buff, sizeof(buff));

		if (iBytes < 0)
			// Error
			return false;

		if (iBytes == 0)
			// EOF
			return true;

		sData.append(buff, iBytes);
		iBytesRead += iBytes;
	}

	// Buffer limit reached
	return false;
}
Example #3
0
int RageFileObj::Read( CString &sBuffer, int iBytes )
{
	sBuffer.reserve( iBytes != -1? iBytes: this->GetFileSize() );

	int iRet = 0;
	char buf[4096];
	while( iBytes == -1 || iRet < iBytes )
	{
		int ToRead = sizeof(buf);
		if( iBytes != -1 )
			ToRead  = min( ToRead, iBytes-iRet );

		const int iGot = Read( buf, ToRead );
		if( iGot == 0 )
			break;
		if( iGot == -1 )
			return -1;

		sBuffer.append( buf, iGot );
		iRet += iGot;
	}

	return iRet;
}
Example #4
0
/* Read up to the next \n, and return it in out.  Strip the \n.  If the \n is
 * preceded by a \r (DOS newline), strip that, too. */
int RageFileObj::GetLine( CString &out )
{
	out = "";

	if( m_bEOF )
		return 0;

	EnableReadBuffering();

	bool GotData = false;
	while( 1 )
	{
		bool done = false;

		/* Find the end of the block we'll move to out. */
		char *p = (char *) memchr( m_pReadBuf, '\n', m_iReadBufAvail );
		bool ReAddCR = false;
		if( p == NULL )
		{
			/* Hack: If the last character of the buffer is \r, then it's likely that an
			 * \r\n has been split across buffers.  Move everything else, then move the
			 * \r to the beginning of the buffer and handle it the next time around the loop. */
			if( m_iReadBufAvail && m_pReadBuf[m_iReadBufAvail-1] == '\r' )
			{
				ReAddCR = true;
				--m_iReadBufAvail;
			}

			p = m_pReadBuf+m_iReadBufAvail; /* everything */
		}
		else
			done = true;

		if( p >= m_pReadBuf )
		{
			char *RealEnd = p;
			if( done && p > m_pReadBuf && p[-1] == '\r' )
				--RealEnd; /* not including \r */
			out.append( m_pReadBuf, RealEnd );

			if( done )
				++p; /* skip \n */

			const int used = p-m_pReadBuf;
			if( used )
			{
				m_iReadBufAvail -= used;
				m_iFilePos += used;
				GotData = true;
				m_pReadBuf = p;
			}
		}

		if( ReAddCR )
		{
			ASSERT( m_iReadBufAvail == 0 );
			m_pReadBuf = m_pReadBuffer;
			m_pReadBuffer[m_iReadBufAvail] = '\r';
			++m_iReadBufAvail;
		}

		if( done )
			break;

		/* We need more data. */
		m_pReadBuf = m_pReadBuffer;

		const int size = FillReadBuf();

		/* If we've read data already, then don't mark EOF yet.  Wait until the
		 * next time we're called. */
		if( size == 0 && !GotData )
		{
			m_bEOF = true;
			return 0;
		}
		if( size == -1 )
			return -1; // error
		if( size == 0 )
			break; // EOF or error
	}
	return GotData? 1:0;
}