SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts

Reply
 
Thread Tools Display Modes
Old 02/05/2012, 04:48 PM   #3221
Y_Less
Spam Machine
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 14,547
Reputation: 2774
Default Re: Useful Functions

Quote:
Originally Posted by Kaczmi View Post
I made my own version of sscanf aka 'UnParams'.
It's slow
It's only test of my practice
Nice attempt, though:

Code:
UnParams("1234567890123456789012345678901234567890", "i", var);
Y_Less is offline   Reply With Quote
Old 02/05/2012, 05:10 PM   #3222
Kaczmi
Big Clucker
 
Join Date: Feb 2012
Posts: 52
Reputation: 3
Default Re: Useful Functions

Y_Less, i know, i don't know how to fix

it prints:

2123804658

fixed (uknown format)

Code:
#define isNull(%0) \
					(%0[0] == 0 || (%0[0] == 1 && %0[1] == 0))

stock
		UnParams(params[],format[],{Float,_}:...)
{
	if(isNull(params)) return 0;
	
	new
	    formatPos = 0,
	    num = numargs(),
	    startPos = 2,
		space = ' ',
		startChar = 0,
		pos = 0
	;
	
	while((startChar = params[pos]) && startChar == space)
	{
	    pos++;
	}
	while(startPos < num)
	{
	    switch(format[formatPos])
	    {
	        case 'd','D','i','I':
	        {
	            new
	                str[35],
	                ch,
	                posParam = pos+1
				;
				while((ch = params[posParam]) && ch != space)
				{
					if(ch > '9' || ch < '0')
					{
						return 0;
					}
				    posParam++;
				}
				strmid(str,params,pos,pos + (posParam - pos));
				pos = posParam;
				setarg(startPos,0,strval(str));
				formatPos++;				
			}
			case 's','S':
			{
			    new
					str[128],
			        ch,
			        posParam = pos + 1, 
			        index
				;
				
				while((ch = params[posParam]) && ch != space)
				{
				    posParam++;
				}
				strmid(str,params,pos + (((ch = params[pos]) && ch == space) ? 1 : 0),pos + (posParam - pos));
				for(new p,x = strlen(str);p < x;p++)
				{
					ch = str[p];
					setarg(startPos,index,ch);
					index++;
				}
				pos = posParam;
				formatPos++;
			}
			case 'f','F':
			{
	            new
	                str[35],
	                ch,
	                posParam = pos+1,
					dots
				;
				while((ch = params[posParam]) && ch != space)
				{
					if(ch > '9' || ch < '0')
					{
						if(ch == '.' && dots < 1) dots++;
						else return 0;
					}
				    posParam++;
				}
				strmid(str,params,pos + (((ch = params[pos]) && ch == space) ? 1 : 0),pos + (posParam - pos));
				pos = posParam;
				setarg(startPos,0,_:floatstr(str));
				formatPos++;
			}
			default: return 0;
		}
		startPos++;
	}
	return 1;
}
Kaczmi is offline   Reply With Quote
Old 02/05/2012, 05:14 PM   #3223
Y_Less
Spam Machine
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 14,547
Reputation: 2774
Default Re: Useful Functions

Actually, that wasn't even what I was getting at. strval used to crash on massive numbers (maybe it doesn't now, not sure), and the number I had there is longer that 35 characters, thus will give a buffer overflow (though you don't actually need a buffer).
Y_Less is offline   Reply With Quote
Old 05/05/2012, 03:17 PM   #3224
Slice
High-roller
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,731
Reputation: 1442
Default Re: Useful Functions

strreplace - Replace occurrences of the search string with the replacement string.

You can see a demo & test the function here.

Code:
/*
 * strreplace - Replace occurrences of the search string with the replacement string.
 * 
 * Supports both packed and unpacked strings.
 *
 * Parameters:
 *   string[] - The string to perform the replacing in.
 *   search[] - The string to look for.
 *   replacement[] - The string to put instead of "search".
 *   ignorecase - Whether the search for "search" should be case-insensitive. Defaults to false.
 *   pos - The position to start at. Defaults to 0 (the beginning).
 *   limit - Limit the number of replacements. Defaults to -1 (no limit).
 *   maxlength - The size of "string". Defaults to sizeof(string), which almost always is what you want.
 *
 * Returns:
 *   The number of replacements that were made.
 */
stock strreplace(string[], const search[], const replacement[], bool:ignorecase = false, pos = 0, limit = -1, maxlength = sizeof(string)) {
	// No need to do anything if the limit is 0.
	if (limit == 0)
		return 0;
	
	new
		     sublen = strlen(search),
		     replen = strlen(replacement),
		bool:packed = ispacked(string),
		     maxlen = maxlength,
	         len = strlen(string),
		     count = 0
	;
	
	
	// "maxlen" holds the max string length (not to be confused with "maxlength", which holds the max. array size).
	// Since packed strings hold 4 characters per array slot, we multiply "maxlen" by 4.
	if (packed)
		maxlen *= 4;
	
	// If the length of the substring is 0, we have nothing to look for..
	if (!sublen)
		return 0;
	
	// In this line we both assign the return value from "strfind" to "pos" then check if it's -1.
	while (-1 != (pos = strfind(string, search, ignorecase, pos))) {
		// Delete the string we found
		strdel(string, pos, pos + sublen);
		
		len -= sublen;
		
		// If there's anything to put as replacement, insert it. Make sure there's enough room first.
		if (replen && len + replen < maxlen) {
			strins(string, replacement, pos, maxlength);
			
			pos += replen;
			len += replen;
		}
		
		// Is there a limit of number of replacements, if so, did we break it?
		if (limit != -1 && ++count >= limit)
			break;
	}
	
	return count;
}

Examples:
Code:
new string[200];

// Simple replacement
string = "You can't compare apples and oranges.";
strreplace(string, "apples", "oranges");
print(string);

// Case-insensitive replace
string = "LoremLoremLoremLorem ipsum dolor sit amet, consectetur adipiscing elit.";
strreplace(string, "lorem", "LOREM", .ignorecase = true);
strreplace(string, "DOLOR", "dooloor", .ignorecase = true);
print(string);

// Using an empty replacement string
string = "1111122222333334444455555";
strreplace(string, "2", "");
print(string);

// Using the "limit" argument
string = "bbbbbbbbbb";
strreplace(string, "b", "a", .limit = 5);
print(string);

// Using the "pos" argument
string = "---bbbbbbbbbb---";
strreplace(string, "b", "a", .pos = 5);
print(string);
__________________
Do you want to script with me? High-end TDM server. Contact me.
Slice is offline   Reply With Quote
Old 06/05/2012, 09:36 AM   #3225
Lorenc_
High-roller
 
Lorenc_'s Avatar
 
Join Date: Jan 2010
Location: Australia
Posts: 4,172
Reputation: 983
Default Re: Useful Functions

Thanks Slice!
Lorenc_ is offline   Reply With Quote
Old 06/05/2012, 10:08 AM   #3226
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 3,913
Reputation: 1528
Default Re: Useful Functions

Damn, thank you for that. (@Slice). Works perfect, and it's gonna be very usefull for my gamemode : ).
Just tested it, and it worked like.... a script that works good.

Code:
main()
{
	new string[100];
	string = "* Kevin the noob is dumb *";
	printf("%s", string);
	strreplace(string, "Kevin", "Kwarde");
	strreplace(string, "*", ">>", .limit = 1);
	strreplace(string, "*", "<<", .pos = 1);
	strreplace(string, "dumb", "just a regular scripter");
	strreplace(string, "noob", "PAWN scripter");
	printf("%s", string);
	return 1;
}

/* 
** Output **

[12:15:24] * Kevin the noob is dumb *
[12:15:24] >> Kwarde the PAWN scripter is just a regular scripter <<

** End Of Output **
*/
A bit of a dumb string, but it's nice to see how it works : )
Kwarde is offline   Reply With Quote
Old 06/05/2012, 05:58 PM   #3227
RyDeR`
High-roller
 
RyDeR`'s Avatar
 
Join Date: Feb 2009
Location: Belgium
Posts: 3,097
Reputation: 657
Default Re: Useful Functions

strDelete(szStr[], ...) ─ Deletes sub-strings from a string in a very neat and fast way:
Code:
stock strDelete(szStr[], ...) {
	new
		iStart,
		iAddr,
		iLen,
		iPos
	;
	#emit ADDR.PRI		szStr
	#emit ADD.C			4
	#emit STOR.S.PRI	iStart
	
	for(new iEnd = iStart + ((numargs() - 1) << 2); iEnd != iStart; iStart += 4) {
		#emit LOAD.S.PRI 	iStart
		#emit LOAD.I
		#emit STOR.S.PRI 	iAddr
		
		#emit PUSH.S		iAddr
		#emit PUSH.C		4
		#emit SYSREQ.C		strlen
		#emit INC.PRI
		#emit STOR.S.PRI 	iLen
		#emit STACK			8
		
	LOOP:
		#emit PUSH.C		0
		#emit PUSH.C		true
		#emit PUSH.S		iAddr
		#emit PUSH.ADR		szStr
		#emit PUSH.C		16
		#emit SYSREQ.C		strfind
		#emit STACK			20
		#emit STOR.S.PRI	iPos
			
		if(iPos != -1) {
			#emit LOAD.S.PRI	iPos
			#emit LOAD.S.ALT	iLen
			#emit ADD
			#emit DEC.PRI
			#emit PUSH.PRI
			#emit PUSH.S		iPos
			#emit PUSH.ADR		szStr
			#emit PUSH.C		12
			#emit SYSREQ.C		strdel
			#emit STACK			16

			#emit JUMP			LOOP
		}
	}
	return 1;
}
Example:
Code:
new string[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
strDelete(string, "ipsum", "sit", ",", "consectetur", "elit");
print(string); // Prints: "Lorem  dolor  amet  adipiscing ."
__________________

Sup?
RyDeR` is offline   Reply With Quote
Old 06/05/2012, 06:53 PM   #3228
Kaczmi
Big Clucker
 
Join Date: Feb 2012
Posts: 52
Reputation: 3
Default Re: Useful Functions

My version of strDel

Code:
strDel(string[],...)
{
	new 
		num = numargs();
	
	for(new i=1;i<num;i++)
	{
		// GetString
		new str[25],a,b;
		while(!false)
		{
			b = getarg(i,a);
			if(b == 0) break;
			str[a] = b;
			a++;
		}
		//
		new pos;
		while((pos = strfind(string,str,true)) != -1)
		{
			strdel(string,pos,pos+strlen(str));
		}
	}
	return true;
}
Kaczmi is offline   Reply With Quote
Old 08/05/2012, 04:36 PM   #3229
Chris'
Little Clucker
 
Chris''s Avatar
 
Join Date: Jan 2012
Posts: 24
Reputation: 1
Default Re: Useful Functions

Code:
stock GetFreeServerSlots()
{
	new count, result;
    for (new i = 0; i < MAX_PLAYERS; i ++) count += IsPlayerConnected(i);
    new maxp = GetMaxPlayers();
	result = maxp - count;
	return result;
}

This function simply gets the nuber of the slots available on the server.
Chris' is offline   Reply With Quote
Old 08/05/2012, 05:43 PM   #3230
TheArcher
High-roller
 
TheArcher's Avatar
 
Join Date: Dec 2009
Location: Home
Posts: 2,465
Reputation: 241
Default Re: Useful Functions

@Chris' Genius. Congrats.
__________________
TheArcher is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Functions? sciman001 Help Archive 7 19/03/2011 03:03 PM
How to get functions? Twain32 Help Archive 4 08/10/2010 07:37 PM
[Include] [INC] SA:MP New Functions 0.4a DKN ipsBruno Lançamentos/Releases 22 17/06/2010 08:38 AM
[Include] [INC] LSF - Lorenc's Simple Functions (w/ gang/clan functions) Lorenc_ Includes 11 03/05/2010 09:47 PM
What this functions do? harrold Help Archive 2 08/05/2009 11:30 PM


All times are GMT. The time now is 10:39 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2015, Jelsoft Enterprises Ltd.