예제 #1
0
//added by BAILOPAN for jtp10181
//takes a string and breaks it into a 1st param and rest params
//different from strbreak because it's more crafted for control
static cell AMX_NATIVE_CALL amx_strtok(AMX *amx, cell *params)
{
	int left_pos = 0;
	int right_pos = 0;
	unsigned int i = 0;
	bool done_flag = false;
	int len = 0;

	//string[]
	char *string = get_amxstring(amx, params[1], 0, len);
	//left[]
	char *left = new char[len + 1];
	//right[]
	char *right = new char[len + 1];
	int leftMax = params[3];
	int rightMax = params[5];
	//token
	char token = static_cast<char>(params[6]);
	//trim
	int trim = params[7];
	
	for (i = 0; i < (unsigned int)len; i++)
	{
		if (trim && !done_flag)
		{
			if (isspace(string[i]))
			{
				while (isspace(string[++i]));
				done_flag = true;
			}
		}

		if (!done_flag && string[i] == token)
		{
			done_flag = true;
			i++;
		}

		if (done_flag)
		{
			right[right_pos++] = string[i];
		} else {
			left[left_pos++] = string[i];
		}
	}

	right[right_pos] = 0;
	left[left_pos] = 0;
	set_amxstring_utf8(amx, params[2], left, strlen(left), leftMax);
	set_amxstring_utf8(amx, params[4], right, strlen(right), rightMax);
	delete [] left;
	delete [] right;
	
	return 1;
}
예제 #2
0
// native bool:TrieGetString(Trie:handle, const key[], buff[], len, &size = 0);
static cell AMX_NATIVE_CALL TrieGetString(AMX *amx, cell *params)
{
	CellTrie *t = TrieHandles.lookup(params[1]);

	if (!t)
	{
		LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
		return 0;
	}

	if (params[4] < 0)
	{
		LogError(amx, AMX_ERR_NATIVE, "Invalid buffer size (%d)", params[4]);
		return 0;
	}

	int len;
	const char *key = get_amxstring(amx, params[2], 0, len);
	cell *pSize = get_amxaddr(amx, params[5]);

	StringHashMap<Entry>::Result r = t->map.find(key);
	if (!r.found() || !r->value.isString())
	{
		return 0;
	}

	*pSize = (cell)set_amxstring_utf8(amx, params[3], r->value.chars(), strlen(r->value.chars()), params[4]);

	return 1;
}
예제 #3
0
// native bool:PopStackString(Stack:handle, buffer[], maxlength, &written = 0);
static cell AMX_NATIVE_CALL PopStackString(AMX* amx, cell* params)
{
	CellArray* vec = ArrayHandles.lookup(params[1]);

	if (!vec)
	{
		LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
		return 0;
	}

	if (vec->size() == 0)
	{
		return 0;
	}

	size_t idx = vec->size() - 1;
	cell *blk = vec->at(idx);

	int numWritten = set_amxstring_utf8(amx, params[2], blk, amxstring_len(blk), params[3]);
	*get_amxaddr(amx, params[4]) = numWritten;

	vec->remove(idx);

	return 1;
}
예제 #4
0
static cell AMX_NATIVE_CALL format_args(AMX *amx, cell *params)
{
	int len;
	int pos = params[3];
	
	if (pos < 0)
	{
		LogError(amx, AMX_ERR_NATIVE, "Pos has to be a positive number");
		return 0;
	}

	char* string = format_arguments(amx, pos, len); // indexed from 0
	
	return set_amxstring_utf8(amx, params[1], string, len, params[2]);
}
예제 #5
0
static cell AMX_NATIVE_CALL split_string(AMX *amx, cell *params)
{
	int textLen, splitLen;
	char *text = get_amxstring(amx, params[1], 0, textLen);
	const char *split = get_amxstring(amx, params[2], 1, splitLen);

	if (splitLen > textLen)
	{
		return -1;
	}

	int maxLen = params[4];

	/**
	* Note that it's <= ... you could also just add 1,
	* but this is a bit nicer
	*/
	for (int i = 0; i <= textLen - splitLen; i++)
	{
		if (strncmp(&text[i], split, splitLen) == 0)
		{
			/* Split hereeeee */
			if (i > maxLen)
			{
				set_amxstring_utf8(amx, params[3], text, textLen, maxLen);
			}
			else
			{
				set_amxstring_utf8(amx, params[3], text, textLen, i);
			}
			return i + splitLen;
		}
	}

	return -1;
}
예제 #6
0
// native ArrayGetString(Array:which, item, output[], size);
static cell AMX_NATIVE_CALL ArrayGetString(AMX* amx, cell* params)
{
	CellArray* vec = HandleToVector(amx, params[1]);

	if (vec == NULL)
	{
		return 0;
	}

	size_t idx = (size_t)params[2];

	if (idx >= vec->size())
	{
		LogError(amx, AMX_ERR_NATIVE, "Invalid index %d (count: %d)", idx, vec->size());
		return 0;
	}

	cell *blk = vec->at(idx);
	return set_amxstring_utf8(amx, params[3], blk, amxstring_len(blk), params[4] + 1); // + EOS.
}
예제 #7
0
static cell AMX_NATIVE_CALL TrieSnapshotGetKey(AMX *amx, cell *params)
{
	TrieSnapshot *snapshot = TrieSnapshotHandles.lookup(params[1]);

	if (!snapshot)
	{
		LogError(amx, AMX_ERR_NATIVE, "Invalid snapshot handle provided (%d)", params[1]);
		return 0;
	}

	unsigned index = params[2];

	if (index >= snapshot->length)
	{
		LogError(amx, AMX_ERR_NATIVE, "Invalid index %d", index);
		return 0;
	}

	const char *str = snapshot->strings.GetString(snapshot->keys[index]);
	return set_amxstring_utf8(amx, params[3], str, strlen(str), params[4]);
}
예제 #8
0
// native ArrayGetString(Array:which, item, output[], size);
static cell AMX_NATIVE_CALL ArrayGetString(AMX* amx, cell* params)
{
	CellArray* vec = ArrayHandles.lookup(params[1]);

	if (!vec)
	{
		LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
		return 0;
	}

	size_t idx = (size_t)params[2];

	if (idx >= vec->size())
	{
		LogError(amx, AMX_ERR_NATIVE, "Invalid index %d (count: %d)", idx, vec->size());
		return 0;
	}

	cell *blk = vec->at(idx);
	return set_amxstring_utf8(amx, params[3], blk, amxstring_len(blk), params[4]);
}
예제 #9
0
// native bool:GameConfGetKeyValue(GameConfig:handle, const key[], buffer[], maxlen);
static cell AMX_NATIVE_CALL GameConfGetKeyValue(AMX *amx, cell *params)
{
	GameConfigNative *handle = GameConfigHandle.lookup(params[1]);

	if (!handle)
	{
		LogError(amx, AMX_ERR_NATIVE, "Invalid game config handle %d", params[1]);
		return 0;
	}

	int length;
	const char *value;
	const char *key = get_amxstring(amx, params[2], 0, length);

	if (!(value = handle->m_config->GetKeyValue(key)))
	{
		return 0;
	}

	set_amxstring_utf8(amx, params[3], value, strlen(value), params[4]);

	return 1;
}
예제 #10
0
// native bool:PopStackString(Stack:handle, buffer[], maxlength, &written = 0);
static cell AMX_NATIVE_CALL PopStackString(AMX* amx, cell* params)
{
	CellArray* vec = HandleToVector(amx, params[1]);

	if (vec == NULL)
	{
		return 0;
	}

	if (vec->size() == 0)
	{
		return 0;
	}

	size_t idx = vec->size() - 1;
	cell *blk = vec->at(idx);

	int numWritten = set_amxstring_utf8(amx, params[2], blk, amxstring_len(blk), params[3] + 1);
	*get_amxaddr(amx, params[4]) = numWritten;

	vec->remove(idx);

	return 1;
}
예제 #11
0
// Same as amx_strtok but fixes and expands trim, returns token pos if token was found, -1 otherwise
static cell AMX_NATIVE_CALL amx_strtok2(AMX *amx, cell *params)
{
	int left_pos = 0, right_pos = 0, len, pos = -1;
	unsigned int i = 0;

	char *string = get_amxstring(amx, params[1], 0, len);
	char *left = new char[len + 1], *right = new char[len + 1];
	int left_max = params[3], right_max = params[5];
	char token = static_cast<char>(params[6]);
	
	/*	Trim flags:
			1 - ltrim left
			2 - rtrim left
			4 - ltrim right
			8 - rtrim right
	*/
	int trim = params[7];

	// ltrim left
	if (trim & 1 && isspace(string[i]))
	{
		while (isspace(string[++i]));
	}

	for (; i < (unsigned int) len; ++i)
	{
		if (string[i] == token)
		{
			pos = i;
			++i;
			break;
		}

		left[left_pos++] = string[i];
	}

	// rtrim left
	if (trim & 2 && left_pos && isspace(left[left_pos - 1]))
	{
		while (--left_pos >= 0 && isspace(left[left_pos]));
		
		++left_pos;
	}

	// ltrim right
	if (trim & 4 && isspace(string[i]))
	{
		while (isspace(string[++i]));
	}

	for (; i < (unsigned int) len; ++i)
	{
		right[right_pos++] = string[i];	
	}

	// rtrim right
	if (trim & 8 && right_pos && isspace(right[right_pos - 1]))
	{
		while (--right_pos >= 0 && isspace(right[right_pos]));

		++right_pos;
	}

	right[right_pos] = 0;
	left[left_pos] = 0;

	set_amxstring_utf8(amx, params[2], left, strlen(left), left_max);
	set_amxstring_utf8(amx, params[4], right, strlen(right), right_max);

	delete [] left;
	delete [] right;

	return pos;
}
예제 #12
0
int set_amxstring_utf8_cell(AMX *amx, cell amx_addr, const cell *source, size_t sourcelen, size_t maxlen)
{
	return set_amxstring_utf8(amx, amx_addr, source, sourcelen, maxlen);
}
예제 #13
0
//added by BAILOPAN :(
//Takes a string and breaks it into a 1st param and rest params
//strbreak(String[], First[], FirstLen, Rest[], RestLen)
static cell AMX_NATIVE_CALL strbreak(AMX *amx, cell *params)	/* 5 param */
{
	int _len;
	bool in_quote = false;
	bool had_quotes = false;
	size_t i = 0;
	size_t beg = 0;

	char *string = get_amxstring(amx, params[1], 0, _len);
	cell *right = get_amxaddr(amx, params[4]);
	int LeftMax = params[3];
	int RightMax = params[5];

	size_t len = (size_t)_len;

	while (isspace(string[i]) && i<len)
		i++;
	beg = i;
	for (; i<len; i++)
	{
		if (string[i] == '"' && !in_quote)
		{
			in_quote = (had_quotes = true);
		} else if (string[i] == '"' && in_quote) {
			in_quote = false;
			if (i == len-1)
				goto do_copy;
		} else {
			if (isspace(string[i]) && !in_quote)
			{
do_copy:
				size_t pos = i;
				while (isspace(string[i]))
					i++;
				const char *start = had_quotes ? &(string[beg+1]) : &(string[beg]);
				size_t _end = had_quotes ? (i==len-1 ? 1 : 2) : 0;
				size_t end = (pos - _end > (size_t)LeftMax) ? (size_t)LeftMax : pos - _end;
				
				// If there is anything to copy, make sure we copy min(maxlen, slicelen).
				size_t copylen = end >= beg
				                 ? ((end - beg > size_t(LeftMax))
				                    ? size_t(LeftMax)
				                    : end - beg
				                   )
				                 : 0;
				set_amxstring_utf8(amx, params[2], start, strlen(start), copylen);

				end = (len-i+1 > (size_t)RightMax) ? (size_t)RightMax : len-i+1;
				if (end)
				{
					start = &(string[i]);
					while (end--)
						*right++ = (cell)*start++;
				}
				*right = '\0';
				return 1;
			}
		}
	}

	//if we got here, there was nothing to break
	set_amxstring_utf8(amx, params[2], &(string[beg]), strlen(&(string[beg])), LeftMax);
	if (RightMax)
		*right = '\0';

	return 1;
}