Esempio n. 1
0
// GetString():
//  Reads a null-terminated string from a binary file (meaning it stops
//   reading when it finds a null character).  Returns false if no
//   null was found before _len was reached or if the end of the buffer
//   was read past.
char * iBufferStream::GetString( char * string, int _len ) {
  if( is_error || IsEOB() ) {
    is_error = true;
    return NULL;
  }

  for( int i=0; i < _len; i++ ) {
    if( (pos + i) >= len ) {          // Check for the end of the buffer
      is_error = true;
      return NULL;
    }

    string[i] = buffer[pos + i];      // Store the char (including '\0')
    if( buffer[pos + i] == '\0' )     // Test a char
      break;
  }

  // See if we passed _len before finding a null char
  if( i == _len ) {
    is_error = true;
    return NULL;
  }

  pos += i + 1;  // Set pos past the '\0' (hence the +1)

  return string;
}
Esempio n. 2
0
// Get( char )
//  Reads a single char from the buffer.  Returns
//   false if an error occured (like hitting the eob).
bool iBufferStream::Get( char & byte ) {
  if( is_error || IsEOB() ) {
    is_error = true;
    return NULL;
  }

  byte = buffer[pos++];

  return true;
}
Esempio n. 3
0
// Get( char *, int )
//  Reads _len characters from the buffer into _buf.
//   Returns NULL if there aren't that many characters
//   to read, and a pointer to _buf if everything's OK.
char * iBufferStream::Get( char * _buf, int _len ) {
  if( is_error || IsEOB() ) {
    is_error = true;
    return NULL;
  }

  if( pos + _len > len ) {
    is_error = true;
    return NULL;
  }

  memcpy( _buf, &(buffer[ pos ]), _len );
  pos += _len;

  return _buf;
}
Esempio n. 4
0
//Inserts a character into the buffer.
void CConsole::InsertChar(const int szChar, const wchar_t cooked)
{
	static int iHistoryPos = -1;

	if (!m_bVisible) return;

	switch (szChar)
	{
	case SDLK_RETURN:
		iHistoryPos = -1;
		m_iMsgHistPos = 1;
		ProcessBuffer(m_szBuffer);
		FlushBuffer();
		return;

	case SDLK_TAB:
		// Auto Complete
		return;

	case SDLK_BACKSPACE:
		if (IsEmpty() || IsBOB()) return;

		if (m_iBufferPos == m_iBufferLength)
			m_szBuffer[m_iBufferPos - 1] = '\0';
		else
		{
			for (int j = m_iBufferPos-1; j < m_iBufferLength-1; j++)
				m_szBuffer[j] = m_szBuffer[j+1]; // move chars to left
			m_szBuffer[m_iBufferLength-1] = '\0';
		}

		m_iBufferPos--;
		m_iBufferLength--;
		return;

	case SDLK_DELETE:
		if (IsEmpty() || IsEOB()) return;

		if (m_iBufferPos == m_iBufferLength-1)
		{
			m_szBuffer[m_iBufferPos] = '\0';
			m_iBufferLength--;
		}
		else
		{
			if (g_keys[SDLK_RCTRL] || g_keys[SDLK_LCTRL])
			{
				// Make Ctrl-Delete delete up to end of line
				m_szBuffer[m_iBufferPos] = '\0';
				m_iBufferLength = m_iBufferPos;
			}
			else
			{
				// Delete just one char and move the others left
				for(int j=m_iBufferPos; j<m_iBufferLength-1; j++)
					m_szBuffer[j] = m_szBuffer[j+1];
				m_szBuffer[m_iBufferLength-1] = '\0';
				m_iBufferLength--;
			}
		}

		return;

	case SDLK_HOME:
		if (g_keys[SDLK_RCTRL] || g_keys[SDLK_LCTRL])
		{
			CScopeLock lock(m_Mutex); // needed for safe access to m_deqMsgHistory

			int linesShown = (int)m_fHeight/m_iFontHeight - 4;
			m_iMsgHistPos = clamp((int)m_deqMsgHistory.size() - linesShown, 1, (int)m_deqMsgHistory.size());
		}
		else
		{
			m_iBufferPos = 0;
		}
		return;

	case SDLK_END:
		if (g_keys[SDLK_RCTRL] || g_keys[SDLK_LCTRL])
		{
			m_iMsgHistPos = 1;
		}
		else
		{
			m_iBufferPos = m_iBufferLength;
		}
		return;

	case SDLK_LEFT:
		if (m_iBufferPos) m_iBufferPos--;
		return;

	case SDLK_RIGHT:
		if (m_iBufferPos != m_iBufferLength) m_iBufferPos++;
		return;

	// BEGIN: Buffer History Lookup
	case SDLK_UP:
		if (m_deqBufHistory.size() && iHistoryPos != (int)m_deqBufHistory.size() - 1)
		{
			iHistoryPos++;
			SetBuffer(m_deqBufHistory.at(iHistoryPos).c_str());
			m_iBufferPos = m_iBufferLength;
		}
		return;

	case SDLK_DOWN:
		if (m_deqBufHistory.size())
		{
			if (iHistoryPos > 0)
			{
				iHistoryPos--;
				SetBuffer(m_deqBufHistory.at(iHistoryPos).c_str());
				m_iBufferPos = m_iBufferLength;
			}
			else if (iHistoryPos == 0)
			{
				iHistoryPos--;
				FlushBuffer();
			}
		}
		return;
	// END: Buffer History Lookup

	// BEGIN: Message History Lookup
	case SDLK_PAGEUP:
	{
		CScopeLock lock(m_Mutex); // needed for safe access to m_deqMsgHistory

		if (m_iMsgHistPos != (int)m_deqMsgHistory.size()) m_iMsgHistPos++;
		return;
	}

	case SDLK_PAGEDOWN:
		if (m_iMsgHistPos != 1) m_iMsgHistPos--;
		return;
	// END: Message History Lookup

	default: //Insert a character
		if (IsFull()) return;
		if (cooked == 0) return;

		if (IsEOB()) //are we at the end of the buffer?
			m_szBuffer[m_iBufferPos] = cooked; //cat char onto end
		else
		{ //we need to insert
			int i;
			for(i=m_iBufferLength; i>m_iBufferPos; i--)
				m_szBuffer[i] = m_szBuffer[i-1]; // move chars to right
			m_szBuffer[i] = cooked;
		}

		m_iBufferPos++;
		m_iBufferLength++;

		return;
	}
}
Esempio n. 5
0
//Inserts a character into the buffer.
void CConsole::InsertChar(const int szChar, const wchar_t cooked )
{
	static int iHistoryPos = -1;

	if (!m_bVisible) return;

	switch (szChar){
		case SDLK_RETURN:
			iHistoryPos = -1;
			m_iMsgHistPos = 1;
			ProcessBuffer(m_szBuffer);
			FlushBuffer();
			return;

		case SDLK_TAB:
			// Auto Complete
			return;

		case SDLK_BACKSPACE:
			if (IsEmpty() || IsBOB()) return;

			if (m_iBufferPos == m_iBufferLength)
				m_szBuffer[m_iBufferPos - 1] = '\0';
			else{
				for(int j=m_iBufferPos-1; j<m_iBufferLength-1; j++)
					m_szBuffer[j] = m_szBuffer[j+1]; // move chars to left
				m_szBuffer[m_iBufferLength-1] = '\0';
			}

			m_iBufferPos--;
			m_iBufferLength--;
			return;

		case SDLK_DELETE:
			if (IsEmpty() || IsEOB()) return;

			if (m_iBufferPos == m_iBufferLength-1)
			{
				m_szBuffer[m_iBufferPos] = '\0';
				m_iBufferLength--;
			}
			else
			{
				if (g_keys[SDLK_RCTRL] || g_keys[SDLK_LCTRL])
				{
					// Make Ctrl-Delete delete up to end of line
					m_szBuffer[m_iBufferPos] = '\0';
					m_iBufferLength = m_iBufferPos;
				}
				else 
				{
					// Delete just one char and move the others left
					for(int j=m_iBufferPos; j<m_iBufferLength-1; j++)
						m_szBuffer[j] = m_szBuffer[j+1];
					m_szBuffer[m_iBufferLength-1] = '\0';
					m_iBufferLength--;
				}
			}

			return;

		case SDLK_HOME:
			if (g_keys[SDLK_RCTRL] || g_keys[SDLK_LCTRL])
			{
				CScopeLock lock(m_Mutex); // needed for safe access to m_deqMsgHistory

				int linesShown = (int)m_fHeight/m_iFontHeight - 4;
				m_iMsgHistPos = clamp((int)m_deqMsgHistory.size() - linesShown, 1, (int)m_deqMsgHistory.size());
			}
			else
			{
				m_iBufferPos = 0;
			}
			return;

		case SDLK_END:
			if (g_keys[SDLK_RCTRL] || g_keys[SDLK_LCTRL])
			{
				m_iMsgHistPos = 1;
			}
			else
			{
				m_iBufferPos = m_iBufferLength;
			}
			return;

		case SDLK_LEFT:
			if (m_iBufferPos) m_iBufferPos--;
			return;

		case SDLK_RIGHT:
			if (m_iBufferPos != m_iBufferLength) m_iBufferPos++;
			return;

		// BEGIN: Buffer History Lookup
		case SDLK_UP:
			if ( m_deqBufHistory.size() )
			{
				int oldHistoryPos = iHistoryPos;
				while( iHistoryPos != (int)m_deqBufHistory.size() - 1)
				{
					iHistoryPos++;
					std::wstring& histString = m_deqBufHistory.at(iHistoryPos);
					if((int)histString.length() >= m_iBufferPos)
					{
						bool bad = false;
						for(int i=0; i<m_iBufferPos; i++)
						{
							if(histString[i] != m_szBuffer[i])
							{
								bad = true; break;
							}
						}
						if(!bad)
						{
							SetBuffer(m_deqBufHistory.at(iHistoryPos).c_str());
							return;
						}
					}
				}
				// if we got here, failed to find a string with the right prefix;
				// revert to the old position in case the user types more stuff
				iHistoryPos = oldHistoryPos;
			}
			return;

		case SDLK_DOWN:
			if ( m_deqBufHistory.size() && iHistoryPos > 0 )
			{
				int oldHistoryPos = iHistoryPos;
				while( iHistoryPos != 0)
				{
					iHistoryPos--;
					std::wstring& histString = m_deqBufHistory.at(iHistoryPos);
					if((int)histString.length() >= m_iBufferPos)
					{
						bool bad = false;
						for(int i=0; i<m_iBufferPos; i++)
						{
							if(histString[i] != m_szBuffer[i])
							{
								bad = true; break;
							}
						}
						if(!bad)
						{
							SetBuffer(m_deqBufHistory.at(iHistoryPos).c_str());
							return;
						}
					}
				}
				// if we got here, failed to find a string with the right prefix;
				// revert to the old position in case the user types more stuff,
				// and also clear any complietion we might've added going up
				iHistoryPos = oldHistoryPos;
				m_szBuffer[m_iBufferPos] = 0;
				m_iBufferLength = m_iBufferPos;
			}
			return;
		// END: Buffer History Lookup

		// BEGIN: Message History Lookup
		case SDLK_PAGEUP:
		{
			CScopeLock lock(m_Mutex); // needed for safe access to m_deqMsgHistory

			if (m_iMsgHistPos != (int)m_deqMsgHistory.size()) m_iMsgHistPos++;
			return;
		}

		case SDLK_PAGEDOWN:
			if (m_iMsgHistPos != 1) m_iMsgHistPos--;
			return;
		// END: Message History Lookup

		default: //Insert a character
			if (IsFull()) return;
			if (cooked == 0) return;

			if (IsEOB()) //are we at the end of the buffer?
				m_szBuffer[m_iBufferPos] = cooked; //cat char onto end
			else{ //we need to insert
				int i;
				for(i=m_iBufferLength; i>m_iBufferPos; i--)
					m_szBuffer[i] = m_szBuffer[i-1]; // move chars to right
				m_szBuffer[i] = cooked;
			}

			m_iBufferPos++;
			m_iBufferLength++;

			return;
	}
}