SA-MP Forums Archive
[Include] Packed-safe format! - 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] Packed-safe format! (/showthread.php?tid=481257)



Packed-safe format! - Emmet_ - 14.12.2013

Packed-safe format!

Introduction
As you may notice, the format function doesn't work at all with packed strings. And that couldn't be fixed without heavily modifying the format function. I've decided to add a solution for this - make format support packed strings!

What does it do?
This include allows you to pass a packed string to any parameter in format(). It also hooks printf() aswell, so it supports both functions aswell.

Instructions
Download ps-format.inc at the bottom of this post, and add it at the top of your script:

pawn Code:
#include <ps-format>
Make sure you include it AFTER all YSI includes!

You can now do this, something you couldn't do before:

pawn Code:
new
    Packed:str[32],
    Packed:str2[64];

format(str, sizeof(str), !"Cookies.");
format(str2, sizeof(str2), !"I like to eat %s.", str);

print(str2);
Which outputs the following:

pawn Code:
I like to eat Cookies.
You can also use "printf" to print packed strings:

pawn Code:
printf(!"I like to eat %s with %d bottles of ketchup.", !"fried chicken", 38);
Which isn't possible without this include!

Download
Pastebin

Thanks to ****** for some #emit tips, and Zeex for the #emit pushing method.


Re: Packed-safe format! - Wizzy951 - 14.12.2013

Nice!


Re: Packed-safe format! - DavidLuango - 14.12.2013

Awesome release emmet!


Re: Packed-safe format! - Emmet_ - 14.12.2013

Thanks, guys.

Also everyone, feel free to perform some tests with this! Packed strings are a great feature for saving memory and it's a shame that format doesn't support it entirely - now it does!


Re: Packed-safe format! - Jimmy0wns - 14.12.2013

Very nice, thanks!


Re: Packed-safe format! - Zamora - 14.12.2013

Very nice...!


Re: Packed-safe format! - Emmet_ - 14.12.2013

Thanks, people.

I should also say that the 3rd argument in format (after the size / length) can't take a packed parameter. This is good as it lets people use more wiser methods of copying strings (strcat and strpack).


Re: Packed-safe format! - iJumbo - 14.12.2013

Good


Re: Packed-safe format! - BizzyD - 14.12.2013

Very nice release


Re: Packed-safe format! - Emmet_ - 15.12.2013

Anybody have any ideas on what functions I should make packed-safe? AFAIK, all string.inc functons support packed strings - but there a few SA-MP natives that don't.


Re: Packed-safe format! - kristo - 11.04.2014

I fixed it the same way and as I said, there are some issues with it. For example, I have this code:

Code:
new user[24], string[256];
for (new i; i < rows; i++)
{
	cache_get_field_content(i, "user", user);
	for (new j, g = sizeof(user); j < g; j++) if (user[j] == '_') user[j] = ' ';
	format(string, sizeof(string), "%s%s\n", string, user);
}
cache_delete®;
ShowPlayerDialog(playerid, DIALOG_CHOOSECHARACTER, DIALOG_STYLE_LIST, "Select your character!", string, "Choose", "Quit");
When I have three characters, I normally get this dialog (That's in estonian, sorry):



But with this include I get only the last one:




Edit: This include also messes up other formats:



Kiirus (Speed) is always 75.
Kьtus (Fuel) changes according to speed and is a random character.
Odomeeter (Odometer) also doesn't change.
Same with Aku (Battery).

(Sorry for ruining your day)


Re: Packed-safe format! - Emmet_ - 11.04.2014

Alright, try redownloading. About the random character bug, it should be fixed.


Re: Packed-safe format! - kristo - 11.04.2014

Nope, it isn't, but everything else seems to be fine. I can provide you with my textdraw if you want.


Re: Packed-safe format! - Emmet_ - 11.04.2014

Sure thing, provide the code that formats the textdraw.


Re: Packed-safe format! - kristo - 11.04.2014

Quote:
Originally Posted by Emmet_
View Post
Sure thing, provide the code that formats the textdraw.
PMed it a couple of minutes ago.


Re: Packed-safe format! - Emmet_ - 11.04.2014

Fixed! The problem was the "%" sign at the end of your format, which somehow collided with the system and marked the format string a parameter. I've added proper detection for it, so it shouldn't ever collide again .

(Reinstall if you haven't already.)


Re: Packed-safe format! - kristo - 11.04.2014

I never thought about that, thanks!

Edit: You left your debug in the code

Edit2: It was kinda my mistake too because I didn't use a proper placeholder so I replaced "%" with "%%" and now the textdraw is messed up again.


Re: Packed-safe format! - Emmet_ - 11.04.2014

Quote:
Originally Posted by kvann
View Post
I never thought about that, thanks!

Edit: You left your debug in the code

Edit2: It was kinda my mistake too because I didn't use a proper placeholder so I replaced "%" with "%%" and now the textdraw is messed up again.
1) Removed, thanks.

2) Fixed that also, I keep forgetting about the "%%" specifier .


Re: Packed-safe format! - Baltazar - 12.04.2014

Can I replace strings to packed strings EVERYWHERE now? And comiling process crashes somewhow if it's included before <YSI\y_commands>


Re: Packed-safe format! - Baltazar - 12.04.2014

Ok, formatted packed string several times and put it to dialog. Shows shit. Any examples of properly usage of packed strings in dialogs?