[Include] formatex - Improved "format" function!
#21

Quote:
Originally Posted by Slice
Посмотреть сообщение
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
        ;
       
        // The spaces must be there, otherwise strdel below won't work
        // on lower numbers.
        output = "$                ";
       
        // Null-out the end of it
        output[i] = 0;
       
        // 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);
    } else {
        output = "$0";
    }
}
How do I use/install this?

Undefined symbol "FormatSpecifier"
Reply
#22

You need the include file. Simply:
pawn Код:
#include <formatex>
Reply
#23

Veeery useful, thanks for sharing!
Reply
#24

Very nice release, Slice.
I'm definitely going to be using this include from now on.
Reply
#25

Wow, damn dude, i seriously didn't understood at all most of your codes, you're a great scripter, thanks for releasing such stuff into the SA-MP forums, this will be usefull for me some day..
Reply
#26

Is there any way to use your formatex and the va_format from y_va together?

Cause I use SendClientMessageEx with va_format and I want to use your custom format in SCMEx.
Reply
#27

AWW i see money part now :P and surely wanted to comment
Its great
simplyfing the work load great
another good job Slice
Reply
#28

Quote:
Originally Posted by Joey^
Посмотреть сообщение
Is there any way to use your formatex and the va_format from y_va together?

Cause I use SendClientMessageEx with va_format and I want to use your custom format in SCMEx.
pawn Код:
stock va_formatex(output[], size = sizeof(output), const fmat[], va_:STATIC_ARGS) {
    new
        num_args,
        arg_start,
        arg_end;
    // Get the pointer to the number of arguments to the last function.
    #emit LOAD.S.pri   0
    #emit ADD.C        8
    #emit MOVE.alt
    // Get the number of arguments.
    #emit LOAD.I
    #emit STOR.S.pri   num_args
    // Get the variable arguments (end).
    #emit ADD
    #emit STOR.S.pri   arg_end
    // Get the variable arguments (start).
    #emit LOAD.S.pri   STATIC_ARGS
    #emit SMUL.C       4
    #emit ADD
    #emit STOR.S.pri   arg_start
    // Using an assembly loop here screwed the code up as the labels added some
    // odd stack/frame manipulation code...
    while (arg_end != arg_start)
    {
        #emit MOVE.pri
        #emit LOAD.I
        #emit PUSH.pri
        #emit CONST.pri    4
        #emit SUB.alt
        #emit STOR.S.pri   arg_end
    }
    // Push the additional parameters.
    #emit PUSH.S       fmat
    #emit PUSH.S       size
    #emit PUSH.S       output
    // Push the argument count.
    #emit LOAD.S.pri   num_args
    #emit ADD.C        12
    #emit LOAD.S.alt   STATIC_ARGS
    #emit XCHG
    #emit SMUL.C       4
    #emit SUB.alt
    #emit PUSH.pri
    #emit MOVE.alt
    // Push the return address.
    #emit LCTRL        6
    #emit ADD.C        28
    #emit PUSH.pri
    // Call formatex
    #emit CONST.pri    formatex
    #emit SCTRL        6
}
You can now do:
pawn Код:
static stock
    gs_Buffer[256]
;

stock SendClientMessagef(playerid, color, fmat[], va_args<>) {
    va_formatex(gs_Buffer, _, fmat, va_start<3>);

    SendClientMessage(playerid, color, gs_Buffer);
}

stock SendClientMessageToAllf(color, fmat[], va_args<>) {
    va_formatex(gs_Buffer, _, fmat, va_start<2>);

    SendClientMessageToAll(color, gs_Buffer);
}
Thus:
pawn Код:
color = 0xFFFFFFFF;

SendClientMessageToAllf(color, "* %P%C just joined the server.", playerid, color);
Reply
#29

Could you make a few lines I can run to reproduce this?
Reply
#30

This is really awesome ! With this i can script faster.
Reply
#31

Very nice man, I like it.
Reply
#32

Amazing!
Reply
#33

Did the vehicle names break in 0.3.7 R2?

I've created a simple loop to test.
Код:
for (new i = 400; i < 611; i++)
    printf("%v", i);
Here is the server log.

Sscanf2 is included before formatex and is updated to the most recent version. I have not modified formatex.

I know the include works because %p works just fine.
Reply
#34

How to substitute native mysql_format for formatex without lost the mysql log. I see the include, maybe this?
PHP код:
stock mysql_formatex(connectionHandleszOutput[], iLength sizeof(szOutput), const szFormatString[], GLOBAL_TAG_TYPES:...) {
... 
Same Codes
        
// New format specifier
        #emit PUSH.C     s_szBuffer
    
        // Max length
        #emit PUSH.S     iLength
    
        // Output string
        #emit PUSH.S     szOutput
           // Handle
        #emit PUSH.S     connectionHandle
        // Argument count
        #emit LOAD.S.pri iArgCount
        #emit SHL.C.pri  2
        #emit ADD.C      16
        #emit PUSH.pri
    
        // Save the argument count for later
        #emit MOVE.alt
    
        // Call format (duh)
        #emit SYSREQ.C   mysql_format
... Same More codes 
But i not understand completely emit, and i lost others address.

Slice, your releases are awesome.
Reply
#35

"Yeah day, i have the same question"
Reply
#36

The %q specifier returns null when using this include:

PHP код:
#include <a_samp>
main() {
    
printf("Straight: '%q'""AndySedeyn");

Prints:
Код:
[11:49:32] Straight: 'AndySedeyn'
And
PHP код:
#include <a_samp>
#include <formatex>
main() {
    
printf("Straight: '%q'""AndySedeyn");

Prints:
Код:
[11:53:11] Straight: ''
EDIT:

Here's the fixed formatex function: http://pastebin.com/gvE8PqBK , altered code:
PHP код:
// Handled by the original format function
case '*''i''d''x''h''c''s''f''b''q': { // I added 'q'
    // Get the argument address and save it for later
    #emit LCTRL        5
    #emit LOAD.S.alt  iArg
    #emit ADD
    #emit LOAD.I
    #emit STOR.S.pri  iAddress
    #emit MOVE.pri
    #emit ADD.C       4
    #emit STOR.S.pri  iArg
    
aiArgs[iArgCount++] = iAddress;
    if (
s_szBuffer[iPos] == '*')
        continue;
    break;

Reply
#37

I have formatex in my server but in some messages instead of appearing the value appears the letter m

PHP код:
FormatSpecifier<'m'>(output[], amount) {
    if (
amount) {
        new
            
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 (!((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(output1i);
        
        
// Add a minus sign if needed
        
if (neg)
            
strins(output"-"1);
    } else {
        
output "$0";
    }

Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)