AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   fprint o formatex+fputs (https://forums.alliedmods.net/showthread.php?t=187556)

rak 06-14-2012 17:25

fprint o formatex+fputs
 
what is better

this
PHP Code:

fprintf(File"^n^"%s^""data

or this
PHP Code:

formatex(buffercharsmax(buffer"^n^"%s^""data)
fputs(Filebuffer

thanks for read :B

^SmileY 06-14-2012 17:40

Re: fprint o formatex+fputs
 
First, isnot necessary to create a buffer variable and use the formatex()

i guess

Arkshine 06-14-2012 17:48

Re: fprint o formatex+fputs
 
First of course, I don't see why you would use two natives when you could use one. More efficient. Also, fprintf is exists because fputs can't format.

rak 06-14-2012 17:55

Re: fprint o formatex+fputs
 
thanks ^-^

Exolent[jNr] 06-14-2012 18:02

Re: fprint o formatex+fputs
 
Code:

static cell AMX_NATIVE_CALL amx_fprintf(AMX *amx, cell *params)
{
        FILE *fp = (FILE *)params[1];

        if (!fp)
                return 0;

        int len;
        char *str = format_amxstring(amx, params, 2, len);
        return fprintf(fp, "%s", str);
}

static cell AMX_NATIVE_CALL formatex(AMX *amx, cell *params)
{
        cell *buf = get_amxaddr(amx, params[1]);
        size_t maxlen = static_cast<size_t>(params[2]);
    cell *fmt = get_amxaddr(amx, params[3]);
        int param = 4;
        size_t total = atcprintf(buf, maxlen, fmt, amx, params, &param);
        return static_cast<cell>(total);
}

static cell AMX_NATIVE_CALL amx_fputs(AMX *amx, cell *params)
{
        FILE *fp = (FILE *)params[1];

        if (!fp)
                return 0;

        int len;
        char *str = get_amxstring(amx, params[2], 0, len);
        return fputs(str, fp);
}

char* format_amxstring(AMX *amx, cell *params, int parm, int& len)
{
#if !defined BINLOG_ENABLED
        return g_langMngr.FormatAmxString(amx, params, parm, len);
#else
        char *ans = g_langMngr.FormatAmxString(amx, params, parm, len);
        if (g_binlog_level & 4)
        {
                CPluginMngr::CPlugin *pl = g_plugins.findPluginFast(amx);
                if (pl)
                        g_BinLog.WriteOp(BinLog_FormatString, pl->getId(), parm, len, ans);
        }
        return ans;
#endif
}

char * CLangMngr::FormatAmxString(AMX *amx, cell *params, int parm, int &len)
{
        //do an initial run through all this
        static char outbuf[4096];
        cell *addr = get_amxaddr(amx, params[parm++]);

        if (amx->flags & AMX_FLAG_OLDFILE)
        {
                if (*addr & BCOMPAT_TRANSLATE_BITS)
                {
                        const char *key, *def;
                        if (!translate_bcompat(amx, addr, &key, &def))
                        {
                                goto normal_string;
                        }
                        len = atcprintf(outbuf, sizeof(outbuf)-1, def, amx, params, &parm);
                } else {
                        goto normal_string;
                }
        } else {
normal_string:
                len = atcprintf(outbuf, sizeof(outbuf)-1, addr, amx, params, &parm);
        }

        return outbuf;
}

template <typename D, typename S>
size_t atcprintf(D *buffer, size_t maxlen, const S *format, AMX *amx, cell *params, int *param)
{
        int                arg;
        int                args = params[0] / sizeof(cell);
        D                *buf_p;
        D                ch;
        int                flags;
        int                width;
        int                prec;
        int                n;
        char        sign;
        const S        *fmt;
        size_t        llen = maxlen;

        buf_p = buffer;
        arg = *param;
        fmt = format;

        while (true)
        {
                // run through the format string until we hit a '%' or '\0'
                for (ch = static_cast<D>(*fmt);
                        llen && ((ch = static_cast<D>(*fmt)) != '\0' && ch != '%');
                        fmt++)
                {
                        *buf_p++ = static_cast<D>(ch);
                        llen--;
                }
                if (ch == '\0' || llen <= 0)
                        goto done;

                // skip over the '%'
                fmt++;

                // reset formatting state
                flags = 0;
                width = 0;
                prec = -1;
                sign = '\0';

rflag:
                ch = static_cast<D>(*fmt++);
reswitch:
                switch(ch)
                {
                case '-':
                        flags |= LADJUST;
                        goto rflag;
                case '.':
                        n = 0;
                        while( is_digit( ( ch = static_cast<D>(*fmt++)) ) )
                                n = 10 * n + ( ch - '0' );
                        prec = n < 0 ? -1 : n;
                        goto reswitch;
                case '0':
                        flags |= ZEROPAD;
                        goto rflag;
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                        n = 0;
                        do {
                                n = 10 * n + ( ch - '0' );
                                ch = static_cast<D>(*fmt++);
                        } while( is_digit( ch ) );
                        width = n;
                        goto reswitch;
                case 'c':
                        CHECK_ARGS(0);
                        *buf_p++ = static_cast<D>(*get_amxaddr(amx, params[arg]));
                        llen--;
                        arg++;
                        break;
                case 'd':
                case 'i':
                        CHECK_ARGS(0);
                        AddInt(&buf_p, llen, *get_amxaddr(amx, params[arg]), width, flags);
                        arg++;
                        break;
                case 'u':
                        CHECK_ARGS(0);
                        AddUInt(&buf_p, llen, static_cast<unsigned int>(*get_amxaddr(amx, params[arg])), width, flags);
                        arg++;
                        break;
                case 'f':
                        CHECK_ARGS(0);
                        AddFloat(&buf_p, llen, amx_ctof(*get_amxaddr(amx, params[arg])), width, prec, flags);
                        arg++;
                        break;
                case 'X':
                        CHECK_ARGS(0);
                        flags |= UPPERDIGITS;
                        AddHex(&buf_p, llen, static_cast<unsigned int>(*get_amxaddr(amx, params[arg])), width, flags);
                        arg++;
                        break;
                case 'x':
                        CHECK_ARGS(0);
                        AddHex(&buf_p, llen, static_cast<unsigned int>(*get_amxaddr(amx, params[arg])), width, flags);
                        arg++;
                        break;
                case 'a':
                        {
                                CHECK_ARGS(0);
                                // %a is passed a pointer directly to a cell string.
                                cell* ptr=reinterpret_cast<cell*>(*get_amxaddr(amx, params[arg]));
                                if (!ptr)
                                {
                                        LogError(amx, AMX_ERR_NATIVE, "Invalid vector string handle provided (%d)", *get_amxaddr(amx, params[arg]));
                                        return 0;
                                }

                                AddString(&buf_p, llen, ptr, width, prec);
                                arg++;
                                break;
                        }
                case 's':
                        CHECK_ARGS(0);
                        if (amx->flags & AMX_FLAG_OLDFILE)
                        {
                                cell *addr = get_amxaddr(amx, params[arg]);
                                if (*addr & BCOMPAT_TRANSLATE_BITS)
                                {
                                        const char *key, *def;
                                        if (!translate_bcompat(amx, addr, &key, &def))
                                        {
                                                goto break_to_normal_string;
                                        }
                                        arg++;
                                        size_t written = atcprintf(buf_p, llen, def, amx, params, &arg);
                                        buf_p += written;
                                        llen -= written;
                                        break;
                                }
                        }
break_to_normal_string:
                        AddString(&buf_p, llen, get_amxaddr(amx, params[arg]), width, prec);
                        arg++;
                        break;
                case 'L':
                        {
                                CHECK_ARGS(1);
                                cell addr = params[arg++];
                                int len;
                                const char *key = get_amxstring(amx, params[arg++], 3, len);
                                const char *def = translate(amx, addr, key);
                                if (!def)
                                {
                                        static char buf[255];
                                        snprintf(buf, sizeof(buf)-1, "ML_NOTFOUND: %s", key);
                                        def = buf;
                                }
                                size_t written = atcprintf(buf_p, llen, def, amx, params, &arg);
                                buf_p += written;
                                llen -= written;
                                break;
                        }
                case '%':
                        *buf_p++ = static_cast<D>(ch);
                        if (!llen)
                                goto done;
                        llen--;
                        break;
                case '\0':
                        *buf_p++ = static_cast<D>('%');
                        if (!llen)
                                goto done;
                        llen--;
                        goto done;
                        break;
                default:
                        *buf_p++ = static_cast<D>(ch);
                        if (!llen)
                                goto done;
                        llen--;
                        break;
                }
        }

done:
        *buf_p = static_cast<D>(0);
        *param = arg;
        return maxlen-llen;
}

They both have generally the same process. The difference is that you use 2 natives + 1 variable, VS 1 native.

Use fprintf().

rak 06-14-2012 18:19

Re: fprint o formatex+fputs
 
thanks Exolent[jNr].. very useful ^-^


All times are GMT -4. The time now is 06:15.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.