SA-MP Forums Archive
[Include] formatex - Improved "format" function! - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Filterscripts (https://sampforum.blast.hk/forumdisplay.php?fid=17)
+---- Forum: Includes (https://sampforum.blast.hk/forumdisplay.php?fid=83)
+---- Thread: [Include] formatex - Improved "format" function! (/showthread.php?tid=313488)

Pages: 1 2


formatex - Improved "format" function! - Slice - 26.01.2012

Hey,

After seeing a great new feature in sscanf that allows you to create custom specifiers, I figured it was only right to do the same to format and printf!

For example, the following code could be made a lot shorter:
pawn Код:
// This will put into "msg" the weapon you were given by a player, and the color of the player's name will match the player's color.

new msg[128], name[MAX_PLAYER_NAME], color;

color = GetPlayerColor(playerid);

GetPlayerName(playerid, name, sizeof(name));

format(msg, sizeof(msg), "You were given a %s by {%06x}%s", g_WeaponNameArray[weapon], color >>> 8, name);
This will do the same thing:
Код:
new msg[128];

format(msg, _, "You were given %w by %P", weapon, playerid);

// msg is for example: You were given an M4 by Slice
New specifiers
SpecifierDescription
%pName of the player ID given.
%PName of the player ID given, with the player's color before it.
%CInline color (ex. {FFFFFF}) from a normal color (ex. colors you get from GetPlayerColor).
%vVehicle model name from the model given (not vehicle ID, vehicle model).
%wWeapon name, lower-case singular (to be used in sentences). Example: "an M4", "a combat shotgun", "a knife".
%WWeapon's name.
%X8-byte, unsigned hex string (ex. FFFFFFFF).
%uUnsigned integer.
You can create your own specifiers!
You can do this very easily; example of one that puts an upper-case string at %S:
Add this anywhere outside of a function:
pawn Код:
// Upper-case string
FormatSpecifier<'S'>(output[], const param[]) {
    for (new i = 0, l = min(sizeof(output), strlen(param)); i < l; i++)
        output[i] = toupper(param[i]);
}
You can now do this:
pawn Код:
printf("hello %S!", "world");

// prints: hello WORLD!
If you want an integer, float, or anything else instead of a string you just change it:
pawn Код:
// You can call the argument whatever you like, and it can be a string/array:
FormatSpecifier<'A'>(output[], Float:health) { ... }
FormatSpecifier<'B'>(output[], objectid) { ... }
FormatSpecifier<'C'>(output[], playerid) { ... }
FormatSpecifier<'D'>(output[], Text:td) { ... }
FormatSpecifier<'D'>(output[], const string[]) { ... }
Additional notes
Worth mentioning is this is a superset of format, meaning it has exactly all the features of format and those behave like always (the only exception is it's a bit friendlier to packed strings, though %s still doesn't support it).
It actually uses the original native "format" function to do the heavy lifting.

Download

Download: http://pastebin.com/xMAMtB6M


Re: formatex - Improved "format" function! - KingHual - 26.01.2012

Kewl, I actually need this.


Re: formatex - Improved "format" function! - kizla - 26.01.2012

very nice, i gonna use it for sure

edit: Why you use "GetMaxPlayers" instead of "MAX_PLAYERS" ??


Re: formatex - Improved "format" function! - [HK]Ryder[AN] - 26.01.2012

Great.Really Great.Don't know what to say anymore.....


Re: formatex - Improved "format" function! - Slice - 26.01.2012

Quote:
Originally Posted by kizla
Посмотреть сообщение
very nice, i gonna use it for sure

edit: Why you use "GetMaxPlayers" instead of "MAX_PLAYERS" ??
Well I was using it to see if the player ID was in range. It can't be more than GetMaxPlayers, so that's why I used it.


Re: formatex - Improved "format" function! - iPLEOMAX - 26.01.2012

There you go, another awesome release. I'm obviously gonna use it.


Re: formatex - Improved "format" function! - fiki574 - 26.01.2012

Nice one, Slice!

rep+


Re: formatex - Improved "format" function! - T0pAz - 26.01.2012

Yet another useful release. Nice work Slice(The next ******).


Re: formatex - Improved "format" function! - Kaperstone - 26.01.2012

awesome,thanks


Re: formatex - Improved "format" function! - System64 - 26.01.2012

fucking awesome man

rep+

edit: shit I can't give rep


Re: formatex - Improved "format" function! - Lorenc_ - 26.01.2012

Nice release! You're making great usage for emit!


Re: formatex - Improved "format" function! - [Diablo] - 26.01.2012

awesome and useful include.


Re: formatex - Improved "format" function! - [XST]O_x - 26.01.2012

Amazing work there!


Re: formatex - Improved "format" function! - hossa - 26.01.2012

awesome





Re: formatex - Improved "format" function! - iZN - 26.01.2012

Fabulous work Slice!


Re: formatex - Improved "format" function! - TheArcher - 26.01.2012

I have understand everything LOL


Re: formatex - Improved "format" function! - TheBetaFox - 26.01.2012

>Inline color from normal color.
Slice, I love you. I love you from the bottom of my heart.
+rep every day for the rest of infinity


Respuesta: formatex - Improved "format" function! - [Nikk] - 26.01.2012

very nice, useful, thanks


Re: formatex - Improved "format" function! - Slice - 29.01.2012

Here's a neat way to always have nicely formatted money values!

Example:
pawn Код:
printf("You just earned %m. You now have a total of %m in your bank.", 12000, 554279811);
printf("%m", 1000000000);
printf("%m", 100000000);
printf("%m", 10000000);
printf("%m", 1000000);
printf("%m", 100000);
printf("%m", 10000);
printf("%m", 1000);
printf("%m", 100);
printf("%m", 10);
printf("%m", 1);
printf("%m", 0);
Output:
Код:
You just earned $12,000. You now have a total of $554,279,811 in your bank.
$1,000,000,000
$100,000,000
$10,000,000
$1,000,000
$100,000
$10,000
$1,000
$100
$10
$1
$0
Specifier code:
pawn Код:
// Money
FormatSpecifier<'m'>(output[], amount) {
    if (amount) {
        new
            i = 18,
            neg = amount < 0
        ;
       
        // The spaces must be there, otherwise strdel below won't work
        // on lower numbers.
        output = "$                ";
       
        // Null-out the end of it
        output[i] = 0;
       
        if (neg)
            amount = -amount;
       
        // Going right-left, add one number each time
        while (amount) {
            // Add a thousand separator every 3rd time
            if (!((i + 1) % 4))
                output[--i] = ',';
           
            // Now add the last digit of the number
            output[--i] = '0' + (amount % 10);
           
            // Then divide the number by 10, so digit in the end will be gone
            amount /= 10;
        }
       
        // Delete the spaces between the $-sign and the first (last) number
        strdel(output, 1, i);
       
        // Add a minus sign if needed
        if (neg)
            strins(output, "-", 1);
    } else {
        output = "$0";
    }
}



Re: formatex - Improved "format" function! - Niko_boy - 29.01.2012

Kewl i knew that you will / or any one will make a thing like thAT
Its very kewl awesome buddy!