1

Topic: Multiple str_format at Linux?

Is there a way to use multiple str_format at linux?
at the moment i need to rewrite all my str_format's to one str_format...

if it possible then i would be happy if someone could help me with that ^^

~ http://irataprojects.de/ - Share your projects ~

2 (edited by TeePhoenix 2012-06-10 16:19:22)

Re: Multiple str_format at Linux?

Deleted.

The face of terrorism is called United States of America!

3

Re: Multiple str_format at Linux?

Irata wrote:

Is there a way to use multiple str_format at linux?
at the moment i need to rewrite all my str_format's to one str_format...

if it possible then i would be happy if someone could help me with that ^^

The baselib in Teeworlds should be cross-platform, so you don't have to rewrite anything - unless I've misunderstood something.

Not Luck, Just Magic.

4

Re: Multiple str_format at Linux?

The problem is, on windows i can use multiple lines and the command ingame show all lines without problems.
On linux it shows only the last line.

~ http://irataprojects.de/ - Share your projects ~

5

Re: Multiple str_format at Linux?

You mean appending the last string?
Like this:
str_format(aBuf, sizeof(aBuf), "I am %s, ", "gay");
str_format(aBuf, sizeof(aBuf), "very very %s", "gay");
.....
GameServer()->SendChat(aBuf, -1)

str_format overwrites the content of the string so you'll only see "very very gay"
You need to send it to the chat if you want more lines, i think when you use \n it will overlap with other text.

6 (edited by Irata 2012-04-29 11:45:11)

Re: Multiple str_format at Linux?

yeah right, xD
Mhmm i fixxed it with a query for Windows and Linux because of the different buf sizes.
for Windows i use this:
str_format(aBuf, sizeof(aBuf), "I am %s, ", "not gay");
str_format(aBuf, sizeof(aBuf), "\nvery very %s", "not gay");
and linux:
str_format(aBuf, sizeof(aBuf), "I am %s, \nvery very %s", "not gay", not gay);

it works, so~

~ http://irataprojects.de/ - Share your projects ~

7

Re: Multiple str_format at Linux?

For windows it's probably \r\n try it out. should work for both also.

8 (edited by TT <3 2012-04-29 04:49:17)

Re: Multiple str_format at Linux?

When you want to append a string to an other, why do you not use str_append which is also part of the baselib?

char aBuf[128] = {0};
str_append(aBuf, "Hello?", sizeof(aBuf));
str_append(aBuf, " Yes, this", sizeof(aBuf));
str_append(aBuf, " is dog.", sizeof(aBuf));

then aBuf contains "Hello? Yes, this is dog."

9

Re: Multiple str_format at Linux?

The problem is for a stats it look like
example:
- Kills
- Deaths

For Windows i would use now
str_format(aBuf, sizeof(aBuf), "Kills %s, ", playerkills);
str_format(aBuf, sizeof(aBuf), "\nDeaths %s", playerdeaths);

after a Linux compile it would me show only Deaths.
So to make it work on linux and windows without use 2 different char abuf sizes (dont ask why) i use for Linux this
str_format(aBuf, sizeof(aBuf), "Kills %s\nDeaths %s", playerkills, playerdeaths);
as a example.

i check with if for windows or linux.

~ http://irataprojects.de/ - Share your projects ~

10 (edited by KillaBilla 2012-04-29 13:32:05)

Re: Multiple str_format at Linux?

Irata wrote:

The problem is for a stats it look like
example:
- Kills
- Deaths

For Windows i would use now
str_format(aBuf, sizeof(aBuf), "Kills %s, ", playerkills);
str_format(aBuf, sizeof(aBuf), "\nDeaths %s", playerdeaths);

after a Linux compile it would me show only Deaths.
So to make it work on linux and windows without use 2 different char abuf sizes (dont ask why) i use for Linux this
str_format(aBuf, sizeof(aBuf), "Kills %s\nDeaths %s", playerkills, playerdeaths);
as a example.

i check with if for windows or linux.

You have to use the buffer before you overwrite it:

str_format(aBuf, sizeof(aBuf), "Kills: %d", playerkills);
SendChatTarget(ClientID, aBuf);
str_format(aBuf, sizeof(aBuf), "Deaths: %d", playerdeaths);
SendChatTarget(ClientID, aBuf);

And you have to use %d for numbers in str_format or your server will crash.

11

Re: Multiple str_format at Linux?

KillaBilla wrote:
Irata wrote:

The problem is for a stats it look like
example:
- Kills
- Deaths

For Windows i would use now
str_format(aBuf, sizeof(aBuf), "Kills %s, ", playerkills);
str_format(aBuf, sizeof(aBuf), "\nDeaths %s", playerdeaths);

after a Linux compile it would me show only Deaths.
So to make it work on linux and windows without use 2 different char abuf sizes (dont ask why) i use for Linux this
str_format(aBuf, sizeof(aBuf), "Kills %s\nDeaths %s", playerkills, playerdeaths);
as a example.

i check with if for windows or linux.

You have to use the buffer before you overwrite it:

str_format(aBuf, sizeof(aBuf), "Kills: %d", playerkills);
SendChatTarget(ClientID, aBuf);
str_format(aBuf, sizeof(aBuf), "Deaths: %d", playerdeaths);
SendChatTarget(ClientID, aBuf);

And you have to use %d for numbers in str_format or your server will crash.

i have forgot to change %s to %d by copy and past wink
at windows i dont need to use the buffer.

~ http://irataprojects.de/ - Share your projects ~

12 (edited by ghost 2012-04-30 19:11:49)

Re: Multiple str_format at Linux?

Irata wrote:

at windows i dont need to use the buffer.

If this is really the case (wich I completely doubt), then this is a bug on the windows side. str_format will write into the buf from it's very begininng (the address aBuf is pointing to) regardless of the fact if there is something in it, so it's only natural that the first message doesn't get printed.

What you can do is either put both stuff into the string with str_format(aBuf, sizeof(aBuf), "%s\n%s", first_string, second_string) (don't know how \n will work in the default chat though) or send twice. Both methods have been suggested before, and i believe the only proper one is the second one. So in fact, you should do it like this:

str_format(aBuf, sizeof(aBuf), "Kills: %d", playerkills);
SendChatTarget(ClientID, aBuf);
str_format(aBuf, sizeof(aBuf), "Deaths: %d", playerdeaths);
SendChatTarget(ClientID, aBuf);

13

Re: Multiple str_format at Linux?

Irata wrote:

at windows i dont need to use the buffer.

Sry forgot to write "everytime" big_smile

And i found now the problem, why all what you they  is a little bit different.
I use this not for the Chat, i show it by the MOTD Box.

~ http://irataprojects.de/ - Share your projects ~

14

Re: Multiple str_format at Linux?

So you do  this on the server side with

str_format(g_Config.m_SvMotd, sizeof(g_Config.m_SvMotd), "Kills: %d", playerkills);
str_format(g_Config.m_SvMotd, sizeof(g_Config.m_SvMotd), "\nDeaths: %d", playerdeaths);

?
There are different problems with this code:
1. As mentioned before, the first "message" will simply be overwritte and won't be shown at all in most cases (usually all)
2. You can't be sure that the "message" get's sent at all, before it get's updated again (assuming that it get's updated everytime a kill/death happens). Changing the motd does not necessarily mean the new motd get's sent to the clients. I believe that clients will ask if there is a new motd after a certain amount of time, but I'm not sure about that. When changing the motd via rcon console (sv_motd "This is the new motd") a callback function (ConchainSpecialMotdupdate) get's called that makes sure the new motd gets sent to all clients and I think this is the approach you should take, too, if you really want to put this stuff in the motd. However, using the already exsisting function is probably a bad idea, because it uses all that callback stuff and you probably don't want to mess with that. I suggest to write a new function in gamecontext.h/.cpp:

void CGameContext::SendMotd()
{
    CNetMsg_Sv_Motd Msg;
    Msg.m_pMessage = g_Config.m_SvMotd;
    for(int i = 0; i < MAX_CLIENTS; ++i)
        if(m_apPlayers[i])
            Server()->SendPackMsg(&Msg, MSGFLAG_VITAL, i);
    return;
}

This is exactly what ConchainSpecialMotdupdate does when you remove all that callback stuff and so on.

Your code for updating the motd the way you want should then look like this:

char aBuf[64]; // or whatever size you need...
str_format(aBuf, sizeof(aBuf), "Kills: %d", playerkills);
str_copy(g_Config.m_SvMotd, aBuf,  sizeof(g_Config.m_SvMotd));
str_format(aBuf, sizeof(aBuf), "\nDeaths: %d", playerdeaths);
str_append(g_Config.m_SvMotd, aBuf,  sizeof(g_Config.m_SvMotd));
m_pGameServer->SendMotd(); // assuming m_pGameServer is a pointer to the gamecontext

You could of course combine those two str_formats, which I recommend, to me it looks cleaner that way, but from what I read in that post, you don't want to do that for some reason.

The code I posted should work the way it is, but I don't guarantee that there are no mistakes and stuff in it. I did not test it. Also it should be more of a guideline to what you should be doing instead of giving you the only possible solution. I hope I could help you with this. smile

15

Re: Multiple str_format at Linux?

char sendData[512];
str_format(sendData, sizeof(sendData), ">> Exp List <<");
str_format(sendData, sizeof(sendData), "%s\nKill Player: %dexp", sendData, g_Config.m_SvExpPerKill);
str_format(sendData, sizeof(sendData), "%s\nHammer Bonus: %dexp", sendData, g_Config.m_SvHamExp);
game->SendMotd(sendData,p->GetCID());

Already do it like this, you all try to help me but dont understand me right big_smile
Okey again, the problem is thats on windows the code i posted with this works, but on Linux i need to rebuild it to something like this

char sendData[512];
str_format(sendData, sizeof(sendData), ">> Exp List <<%s\nKill Player: %dexp\nHammer Bonus: %dexp", sendData, g_Config.m_SvExpPerKill, g_Config.m_SvHamExp);
game->SendMotd(sendData,p->GetCID());

as a fast example ^^
is there a way to make it work like on windows.

~ http://irataprojects.de/ - Share your projects ~

16

Re: Multiple str_format at Linux?

Irata wrote:
char sendData[512];
str_format(sendData, sizeof(sendData), ">> Exp List <<");
str_format(sendData, sizeof(sendData), "%s\nKill Player: %dexp", sendData, g_Config.m_SvExpPerKill);
str_format(sendData, sizeof(sendData), "%s\nHammer Bonus: %dexp", sendData, g_Config.m_SvHamExp);
game->SendMotd(sendData,p->GetCID());

Already do it like this, you all try to help me but dont understand me right big_smile
Okey again, the problem is thats on windows the code i posted with this works, but on Linux i need to rebuild it to something like this

char sendData[512];
str_format(sendData, sizeof(sendData), ">> Exp List <<%s\nKill Player: %dexp\nHammer Bonus: %dexp", sendData, g_Config.m_SvExpPerKill, g_Config.m_SvHamExp);
game->SendMotd(sendData,p->GetCID());

as a fast example ^^
is there a way to make it work like on windows.

You abuse the way the function works on windows, but there's no guarantee that it will work like that on other libc implementations aswell.

Similar code:

char aDataToSend[128];
char aBuf[16];

aDataToSend[0] = 0;
str_format(aBuf, sizeof(aBuf), "bar: %s", "foo"); str_append(aDataToSend, aBuf, sizeof(aDataToSend));
str_format(aBuf, sizeof(aBuf), "wtf: %s", "ftw"); str_append(aDataToSend, aBuf, sizeof(aDataToSend));

17

Re: Multiple str_format at Linux?

As said before, if this works on windows, this is more or less a bug on windows. The behavior on linux is correct so if you need to use 2 str_formats instead of one, you need to use str_append, too, as it has been suggested several times in this thread. There is no solution to only use 2 str_formats on linux, because the function simply does not work in that way (well, it seems on windows it does, but this is wrong). str_format should overwrite what was in the buffer before. So use one of the many solutions given here in this thread (or do what you already suggested befor and just use one str_format, this will work on windows, too, so no need for #ifdefs....).