Форматирование строки в функции с динамическими аргументами

Статус
В этой теме нельзя размещать новые ответы.
Сообщения
957
Реакции
1,184
Помог
52 раз(а)
Намедни игрался с форматированием и так и не понял логики.

Вариант №1

Код:
#include <amxmodx>

public plugin_init()
{
    new str[100];

    copy(str, 100, "string [%s] [%s] [%s]");

    callfunc_begin("cons");
    callfunc_push_str(str);
    callfunc_push_str("some");
    callfunc_push_str("body");
    callfunc_push_str("text");
    callfunc_end();
}

public cons(string[], any:...)
{
    new text[100];
    vformat(text, charsmax(text), string, 2);
    console_print(0, text);
}
Server console string [some] [body] [text]

Вариант №2

Код:
#include <amxmodx>

public plugin_init()
{
    new str[100];

    copy(str, 100, "string [%d] [%d] [%d]");

    callfunc_begin("cons");
    callfunc_push_str(str);
    callfunc_push_int(1);
    callfunc_push_int(2);
    callfunc_push_int(3);
    callfunc_end();
}

public cons(string[], any:...)
{
    new text[100];
    vformat(text, charsmax(text), string, 2);
    console_print(0, text);
}
Server console string [0] [0] [0]

Вариант №3

Код:
#include <amxmodx>

public plugin_init()
{
    new str[100];

    copy(str, 100, "string [%s] [%f] [%d]");

    callfunc_begin("cons");
    callfunc_push_str(str);
    callfunc_push_str("some");
    callfunc_push_float(1.1);
    callfunc_push_int(2);
    callfunc_end();
}

public cons(string[], any:...)
{
    new text[100];
    vformat(text, charsmax(text), string, 2);
    console_print(0, text);
}
Server console crash
14 Сен 2018
Тестировалось на AMX 1.8.3-5144, -5158, -5199, 1.9.0-5216
 
Последнее редактирование:
Сообщения
207
Реакции
420
Помог
10 раз(а)
В варианте 2 первый аргумент для форматирования по прежнему в виде строки представлен. Возможно и является корнем проблем. Не уверен, т.к. не вникал в работу виртуальной машины Павна.

Попробуй вот такую функцию воткнуть вместо своего cons:
C++:
public cons(string[], any:...)
{
    new text[100];
    vformat(text, charsmax(text), string, 2);
    console_print(0, "%s", text);
}
Судя по документации, AMXX допускает форматирование в ней, что может вызвать проблемы при выводе уже отформатированного буфера.
 
Сообщения
957
Реакции
1,184
Помог
52 раз(а)
В варианте 2 первый аргумент для форматирования по прежнему в виде строки представлен. Возможно и является корнем проблем. Не уверен, т.к. не вникал в работу виртуальной машины Павна.
сори - моя невнимательность) писал код на форуме по памяти - зашпарился) там везде %d
 
Сообщения
23
Реакции
84
Помог
1 раз(а)
Your issue is you are trying to send parameters by value whereas vformat expects references.
Internally this will access to the value pointed by the parameter address, but if you pass a straight value like do callfunc_push_int and callfunc_push_float, there is no address to look at.

This works with arrays/strings, because they are always passed by reference.

If you want really to make this code works, you need to set the value in a variable and then use callfunc_push_intrf or callfunc_push_floatrf.

That's said, what you are doing doesn't make much sense. You should format before sending, like for example; callfunc_push_str(fm("string [%s] [%f] [%d]", "some", 1.1, 2)); and public cons(string[]) {}
 
Сообщения
957
Реакции
1,184
Помог
52 раз(а)
Arkshine, thanks
That's said, what you are doing doesn't make much sense. You should format before sending, like for example; callfunc_push_str(fm("string [%s] [%f] [%d]", "some", 1.1, 2)); and public cons(string[]) {}
this option is not suitable for my task - I need to substitute an unknown number of arguments in the string
14 Сен 2018
Решение

Код:
#include <amxmodx>

public plugin_init()
{
    new str[100];
    new i;
    new Float:j;

    copy(str, 100, "string [%s] [%d] [%f]");
    i = 2;
    j = 0.5;

    callfunc_begin("cons");
    callfunc_push_str(str);
    callfunc_push_str("some");
    callfunc_push_intrf(i);
    callfunc_push_floatrf(j);
    callfunc_end();
}

public cons(string[], any:...)
{
    new text[100];
    vformat(text, charsmax(text), string, 2);
    console_print(0, text);
}
 
Статус
В этой теме нельзя размещать новые ответы.

Пользователи, просматривающие эту тему

Сейчас на форуме нет ни одного пользователя.
Сверху Снизу