[Include] Packed-safe format!
#1

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.
Reply
#2

Nice!
Reply
#3

Awesome release emmet!
Reply
#4

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!
Reply
#5

Very nice, thanks!
Reply
#6

Very nice...!
Reply
#7

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).
Reply
#8

Good
Reply
#9

Very nice release
Reply
#10

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.
Reply
#11

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)
Reply
#12

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

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

Sure thing, provide the code that formats the textdraw.
Reply
#15

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

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.)
Reply
#17

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.
Reply
#18

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 .
Reply
#19

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

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


Forum Jump:


Users browsing this thread: 1 Guest(s)