AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [INC] text.inc (https://forums.alliedmods.net/showthread.php?t=133895)

Zefir 07-29-2010 20:08

[INC] text.inc
 
1 Attachment(s)
Some simple string functions and macroses...

Macroses:
ptr = get_ptr_to_str(str) - return pointer to allocated str.
get_str_from_ptr(ptr, str, maxlen) - copy string from pointer to variable str[]

scopy(str1, str2) - copy predefined strings
PHP Code:

new str1[32], str2[12] = "Hello!"
scopy(str1str2)
//same as:
copy(str1charsmax(str1), str2)
//or:
copy(str1sizeof(str1) - 1str2

Functions:
replace_all_format(string[], len, const format_char, const with[])
allow define any formatted string, see example for more info.
return count of replaced charapters.

text.inc:
PHP Code:

#if defined _text_included
        #endinput
#endif
#define _text_included

/*
        String functions by Zefir<[email protected]>
                developed for Cerberus project
                http://cerberus.cstrike.in.ua/
        5 January 2010 (c) Zefir
*/

#include <fakemeta>

// Allocate memory for string
// Used for store parameters between call of set_task and other callbacks
#define get_ptr_to_str(%1) engfunc(EngFunc_AllocString, %1)
#define get_str_from_ptr(%1,%2,%3) global_get(glb_pStringBase, %1, %2, %3)
#define free_ptr_to_str(%1) %1 = 0

// simple copy with autodetected size of recive buffer
#define scopy(%1,%2) copy(%1, sizeof(%1) - 1, %2)

/*
        For create sub formatted string

Example:

#include <amxmodx>
#include <text>

public plugin_init() {
        register_plugin("Dinamic Hostname", "1.0", "Zefir")
        register_cvar("amx_hostname", "CS Server (next map: #m; timeleft: #t)")
        set_task(1.0, "set_hostname", 0, _, _, "b")
}

public set_hostname() {
        static hostname[64]

        get_cvar_string("amx_hostname", hostname, charsmax(hostname))

        static time_string[32], next_map[32]
        static h,m,s

        if (!get_cvar_float("mp_timelimit"))
                formatex(time_string, charsmax(time_string), "Never")
        else {
                s = get_timeleft()
                m = s/60
                h = m/60
                s = s-m*60
                m = m-h*60
                if(h)
                        formatex(time_string, charsmax(time_string), "%d:%02d:%02d", h, m, s)
                else
                        formatex(time_string, charsmax(time_string), "%d:%02d", m, s)
        }
        replace_all_format(hostname, charsmax(hostname), 't', time_string)

        get_cvar_string("amx_nextmap", next_map, charsmax(next_map))
        replace_all_format(hostname, charsmax(hostname), 'm', next_map)

        set_cvar_string("hostname", hostname)
}


*/
stock replace_all_format(string[], len, const format_char, const with[]) {

        static 
totalformat_str[3]
        
total 0
        formatex
(format_str2"#%c"format_char)

        while (
replace(stringlenformat_str"%s") != 0) {
                
format(stringlenstringwith)
                
total++;
        }

        return 
total;



Bugsy 07-29-2010 20:09

Re: [INC] text.inc
 
Wrong forum, chief.

Zefir 07-29-2010 20:16

Re: [INC] text.inc
 
Oops, again mistake :( sorry

Moderators, move please...

Ps: Bugsy, tnx


All times are GMT -4. The time now is 00:14.

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