Beispiel #1
0
static int GM_CDECL gmfStringMid(gmThread * a_thread)
{
  GM_CHECK_NUM_PARAMS(2);

  int first = 0, count = 0;

  if(!gmGetFloatOrIntParamAsInt(a_thread, 0, first)) {return GM_EXCEPTION;}
  if(!gmGetFloatOrIntParamAsInt(a_thread, 1, count)) {return GM_EXCEPTION;}

  const gmVariable * var = a_thread->GetThis();
  
  GM_ASSERT(var->m_type == GM_STRING);

  gmStringObject * strObj = (gmStringObject *) GM_OBJECT(var->m_value.m_ref);
  const char * str = (const char *) *strObj;
  
  int length = strObj->GetLength();

  //Check bounds
  if (first < 0)
  {
    first = 0;
  }
  if (count < 0)
  {
    count = 0;
  }
  if (first + count > length)
  {
    count = length - first;
  }
  if (first > length)
  {
    count = 0;
  }

  char * buffer = (char *) alloca(count + 1);
  memcpy(buffer, str + first, count);
  buffer[count] = 0;

  a_thread->PushNewString(buffer);

  return GM_OK;
}
	// *-------------------------------------------------------------
	// 脚本中ByteBuffer类型的构造函数
	static int GM_CDECL Buffer(gmThread* a_thread)
	{
		gmByteBuffer* buf = new gmByteBuffer();

		int numParams = a_thread->GetNumParams();
		if (numParams > 0)
			buf->m_byteBuffer->reserve(gmGetFloatOrIntParamAsInt(a_thread, 0));

		a_thread->PushNewUser(buf, GM_BYTEBUFFER);
		return GM_OK;
	}